-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_CONN.py
214 lines (181 loc) · 6.58 KB
/
run_CONN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Script that applies the CONN pipeline to a set of subjects
Uses the same structure as run_pipeline_prime.py
COPY script/Base_batch.m to the CONN base directory for this to work!
For each subject:
- Create new output directories
- Moves the necessary data to the new directories
- Converts the label generated by fastsurfer to MNI 1mm
- Generates a .mat file with all the selected subjects
- Runs it in parallel
- Extract the timeseries per region and the FC, save them with the
same format as the other pipeline
"""
import argparse
import pandas as pd
import os
from lib.data_loading import load_data
from joblib import Parallel, delayed
import scipy.io as sio
import subprocess
import numpy as np
def run_pipeline(row, out_dir, base_data_dir, batch):
"""
Run conn pipeline for a single subject
"""
# NOTE THAT WE NEED TO HAVE THE SAME TR FOR ALL SUBJECTS in a SINGLE project
# so, no need to do separate ones. Try to do single them
i = 0
# Start the loop
# subject information
subID = row.SubjID
center_to_process = row.CENTER
if row.QC == "N":
return None
# load data using same function
d = load_data(
f"{base_data_dir}/{center_to_process}/{subID}", subID, center_to_process
)
# create new directory in the output directory
# make directory
out_dir_subject = f"{out_dir}/{center_to_process}_Post/{subID}/CONN"
if not os.path.exists(out_dir_subject):
os.makedirs(out_dir_subject)
# create two directories for anat and fmri
if not os.path.exists(f"{out_dir_subject}/results/"):
os.makedirs(f"{out_dir_subject}/results/")
if not os.path.exists(f"{out_dir_subject}/fmri/"):
os.makedirs(f"{out_dir_subject}/fmri/")
# register labels to MNI 1mm
# and create labels with the same name (copying existing docs)
# copy:
# - fmri (nii and json, if it exists)
# - T1 base
# - MNI ROI
# - MNI ROI text
# THE CENTERS THAT HAVE JSON
if center_to_process in ["MILAN", "MAINZ", "NAPLES", "OSLO"]:
fmri_json = d["fMRI_json"]
os.system(f"cp {fmri_json} {out_dir_subject}/fmri")
fmri = d["fMRI"]
os.system(f"cp {fmri} {out_dir_subject}/fmri")
TR = d["TR"] / 1000
# Get recon_all path
recon_all_path = f"{out_dir}/{center_to_process}_Post/{subID}"
# Copy a .txt file for the labels of freesurfer (located somewhere) to the same place as the ROIs
file_newseg = f"{out_dir}/aparc.DKTatlas+aseg_newSeg.txt"
os.system(f"cp {file_newseg} {recon_all_path}/recon_all")
# Run the batch
# parameters
matlab_path = "''"
conn_path = "''"
func_path = f"{out_dir_subject}/fmri/{os.path.basename(fmri)}"
sliceorder = "'BIDS'" # DEPEND ON CENTER, TODO
scans_to_remove = 5
# if results doesnt exist, need to be done
if not os.path.exists(
f"{out_dir_subject}/conn_FC/results/preprocessing/ROI_Subject001_Condition000.mat"
):
function_call = (
"Base_batch('"
+ func_path
+ "', '"
+ recon_all_path
+ "', '"
+ out_dir_subject
+ "', str2num('"
+ str(TR)
+ "'), "
+ str(sliceorder)
+ ", "
+ str(scans_to_remove)
+ ");"
)
# run
print(
f'export MATLABPATH={conn_path}; {matlab_path} -nosplash -nodisplay -nodesktop -r "cd {out_dir_subject}; {function_call}"'
)
# redirect all output to file
# with open(output_file, 'w') as f:
cmd = subprocess.Popen(
f'export MATLABPATH={conn_path}; {matlab_path} -nosplash -nodesktop -r "cd {out_dir_subject}; {function_call}exit;"',
shell=True,
) # , stdout=f, stderr=f)
cmd.wait()
# load mat file with the timeseries per row
try:
mat_timeseries_path = f"{out_dir_subject}/conn_FC/results/preprocessing/ROI_Subject001_Condition000.mat"
mat_FC_path = f"{out_dir_subject}/conn_FC/results/firstlevel/FC1/resultsROI_Subject001_Condition001.mat"
mat_timeseries = sio.loadmat(mat_timeseries_path, squeeze_me=True)
mat_FC = sio.loadmat(mat_FC_path, squeeze_me=True)
except FileNotFoundError:
print("Results not found! Something went wrong with processing")
return 0
# save the ROI timeseries
# select last 76 items
timeseries = [ts for ts in mat_timeseries["data"][3:]]
corrlabel_ts = np.array(timeseries).T
# load FC
FC_CONN = mat_FC["Z"]
# Do own FC
fMRI_syn = np.corrcoef(corrlabel_ts.T)
fMRI_syn = np.nan_to_num(fMRI_syn)
# compute the zfisher correlation
z_fmri_syn = np.arctanh(fMRI_syn)
infs = np.isinf(z_fmri_syn).nonzero()
# replace the infs with 0
for idx in range(len(infs[0])):
z_fmri_syn[infs[0][idx]][infs[1][idx]] = 0
np.fill_diagonal(z_fmri_syn, 0)
# Save all versions (zscored and normal)
np.savetxt(f"{out_dir_subject}/results/r_matrix.csv", fMRI_syn, delimiter=",")
np.savetxt(f"{out_dir_subject}/results/zr_matrix.csv", z_fmri_syn, delimiter=",")
np.savetxt(f"{out_dir_subject}/results/conn_matrix.csv", FC_CONN, delimiter=",")
np.savetxt(f"{out_dir_subject}/results/corrlabel_ts.txt", corrlabel_ts)
# Copy QC results to a shared directory
parser = argparse.ArgumentParser()
parser.add_argument(
"--in_dir",
type=str,
required=True,
help="input dir with subject data, general input dir (MAGNIMS2021",
)
parser.add_argument(
"--in_csv", type=str, required=True, help="csv with the subject info (general csv)"
)
parser.add_argument(
"--out_dir",
type=str,
required=True,
help="A string argument (also general directory)",
)
parser.add_argument(
"--center", type=str, required=True, help="Select the centers to use"
)
parser.add_argument(
"--batch",
type=str,
required=True,
help="location of the .mat file with the base batch information",
)
parser.add_argument(
"--njobs", type=int, required=True, default=1, help="Number of jobs to use"
)
# Parse and print the results
args = parser.parse_args()
# select type of data loading
base_data_dir = args.in_dir
out_dir = args.out_dir
# get njobs and center to process
njobs = args.njobs
center_to_process = args.center
batch = args.batch
# read the csv
# esta a base dir, copiar
df_connect = pd.read_csv(args.in_csv)
currentDirectory = os.getcwd()
df_connect_todo = df_connect[df_connect.CENTER == center_to_process]
outputs = Parallel(n_jobs=args.njobs, backend="threading")(
delayed(run_pipeline)(row, out_dir, base_data_dir, batch)
for row in df_connect_todo.itertuples()
)