使用nilearn里的Dictlearning函数实现fMRI图像激活区域显示
说明:Dictlearning提取的信号比ICA更干净,所以要注意控制步骤二中alpha参数的大小
一、加载数据
import nilearn
from nilearn import datasets
func_filenames = nilearn.image.load_img("data/4D/Ontario_sub94652_rest.nii")
print(func_filenames.shape)
二、调用Dictlearning
from nilearn.decomposition import DictLearning
dict_learning = DictLearning(n_components=20,
memory="nilearn_cache",
memory_level=2,
verbose=12,
random_state=0,
n_epochs=1,
alpha=3,
mask_strategy='template')
print('[Example] Fitting dicitonary learning model')
dict_learning.fit(func_filenames)
三、存储提取激活区域后的图像
print('[Example] Saving results')
dictlearning_components_img = dict_learning.components_img_
dictlearning_components_img.to_filename('dictionary_learning_resting_state.nii.gz')
四、显示字典学习的组成分显示
from nilearn import plotting
from nilearn.image import iter_img
from nilearn.plotting import plot_stat_map, show
plotting.plot_prob_atlas(dictlearning_components_img,
title='All DictLearning components')
print(dictlearning_components_img.shape)
print(dictlearning_components_img)
五、按切片显示激活成分
for i, cur_img in enumerate(iter_img(dictlearning_components_img)):
plot_stat_map(cur_img, display_mode="z", title="Comp %d" % i,
cut_coords=1, colorbar=False)
show()