Hypothesis Testing

This code does the following:

  • Reads the FC files for all the subjects
  • Z-Standardize all the voxel-roi correlation values of each ROI
  • Perform two tailed t-test for each voxel-roi pair correlation across subjects (Autism vs TD)

In [3]:
import os
from os.path import join as opj
# from nipype.interfaces import afni
import nibabel as nib
import json    
import numpy as np
import os

In [21]:
# Paths

path_cwd = os.getcwd()
path_split_list = path_cwd.split('/')
s = path_split_list[0:-1] # for getting to the parent dir of pwd
s_main = path_split_list[0:-2]
s = opj('/',*s) # *s converts list to path, # very important to add '/' in the begining so it is read as directory later
s_main = opj('/',*s_main)

In [22]:
s_main


Out[22]:
'/home1/varunk'

In [6]:
# json_path = opj(data_directory,'task-rest_bold.json')

json_path = '../scripts/json/paths.json'
with open(json_path, 'rt') as fp:
    task_info = json.load(fp)

In [7]:
base_directory = opj(s,task_info["base_directory_for_results"]) 
motion_correction_bet_directory = task_info["motion_correction_bet_directory"]
parent_wf_directory = task_info["parent_wf_directory"]
# functional_connectivity_directory = task_info["functional_connectivity_directory"]
functional_connectivity_directory = 'temp_fc'
coreg_reg_directory = task_info["coreg_reg_directory"]
atlas_resize_reg_directory = task_info["atlas_resize_reg_directory"]
data_directory = opj(s,task_info["data_directory"])
datasink_name = task_info["datasink_name"]
# fc_datasink_name = task_info["fc_datasink_name"]
fc_datasink_name = 'temp_dataSink'
atlasPath = opj(s,task_info["atlas_path"])

hypothesis_test_dir = opj(base_directory, task_info["hypothesis_test_dir"])

Use python pandas to read the composite phenotype file


In [8]:
#  Runall:
import pandas as pd

df = pd.read_csv('/home1/varunk/data/ABIDE1/RawDataBIDs/composite_phenotypic_file.csv') # , index_col='SUB_ID'

df = df.sort_values(['SUB_ID'])
# df = df.sort_values(['SUB+AF8-ID'])

# These subjects didn't have anatomical highres file: 
# 51232','51233','51242','51243','51244','51245','51246','51247','51270','51310'

bugs = ['51232','51233','51242','51243','51244','51245','51246','51247','51270','51310', '50045']
# sub 50045 has fewer ROIs coz of more shift of head

In [9]:
# '0051242' in bugs

In [42]:
# df

In [45]:
tr_paths = opj(s_main,task_info["base_directory_for_results"],datasink_name,'tr_paths','tr_list.npy')
tr_list = np.load(tr_paths)
# tr_list.max()
# tr_list
# len(np.where(tr_list == 1.5)[0]),len(np.where(tr_list == 3.0)[0])
# np.where(tr_list == 3.0)

df_array = df.as_matrix(['SUB_ID'])

df_array[np.where(tr_list == 3.0)]


Out[45]:
array([[51201],
       [51202],
       [51203],
       [51204],
       [51205],
       [51206],
       [51207],
       [51208],
       [51209],
       [51210],
       [51211],
       [51212],
       [51213],
       [51214],
       [51215],
       [51216],
       [51217],
       [51218],
       [51219],
       [51220],
       [51221],
       [51222],
       [51223],
       [51224],
       [51225],
       [51226],
       [51227],
       [51228],
       [51229],
       [51230],
       [51231],
       [51232],
       [51233],
       [51234],
       [51235],
       [51236],
       [51237],
       [51238],
       [51239],
       [51240],
       [51241],
       [51242],
       [51243],
       [51244],
       [51245],
       [51246],
       [51247],
       [51248],
       [51249],
       [51250],
       [51251],
       [51252],
       [51253],
       [51254],
       [51255],
       [51256],
       [51257],
       [51258],
       [51259],
       [51260],
       [51261],
       [51262],
       [51263],
       [51264],
       [51265],
       [51266],
       [51267],
       [51268],
       [51269],
       [51270],
       [51271],
       [51272],
       [51273],
       [51274],
       [51275],
       [51276],
       [51277],
       [51278],
       [51279],
       [51280],
       [51281],
       [51282],
       [51291],
       [51292],
       [51293],
       [51294],
       [51295],
       [51296],
       [51297],
       [51298],
       [51299],
       [51300],
       [51301],
       [51302],
       [51303],
       [51304],
       [51305],
       [51306],
       [51307],
       [51308],
       [51309],
       [51310],
       [51311],
       [51312],
       [51313],
       [51314],
       [51315],
       [51316],
       [51317],
       [51318],
       [51319],
       [51320],
       [51321],
       [51322],
       [51323],
       [51324],
       [51325],
       [51326],
       [51327],
       [51328],
       [51329],
       [51330],
       [51331],
       [51332],
       [51333],
       [51334],
       [51335],
       [51336],
       [51338],
       [51339],
       [51340],
       [51341],
       [51342],
       [51343],
       [51344],
       [51345],
       [51346],
       [51347],
       [51348],
       [51349],
       [51350],
       [51351],
       [51352],
       [51353],
       [51354],
       [51355],
       [51356],
       [51357],
       [51358],
       [51359],
       [51360],
       [51361],
       [51362],
       [51363],
       [51576],
       [51577]])

In [ ]:
# tr = 
/home1/varunk/results_again_again/ABIDE1_Preprocess_Datasink/tr_paths/tr_list.npy

In [25]:
# selecting Autistic males(DSM IV) of age <= 18 years 
df_aut_lt18_m = df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 1)]

In [29]:
df_aut_lt18_m.shape


Out[29]:
(214, 74)

In [15]:
df_aut_lt18_m_eyesopen = df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 1) & df['EYE_STATUS_AT_SCAN'] == 1]
df_aut_lt18_m_eyesopen


Out[15]:
SITE_ID SUB_ID DX_GROUP DSM_IV_TR AGE_AT_SCAN SEX HANDEDNESS_CATEGORY HANDEDNESS_SCORES FIQ VIQ ... WISC_IV_BLK_DSN_SCALED WISC_IV_PIC_CON_SCALED WISC_IV_MATRIX_SCALED WISC_IV_DIGIT_SPAN_SCALED WISC_IV_LET_NUM_SCALED WISC_IV_CODING_SCALED WISC_IV_SYM_SCALED EYE_STATUS_AT_SCAN AGE_AT_MPRAGE BMI
577 SDSU 50183 1 1 14.14 1 R NaN 139.0 128.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
586 SDSU 50192 1 1 12.99 1 R NaN 85.0 93.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
810 UM_1 50272 1 1 14.20 1 R NaN 98.5 100.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
811 UM_1 50273 1 1 16.80 1 R NaN 112.5 109.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
812 UM_1 50274 1 1 14.20 1 R NaN 111.5 106.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
813 UM_1 50275 1 1 11.50 1 L NaN 85.0 106.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
815 UM_1 50277 1 1 15.40 1 L NaN 107.5 96.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
817 UM_1 50279 1 1 12.20 1 R NaN 87.5 75.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
818 UM_1 50280 1 1 15.60 1 -9999 NaN 85.0 81.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
820 UM_1 50282 1 1 15.20 1 NaN NaN 121.0 118.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
821 UM_1 50283 1 1 11.20 1 R NaN 97.5 91.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
825 UM_1 50287 1 1 14.40 1 L NaN 96.5 90.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
826 UM_1 50288 1 1 11.10 1 R NaN 105.0 104.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
828 UM_1 50290 1 1 14.00 1 R NaN 108.5 103.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
829 UM_1 50291 1 1 13.90 1 R NaN 96.0 98.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
830 UM_1 50292 1 1 12.60 1 R NaN 89.5 91.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
831 UM_1 50293 1 1 12.40 1 R NaN -9999.0 126.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
833 UM_1 50295 1 1 11.50 1 R NaN 135.0 133.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
834 UM_1 50296 1 1 15.10 1 R NaN 89.0 78.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
836 UM_1 50298 1 1 12.80 1 R NaN 101.0 121.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
837 UM_1 50299 1 1 12.20 1 R NaN 87.5 96.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
839 UM_1 50301 1 1 16.10 1 L NaN 97.0 101.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
841 UM_1 50303 1 1 11.60 1 R NaN 84.0 75.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
842 UM_1 50304 1 1 11.00 1 -9999 NaN 96.5 99.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
843 UM_1 50305 1 1 10.90 1 R NaN 85.0 103.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
844 UM_1 50306 1 1 8.50 1 R NaN 76.0 86.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
845 UM_1 50307 1 1 9.70 1 Ambi NaN 81.5 87.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
846 UM_1 50308 1 1 8.90 1 -9999 NaN 113.0 113.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
848 UM_1 50310 1 1 9.70 1 R NaN 78.5 82.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
849 UM_1 50311 1 1 9.80 1 -9999 NaN 89.0 96.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
734 UCLA_1 51234 1 1 10.91 1 R NaN 87.0 93.0 ... NaN NaN NaN NaN NaN NaN NaN 1 10.91 NaN
735 UCLA_1 51235 1 1 10.67 1 R NaN 132.0 130.0 ... NaN NaN NaN NaN NaN NaN NaN 1 10.67 NaN
736 UCLA_1 51236 1 1 12.42 1 R NaN 89.0 85.0 ... NaN NaN NaN NaN NaN NaN NaN 1 12.42 NaN
737 UCLA_1 51237 1 1 17.40 1 R NaN 100.0 112.0 ... NaN NaN NaN NaN NaN NaN NaN 1 17.40 NaN
738 UCLA_1 51238 1 1 10.85 1 R NaN 73.0 67.0 ... NaN NaN NaN NaN NaN NaN NaN 1 10.64 NaN
739 UCLA_1 51239 1 1 13.67 1 R NaN 86.0 98.0 ... NaN NaN NaN NaN NaN NaN NaN 1 13.67 NaN
740 UCLA_1 51240 1 1 14.96 1 R NaN 121.0 106.0 ... NaN NaN NaN NaN NaN NaN NaN 1 15.40 NaN
741 UCLA_1 51241 1 1 10.90 1 R NaN 95.0 87.0 ... NaN NaN NaN NaN NaN NaN NaN 1 11.03 NaN
743 UCLA_1 51243 1 1 11.69 1 R NaN 101.0 96.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
744 UCLA_1 51244 1 1 13.09 1 R NaN -9999.0 83.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
745 UCLA_1 51245 1 1 14.98 1 R NaN 64.0 59.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
746 UCLA_1 51246 1 1 13.10 1 R NaN 84.0 85.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
747 UCLA_1 51247 1 1 11.97 1 R NaN 118.0 108.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
748 UCLA_1 51248 1 1 13.50 1 R NaN 110.0 99.0 ... NaN NaN NaN NaN NaN NaN NaN 1 13.50 NaN
749 UCLA_1 51249 1 1 8.49 1 R NaN 107.0 112.0 ... NaN NaN NaN NaN NaN NaN NaN 1 8.49 NaN
783 UCLA_2 51291 1 1 16.47 1 R NaN 86.0 83.0 ... NaN NaN NaN NaN NaN NaN NaN 1 16.63 NaN
784 UCLA_2 51292 1 1 12.24 1 R NaN 75.0 83.0 ... NaN NaN NaN NaN NaN NaN NaN 1 12.24 NaN
785 UCLA_2 51293 1 1 13.08 1 R NaN 92.0 102.0 ... NaN NaN NaN NaN NaN NaN NaN 1 13.08 NaN
786 UCLA_2 51294 1 1 11.70 1 R NaN 87.0 89.0 ... NaN NaN NaN NaN NaN NaN NaN 1 12.06 NaN
787 UCLA_2 51295 1 1 10.04 1 L NaN 108.0 95.0 ... NaN NaN NaN NaN NaN NaN NaN 1 10.33 NaN
788 UCLA_2 51296 1 1 11.16 1 R NaN 77.0 71.0 ... NaN NaN NaN NaN NaN NaN NaN 1 11.16 NaN
789 UCLA_2 51297 1 1 14.27 1 L NaN 88.0 98.0 ... NaN NaN NaN NaN NaN NaN NaN 1 14.27 NaN
790 UCLA_2 51298 1 1 10.57 1 R NaN 118.0 104.0 ... NaN NaN NaN NaN NaN NaN NaN 1 10.69 NaN
791 UCLA_2 51299 1 1 14.77 1 R NaN 104.0 106.0 ... NaN NaN NaN NaN NaN NaN NaN 1 14.77 NaN
792 UCLA_2 51300 1 1 14.08 1 L NaN 92.0 104.0 ... NaN NaN NaN NaN NaN NaN NaN 1 14.08 NaN
793 UCLA_2 51301 1 1 13.28 1 R NaN 91.0 108.0 ... NaN NaN NaN NaN NaN NaN NaN 1 13.28 NaN
794 UCLA_2 51302 1 1 10.85 1 R NaN 87.0 75.0 ... NaN NaN NaN NaN NaN NaN NaN 1 10.85 NaN
809 UCLA_2 51317 1 1 12.82 1 R NaN 102.0 99.0 ... NaN NaN NaN NaN NaN NaN NaN 1 12.82 NaN
217 MAX_MUN 51350 1 1 11.00 1 R NaN 79.0 NaN ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
218 MAX_MUN 51351 1 1 11.00 1 R NaN 79.0 NaN ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN

174 rows × 74 columns


In [ ]:


In [136]:
df_aut_lt18_m_eyesclosed = df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 1) & (df['EYE_STATUS_AT_SCAN'] == 2)]
df_aut_lt18_m_eyesclosed.shape


Out[136]:
(40, 74)

In [ ]:
bugs = ['50045'] # ROI Missing in population under study
# ROI Missing:
# 253	./_subject_id_0050655/func2std_xform/0050655_fc_map_flirt.nii.gz
# 254	./_subject_id_0050645/func2std_xform/0050645_fc_map_flirt.nii.gz
# 256	./_subject_id_0050651/func2std_xform/0050651_fc_map_flirt.nii.gz
# 256	./_subject_id_0050658/func2std_xform/0050658_fc_map_flirt.nii.gz
# 259	./_subject_id_0050643/func2std_xform/0050643_fc_map_flirt.nii.gz
# 259	./_subject_id_0050652/func2std_xform/0050652_fc_map_flirt.nii.gz
# 270	./_subject_id_0050644/func2std_xform/0050644_fc_map_flirt.nii.gz
# 270	./_subject_id_0051558/func2std_xform/0051558_fc_map_flirt.nii.gz
# 273	./_subject_id_0050648/func2std_xform/0050648_fc_map_flirt.nii.gz
# 273	./_subject_id_0050667/func2std_xform/0050667_fc_map_flirt.nii.gz
# 273	./_subject_id_0050045/func2std_xform/0050045_fc_map_flirt.nii.gz
# 273	./_subject_id_0051575/func2std_xform/0051575_fc_map_flirt.nii.gz
# 273	./_subject_id_0050657/func2std_xform/0050657_fc_map_flirt.nii.gz
# 273	./_subject_id_0050661/func2std_xform/0050661_fc_map_flirt.nii.gz
# 273	./_subject_id_0050653/func2std_xform/0050653_fc_map_flirt.nii.gz
# 273	./_subject_id_0050669/func2std_xform/0050669_fc_map_flirt.nii.gz

In [135]:
df_td_lt18_m_eyesclosed = df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 0) & (df['EYE_STATUS_AT_SCAN'] == 2)]
df_td_lt18_m_eyesclosed


Out[135]:
SITE_ID SUB_ID DX_GROUP DSM_IV_TR AGE_AT_SCAN SEX HANDEDNESS_CATEGORY HANDEDNESS_SCORES FIQ VIQ ... WISC_IV_BLK_DSN_SCALED WISC_IV_PIC_CON_SCALED WISC_IV_MATRIX_SCALED WISC_IV_DIGIT_SPAN_SCALED WISC_IV_LET_NUM_SCALED WISC_IV_CODING_SCALED WISC_IV_SYM_SCALED EYE_STATUS_AT_SCAN AGE_AT_MPRAGE BMI
520 PITT 50031 2 0 12.92 1 R NaN 106.0 102.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
522 PITT 50033 2 0 12.15 1 R NaN 98.0 98.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
523 PITT 50034 2 0 14.77 1 R NaN 97.0 98.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
524 PITT 50035 2 0 17.36 1 R NaN 107.0 109.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
532 PITT 50043 2 0 13.78 1 R NaN 113.0 115.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
533 PITT 50044 2 0 17.13 1 R NaN 110.0 117.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
534 PITT 50045 2 0 15.70 1 R NaN 114.0 115.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
536 PITT 50047 2 0 15.35 1 R NaN 103.0 95.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
537 PITT 50048 2 0 11.81 1 R NaN 107.0 98.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
539 PITT 50050 2 0 14.37 1 L NaN 113.0 104.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
540 PITT 50051 2 0 12.83 1 R NaN 107.0 101.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
542 PITT 50054 2 0 9.44 1 R NaN 95.0 99.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
676 TRINITY 50257 2 0 14.91 1 R NaN 113.0 105.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
683 TRINITY 50265 2 0 12.25 1 R NaN 101.0 97.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
684 TRINITY 50266 2 0 15.91 1 R NaN 116.0 118.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
685 TRINITY 50267 2 0 17.50 1 R NaN 116.0 114.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
686 TRINITY 50268 2 0 14.41 1 R NaN 96.0 107.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
687 TRINITY 50269 2 0 14.66 1 R NaN 113.0 110.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
151 LEUVEN_2 50724 2 0 15.10 1 R NaN NaN 103.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
152 LEUVEN_2 50725 2 0 14.20 1 L NaN NaN 105.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
153 LEUVEN_2 50726 2 0 16.60 1 R NaN NaN 130.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
154 LEUVEN_2 50727 2 0 13.90 1 L->R NaN NaN 122.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
155 LEUVEN_2 50728 2 0 15.60 1 R NaN NaN 116.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
157 LEUVEN_2 50731 2 0 16.20 1 R NaN NaN 124.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
158 LEUVEN_2 50732 2 0 12.40 1 R NaN NaN 127.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
159 LEUVEN_2 50733 2 0 14.70 1 R NaN NaN 124.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
160 LEUVEN_2 50734 2 0 16.60 1 R NaN NaN 97.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
163 LEUVEN_2 50737 2 0 12.20 1 R NaN NaN 119.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
164 LEUVEN_2 50738 2 0 12.80 1 R NaN NaN 124.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
165 LEUVEN_2 50739 2 0 15.20 1 R NaN NaN 119.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
166 LEUVEN_2 50740 2 0 12.40 1 R NaN NaN 127.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
167 LEUVEN_2 50741 2 0 14.20 1 R NaN NaN 119.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
168 LEUVEN_2 50742 2 0 16.90 1 R NaN NaN 122.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
346 NYU 51064 2 0 7.26 1 NaN 61.0 119.0 127.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN 16.98
347 NYU 51065 2 0 10.52 1 NaN 46.0 105.0 111.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN 29.62
352 NYU 51070 2 0 7.29 1 NaN 82.0 109.0 112.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN -9999.00
353 NYU 51071 2 0 10.69 1 NaN -9999.0 80.0 92.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN 18.59
354 NYU 51072 2 0 11.91 1 NaN 100.0 121.0 120.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN -9999.00
355 NYU 51073 2 0 12.49 1 NaN 54.0 126.0 109.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN -9999.00
356 NYU 51074 2 0 12.81 1 NaN 75.0 123.0 117.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN -9999.00
357 NYU 51075 2 0 14.20 1 NaN -9999.0 109.0 121.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN -9999.00
358 NYU 51076 2 0 16.93 1 NaN 25.0 124.0 120.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN -9999.00
359 NYU 51077 2 0 17.30 1 NaN 69.0 109.0 113.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN 26.46
691 TRINITY 51133 2 0 12.04 1 R NaN 91.0 100.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
692 TRINITY 51134 2 0 16.83 1 R NaN 89.0 81.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
693 TRINITY 51135 2 0 13.75 1 R NaN 101.0 95.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
694 TRINITY 51136 2 0 12.66 1 R NaN 126.0 125.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
695 TRINITY 51137 2 0 13.25 1 R NaN 131.0 137.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
696 TRINITY 51138 2 0 12.66 1 R NaN 99.0 94.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
698 TRINITY 51140 2 0 15.75 1 R NaN 128.0 132.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
699 TRINITY 51141 2 0 15.91 1 R NaN 97.0 91.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
700 TRINITY 51142 2 0 14.91 1 R NaN 104.0 101.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
424 NYU 51159 2 0 12.81 1 NaN 100.0 118.0 122.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN 22.04
228 MAX_MUN 51361 2 0 16.00 1 R NaN 114.0 NaN ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
31 CALTECH 51487 2 0 17.00 1 R NaN 113.0 106.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN

55 rows × 74 columns


In [131]:
# df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 1) ]

In [91]:
df_aut_lt18_m_eyesopen.shape


Out[91]:
(174, 74)

In [92]:
df_td_lt18_m_eyesopen = df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 0) & df['EYE_STATUS_AT_SCAN'] == 1]

In [132]:
# df_td_lt18_m_eyesopen['SUB_ID'][100:]

In [94]:
df.loc[(df['EYE_STATUS_AT_SCAN'] == 1)].shape[0] + df.loc[(df['EYE_STATUS_AT_SCAN'] == 2)].shape[0]


Out[94]:
1112

In [95]:
df_aut_lt18_m[103:106]


Out[95]:
SITE_ID SUB_ID DX_GROUP DSM_IV_TR AGE_AT_SCAN SEX HANDEDNESS_CATEGORY HANDEDNESS_SCORES FIQ VIQ ... WISC_IV_BLK_DSN_SCALED WISC_IV_PIC_CON_SCALED WISC_IV_MATRIX_SCALED WISC_IV_DIGIT_SPAN_SCALED WISC_IV_LET_NUM_SCALED WISC_IV_CODING_SCALED WISC_IV_SYM_SCALED EYE_STATUS_AT_SCAN AGE_AT_MPRAGE BMI
178 LEUVEN_2 50752 1 1 14.7 1 L NaN NaN 92.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
179 LEUVEN_2 50753 1 1 14.3 1 R NaN NaN 78.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
180 LEUVEN_2 50754 1 1 12.3 1 R NaN NaN 97.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN

3 rows × 74 columns


In [96]:
df[520:550]


Out[96]:
SITE_ID SUB_ID DX_GROUP DSM_IV_TR AGE_AT_SCAN SEX HANDEDNESS_CATEGORY HANDEDNESS_SCORES FIQ VIQ ... WISC_IV_BLK_DSN_SCALED WISC_IV_PIC_CON_SCALED WISC_IV_MATRIX_SCALED WISC_IV_DIGIT_SPAN_SCALED WISC_IV_LET_NUM_SCALED WISC_IV_CODING_SCALED WISC_IV_SYM_SCALED EYE_STATUS_AT_SCAN AGE_AT_MPRAGE BMI
61 CMU 50666 2 0 31.0 1 R NaN 107.0 101.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
62 CMU 50667 2 0 40.0 1 R NaN 128.0 122.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
63 CMU 50668 2 0 25.0 1 R NaN 110.0 109.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
64 CMU 50669 2 0 30.0 2 R NaN 126.0 123.0 ... NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN
120 LEUVEN_1 50682 2 0 23.0 1 R NaN 128.0 118.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
121 LEUVEN_1 50683 2 0 24.0 1 R NaN 106.0 113.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
122 LEUVEN_1 50685 2 0 23.0 1 R NaN 112.0 114.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
123 LEUVEN_1 50686 1 1 19.0 1 R NaN 92.0 88.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
124 LEUVEN_1 50687 2 0 22.0 1 R NaN 124.0 121.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
125 LEUVEN_1 50688 2 0 21.0 1 R NaN 98.0 103.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
126 LEUVEN_1 50689 1 1 21.0 1 R NaN 106.0 115.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
127 LEUVEN_1 50690 1 1 22.0 1 R NaN 101.0 95.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
128 LEUVEN_1 50691 2 0 22.0 1 R NaN 146.0 133.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
129 LEUVEN_1 50692 2 0 22.0 1 R NaN 109.0 112.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
130 LEUVEN_1 50693 1 1 22.0 1 R NaN 128.0 128.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
131 LEUVEN_1 50694 1 1 19.0 1 R NaN 109.0 103.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
132 LEUVEN_1 50695 1 1 19.0 1 R NaN 101.0 126.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
133 LEUVEN_1 50696 1 1 20.0 1 R NaN 121.0 118.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
134 LEUVEN_1 50697 1 1 19.0 1 R NaN 101.0 99.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
135 LEUVEN_1 50698 2 0 28.0 1 R NaN 109.0 107.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
136 LEUVEN_1 50699 2 0 21.0 1 R NaN 104.0 114.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
137 LEUVEN_1 50700 1 1 23.0 1 L NaN 128.0 110.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
138 LEUVEN_1 50701 2 0 18.0 1 R NaN 109.0 131.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
139 LEUVEN_1 50702 1 1 18.0 1 R NaN 100.0 110.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
140 LEUVEN_1 50703 2 0 29.0 1 R NaN 108.0 112.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
141 LEUVEN_1 50704 1 1 29.0 1 R NaN 119.0 121.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
142 LEUVEN_1 50705 1 1 24.0 1 R NaN 111.0 113.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
143 LEUVEN_1 50706 2 0 22.0 1 L NaN 134.0 136.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
144 LEUVEN_1 50707 2 0 22.0 1 R NaN 113.0 121.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN
145 LEUVEN_1 50708 1 1 32.0 1 R NaN 89.0 97.0 ... NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN

30 rows × 74 columns


In [97]:
df_td_lt18_m = df.loc[(df['SEX'] == 1) & (df['AGE_AT_SCAN'] <=18) & (df['DSM_IV_TR'] == 0) ]

In [98]:
df_td_lt18_m.shape


Out[98]:
(296, 74)

In [99]:
# table_males_np = table_males.as_matrix(columns=['SUB_ID','DX_GROUP', 'DSM_IV_TR', 'AGE_AT_SCAN' ,'SEX' ,'EYE_STATUS_AT_SCAN'])

In [113]:
# df_td_lt18_m_eyesclosed

df_aut_subid = df_aut_lt18_m_eyesopen.as_matrix(columns=['SUB_ID'])
df_td_subid = df_td_lt18_m_eyesopen.as_matrix(columns=['SUB_ID'])

In [114]:
# df_aut_subid#, df_td_subid

In [115]:
# Now construct a function that takes a list of SUB_ID's and returns the FC Maps
def get_subject_fc_file(subject_id_list,fc_file_path, bugs):
    import re
    
    return_fc_maps = []
    fc_file_list = np.load(fc_file_path)
    for subject_id in subject_id_list:
#         print("For subject: ",subject_id)
        found =  False
        for brain in fc_file_list:
            sub_id_extracted = re.search('.+_subject_id_(\d+)', brain).group(1)
            if str(subject_id) in bugs:
#                 print("In Bugs with subject id ",subject_id)
                found = True
            elif (subject_id == int(sub_id_extracted)):
                found = True
                return_fc_maps.append(brain)
#                 print("Found for subject: ",subject_id)
        if found == False: # Some subject was not found Problem!
            print ('Unable to locate Subject: ',int(subject_id),'extracted: ',int(sub_id_extracted))
            return 0
    return return_fc_maps

In [116]:
# Again:

fc_datasink_name = 'fc_datasink'
import itertools

# itr = (list(itertools.product([0, 1], repeat=3)))

itr = [(1,1,1)]
number_of_subjects = -1
for motion_param_regression, global_signal_regression, band_pass_filtering in itr:
    combination = 'pearcoff_motionRegress' + str(int(motion_param_regression)) + 'filt' + str(int(band_pass_filtering)) + 'global' + str(int(global_signal_regression))
    print("Combination: ",combination)
    print(motion_param_regression, global_signal_regression, band_pass_filtering)
    fc_file_list = opj(base_directory,fc_datasink_name,combination,'fc_map_brain_file_list.npy')
#     print(fc_file_list)
#     apply_fisher = True


    autistic_list = (get_subject_fc_file(df_aut_subid.squeeze(), fc_file_list, bugs))
    print("Number of autistic participants ", len(autistic_list))
    
    td_list = (get_subject_fc_file(df_td_subid.squeeze(), fc_file_list, bugs))
    print("Number of TD participants ", len(td_list))


Combination:  pearcoff_motionRegress1filt1global1
1 1 1
Number of autistic participants  167
Number of TD participants  239

In [104]:
participants_considered = min(len(autistic_list), len(td_list))

# participants_considered = 4

autistic_list = autistic_list[0:participants_considered]
td_list = td_list[0:participants_considered]

In [117]:
autistic_list


Out[117]:
['/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050183/func2std_xform/0050183_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050192/func2std_xform/0050192_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050272/func2std_xform/0050272_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050273/func2std_xform/0050273_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050274/func2std_xform/0050274_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050275/func2std_xform/0050275_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050277/func2std_xform/0050277_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050279/func2std_xform/0050279_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050280/func2std_xform/0050280_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050282/func2std_xform/0050282_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050283/func2std_xform/0050283_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050287/func2std_xform/0050287_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050288/func2std_xform/0050288_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050290/func2std_xform/0050290_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050291/func2std_xform/0050291_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050292/func2std_xform/0050292_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050293/func2std_xform/0050293_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050295/func2std_xform/0050295_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050296/func2std_xform/0050296_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050298/func2std_xform/0050298_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050299/func2std_xform/0050299_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050301/func2std_xform/0050301_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050303/func2std_xform/0050303_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050304/func2std_xform/0050304_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050305/func2std_xform/0050305_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050306/func2std_xform/0050306_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050307/func2std_xform/0050307_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050308/func2std_xform/0050308_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050310/func2std_xform/0050310_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050311/func2std_xform/0050311_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050312/func2std_xform/0050312_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050313/func2std_xform/0050313_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050315/func2std_xform/0050315_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050316/func2std_xform/0050316_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050317/func2std_xform/0050317_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050318/func2std_xform/0050318_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050322/func2std_xform/0050322_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050323/func2std_xform/0050323_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050324/func2std_xform/0050324_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050326/func2std_xform/0050326_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050397/func2std_xform/0050397_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050402/func2std_xform/0050402_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050404/func2std_xform/0050404_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050405/func2std_xform/0050405_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050406/func2std_xform/0050406_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050408/func2std_xform/0050408_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050410/func2std_xform/0050410_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050412/func2std_xform/0050412_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050413/func2std_xform/0050413_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050481/func2std_xform/0050481_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050486/func2std_xform/0050486_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050489/func2std_xform/0050489_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050500/func2std_xform/0050500_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050501/func2std_xform/0050501_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050504/func2std_xform/0050504_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050506/func2std_xform/0050506_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050509/func2std_xform/0050509_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050510/func2std_xform/0050510_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050511/func2std_xform/0050511_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050512/func2std_xform/0050512_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050515/func2std_xform/0050515_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050516/func2std_xform/0050516_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050517/func2std_xform/0050517_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050519/func2std_xform/0050519_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050520/func2std_xform/0050520_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050522/func2std_xform/0050522_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050523/func2std_xform/0050523_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050524/func2std_xform/0050524_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050528/func2std_xform/0050528_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050532/func2std_xform/0050532_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050603/func2std_xform/0050603_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050605/func2std_xform/0050605_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050614/func2std_xform/0050614_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050618/func2std_xform/0050618_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050702/func2std_xform/0050702_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050791/func2std_xform/0050791_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050793/func2std_xform/0050793_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050794/func2std_xform/0050794_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050799/func2std_xform/0050799_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050801/func2std_xform/0050801_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050802/func2std_xform/0050802_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050804/func2std_xform/0050804_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050807/func2std_xform/0050807_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050815/func2std_xform/0050815_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050825/func2std_xform/0050825_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050964/func2std_xform/0050964_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050981/func2std_xform/0050981_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050982/func2std_xform/0050982_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050983/func2std_xform/0050983_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050984/func2std_xform/0050984_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050985/func2std_xform/0050985_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050986/func2std_xform/0050986_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050987/func2std_xform/0050987_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050989/func2std_xform/0050989_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050990/func2std_xform/0050990_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050991/func2std_xform/0050991_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050992/func2std_xform/0050992_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050993/func2std_xform/0050993_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050994/func2std_xform/0050994_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050995/func2std_xform/0050995_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050996/func2std_xform/0050996_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050997/func2std_xform/0050997_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050998/func2std_xform/0050998_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0050999/func2std_xform/0050999_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051000/func2std_xform/0051000_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051001/func2std_xform/0051001_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051003/func2std_xform/0051003_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051006/func2std_xform/0051006_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051007/func2std_xform/0051007_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051009/func2std_xform/0051009_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051011/func2std_xform/0051011_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051012/func2std_xform/0051012_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051013/func2std_xform/0051013_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051014/func2std_xform/0051014_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051033/func2std_xform/0051033_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051034/func2std_xform/0051034_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051035/func2std_xform/0051035_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051201/func2std_xform/0051201_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051202/func2std_xform/0051202_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051203/func2std_xform/0051203_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051204/func2std_xform/0051204_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051205/func2std_xform/0051205_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051206/func2std_xform/0051206_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051208/func2std_xform/0051208_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051209/func2std_xform/0051209_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051210/func2std_xform/0051210_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051211/func2std_xform/0051211_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051212/func2std_xform/0051212_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051213/func2std_xform/0051213_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051214/func2std_xform/0051214_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051216/func2std_xform/0051216_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051217/func2std_xform/0051217_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051218/func2std_xform/0051218_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051220/func2std_xform/0051220_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051221/func2std_xform/0051221_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051222/func2std_xform/0051222_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051223/func2std_xform/0051223_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051224/func2std_xform/0051224_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051225/func2std_xform/0051225_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051227/func2std_xform/0051227_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051229/func2std_xform/0051229_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051231/func2std_xform/0051231_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051234/func2std_xform/0051234_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051235/func2std_xform/0051235_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051236/func2std_xform/0051236_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051237/func2std_xform/0051237_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051238/func2std_xform/0051238_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051239/func2std_xform/0051239_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051240/func2std_xform/0051240_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051241/func2std_xform/0051241_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051248/func2std_xform/0051248_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051249/func2std_xform/0051249_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051291/func2std_xform/0051291_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051292/func2std_xform/0051292_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051293/func2std_xform/0051293_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051294/func2std_xform/0051294_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051295/func2std_xform/0051295_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051296/func2std_xform/0051296_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051297/func2std_xform/0051297_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051298/func2std_xform/0051298_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051299/func2std_xform/0051299_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051300/func2std_xform/0051300_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051301/func2std_xform/0051301_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051302/func2std_xform/0051302_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051317/func2std_xform/0051317_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051350/func2std_xform/0051350_fc_map_flirt.nii.gz',
 '/home1/varunk/results_again_again/fc_motionRegress1filt1global1/_subject_id_0051351/func2std_xform/0051351_fc_map_flirt.nii.gz']

In [105]:
len(autistic_list),len(td_list)


Out[105]:
(40, 40)

In [106]:
# # To Stop execution Raise error:
# raise Exception('Execution stops here!')

In [107]:
# number_of_fcmaps = len(fc_file_list) #184

In [108]:
# number_of_fcmaps

In [109]:
# Author Deepak Singla: singlakdeepak5@gmail.com

import nibabel as nib
import numpy as np
import os
from scipy import stats
from numpy import ma
import scipy.special as special
from statsmodels.stats import multitest

def div0( a, b ):
    '''
    It is meant for ignoring the places where standard deviation 
    is zero.
    '''
    """ ignore / 0, div0( [-1, 0, 1], 0 ) -> [0, 0, 0] """
    with np.errstate(divide='ignore', invalid='ignore'):
        c = np.divide( a, b )
        # c[ ~ np.isfinite( c )] = 0  # -inf inf NaN
    return c

def calc_mean_and_std(ROICorrMaps, n_subjects, ROIAtlasmask, ddof =1, applyFisher = False):
    '''
	Function for calculating the mean and standard 
	deviation of the data. At a time, only one of the nii
	file is loaded and the elements keep on adding as we 
	enumerate over the subjects.
	'''
    mask = nib.load(ROIAtlasmask).get_data()
    mask = ma.masked_object(mask,0).mask
    if (n_subjects != 0):
        f = nib.load(ROICorrMaps[0])
        dimensions = f.get_header().get_data_shape()
        print(dimensions)
    else:
        exit
    mask = np.repeat(mask[:, :, :, np.newaxis], dimensions[3], axis=3)
#     print(ROICorrMaps)

    Sample_mean_Array = np.zeros(dimensions)
    Sample_std_Array = np.zeros(dimensions)
    Sample_mean_Array = ma.masked_array(Sample_mean_Array, 
                                       mask = mask, 
                                        fill_value = 0)
    Sample_std_Array = ma.masked_array(Sample_std_Array, 
                                      mask = mask , 
                                       fill_value = 0)
    for count, subject in enumerate(ROICorrMaps):
        Corr_data = nib.load(subject).get_data()
#         dimensions = Corr_data.get_header().get_data_shape()
#         mask = np.repeat(mask[:, :, :, np.newaxis], dimensions[3], axis=3)
        Corr_data = ma.masked_array(Corr_data, mask = mask)
        if applyFisher:
            Corr_data = np.arctanh(Corr_data)
        
        Sample_mean_Array += Corr_data
        Sample_std_Array += np.square(Corr_data)
        print('Done subject ', count+1)                                                                                                                                                                                                       
    Sample_mean_Array /= n_subjects
    Sample_std_Array = np.sqrt((Sample_std_Array - n_subjects*np.square(Sample_mean_Array))/(n_subjects - ddof))
    return Sample_mean_Array,Sample_std_Array

def calc_mean_and_std_if_npy(ROICorrMaps, n_subjects, ddof =1, applyFisher = False):
    '''
    Function to be used if the file is given in the format 
    No of ROIs versus All brain voxels in the ROI mapped.
    '''
    print(ROICorrMaps)
    initialize = np.load(ROICorrMaps[0])
    initialize = ma.masked_array(initialize)
    if applyFisher:
        initialize = np.arctanh(initialize)
    Sample_mean_Array = ma.masked_array(initialize, 
                                        fill_value = 0)
    Sample_std_Array = ma.masked_array(np.square(initialize), 
                                       fill_value = 0)
    del initialize
    print('Done subject ', 0)
    for count, subject in enumerate(ROICorrMaps[1:]):

        Corr_data = np.load(subject)
        Corr_data = ma.masked_array(Corr_data) 
        if applyFisher:
            Corr_data = np.arctanh(Corr_data)
        Sample_mean_Array += Corr_data
        Sample_std_Array += np.square(Corr_data)
        print('Done subject ', count+1)                                                                                                                                                                                               
    Sample_mean_Array /= n_subjects
    Sample_std_Array = np.sqrt((Sample_std_Array - n_subjects*np.square(Sample_mean_Array))/(n_subjects - ddof))
    return Sample_mean_Array,Sample_std_Array

def _ttest_1samp(Sample_mean_Array, Sample_std_Array, n_subjects, PopMean = 0.0):
    ttest_1samp_for_all = div0((Sample_mean_Array - PopMean) \
                            * np.sqrt(n_subjects), Sample_std_Array)
    df = n_subjects - 1
    # pval = stats.t.sf(np.abs(ttest_1samp_for_all), df)*2
    pval = special.betainc(0.5*df, 0.5, df/ \
            (df + ttest_1samp_for_all*ttest_1samp_for_all)).reshape(ttest_1samp_for_all.shape)
    # ttest_1samp_for_all, pval = ma.filled(ttest_1samp_for_all), ma.filled(pval)

    return ttest_1samp_for_all, pval


def ttest_1samp_for_all_ROIs(ROICorrMaps, 
                                ROIAtlasmask, 
                                PopMean = 0.0, 
                                applyFisher = False):
    '''
    This is the 1 sample t-test for ROI correlation maps.
    df = no of subjects - 1
    * ROICorrMaps is the list of filepaths of ROI correlation 
    maps for a group.
    * Each ROI correlation map has the 4th dimension equal to 
    the number of ROIs.
    * It calculates both the ttest as well as the p values.
    
    QUESTIONS???????????????????????????????????????????????
    For application of the Fisher transform, I saw that it is 
    same as the inverse hyperbolic tangent function. 
    Doubt is regarding the standard deviation of the distribution after
    applying Fisher. It was written that the sd is now 1/sqrt(no_of_subjs - 3).
    So, that means for each voxel or variable, the sd now becomes this.

    Ref: https://docs.scipy.org/doc/numpy/reference/generated/numpy.arctanh.html
     https://en.wikipedia.org/wiki/Fisher_transformation
    TO BE ASKED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    # The ttest will return t value Inf or NaN where the denom is
    # zero. See what to return in these places. Ask tomorrow.
    '''


    n_subjects = len(ROICorrMaps)
    assert (n_subjects>0)
    Sample_mean_Array, Sample_std_Array = calc_mean_and_std(ROICorrMaps, 
                                                            n_subjects,
                                                            ROIAtlasmask, ddof =1,
                                                            applyFisher = applyFisher)
    ttest_1samp_for_all, pval = _ttest_1samp(Sample_mean_Array, 
                                             Sample_std_Array,
                                             n_subjects,
                                             PopMean = PopMean)
    return ttest_1samp_for_all, pval


def ttest_1samp_ROIs_if_npy(ROICorrMaps,
                            PopMean = 0.0,
                            applyFisher = False):
    n_subjects = len(ROICorrMaps)
    assert (n_subjects>0)
    Sample_mean_Array, Sample_std_Array = \
                                calc_mean_and_std_if_npy( ROICorrMaps,
                                                        n_subjects, ddof =1,
                                                        applyFisher = applyFisher)
    return _ttest_1samp(Sample_mean_Array,
                        Sample_std_Array,
                        n_subjects,
                        PopMean = PopMean)


def _ttest_ind(Sample_mean_ArrayA, Sample_var_ArrayA, n_subjectsA,
                Sample_mean_ArrayB,Sample_var_ArrayB, n_subjectsB,
                equal_var = True):
    if equal_var:
        # force df to be an array for masked division not to throw a warning
        df = ma.asanyarray(n_subjectsA + n_subjectsB - 2.0)
        svar = ((n_subjectsA-1)*Sample_var_ArrayA+(n_subjectsB-1)*Sample_var_ArrayB)/ df
        denom = ma.sqrt(svar*(1.0/n_subjectsA + 1.0/n_subjectsB))  # n-D computation here!
    else:
        vn1 = Sample_var_ArrayA/n_subjectsA
        vn2 = Sample_var_ArrayB/n_subjectsB
        df = (vn1 + vn2)**2 / (vn1**2 / (n_subjectsA - 1) + vn2**2 / (n_subjectsB - 1))

        # If df is undefined, variances are zero.
        # It doesn't matter what df is as long as it is not NaN.
        df = np.where(np.isnan(df), 1, df)
        denom = ma.sqrt(vn1 + vn2)

    with np.errstate(divide='ignore', invalid='ignore'):
        ttest_ind = (Sample_mean_ArrayA - Sample_mean_ArrayB) / denom
    pvalues = special.betainc(0.5*df, 0.5, df/(df + ttest_ind*ttest_ind)).reshape(ttest_ind.shape)
    
    # ttest_ind, pvalues = ma.filled(ttest_ind), ma.filled(pvalues)
    return ttest_ind, pvalues,Sample_mean_ArrayA ,Sample_mean_ArrayB

def ttest_ind_samples(ROICorrMapsA, ROICorrMapsB, ROIAtlasmask, 
                    equal_var = True, applyFisher = False):
    '''
    Modified from https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.stats.ttest_ind.html ,
    https://github.com/scipy/scipy/blob/v0.19.1/scipy/stats/stats.py#L3950-L4072

    Since it didn't support if the data is large and everything can't be loaded at once. So,
    such modification has been made.
    '''

    n_subjectsA = len(ROICorrMapsA)
    n_subjectsB = len(ROICorrMapsB)
    assert (n_subjectsA > 0)
    assert (n_subjectsB > 0)
    Sample_mean_ArrayA, Sample_std_ArrayA = calc_mean_and_std(ROICorrMapsA, 
                                                              n_subjectsA,
                                                              ROIAtlasmask, ddof =1,
                                                              applyFisher = applyFisher)
    Sample_var_ArrayA = np.square(Sample_std_ArrayA)
    del(Sample_std_ArrayA)

    # n_subjectsB = len(ROICorrMapsB)
    Sample_mean_ArrayB, Sample_std_ArrayB = calc_mean_and_std(ROICorrMapsB, 
                                                              n_subjectsB,
                                                              ROIAtlasmask, ddof =1,
                                                              applyFisher = applyFisher)
    Sample_var_ArrayB = np.square(Sample_std_ArrayB)
    del(Sample_std_ArrayB)

    # pvalues = stats.t.sf(np.abs(ttest_ind), df)*2
    return _ttest_ind(Sample_mean_ArrayA, Sample_var_ArrayA, n_subjectsA,
                Sample_mean_ArrayB, Sample_var_ArrayB, n_subjectsB,
                equal_var = equal_var)

def ttest_ind_samples_if_npy(ROICorrMapsA, ROICorrMapsB, equal_var = True, applyFisher = False):
    '''
    Modified from https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.stats.ttest_ind.html ,
    https://github.com/scipy/scipy/blob/v0.19.1/scipy/stats/stats.py#L3950-L4072

    Since it didn't support if the data is large and everything can't be loaded at once. So,
    such modification has been made.
    '''

    n_subjectsA = len(ROICorrMapsA)
    n_subjectsB = len(ROICorrMapsB)
    assert (n_subjectsA > 0)
    assert (n_subjectsB > 0)
    Sample_mean_ArrayA, Sample_std_ArrayA = calc_mean_and_std_if_npy(ROICorrMapsA, 
                                                              n_subjectsA, ddof = 1,
                                                              applyFisher = applyFisher)
    Sample_var_ArrayA = np.square(Sample_std_ArrayA)
    del(Sample_std_ArrayA)
    Sample_mean_ArrayB, Sample_std_ArrayB = calc_mean_and_std_if_npy(ROICorrMapsB, 
                                                              n_subjectsB, ddof =1,
                                                              applyFisher = applyFisher)
    Sample_var_ArrayB = np.square(Sample_std_ArrayB)
    del(Sample_std_ArrayB)
    # pvalues = stats.t.sf(np.abs(ttest_ind), df)*2
    return _ttest_ind(Sample_mean_ArrayA, Sample_var_ArrayA, n_subjectsA,
                Sample_mean_ArrayB, Sample_var_ArrayB, n_subjectsB,
                equal_var = equal_var)

def convert_ma_to_np(MaskedArrayObj):
    return ma.filled(MaskedArrayObj)

def fdr_correction(pvals):
#     mask = np.ma.masked_object(mask,0).mask # Converts 1 -> 0 & 0 -> 1 in the mask
    brain_pvals = pvals[~pvals.mask]

Create an MNI 3mm brain mask


In [110]:
mask = opj(base_directory,parent_wf_directory,motion_correction_bet_directory,coreg_reg_directory,'resample_mni/MNI152_T1_2mm_brain_resample_mask.nii.gz')

In [111]:
mask


Out[111]:
'/home1/varunk/results_again_again/ABIDE1_Preprocess/motion_correction_bet/coreg_reg/resample_mni/MNI152_T1_2mm_brain_resample_mask.nii.gz'

In [ ]:


In [112]:
%%time
#  Author Deepak Singla : singlakdeepak5@gmail.com

import os
import numpy as np
import timeit
# start = timeit.default_timer()

# list1 = np.load('Grp1.npy')
# list2 = np.load('Grp2.npy')

apply_fisher = True
list1 = autistic_list[3:8]
list2 = td_list[3:8]
#Tvals, Pvals = ttest.ttest_1samp_for_all_ROIs(list1,'MNI152_T1_2mm_brain_mask.nii.gz')

Tvals, Pvals, meanC1, meanC2 = ttest_ind_samples(list1,list2,mask,applyFisher=apply_fisher)

fdr_mask_05 , fdr_p_vals = fdr_correction(Pvals, mask)

Tvals, Pvals, meanC1, meanC2 = convert_ma_to_np(Tvals), convert_ma_to_np(Pvals), convert_ma_to_np(meanC1), convert_ma_to_np(meanC2)
# stop = timeit.default_timer()
# print( Tvals)

# print (Pvals)



save_destination = hypothesis_test_dir

if not os.path.exists(save_destination):
    os.makedirs(save_destination) # Use makedirs if you want to create a nested directory structure
Tvals_path = opj(save_destination,'Tvals')
Pvals_path = opj(save_destination,'Pvals')
mean1_path = opj(save_destination,'meanC1')
mean2_path = opj(save_destination,'meanC2')

np.save(Tvals_path,Tvals)
np.save(Pvals_path,Pvals)
np.save(mean1_path,meanC1)
np.save(mean2_path,meanC2)


# print ('Time taken = %s'%(stop - start))


/root/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:33: 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
(61, 73, 61, 274)
Done subject  1
Done subject  2
Done subject  3
Done subject  4
Done subject  5
(61, 73, 61, 274)
Done subject  1
Done subject  2
Done subject  3
---------------------------------------------------------------------------
MaskError                                 Traceback (most recent call last)
<timed exec> in <module>()

<ipython-input-109-9bece2979d2e> in ttest_ind_samples(ROICorrMapsA, ROICorrMapsB, ROIAtlasmask, equal_var, applyFisher)
    206                                                               n_subjectsB,
    207                                                               ROIAtlasmask, ddof =1,
--> 208                                                               applyFisher = applyFisher)
    209     Sample_var_ArrayB = np.square(Sample_std_ArrayB)
    210     del(Sample_std_ArrayB)

<ipython-input-109-9bece2979d2e> in calc_mean_and_std(ROICorrMaps, n_subjects, ROIAtlasmask, ddof, applyFisher)
     50 #         dimensions = Corr_data.get_header().get_data_shape()
     51 #         mask = np.repeat(mask[:, :, :, np.newaxis], dimensions[3], axis=3)
---> 52         Corr_data = ma.masked_array(Corr_data, mask = mask)
     53         if applyFisher:
     54             Corr_data = np.arctanh(Corr_data)

~/anaconda3/lib/python3.6/site-packages/numpy/ma/core.py in __new__(cls, data, mask, dtype, copy, subok, ndmin, fill_value, keep_mask, hard_mask, shrink, order, **options)
   2891                     msg = "Mask and data not compatible: data size is %i, " + \
   2892                           "mask size is %i."
-> 2893                     raise MaskError(msg % (nd, nm))
   2894                 copy = True
   2895             # Set the mask to the new value

MaskError: Mask and data not compatible: data size is 74155809, mask size is 74427442.

In [ ]:
p = np.load('../results_again_again/hypothesis_test/hypothesis_test_pearcoff_motionRegress0filt0global0/Pvals.npy')

In [ ]:
orig_shape = p.shape
one_vector = p.reshape()

In [ ]:


In [ ]:


In [ ]:
p.min(),p.max() # Pmax is too high , maybe it is coz of the mask that I made. Coz all the p's almost unique
# Only the max p has highest frequency, it maybe coz they represent the number of non zero voxels

In [ ]:
uni, counts = np.unique(p, return_counts=True)

In [ ]:
X = zip(uni,counts)

In [ ]:
uni[-1]

In [ ]:
t = np.load('../results_again_again/hypothesis_test/Tvals.npy')

In [ ]:


In [ ]:


In [ ]:
t.min(), t.max()

In [ ]:
C1 = np.load('../results_again_again/hypothesis_test/pearcoff_motionRegress1filt1global1/meanC1.npy')

In [ ]:
C1.min(),C1.max()

In [ ]:
C2 = np.load('../results_again_again/hypothesis_test/meanC2.npy')

In [ ]:
C2.min(),C2.max()

In [ ]:
p = np.load('../results_again_again/hypothesis_test/pearcoff_motionRegress1filt0global0/Pvals.npy')

In [ ]:
p.min(),p.max()

Bugs

  • LEUVEN_1 50703 - 0050703_fc_map_flirt.nii.gz - It is the FC map transformed to MNI 3mm Space. But the transformation looks really bad.
  • 0050706_fc_map_flirt.nii.gz
  • 0050753_fc_map_flirt.nii.gz Coz the original brain was translated to left
  • check 0050752 ???

In [ ]:
import os
save_destination = '/home1/varunk/results_again_again/hypothesis_test/hypothesis_test_pearcoff_motionRegress0filt0global0'
os.path.exists(save_destination)

In [ ]:
os.mkdir(save_destination)

In [ ]:
import numpy as np
l = '/home1/varunk/results_again_again/hypothesis_test/hypothesis_test_pearcoff_motionRegress0filt0global0/Pvals.npy'

X = np.load(l)
X.shape