from bids.grabbids import BIDSLayout
from nipype.interfaces.fsl import (BET, ExtractROI, FAST, FLIRT, ImageMaths,
                                   MCFLIRT, SliceTimer, Threshold,Info, ConvertXFM,MotionOutliers)
from nipype.interfaces.afni import Resample
from nipype.interfaces.io import DataSink
from nipype.pipeline import Node, MapNode, Workflow, JoinNode
from nipype.interfaces.utility import IdentityInterface, Function
import os
from os.path import join as opj
from nipype.interfaces import afni
import nibabel as nib
import json    
import numpy as np

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




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

# base_directory = opj(s,'result') 
# parent_wf_directory = 'preprocessPipeline_ABIDE2_GU1_withfloat'
# child_wf_directory = 'coregistrationPipeline'

# data_directory = opj(s,"data/ABIDE2-BIDS/GU1")

# datasink_name = 'datasink_preprocessed_ABIDE2_GU1_withfloat'

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"])


# mask_file = '/media/varun/LENOVO4/Projects/result/preprocessPipeline/coregistrationPipeline/_subject_id_0050952/skullStrip/sub-0050952_T1w_resample_brain_mask.nii.gz'
# os.chdir(path)

# opj(base_directory,parent_wf_directory,motion_correction_bet_directory,coreg_reg_directory,'resample_mni')

brain_path = opj(base_directory,datasink_name,'preprocessed_brain_paths/brain_file_list.npy')
mask_path = opj(base_directory,datasink_name,'preprocessed_mask_paths/mask_file_list.npy')
atlas_path = opj(base_directory,datasink_name,'atlas_paths/atlas_file_list.npy')
tr_path = opj(base_directory,datasink_name,'tr_paths/tr_list.npy')
motion_params_path = opj(base_directory,datasink_name,'motion_params_paths/motion_params_file_list.npy')

func2std_mat_path = opj(base_directory, datasink_name,'joint_xformation_matrix_paths/joint_xformation_matrix_file_list.npy')

MNI3mm_path = opj(base_directory,parent_wf_directory,motion_correction_bet_directory,coreg_reg_directory,'resample_mni/MNI152_T1_2mm_brain_resample.nii') 

# brain_list = np.load('../results_again_again/ABIDE1_Preprocess_Datasink/preprocessed_brain_paths/brain_file_list.npy')

# brain_path,mask_path,atlas_path,tr_path,motion_params_path,func2std_mat_path

brain_path = np.load(brain_path)
mask_path = np.load(mask_path)
atlas_path = np.load(atlas_path)
tr_path = np.load(tr_path)
motion_params_path = np.load(motion_params_path)
func2std_mat_path = np.load(func2std_mat_path)

# for a,b,c,d,e in zip(brain_path,mask_path,atlas_path,tr_path,motion_params_path):
#     print (a,b,c,d,e,'\n')

"""
Explanation: Post processing:

Global Signal Regression using orthogonalization
Band Pass filtering 0.1 - 0.01 Hz
Motion regression using GLM
End of explanation
"""


smoothed_brains_in_file = '/home1/varunk/results_again_again/smoothed_brains.txt'
import re


smoothed_brains_paths = np.loadtxt(smoothed_brains_in_file, dtype = np.str)

smoothed_brains_subid = []
for path in smoothed_brains_paths:
    sub_id_extracted = re.search('.+_subject_id_(\d+)', path).group(1)
    print(sub_id_extracted)
    smoothed_brains_subid.append(int(sub_id_extracted))



smoothed_brains_paths

base_directory

smoothed_brains_subid

# Append the conplete brain path to smoothed brain paths
new_brain_paths = []
for path in smoothed_brains_paths:
    new_brain_paths.append(opj(base_directory, 'motionRegress1filt1global0smoothing1',path[2:]))

new_brain_paths

"""
Explanation: Read the smoothed files

The following files are filtered and smoothed motion parameter regressed
I got the file name using the command:
find . -name "sub-*_task-rest_run-1_bold_roi_st_mcf_residual_bp_smoothed.nii.gz" &gt; smoothed_brains.txt
Using the above extracted filenames to do the following:
Select the subjects having volumes atlest the required fixed number of volumes
perform correlation using just the fixed number of volumes
Register the file to the standard 3mm template
End of explanation
"""


import pandas as pd

demographics_file_path = '/home1/varunk/Autism-Connectome-Analysis-brain_connectivity/notebooks/demographics.csv'
phenotype_file_path = '/home1/varunk/data/ABIDE1/RawDataBIDs/composite_phenotypic_file.csv'

df_phenotype = pd.read_csv(phenotype_file_path)
df_phenotype = df_phenotype.sort_values(['SUB_ID'])
df_phenotype_sub_id = df_phenotype.as_matrix(['SITE_ID','SUB_ID']).squeeze()

df_demographics = pd.read_csv(demographics_file_path)
df_demographics_volumes = df_demographics.as_matrix(['SITE_NAME','VOLUMES']).squeeze()


# df_phenotype.sort_values(['SUB_ID'])

df_demographics_volumes

# SUB_ID - Volumes Dictionary
site_vol_dict = dict(zip(df_demographics_volumes[:,0], df_demographics_volumes[:,1]))

# for site_subid in df_demographics_volumes:
    

# subid_site_dict = dict(zip(df_phenotype_sub_id[:,1], df_phenotype_sub_id[:,0]))

subid_vol_dict = dict(zip(df_phenotype_sub_id[:,1],[site_vol_dict[site] for site in df_phenotype_sub_id[:,0]] ))

(subid_vol_dict)

vols = 120
del_idx = []
for idx,df in enumerate(df_demographics_volumes):
#     print(idx,df[1])
    if df[1] < vols:
        del_idx.append(idx)
        
df_demographics_volumes = np.delete(df_demographics_volumes,del_idx, axis = 0)

df_demographics_sites_refined = df_demographics_volumes[:,0]

df_demographics_sites_refined

df_phenotype_sub_id

"""
Explanation: Check which subjects have volumes > 'vol'
End of explanation
"""


subjects_refined = []
for df in df_phenotype_sub_id:
    if df[0] in df_demographics_sites_refined:
#         print(df[1])
        subjects_refined.append(df[1])

subjects_refined;

subjects_refined = list(set(subjects_refined) - (set(df_phenotype_sub_id[:,1]) - set(smoothed_brains_subid) ) )

len(subjects_refined)

"""
Explanation: Subject IDs to be considered -
subjects_refined
End of explanation
"""


#sanity check
set(df_phenotype_sub_id[:,1]) - set(subjects_refined)

"""
Explanation: Below are subject IDs to be ignored
End of explanation
"""


vols = vols - 4

def vol_correct(sub_id, subid_vol_dict, vols):
    sub_vols = subid_vol_dict[sub_id] - 4
    if sub_vols > vols:
        t_min = sub_vols - vols
    elif sub_vols == vols:
        t_min = 0
    else:
        raise Exception('Volumes of Sub ',sub_id,' less than desired!')
    return int(t_min)


volCorrect = Node(Function(function=vol_correct, input_names=['sub_id','subid_vol_dict','vols'],
                                output_names=['t_min']), name='volCorrect')

volCorrect.inputs.subid_vol_dict = subid_vol_dict
volCorrect.inputs.vols = vols

os.chdir('/home1/varunk/results_again_again/temp/')
volCorrect.inputs.sub_id = 51456
res = volCorrect.run()

res.outputs

# 146 - 116

# 0...145

# 145 - 30 + 1

# 146 - 116

# fslroi /home1/varunk/results_again_again/motionRegress1filt1global0smoothing1/_subject_id_0051456/spatialSmooth/sub-0051456_task-rest_run-1_bold_roi_st_mcf_residual_bp_smoothed.nii.gz /home1/varunk/results_again_again/temp_fc/motionRegress1filt1global0smoothing1/_subject_id_51456/extract/sub-0051456_task-rest_run-1_bold_roi_st_mcf_residual_bp_smoothed_roi.nii.gz 35 -1


# # ExtractROI - skip dummy scans
# extract = Node(ExtractROI(t_size=-1),
#                output_type='NIFTI',
#                name="extract")

# # t_min=4,

# layout = BIDSLayout(data_directory)

number_of_subjects = 2 # Number of subjects you wish to preprocess
# number_of_subjects = len(layout.get_subjects())

"""
Explanation: Volume correction

I have already extracted 4 volumes.
Now extract 120 - 4 = 116 volumes from each subject
So define vols = 114
End of explanation
"""


def get_subject_filenames_old(subject_id,brain_path,mask_path,atlas_path,tr_path,motion_params_path,func2std_mat_path,MNI3mm_path):
    import re

    for brain,mask,atlas,tr,motion_param,func2std_mat in zip(brain_path,mask_path,atlas_path,tr_path,motion_params_path,func2std_mat_path):
        sub_id_extracted = re.search('.+_subject_id_(\d+)', brain)
        
        if str(subject_id) in brain:
#             print("Files for subject ",subject_id,brain,mask,atlas,tr,motion_param)
            return brain,mask,atlas,tr,motion_param,func2std_mat,MNI3mm_path
        
    print ('Unable to locate Subject: ',subject_id,'extracted: ',sub_id_extracted)
    return 0
    

def get_subject_filenames(subject_id,brain_path,mask_path,atlas_path,tr_path,motion_params_path,func2std_mat_path,MNI3mm_path):
    import re

    for brain in brain_path:
        sub_id_extracted = re.search('.+_subject_id_(\d+)', brain)
        print(sub_id_extracted)
        
        if str(subject_id) in brain:
            for mask,atlas,tr,motion_param,func2std_mat in zip(mask_path,atlas_path,tr_path,motion_params_path,func2std_mat_path):
                if str(subject_id) in mask:
        #             print("Files for subject ",subject_id,brain,mask,atlas,tr,motion_param)
                    return brain,mask,atlas,tr,motion_param,func2std_mat,MNI3mm_path
        
    print ('Unable to locate Subject: ',subject_id,'extracted: ',sub_id_extracted)
    raise Exception('Unable to locate Subject: ',subject_id,'extracted: ',sub_id_extracted)
    return 0
    

# 555/60 # mask_path,new_brain_paths

len(new_brain_paths),len(mask_path), len(subjects_refined)

# Make a node
getSubjectFilenames = Node(Function(function=get_subject_filenames, input_names=['subject_id','brain_path','mask_path','atlas_path','tr_path','motion_params_path','func2std_mat_path','MNI3mm_path'],
                                output_names=['brain','mask','atlas','tr','motion_param','func2std_mat', 'MNI3mm_path']), name='getSubjectFilenames')


# getSubjectFilenames.inputs.brain_path = brain_path
getSubjectFilenames.inputs.brain_path = new_brain_paths
getSubjectFilenames.inputs.mask_path = mask_path
getSubjectFilenames.inputs.atlas_path = atlas_path
getSubjectFilenames.inputs.tr_path = tr_path
getSubjectFilenames.inputs.motion_params_path = motion_params_path
getSubjectFilenames.inputs.func2std_mat_path = func2std_mat_path
getSubjectFilenames.inputs.MNI3mm_path = MNI3mm_path

getSubjectFilenames.inputs.subject_id = 51270
res = getSubjectFilenames.run()

# import re
# text = '/home1/varunk/results_again_again/ABIDE1_Preprocess/motion_correction_bet/coreg_reg/atlas_resize_reg_directory/_subject_id_0050004/111std2func_xform/fullbrain_atlas_thr0-2mm_resample_flirt.nii'

# try:
#     found = re.search('.+_subject_id_(\d+)', text).group(1)
# except AttributeError:
#     # AAA, ZZZ not found in the original string
#     found = '' # apply your error handling

# # found: 1234


# found

subject_list = subjects_refined[0:number_of_subjects]

infosource = Node(IdentityInterface(fields=['subject_id']),
                  name="infosource")

infosource.iterables = [('subject_id',subject_list)]


# ,'brain_path','mask_path','atlas_path','tr_path','motion_params_path'
# infosource.brain_path = brain_path
# infosource.mask_path = mask_path
# infosource.atlas_path = atlas_path
# infosource.tr_path = tr_path
# infosource.motion_params_path = motion_params_path

"""
Explanation: Define a function to fetch the filenames of a particular subject ID
End of explanation
"""


# ExtractROI - skip dummy scans
extract = Node(ExtractROI(t_size=-1),
               output_type='NIFTI',
               name="extract")

# t_min=4,

"""
Explanation: Extract volumes
End of explanation
"""


# Create DataSink object
dataSink = Node(DataSink(), name='datasink')

# Name of the output folder
dataSink.inputs.base_directory = opj(base_directory,fc_datasink_name)


"""
Explanation: Datasink
I needed to define the structure of what files are saved and where.
End of explanation
"""


# Define substitution strings so that the data is similar to BIDS
substitutions = [('_subject_id_', 'sub-')]

# Feed the substitution strings to the DataSink node
dataSink.inputs.substitutions = substitutions

# ('_resample_brain_flirt.nii_brain', ''),
# ('_roi_st_mcf_flirt.nii_brain_flirt', ''),
                

base_directory

"""
Explanation: To create the substitutions I looked the datasink folder where I was redirecting the output. I manually selected the part of file/folder name that I wanted to change and copied below to be substituted.
End of explanation
"""


def save_file_list_function(in_fc_map_brain_file):
    # Imports
    import numpy as np
    import os
    from os.path import join as opj
    
    
    file_list = np.asarray(in_fc_map_brain_file)
    print('######################## File List ######################: \n',file_list)

    np.save('fc_map_brain_file_list',file_list)
    file_name = 'fc_map_brain_file_list.npy'
    out_fc_map_brain_file = opj(os.getcwd(),file_name) # path

    
    
    
    
    
    return out_fc_map_brain_file
    

save_file_list = JoinNode(Function(function=save_file_list_function, input_names=['in_fc_map_brain_file'],
                 output_names=['out_fc_map_brain_file']),
                 joinsource="infosource",
                 joinfield=['in_fc_map_brain_file'],
                 name="save_file_list")

"""
Explanation: Following is a Join Node that collects the preprocessed file paths and saves them in a file
End of explanation
"""


# Saves the brains instead of FC matrix files
def pear_coff(in_file, atlas_file, mask_file):
    # code to find how many voxels are in the brain region using the mask
    
        # imports
    import numpy as np
    import nibabel as nib
    import os
    from os.path import join as opj

    mask_data = nib.load(mask_file)
    mask = mask_data.get_data()

    x_dim, y_dim, z_dim = mask_data.shape

                    
    atlasPath = atlas_file
    # Read the atlas
    atlasObject = nib.load(atlasPath)
    atlas = atlasObject.get_data()
    
    num_ROIs = int((np.max(atlas) - np.min(atlas) ))


    # Read the brain in_file

    brain_data = nib.load(in_file)
    brain = brain_data.get_data()

    x_dim, y_dim, z_dim, num_volumes = brain.shape
    
    
    num_brain_voxels = 0

    x_dim, y_dim, z_dim = mask_data.shape

    for i in range(x_dim):
        for j in range(y_dim):
            for k in range(z_dim):
                if mask[i,j,k] == 1:
                    num_brain_voxels = num_brain_voxels + 1
    
    # Initialize a matrix of ROI time series and voxel time series

    ROI_matrix = np.zeros((num_ROIs, num_volumes))
    voxel_matrix = np.zeros((num_brain_voxels, num_volumes))
    
    # Fill up the voxel_matrix 

    voxel_counter = 0
    for i in range(x_dim):
        for j in range(y_dim):
            for k in range(z_dim):
                if mask[i,j,k] == 1:
                    voxel_matrix[voxel_counter,:] = brain[i,j,k,:] 
                    voxel_counter = voxel_counter + 1

                    
    # Fill up the ROI_matrix
    # Keep track of number of voxels per ROI as well by using an array - num_voxels_in_ROI[]

    num_voxels_in_ROI = np.zeros((num_ROIs,1)) # A column arrray containing number of voxels in each ROI

    for i in range(x_dim):
        for j in range(y_dim):
            for k in range(z_dim):
                label = int(atlas[i,j,k]) - 1
                if label != -1:
                    ROI_matrix[label,:] = np.add(ROI_matrix[label,:], brain[i,j,k,:])
                    num_voxels_in_ROI[label,0] = num_voxels_in_ROI[label,0] + 1

    ROI_matrix = np.divide(ROI_matrix,num_voxels_in_ROI) # I get nan coz of this! At places where num voxels = 0

    X, Y = ROI_matrix, voxel_matrix


    # Subtract mean from X and Y

    X = np.subtract(X, np.mean(X, axis=1, keepdims=True))
    Y = np.subtract(Y, np.mean(Y, axis=1, keepdims=True))

    temp1 = np.dot(X,Y.T)
    temp2 = np.sqrt(np.sum(np.multiply(X,X), axis=1, keepdims=True))
    temp3 = np.sqrt(np.sum(np.multiply(Y,Y), axis=1, keepdims=True))
    temp4 = np.dot(temp2,temp3.T)
    coff_matrix = np.divide(temp1, (temp4 + 1e-7))
    
    
    print('Saving X ')
    np.save('X',X)
    
    print('saved')
        
    # Check if any ROI is missing and replace the NAN values in coff_matrix by 0
    if np.argwhere(np.isnan(coff_matrix)).shape[0] != 0:
        print("Some ROIs are not present. Replacing NAN in coff matrix by 0")
        np.nan_to_num(coff_matrix, copy=False)

    # TODO: when I have added 1e-7 in the dinominator, then why did I feel the need to replace NAN by zeros 
    sub_id = in_file.split('/')[-1].split('.')[0].split('_')[0].split('-')[1]
    
    
    fc_file_name = sub_id + '_fc_map'
    
    print ("Pear Matrix calculated for subject: ",sub_id)

    roi_brain_matrix = coff_matrix
    brain_file = in_file


    x_dim, y_dim, z_dim, t_dim = brain.shape

    (brain_data.header).set_data_shape([x_dim,y_dim,z_dim,num_ROIs])

    brain_roi_tensor = np.zeros((brain_data.header.get_data_shape()))
    
    print("Creating brain for Subject-",sub_id)
    for roi in range(num_ROIs):
        brain_voxel_counter = 0
        for i in range(x_dim):
            for j in range(y_dim):
                for k in range(z_dim):
                    if mask[i,j,k] == 1:
                        brain_roi_tensor[i,j,k,roi] = roi_brain_matrix[roi,brain_voxel_counter]
                        brain_voxel_counter = brain_voxel_counter + 1

        
        assert (brain_voxel_counter == len(roi_brain_matrix[roi,:])) 
    print("Created brain for Subject-",sub_id)


    path = os.getcwd()
    fc_file_name = fc_file_name + '.nii.gz'
    out_file = opj(path,fc_file_name)
    
    brain_with_header = nib.Nifti1Image(brain_roi_tensor, affine=brain_data.affine,header = brain_data.header)
    nib.save(brain_with_header,out_file)
    
    
    fc_map_brain_file = out_file
    return fc_map_brain_file



# Again Create the Node and set default values to paths

pearcoff = Node(Function(function=pear_coff, input_names=['in_file','atlas_file','mask_file'],
                                output_names=['fc_map_brain_file']), name='pearcoff')


# output_names=['fc_map_brain_file']
# pearcoff.inputs.atlas_file = atlasPath
# pearcoff.inputs.num_brain_voxels = num_brain_voxels
# pearcoff.inputs.mask_file = mask_file

"""
Explanation: Create a FC node
This node:
1. Exracts the average time series of the brain ROI's using the atlas and stores 
    it as a matrix of size [ROIs x Volumes].
2. Extracts the Voxel time series and stores it in matrix of size [Voxels x Volumes]
Saving the Brains instead of FC matrices
End of explanation
"""


func2std_xform = Node(FLIRT(output_type='NIFTI_GZ',
                         apply_xfm=True), name="func2std_xform")

# %%time
# pearcoff.run()

# motion_param_reg = [True, False]
# global_signal_reg = [True, False]
# band_pass_filt= [True, False]
# for motion_param_regression, global_signal_regression, band_pass_filtering in zip(motion_param_reg, global_signal_reg, band_pass_filt):
#     print (motion_param_regression, global_signal_regression, band_pass_filtering)

"""
Explanation: IMPORTANT:

The ROI 255 has been removed due to resampling. Therefore the FC maps will have nan at that row. So don't use that ROI :)
I came to know coz I keep getting this error: RuntimeWarning: invalid value encountered in true_divide
To debug it, I read the coff matrix and checked its diagnol to discover the nan value.

Node for applying xformation matrix to functional data
End of explanation
"""


motion_param_regression = 1
band_pass_filtering = 1
global_signal_regression = 0
smoothing = 1
volcorrect = 1

num_proc = 7

combination = 'motionRegress' + str(int(motion_param_regression)) + 'filt' + \
              str(int(band_pass_filtering)) + 'global' + str(int(global_signal_regression)) + \
              'smoothing' + str(int(smoothing))
        
print("Combination: ",combination)

base_dir = opj(base_directory,functional_connectivity_directory)
# wf = Workflow(name=functional_connectivity_directory)
wf = Workflow(name=combination)

wf.base_dir = base_dir # Dir where all the outputs will be stored.

wf.connect([(infosource , getSubjectFilenames, [('subject_id','subject_id')])])


if motion_param_regression == 1 and global_signal_regression == 0 and band_pass_filtering == 1 and smoothing == 1 and volcorrect == 1: # 101

    wf.connect([(infosource, volCorrect, [('subject_id','sub_id')])])
    wf.connect([( getSubjectFilenames, extract, [('brain','in_file')])])
    wf.connect([( volCorrect, extract, [('t_min','t_min')])])
    
    wf.connect([( extract, pearcoff, [('roi_file','in_file')])])
    
    # wf.connect([( bandpass, pearcoff, [('out_file','in_file')])])
    wf.connect([( getSubjectFilenames, pearcoff, [('atlas','atlas_file')])])
    wf.connect([( getSubjectFilenames, pearcoff, [('mask','mask_file')])])

    # ---------------------------------------------------------------------------------------
    wf.connect([(pearcoff, func2std_xform, [('fc_map_brain_file','in_file')])])
    wf.connect([(getSubjectFilenames, func2std_xform, [('func2std_mat','in_matrix_file')])])
    wf.connect([(getSubjectFilenames, func2std_xform, [('MNI3mm_path','reference')])])

    #         -- send out file to save file list and then save the outputs



    folder_name = 'pearcoff_' + combination + '.@fc_map_brain_file'



    wf.connect([(func2std_xform,  save_file_list, [('out_file','in_fc_map_brain_file')])])
    # --------------------------------------------------------------------------------------------


    wf.connect([(save_file_list,  dataSink, [('out_fc_map_brain_file',folder_name)])])

#     wf.write_graph(graph2use='flat', format='png')
    from IPython.display import Image
    wf.write_graph(graph2use='exec', format='png', simple_form=True)
    
    wf.run('MultiProc', plugin_args={'n_procs': num_proc})
    file_name = opj(base_dir,combination,'graph_detailed.dot.png')
    Image(filename=file_name)

file_name = opj(base_dir,combination,'graph_detailed.dot.png')
Image(filename=file_name)

X = np.load("../../results_again_again/temp_dataSink/pearcoff_motionRegress1filt1global0smoothing1/fc_map_brain_file_list.npy")
X

!nipypecli show crash-20171228-144509-root-getSubjectFilenames.a1-6a701e42-c600-4b38-b4ba-71c175cbf567.pklz

import numpy as np
X = np.load('../results_again_again/fc_datasink/pearcoff_motionRegress0filt0global1/fc_map_brain_file_list.npy')
X.shape

X_temp4 = np.load('/home1/varunk/results_again_again/temp_fc/motionRegress1filt1global0smoothing1/_subject_id_51456/temp4_again.npy')
np.diag(X_temp4)

X_temp3 = np.load('/home1/varunk/results_again_again/temp_fc/motionRegress1filt1global0smoothing1/_subject_id_51456/temp3.npy')
(X_temp3).shape

X_temp2 = np.load('/home1/varunk/results_again_again/temp_fc/motionRegress1filt1global0smoothing1/_subject_id_51456/temp2.npy')
(X_temp2).shape

np.isnan(X_temp3).sum()

np.isnan(X_temp2).sum()

X_temp2[254]

X = np.load('/home1/varunk/results_again_again/temp_fc/motionRegress1filt1global0smoothing1/_subject_id_51456/X.npy')
X[254,:]

X = np.load('/home1/varunk/results_again_again/fc_datasink_volumes_corrected/pearcoff_motionRegress1filt1global0smoothing1/fc_map_brain_file_list.npy')
X.shape

"""
Explanation: Workflow to do just the pearcoff and fun2std transformation
End of explanation
"""


