Step 4b - Visualize the individual gradients

Write out of individual Nifti files for primary gradient

written by J. Freyberg for the Autism Gradients project at Brainhack Cambridge 2017

In [1]:
%matplotlib inline
# this cell in only necessary for plotting below
import matplotlib.pylab as plt 
import nilearn 
import nilearn.plotting

import numpy as np
import nibabel as nib
import os
from os import listdir
from os.path import isfile, join


C:\Users\k1513504\AppData\Local\Continuum\Miniconda3\envs\rbbrainhack\lib\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)

In [2]:
# first import the input list from the csv file
import pandas as pd
# read in csv
df_phen = pd.read_csv('./data/SelectedSubjects.csv')
selected = list(df_phen.filename_npy)

In [9]:
def rebuild_nii_individ(num):
    
    for index in range(len(selected)):
        
        sub = selected[index]
        #print(sub)
        data = np.load('./data/Outputs/Regs/%s' % sub)
        a = data[:,num].astype('float32')
        nim = nib.load('./ROIs_Mask/cc400_roi_atlas.nii')
        imdat = nim.get_data().astype('float32')
        
        #print(np.unique(a))
        for i in np.unique(imdat):
            #a[a>0.1] = 0
            #a[a<-0.1] = 0
            if i != 0 and i < 392:
                imdat[imdat == i] = a[int(i)-1]
            elif i >= 392:
                imdat[imdat == i] = np.nan

        nim_out = nib.Nifti1Image(imdat, nim.affine, nim.header)
        nim_out.set_data_dtype('float32')
        # to save:
        nim_out.to_filename(os.path.join(os.getcwd(), 'data', 'Outputs', 'individual', 'res' + sub + str(num) + '.nii'))

In [ ]:
nims = rebuild_nii_individ(0)


C:\Users\k1513504\AppData\Local\Continuum\Miniconda3\envs\rbbrainhack\lib\site-packages\ipykernel\__main__.py:21: DeprecationWarning: get_affine method is deprecated.
Please use the ``img.affine`` property instead.

* deprecated from version: 2.1
* Will raise <class 'nibabel.deprecator.ExpiredDeprecationError'> as of version: 4.0
C:\Users\k1513504\AppData\Local\Continuum\Miniconda3\envs\rbbrainhack\lib\site-packages\ipykernel\__main__.py:21: DeprecationWarning: get_header method is deprecated.
Please use the ``img.header`` property instead.

* deprecated from version: 2.1
* Will raise <class 'nibabel.deprecator.ExpiredDeprecationError'> as of version: 4.0