import numpy as np
import os
from os.path import join as opj

# fc_file_list = None

"""
Explanation: This Script runs all the notebook files one by one.
End of explanation
"""


# Paths
path_cwd = os.getcwd()
path_split_list = path_cwd.split('/')
s = path_split_list[0:-1]
s = opj('/',*s) # *s converts list to path, # also it is very important to add '/' in the begining so it is read as directory later
base_directory = opj(s,'result')

# data_path = "data/ABIDE2_NYU1-BIDS-Preprocessed-GSR/NYU1/"
data_path = "data/ABIDE-BIDS-Preprocessed-GSR/NYU/"
wf_name = 'globalSignalRemoval_ABIDE1_UCLA1_GSR_FC'
datasink_name = 'datasink_ABIDE1_UCLA1_GSR_FC'

# % run functional_connectivity.ipynb

%%time
# Paths


# fc_file_path = opj(base_directory,'functionalConnectivity_GSR0_fisher0','save_fc_file_list/fc_file_list.npy')
fc_file_path = opj(base_directory,wf_name,'save_fc_file_list/fc_file_list.npy')
# fc_file_path = opj(base_directory,wf_name,'save_fc_file_list/fc_file_comp_list.npy')
# save_destination = opj(base_directory,wf_name)
# if not os.path.exists(save_destination):
#     os.mkdir(save_destination)


apply_fisher = True
fc_file_list = (np.load(fc_file_path))

# ABIDE1 - NYU
autism_start_index = 0
autism_end_index = 78
td_start_index = 79
td_end_index = 157 #183

# ABIDE2 - NYU1
# autism_start_index = 0
# autism_end_index = 29 # 47
# td_start_index = 48
# td_end_index = 77

# ABIDE1 UCLA_1
autism_start_index = 0
autism_end_index = 48
td_start_index = 49
td_end_index = 81

# GU1-NYU1
# participant_file = 'GU1_NYU1participants.tsv'
# pid_dx = np.loadtxt(participant_file,skiprows=1)

# autism_subject_indexes = np.array(np.where(pid_dx[:,1]==1)).squeeze()[0:84]
# td_subject_indexes = np.array(np.where(pid_dx[:,1]==2)).squeeze()[0:84]

autism_subject_indexes = np.arange(autism_start_index, autism_end_index+1 )[0:33]
td_subject_indexes = np.arange(td_start_index, td_end_index + 1 )

%run hypothesis_test-fast.ipynb

len(autism_subject_indexes),len(td_subject_indexes)

autism_subject_indexes,td_subject_indexes

dx

fc_file_list

%%time
# paths
Pval_file_path = opj(base_directory,wf_name,'hypothesis_testing/Pvals.npy')

%run fdr_correction.ipynb

Pval_file_path

res =(np.unique(np.where(fdrcorrected_brain_4Dtensor_mask[:,:,:,:] == 1)[3],  return_counts= True))
count_roi = zip(list(res[0]), list(res[1]))
set(count_roi)

res =(np.unique(np.where(fdrcorrected_brain_4Dtensor_mask[:,:,:,:] == 1)[3],  return_counts= True))
count_roi = zip(list(res[0]), list(res[1]))
set(count_roi)

opj(base_directory,wf_name,'save_fc_file_list/fc_file_list.npy')

autism_subject_indexes

%%time
# Paths
fc_file_path = opj(base_directory,wf_name,'save_fc_file_list/fc_file_list.npy')
fc_file_list = (np.load(fc_file_path))
Pvals_file_path = opj(base_directory,wf_name,'hypothesis_testing/Pvals.npy')
Qvals_file_path = opj(base_directory,wf_name,'fdr_correction/Qvals.npy')

# participant_file = 'GU1_NYU1participants.tsv'
# pid_dx = np.loadtxt(participant_file,skiprows=1)

# autism_subject_indexes = np.array(np.where(pid_dx[:,1]==1)).squeeze()[0:84]
# td_subject_indexes = np.array(np.where(pid_dx[:,1]==2)).squeeze()[0:84]



%run results.ipynb

wf_name
# To Stop execution Raise error:
# raise Exception('Execution stops here!')

"""
Explanation: By mistake I over wrote the functionalConnectivity and corrupted the FC maps. but datasink is still intact
End of explanation
"""


