In [1]:
import anl
import pandas as pd
import nilearn.datasets
import nilearn.plotting
from nilearn.decomposition import CanICA
import nibabel as nib
import matplotlib.pyplot as plt
import os
Download the ADHD preprocessed dataset from nitrc via nileard data and perform ICA
In [2]:
if not os.path.isfile('./example/ica20/icarest20.nii.gz'):
#Get all 40 preprocessed subject in adhd dataset
adhd_dataset = nilearn.datasets.fetch_adhd()
func_filenames = adhd_dataset.func
#Run ICA with 30 components
canica = CanICA(n_components=20)
canica.fit(func_filenames)
# Retrieve the independent components in brain space
components_img = canica.masker_.inverse_transform(canica.components_)
components_img.to_filename('./example/ica20/icarest20.nii.gz')
In [3]:
netinfo = pd.read_json('./networkMasks/netinfo.json')
netinfo.sort_index(inplace=True)
template_dir= './networkMasks/'
result_dir = './example/ica20/figures'
image_file = './example/ica20/icarest20.nii.gz'
In [4]:
netclass = anl.match_best(image_file,template_dir,netinfo,compareBy='likelihood',threshold='binarize')
icanib = nib.funcs.four_to_three(nib.load(image_file))
for n in range(0,len(netclass)):
fig,ax = plt.subplots(1,figsize=(5,2))
nilearn.plotting.plot_stat_map(icanib[n],figure=fig,axes=ax,draw_cross=False,colorbar=False)
ax.set_title(netclass['network'].iloc[n])
fig.savefig(result_dir + '/ica' + str(n+1) + '.png',r=300)
plt.close('all')