天天看点

用nilearn里的ICA生成fMRI激活区域用nilearn里的ICA功能生成fMRI激活区域

用nilearn里的ICA功能生成fMRI激活区域

一、加载图像

import nilearn
from nilearn import datasets

func_filenames = nilearn.image.load_img("data/4D/Ontario_sub94652_rest.nii")
print(func_filenames.shape)
           

二、调用ICA函数

from nilearn.decomposition import CanICA

canica = CanICA(n_components=20,
                memory="nilearn_cache",
                memory_level=2,
                verbose=10,
                mask_strategy='template',
                random_state=0)
canica.fit(func_filenames)
           

三、存储提取的组成分图像

canica_components_img = canica.components_img_
print(canica_components_img.shape)
canica_components_img.to_filename('canica_resting_state.nii.gz')
           

四、显示组成分图像

from nilearn.plotting import plot_prob_atlas
print(canica_components_img.shape)
print(canica_components_img)
# Plot all ICA components together
plot_prob_atlas(canica_components_img, title='All ICA components')
           
用nilearn里的ICA生成fMRI激活区域用nilearn里的ICA功能生成fMRI激活区域

五、按切片显示激活区域

from nilearn.image import iter_img
from nilearn.plotting import plot_stat_map, show

for i, cur_img in enumerate(iter_img(canica_components_img)):
    plot_stat_map(cur_img, display_mode="z", title="IC %d" % i,
                  cut_coords=1, colorbar=False)
           
用nilearn里的ICA生成fMRI激活区域用nilearn里的ICA功能生成fMRI激活区域