This file should be run in the main directory of the repo.

Example of ANL

(Note this is still very early sortware)


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


/home/william/anaconda3/lib/python3.5/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

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')


Input (net) is a file
Input (template) is a directory containing 29 .nii files
Input image is different size to template. Resampling.
/home/william/anaconda3/lib/python3.5/site-packages/scipy/ndimage/interpolation.py:462: UserWarning: The behaviour of affine_transform with a one-dimensional array supplied for the matrix parameter has changed in scipy 0.18.0.
  "The behaviour of affine_transform with a one-dimensional "
applying binary threshold
--To be fixed: noise masks must be located in current directory
620
620
620
620
/home/william/anaconda3/lib/python3.5/site-packages/matplotlib/cbook.py:136: MatplotlibDeprecationWarning: The axisbg attribute was deprecated in version 2.0. Use facecolor instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)