forked from ahsanMah/braintypicality-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_preprocessing.py
418 lines (376 loc) · 13 KB
/
run_preprocessing.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import glob
import os
import re
import sys
from concurrent.futures import ProcessPoolExecutor
from functools import partial
from time import time
import numpy as np
import tensorflow as tf
from tqdm.auto import tqdm
from mri_utils import (
get_bratsgliomapaths,
get_bratspedpaths,
get_camcanpaths,
get_contepaths,
get_ebdspaths,
get_hcpdpaths,
get_hcppaths,
get_ibispaths,
get_mslubpaths,
get_mssegpaths,
get_twinspaths,
get_contetriopaths,
register_and_match,
)
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
DATADIR = "/BEE/Connectome/ABCD/"
CACHEDIR = "/ASD/ahsan_projects/braintypicality/dataset/template_cache/"
gpus = tf.config.list_physical_devices("GPU")
if gpus:
print("Found GPUs:", gpus)
# Restrict TensorFlow to only use the first GPU
try:
tf.config.set_visible_devices(gpus[0], "GPU")
logical_gpus = tf.config.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
except RuntimeError as e:
# Visible devices must be set before GPUs have been initialized
print(e)
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices("GPU")
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
def runner(
path,
dataset,
save_sub_dir="processed_v2",
label_img=False,
run_segmentation=False,
compute_brain_mask=True,
do_bias_correction=True,
histogram_match=True,
histogram_match_points=5,
):
import ants
import antspynet
from antspynet.utilities import preprocess_brain_image
t1_path = t2_path = label_path = None
os.makedirs(f"/{DATADIR}/Users/amahmood/braintyp/{save_sub_dir}", exist_ok=True)
if dataset == "ABCD":
R = re.compile(r"Data\/sub-(.*)\/ses-")
subject_id = R.search(path).group(1)
t1_path = path
t2_path = path.replace("T1w", "T2w")
elif dataset == "IBIS":
subject_id, t1_path = path
subject_id = dataset + subject_id
t2_path = t1_path.replace("T1w", "T2w")
elif dataset == "EBDS":
subject_id, t1_path = path
subject_id = dataset + subject_id
t2_path = t1_path.replace("T1.nrrd", "T2.nrrd")
elif dataset == "HCPD":
subject_id, t1_path = path
t2_path = t1_path.replace("T1w_", "T2w_")
elif dataset in ["BRATS-PED", "BRATS-GLI"]:
subject_id, t1_path = path
t2_path = t1_path.replace("t1n.nii.gz", "t2w.nii.gz")
label_path = t1_path.replace("t1n.nii.gz", "seg.nii.gz")
elif dataset == "HCP":
subject_id, t1_path = path
t2_path = t1_path.replace("T1w_", "T2w_")
subject_id = dataset + subject_id
elif dataset == "MSLUB":
subject_id, t1_path = path
t2_path = t1_path.replace("T1W.nii.gz", "T2W.nii.gz")
dirpath = os.path.dirname(t1_path).replace("/raw", "")
# sid = os.path.basename(dirpath)
# label_path = os.path.join(dirpath, f"{sid}_consensus_gt.nii.gz")
label_path = t1_path.replace("T1W.nii.gz", "consensus_gt.nii.gz")
mask_path = label_path.replace("consensus_gt.nii.gz", "brainmask.nii.gz")
subject_id = dataset + subject_id
elif dataset == "CAMCAN":
subject_id, t1_path = path
t2_path = t1_path.replace("_T1w", "_T2w")
subject_id = dataset + subject_id
elif dataset == "MSSEG":
subject_id, t1_path = path
t2_path = t1_path.replace("_T1", "_T2")
subject_id = dataset + subject_id
label_path = t1_path.replace("_T1", "_lesion")
elif dataset in ["CONTE", "TWINS"]:
subject_id, t1_path = path
t2_path = t1_path.replace("T1_Bias_", "T2_Bias_regT1_")
subject_id = dataset + subject_id
elif dataset == "CONTE-TRIO":
subject_id, t1_path = path
t2_path = t1_path.replace("T1_", "T2_")
subject_id = dataset + subject_id
else:
raise NotImplementedError
t1_img = ants.image_read(t1_path)
t2_img = ants.image_read(t2_path)
mask_img = None
if dataset in ["CONTE-TRIO", "TWINS", "CONTE", "BRATS-PED", "BRATS-GLI", "EBDS"]:
assert not compute_brain_mask, "{dataset} is already brain masked"
mask_img = (t1_img > 0).astype("float32")
# if dataset == "MSLUB":
# assert not compute_brain_mask, "MSLUB has precomputed brain masks"
# mask_img = ants.image_read(mask_path)
# t1_img *= mask_img
# t2_img *= mask_img
# Rigid regsiter to MNI + hist normalization + min/max scaling
t1_img, t1_mask, registration = register_and_match(
t1_img,
modality="t1",
antsxnet_cache_directory=CACHEDIR,
verbose=False,
mask=None if compute_brain_mask else mask_img,
compute_brain_mask=compute_brain_mask,
histogram_match=histogram_match,
do_bias_correction=do_bias_correction,
histogram_match_points=histogram_match_points,
)
# Register t2 to the t1 already registered to MNI above
t2_img, t2_mask, _ = register_and_match(
t2_img,
modality="t2",
target_img=t1_img,
target_img_mask=t1_mask,
antsxnet_cache_directory=CACHEDIR,
verbose=False,
mask=None if compute_brain_mask else mask_img,
compute_brain_mask=compute_brain_mask,
histogram_match=histogram_match,
do_bias_correction=do_bias_correction,
histogram_match_points=histogram_match_points,
)
# Further apply the opposite modality masks to get a tighter brain crop
# the same as t1_mask & t2_mask
if compute_brain_mask:
combined_mask = t1_mask * t2_mask
t1_img = ants.mask_image(t1_img, combined_mask)
t2_img = ants.mask_image(t2_img, combined_mask)
preproc_img = ants.merge_channels([t1_img, t2_img])
fname = os.path.join(
f"/{DATADIR}/Users/amahmood/braintyp/{save_sub_dir}", f"{subject_id}.nii.gz"
)
preproc_img.to_filename(fname)
# Register label segmentations to new t1
if label_img:
assert label_path is not None
lab_img = ants.image_read(label_path)
lab_img = ants.apply_transforms(
fixed=t1_img,
moving=lab_img,
transformlist=registration["fwdtransforms"],
interpolator="genericLabel",
)
fname = os.path.join(
f"/{DATADIR}/Users/amahmood/braintyp/{save_sub_dir}",
f"{subject_id}_label.nii.gz",
)
lab_img.to_filename(fname)
if run_segmentation:
t1_preprocessing = preprocess_brain_image(
t1_img,
truncate_intensity=(0.01, 0.99),
brain_extraction_modality=None,
template="croppedMni152",
template_transform_type="antsRegistrationSyNQuickRepro[a]",
do_bias_correction=True,
do_denoising=True,
verbose=False,
)
t1_preprocessed = t1_preprocessing["preprocessed_image"]
t1_seg = antspynet.utilities.deep_atropos(
t1_preprocessed,
do_preprocessing=False,
# antsxnet_cache_directory=CACHEDIR,
verbose=True,
)["segmentation_image"]
t1_seg.to_filename(
f"/{DATADIR}/Users/amahmood/braintyp/segs/{subject_id}.nii.gz"
)
# Also register segmentations to new t1
t1_seg = ants.apply_transforms(
fixed=t1_img,
moving=t1_seg,
transformlist=registration["fwdtransforms"],
interpolator="genericLabel",
)
wm_mask = t1_seg == 3
t1_wm = t1_img * wm_mask
t1_wm = t1_wm[t1_wm > 0].ravel()
t2_wm = t2_img * wm_mask
t2_wm = t2_wm[t2_wm > 0].ravel()
# Save outputs
fname = os.path.join(
f"/{DATADIR}/Users/amahmood/braintyp/segs/", f"{subject_id}.npz"
)
np.savez_compressed(fname, **{"t1": t1_wm, "t2": t2_wm})
return
def run(paths, process_fn, chunksize=1):
start_idx = 0
start = time()
progress_bar = tqdm(
range(0, len(paths), chunksize),
total=len(paths) // chunksize,
initial=0,
desc="# Processed: ?",
)
# with ProcessPoolExecutor(max_workers=chunksize) as exc:
# for idx in progress_bar:
# idx_ = idx + start_idx
# result = list(exc.map(process_fn, paths[idx_ : idx_ + chunksize]))
# progress_bar.set_description("# Processed: {:d}".format(idx_))
for idx in progress_bar:
idx_ = idx + start_idx
if idx_ > len(progress_bar):
break
process_fn(paths[idx_])
progress_bar.set_description("# Processed: {:d}".format(idx_))
print("Time Taken: {:.3f}".format(time() - start))
if __name__ == "__main__":
dataset = sys.argv[1]
assert dataset in [
"CONTE-TRIO",
"TWINS",
"CONTE",
"MSSEG",
"CAMCAN",
"MSLUB",
"HCP",
"BRATS-GLI",
"BRATS-PED",
"EBDS",
"IBIS",
"HCPD",
"ABCD",
], "Dataset name must be defined"
if dataset == "CONTE-TRIO":
file_paths = get_contetriopaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower(),
run_segmentation=False,
compute_brain_mask=False,
),
)
elif dataset == "TWINS":
file_paths = get_twinspaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower(),
run_segmentation=False,
compute_brain_mask=False,
),
)
elif dataset == "CONTE":
file_paths = get_contepaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower(),
run_segmentation=False,
compute_brain_mask=False,
),
)
elif dataset == "MSSEG":
file_paths = get_mssegpaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower(),
run_segmentation=False,
label_img=True,
),
)
elif dataset == "CAMCAN":
file_paths = get_camcanpaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower(),
run_segmentation=False,
),
)
elif dataset in ["BRATS-PED", "BRATS-GLI"]:
if dataset == "BRATS-PED":
file_paths = get_bratspedpaths()
else:
file_paths = get_bratsgliomapaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower() + "-hist-m2",
compute_brain_mask=False,
label_img=True,
run_segmentation=False,
histogram_match_points=2,
),
)
elif dataset == "MSLUB":
file_paths = get_mslubpaths()
run(
file_paths,
partial(
runner,
dataset=dataset,
save_sub_dir=dataset.lower() + "-hist-m2",
label_img=True,
run_segmentation=False,
compute_brain_mask=True,
do_bias_correction=True,
histogram_match=True,
histogram_match_points=2,
),
)
elif dataset == "HCP":
file_paths = get_hcppaths()
run(file_paths, partial(runner, dataset=dataset))
elif dataset == "EBDS":
file_paths = get_ebdspaths()
run(
file_paths,
partial(runner, dataset=dataset, compute_brain_mask=False),
)
elif dataset == "IBIS":
file_paths = get_ibispaths()
run(file_paths, partial(runner, dataset=dataset))
elif dataset == "HCPD":
file_paths = get_hcpdpaths()
run(file_paths, partial(runner, dataset=dataset))
else: # get abcd paths
paths = glob.glob(
"/{DATADIR}/ImageData/Data/*/ses-baselineYear1Arm1/anat/*T1w.nii.gz"
)
R = re.compile(r"Data\/sub-(.*)\/ses-")
clean = lambda x: x.strip().replace("_", "")
with open("abcd_qc_passing_keys.txt", "r") as f:
abcd_qc_keys = set([clean(x) for x in f.readlines()])
file_paths = []
id_checker = lambda x: R.search(x).group(1) in abcd_qc_keys
file_paths = list(filter(id_checker, paths))
assert len(file_paths) == len(abcd_qc_keys)
run(file_paths, runner)