-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_backported_labels.py
executable file
·573 lines (484 loc) · 29.9 KB
/
create_backported_labels.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
from transform_handler import img_transform_exec, space_transpose, header_superpose
import os, re, sys, io, struct, socket, datetime
from pathlib import Path
import os
from file_tools import mkcdir, check_files
import glob
import warnings
import shutil
import numpy as np
import errno
import os
def get_info_SAMBA_headfile(SAMBA_headfile, verbose=False):
if os.path.exists(SAMBA_headfile):
with open(SAMBA_headfile, 'rb') as source:
if verbose: print('INFO : Extracting acquisition parameters')
header_size = source.read(4)
header_size = struct.unpack('I', header_size)
if verbose: print('INFO : Header size = ', int(header_size[0]))
i = 0
stopsign = 200
for line in source:
pattern1 = 'original_study_orientation'
rx1 = re.compile(pattern1, re.IGNORECASE | re.MULTILINE | re.DOTALL)
pattern2 = 'working_image_orientation'
rx2 = re.compile(pattern2, re.IGNORECASE | re.MULTILINE | re.DOTALL)
pattern3 = 'original_study_orientation'
rx1 = re.compile(pattern1, re.IGNORECASE | re.MULTILINE | re.DOTALL)
pattern4 = 'working_image_orientation'
rx2 = re.compile(pattern2, re.IGNORECASE | re.MULTILINE | re.DOTALL)
discount = re.compile('#', re.IGNORECASE | re.MULTILINE | re.DOTALL)
i += 1
if i == stopsign:
print("hi")
for a in rx1.findall(str(line)):
if len(discount.findall(str(line))) == 0:
orig_orientation = str(line).split('=')[1]
orig_orientation = orig_orientation.split('\\')[0]
for a in rx2.findall(str(line)):
if len(discount.findall(str(line))) == 0:
working_orientation = str(line).split('=')[1]
working_orientation = working_orientation.split('\\')[0]
else:
raise Exception('Cannot find SAMBA_headfile')
return orig_orientation, working_orientation
def create_backport_labels(subject, mainpath, project_name, prep_folder, atlas_labels, headfile=None, overwrite=False, verbose=True):
#mainpath = "/Volumes/Data/Badea/Lab/mouse/"
#gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
#project_name = "VBM_21ADDecode03_IITmean_RPI_fullrun"
#atlas_labels = "/Volumes/Data/Badea/Lab/atlas/IITmean_RPI/IITmean_RPI_labels.nii.gz"
out_dir_base = os.path.join(mainpath, f"{project_name}-results","connectomics")
out_dir = os.path.join(out_dir_base,subject)
mkcdir([out_dir_base,out_dir])
work_dir = os.path.join(mainpath, f"{project_name}-work")
dirty_dir = os.path.join(mainpath,"burn_after_reading")
mkcdir(dirty_dir)
template_type_prefix = os.path.basename(os.path.dirname(glob.glob(os.path.join(work_dir,"dwi","SyN*/"))[0]))
template_runs = glob.glob((os.path.join(work_dir,"dwi",template_type_prefix,"*/")))
mymax=-1
if mymax==-1:
for template_run in template_runs:
if "NoNameYet" in template_run and template_run[-4:-2] == "_i":
if int(template_run[-2]) > mymax:
mymax = int(template_run[-2])
final_template_run = template_run
if mymax==-1:
for template_run in template_runs:
if "dwiMDT_Control_n72" in template_run and template_run[-4:-2]=="_i":
if int(template_run[-2])>mymax:
mymax=int(template_run[-2])
final_template_run=template_run
if mymax == -1:
raise Exception(f"Could not find template runs in {os.path.join(mainpath, f'{project_name}-work','dwi',template_type_prefix)}")
#template_type_prefix = "faMDT_NoNameYet_n37_i6"
#final_template_run = "SyN_0p5_3_0p5_fa"
#final_ref = f"/mnt/munin6/Badea/Lab/human/AD_Decode/diffusion_prep_locale/diffusion_prep_{subject}/Reg_LPCA_{subject}_nii4D.nii.gz";
orient_string = os.path.join(prep_folder, f'{subject}_relative_orientation.txt')
if not os.path.exists(orient_string):
orient_strings = glob.glob(os.path.join(prep_folder, f'*_relative_orientation.txt'))
if np.size(orient_strings)>0:
orient_string = orient_strings[0]
if os.path.exists(orient_string):
orient_relative = open(orient_string, mode='r').read()
orientation_out = orient_relative.split(',')[0]
orientation_out = orientation_out.split(':')[1]
orientation_in = orient_relative.split(',')[1]
orientation_in = orientation_in.split(':')[1]
else:
orientation_out = "*"
warnings.warn("Could not find orientation file, may cause errors later")
orientation_in = "RAS"
orientation_out = "RAS"
if headfile is not None:
SAMBA_orientation_in, SAMBA_orientation_out= get_info_SAMBA_headfile(headfile)
else:
SAMBA_orientation_in, SAMBA_orientation_out = 'RAS', 'RAS'
trans = os.path.join(work_dir,"preprocess","base_images","translation_xforms",f"{subject}_0DerivedInitialMovingTranslation.mat")
rigid =os.path.join(work_dir,"dwi",f"{subject}_rigid.mat")
affine = os.path.join(work_dir,"dwi",f"{subject}_affine.mat")
MDT_to_subject = os.path.join(final_template_run,"reg_diffeo",f"MDT_to_{subject}_warp.nii.gz")
label_name = os.path.basename(atlas_labels)
label_name = label_name.split("_labels")[0]
MDT_to_atlas_affine = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"MDT_*_to_{label_name}_affine.mat")
atlas_to_MDT = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"{label_name}_to_MDT_warp.nii.gz")
listfiles = [trans, rigid, affine, MDT_to_subject, MDT_to_atlas_affine, atlas_to_MDT]
[trans, rigid, affine, MDT_to_subject, MDT_to_atlas_affine, atlas_to_MDT], exists = check_files([trans,rigid,affine,MDT_to_subject,MDT_to_atlas_affine,atlas_to_MDT])
if not np.all(exists):
for i in np.arange(np.size(exists)):
if exists[i] is False:
print(f'could not find {listfiles[i]}')
filenotfound = listfiles[i]
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), filenotfound)
preprocess_ref = os.path.join(work_dir,"preprocess",f"{subject}_fa_masked.nii.gz")
preprocess_labels = os.path.join(dirty_dir,f"{subject}_preprocess_labels.nii.gz")
fixed_preprocess_labels = os.path.join(dirty_dir,f"{subject}_fixed_preprocess_labels.nii.gz")
coreg_labels = os.path.join(dirty_dir,f"{subject}_{SAMBA_orientation_in}_labels.nii.gz")
coreg_reorient_labels = os.path.join(dirty_dir,f"{subject}_{orientation_in}_labels.nii.gz")
# final_ref=f"/mnt/munin6/Badea/Lab/mouse/co_reg_LPCA_${subject:1:5}_m00-results/Reg_LPCA_${subject:1:5}_nii4D.nii.gz";
subjspace_coreg = os.path.join(prep_folder, f"{subject}_subjspace_coreg.nii.gz")
final_refs = [subjspace_coreg]
abb20s = glob.glob(f'/Volumes/dusom_civm-atlas/20.abb.15/research/diffusion{subject}*/nii4D*{subject}.nii')
if np.size(abb20s)>0:
final_refs.append(abb20s[0])
abb19s = glob.glob(f'/Volumes/dusom_civm-atlas/19.abb.14/research/diffusion{subject}*/nii4D*{subject}.nii')
if np.size(abb19s)>0:
final_refs.append(abb19s[0])
abb18s = glob.glob(f'/Volumes/dusom_civm-atlas/18.abb.11/research/diffusion{subject}*/nii4D*{subject}.nii')
if np.size(abb18s) > 0:
final_refs.append(abb18s[0])
final_ref = None
for pos_final_ref in final_refs:
if isinstance(pos_final_ref, str) and os.path.exists(pos_final_ref):
if Path(pos_final_ref).is_symlink():
final_ref=str(Path(pos_final_ref).resolve())
break
else:
final_ref = pos_final_ref
break
if final_ref is None:
txt = f"Could not find final registered subject file for subject {subject}"
warnings.warn(txt)
return
symbolic_ref = os.path.join(out_dir,f"{subject}_Reg_LPCA_nii4D.nii.gz")
final_labels = os.path.join(out_dir,f"{subject}_{label_name}_labels.nii.gz")
final_labels_backup = os.path.join(dirty_dir,f"{subject}_{label_name}_labels.nii.gz")
superpose=True
if not os.path.exists(final_labels) or overwrite:
if verbose:
print(f"Backporting labels to raw space for subject: {subject} to {final_labels}")
if not os.path.exists(preprocess_labels) or overwrite:
#Note, it used to be MultiLabel[1.0x1.0x1.0,2] but it seems like the default parameters tend to be based on the image themselves and work fine,
#so be careful but I removed it for now so it would work on different image sets
cmd = f"antsApplyTransforms -v 1 -d 3 -i {atlas_labels} -o {preprocess_labels} -r {preprocess_ref} -n MultiLabel -t [{trans},1] [{rigid},1] [{affine},1] {MDT_to_subject} [{MDT_to_atlas_affine},1] {atlas_to_MDT}"
if verbose:
print(f"Runnings the Ants apply transforms to {atlas_labels}")
print(cmd)
check_files([atlas_labels,preprocess_ref,trans,rigid,affine,MDT_to_subject,MDT_to_atlas_affine,atlas_to_MDT])
os.system(cmd)
#if os.path.exists(preprocess_labels) and ((not os.path.exists(fixed_preprocess_labels)) or overwrite):
# header_superpose(final_ref, preprocess_labels, outpath=fixed_preprocess_labels)
if os.path.exists(preprocess_labels) and ((not os.path.exists(fixed_preprocess_labels)) or overwrite):
shutil.copy(preprocess_labels,fixed_preprocess_labels) #must find a temp reference if we want the header superpose to work before rotation!!!!
if os.path.exists(fixed_preprocess_labels) and (not os.path.exists(coreg_labels) or overwrite):
if SAMBA_orientation_out != SAMBA_orientation_in:
if verbose:
print(f"Reorientation from {SAMBA_orientation_out} back to {SAMBA_orientation_in}")
img_transform_exec(fixed_preprocess_labels, SAMBA_orientation_out, SAMBA_orientation_in,
output_path=coreg_labels)
else:
if verbose:
print(f"Orientations are the same, skipping reorientation")
#if coreg_labels != coreg_reorient_labels:
# warnings.warn('There is a discrepancy between the stated input orientation in headfile and the '
# 'one found by find_relative_orientation_by_CoM.bash, beware')
# coreg_reorient_labels = coreg_labels
if os.path.exists(coreg_labels) and (not os.path.exists(coreg_reorient_labels) or overwrite):
if orientation_out != orientation_in:
if verbose:
print(f"Reorientation from {orientation_out} back to {orientation_in}")
img_transform_exec(coreg_labels, orientation_out, orientation_in, coreg_reorient_labels)
else:
if verbose:
print(f"Orientations are the same, skipping reorientation")
shutil.copy(coreg_labels, coreg_reorient_labels)
if os.path.exists(coreg_reorient_labels):
header_superpose(final_ref, coreg_reorient_labels, outpath=final_labels)
#cmd = f"{os.path.join(gunniespath, 'nifti_header_splicer.bash')} {final_ref} {coreg_labels} {final_labels}"
#os.system(cmd)
if os.path.exists(final_labels):
if verbose:
print(f"Applying fsl maths to {final_labels}")
cmd = f"fslmaths {final_labels} -add 0 {final_labels} -odt short"
os.system(cmd)
shutil.copy(final_labels, final_labels_backup)
else:
print(f"Already calculated the label file for subject {subject}")
skip_making_data_package_for_tractography = True
if not skip_making_data_package_for_tractography:
print('if you see this, make the bval fixes')
if not os.path.exists(symbolic_ref):
cmd=f"ln - s {final_ref} {symbolic_ref}"
bvals = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvals.txt")
bvecs = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvecs.txt")
bval_copy = os.path.join(out_dir, f"{subject}_bvals.txt")
bvec_copy = os.path.join(out_dir, f"{subject}_bvecs.txt")
if not os.path.exists(bval_copy):
shutil.copy(bvals, bval_copy)
if not os.path.exists(bvec_copy):
shutil.copy(bvecs, bvec_copy)
def create_backport_labels_2022preaprilChavez_backup(subject, mainpath, project_name, prep_folder, atlas_labels, headfile=None, overwrite=False, verbose=True):
#mainpath = "/Volumes/Data/Badea/Lab/mouse/"
#gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
#project_name = "VBM_21ADDecode03_IITmean_RPI_fullrun"
#atlas_labels = "/Volumes/Data/Badea/Lab/atlas/IITmean_RPI/IITmean_RPI_labels.nii.gz"
out_dir_base = os.path.join(mainpath, f"{project_name}-results","connectomics")
out_dir = os.path.join(out_dir_base,subject)
mkcdir([out_dir_base,out_dir])
work_dir = os.path.join(mainpath, f"{project_name}-work")
dirty_dir = os.path.join(mainpath,"burn_after_reading")
mkcdir(dirty_dir)
template_type_prefix = os.path.basename(os.path.dirname(glob.glob(os.path.join(work_dir,"dwi","SyN*/"))[0]))
template_runs = glob.glob((os.path.join(work_dir,"dwi",template_type_prefix,"*/")))
mymax=-1
if mymax==-1:
for template_run in template_runs:
if "NoNameYet" in template_run and template_run[-4:-2] == "_i":
if int(template_run[-2]) > mymax:
mymax = int(template_run[-2])
final_template_run = template_run
if mymax==-1:
for template_run in template_runs:
if "dwiMDT_Control_n72" in template_run and template_run[-4:-2]=="_i":
if int(template_run[-2])>mymax:
mymax=int(template_run[-2])
final_template_run=template_run
if mymax == -1:
raise Exception(f"Could not find template runs in {os.path.join(mainpath, f'{project_name}-work','dwi',template_type_prefix)}")
#template_type_prefix = "faMDT_NoNameYet_n37_i6"
#final_template_run = "SyN_0p5_3_0p5_fa"
#final_ref = f"/mnt/munin6/Badea/Lab/human/AD_Decode/diffusion_prep_locale/diffusion_prep_{subject}/Reg_LPCA_{subject}_nii4D.nii.gz";
orient_string = os.path.join(prep_folder, f'{subject}_relative_orientation.txt')
if not os.path.exists(orient_string):
orient_strings = glob.glob(os.path.join(prep_folder, f'*_relative_orientation.txt'))
if np.size(orient_strings)>0:
orient_string = orient_strings[0]
if os.path.exists(orient_string):
orient_relative = open(orient_string, mode='r').read()
orientation_out = orient_relative.split(',')[0]
orientation_out = orientation_out.split(':')[1]
orientation_in = orient_relative.split(',')[1]
orientation_in = orientation_in.split(':')[1]
else:
orientation_out = "*"
warnings.warn("Could not find orientation file, may cause errors later")
orientation_in = "RAS"
orientation_out = "RAS"
if headfile is not None:
SAMBA_orientation_in, SAMBA_orientation_out = get_info_SAMBA_headfile(headfile)
else:
SAMBA_orientation_in, SAMBA_orientation_out = 'RAS', 'RAS'
trans = os.path.join(work_dir,"preprocess","base_images","translation_xforms",f"{subject}_0DerivedInitialMovingTranslation.mat")
rigid =os.path.join(work_dir,"dwi",f"{subject}_rigid.mat")
affine = os.path.join(work_dir,"dwi",f"{subject}_affine.mat")
MDT_to_subject = os.path.join(final_template_run,"reg_diffeo",f"MDT_to_{subject}_warp.nii.gz")
label_name = os.path.basename(atlas_labels)
label_name = label_name.split("_labels")[0]
MDT_to_atlas_affine = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"MDT_*_to_{label_name}_affine.mat")
atlas_to_MDT = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"{label_name}_to_MDT_warp.nii.gz")
listfiles = [trans, rigid, affine, MDT_to_subject, MDT_to_atlas_affine, atlas_to_MDT]
[trans, rigid, affine, MDT_to_subject, MDT_to_atlas_affine, atlas_to_MDT], exists = check_files([trans,rigid,affine,MDT_to_subject,MDT_to_atlas_affine,atlas_to_MDT])
if not np.all(exists):
for i in np.arange(np.size(exists)):
if exists[i] is False:
print(f'could not find {listfiles[i]}')
filenotfound = listfiles[i]
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), filenotfound)
preprocess_ref = os.path.join(work_dir,"preprocess",f"{subject}_fa_masked.nii.gz")
preprocess_labels = os.path.join(dirty_dir,f"{subject}_preprocess_labels.nii.gz")
fixed_preprocess_labels = os.path.join(dirty_dir,f"{subject}_fixed_preprocess_labels.nii.gz")
coreg_labels = os.path.join(dirty_dir,f"{subject}_{orientation_out}_labels.nii.gz")
coreg_reorient_labels = os.path.join(dirty_dir,f"{subject}_{SAMBA_orientation_in}_labels.nii.gz")
# final_ref=f"/mnt/munin6/Badea/Lab/mouse/co_reg_LPCA_${subject:1:5}_m00-results/Reg_LPCA_${subject:1:5}_nii4D.nii.gz";
subjspace_coreg = os.path.join(prep_folder, f"{subject}_subjspace_coreg.nii.gz")
final_refs = [subjspace_coreg]
abb20s = glob.glob(f'/Volumes/dusom_civm-atlas/20.abb.15/research/diffusion{subject}*/nii4D*{subject}.nii')
if np.size(abb20s)>0:
final_refs.append(abb20s[0])
abb19s = glob.glob(f'/Volumes/dusom_civm-atlas/19.abb.14/research/diffusion{subject}*/nii4D*{subject}.nii')
if np.size(abb19s)>0:
final_refs.append(abb19s[0])
abb18s = glob.glob(f'/Volumes/dusom_civm-atlas/18.abb.11/research/diffusion{subject}*/nii4D*{subject}.nii')
if np.size(abb18s) > 0:
final_refs.append(abb18s[0])
final_ref = None
for pos_final_ref in final_refs:
if isinstance(pos_final_ref, str) and os.path.exists(pos_final_ref):
if Path(pos_final_ref).is_symlink():
final_ref=str(Path(pos_final_ref).resolve())
break
else:
final_ref = pos_final_ref
break
if final_ref is None:
txt = f"Could not find final registered subject file for subject {subject}"
warnings.warn(txt)
return
symbolic_ref = os.path.join(out_dir,f"{subject}_Reg_LPCA_nii4D.nii.gz")
final_labels = os.path.join(out_dir,f"{subject}_{label_name}_labels.nii.gz")
final_labels_backup = os.path.join(dirty_dir,f"{subject}_{label_name}_labels.nii.gz")
superpose=True
if not os.path.exists(final_labels) or overwrite:
if verbose:
print(f"Backporting labels to raw space for subject: {subject} to {final_labels}")
if not os.path.exists(preprocess_labels) or overwrite:
#Note, it used to be MultiLabel[1.0x1.0x1.0,2] but it seems like the default parameters tend to be based on the image themselves and work fine,
#so be careful but I removed it for now so it would work on different image sets
cmd = f"antsApplyTransforms -v 1 -d 3 -i {atlas_labels} -o {preprocess_labels} -r {preprocess_ref} -n MultiLabel -t [{trans},1] [{rigid},1] [{affine},1] {MDT_to_subject} [{MDT_to_atlas_affine},1] {atlas_to_MDT}"
if verbose:
print(f"Runnings the Ants apply transforms to {atlas_labels}")
print(cmd)
check_files([atlas_labels,preprocess_ref,trans,rigid,affine,MDT_to_subject,MDT_to_atlas_affine,atlas_to_MDT])
os.system(cmd)
if os.path.exists(preprocess_labels) and ((not os.path.exists(fixed_preprocess_labels)) or overwrite):
header_superpose(final_ref, preprocess_labels, outpath=fixed_preprocess_labels)
if os.path.exists(fixed_preprocess_labels) and (not os.path.exists(coreg_labels) or overwrite):
if orientation_out != orientation_in:
if verbose:
print(f"Reorientation from {orientation_out} back to {orientation_in}")
img_transform_exec(fixed_preprocess_labels, orientation_out, orientation_in, coreg_labels)
else:
if verbose:
print(f"Orientations are the same, skipping reorientation")
shutil.copy(fixed_preprocess_labels, coreg_labels)
if os.path.exists(coreg_labels) and (not os.path.exists(coreg_reorient_labels) or overwrite):
if SAMBA_orientation_out != SAMBA_orientation_in:
if verbose:
print(f"Reorientation from {SAMBA_orientation_out} back to {SAMBA_orientation_in}")
img_transform_exec(coreg_labels, SAMBA_orientation_out, SAMBA_orientation_in,
output_path=coreg_reorient_labels)
else:
if verbose:
print(f"Orientations are the same, skipping reorientation")
if coreg_labels != coreg_reorient_labels:
warnings.warn('There is a discrepancy between the stated input orientation in headfile and the '
'one found by find_relative_orientation_by_CoM.bash, beware')
coreg_reorient_labels = coreg_labels
if os.path.exists(coreg_reorient_labels):
header_superpose(final_ref, coreg_reorient_labels, outpath=final_labels)
#cmd = f"{os.path.join(gunniespath, 'nifti_header_splicer.bash')} {final_ref} {coreg_labels} {final_labels}"
#os.system(cmd)
if os.path.exists(final_labels):
if verbose:
print(f"Applying fsl maths to {final_labels}")
cmd = f"fslmaths {final_labels} -add 0 {final_labels} -odt short"
os.system(cmd)
shutil.copy(final_labels, final_labels_backup)
else:
print(f"Already calculated the label file for subject {subject}")
skip_making_data_package_for_tractography = True
if not skip_making_data_package_for_tractography:
print('if you see this, make the bval fixes')
if not os.path.exists(symbolic_ref):
cmd=f"ln - s {final_ref} {symbolic_ref}"
bvals = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvals.txt")
bvecs = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvecs.txt")
bval_copy = os.path.join(out_dir, f"{subject}_bvals.txt")
bvec_copy = os.path.join(out_dir, f"{subject}_bvecs.txt")
if not os.path.exists(bval_copy):
shutil.copy(bvals, bval_copy)
if not os.path.exists(bvec_copy):
shutil.copy(bvecs, bvec_copy)
def convert_images_templatespace(subject, mainpath, project_name, subject_img_path, orient_string, preppath = None, gunniespath="/Users/alex/bass/gitfolder/wuconnectomes/gunnies/", recenter=0, verbose=True):
#mainpath = "/Volumes/Data/Badea/Lab/mouse/"
#gunniespath = "/Users/alex/bass/gitfolder/wuconnectomes/gunnies/"
#project_name = "VBM_21ADDecode03_IITmean_RPI_fullrun"
#atlas_labels = "/Volumes/Data/Badea/Lab/atlas/IITmean_RPI/IITmean_RPI_labels.nii.gz"
out_dir = os.path.join(mainpath, f"{project_name}-results","connectomics",subject)
work_dir = os.path.join(mainpath, f"{project_name}-work")
dirty_dir = os.path.join(mainpath,"burn_after_reading")
mkcdir(dirty_dir)
template_type_prefix = os.path.basename(os.path.dirname(glob.glob(os.path.join(work_dir,"dwi","SyN*/"))[0]))
template_runs = glob.glob((os.path.join(work_dir,"dwi",template_type_prefix,"*/")))
mymax=-1
for template_run in template_runs:
if "NoNameYet" in template_run and template_run[-4:-2]=="_i":
if int(template_run[-2])>mymax:
mymax=int(template_run[-2])
final_template_run=template_run
if mymax==-1:
raise Exception(f"Could not find template runs in {os.path.join(mainpath, f'{project_name}-work','dwi',template_type_prefix)}")
#template_type_prefix = "faMDT_NoNameYet_n37_i6"
#final_template_run = "SyN_0p5_3_0p5_fa"
#final_ref = f"/mnt/munin6/Badea/Lab/human/AD_Decode/diffusion_prep_locale/diffusion_prep_{subject}/Reg_LPCA_{subject}_nii4D.nii.gz";
if os.path.exists(orient_string):
orient_relative = open(orient_string, mode='r').read()
orientation_out = orient_relative.split(',')[0]
orientation_out = orientation_out.split(':')[1]
orientation_in = orient_relative.split(',')[1]
orientation_in = orientation_in.split(':')[1]
else:
orientation_out = "*"
warnings.warn("Could not find orientation file, may cause errors later")
orientation_in = "LPS"
orientation_out = "RAS"
trans = os.path.join(work_dir,"preprocess","base_images","translation_xforms",f"{subject}_0DerivedInitialMovingTranslation.mat")
rigid =os.path.join(work_dir,"dwi",f"{subject}_rigid.mat")
affine = os.path.join(work_dir,"dwi",f"{subject}_affine.mat")
#MDT_to_subject = os.path.join(final_template_run,"reg_diffeo",f"MDT_to_{subject}_warp.nii.gz")
#MDT_to_atlas_affine = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"MDT_*_to_{label_name}_affine.mat")
#atlas_to_MDT = os.path.join(final_template_run,"stats_by_region","labels","transforms",f"{label_name}_to_MDT_warp.nii.gz")
[trans, rigid, affine], _ = check_files([trans,rigid,affine])
preprocess_ref = os.path.join(work_dir,"preprocess",f"{subject}_fa_masked.nii.gz")
preprocess_labels = os.path.join(dirty_dir,f"{subject}_preprocess_labels.nii.gz")
fixed_preprocess_labels = os.path.join(dirty_dir,f"{subject}_fixed_preprocess_labels.nii.gz")
coreg_labels = os.path.join(dirty_dir,f"{subject}_{orientation_in}_labels.nii.gz")
# final_ref="/mnt/munin6/Badea/Lab/mouse/co_reg_LPCA_${subject:1:5}_m00-results/Reg_LPCA_${subject:1:5}_nii4D.nii.gz";
if preppath is None:
inputsfolder = os.path.join(mainpath, f"{project_name}-inputs")
final_ref = os.path.join(inputsfolder, f"{subject}_coreg.nii.gz")
if os.path.exists(final_ref):
if Path(final_ref).is_symlink():
final_ref=str(Path(final_ref).resolve())
else:
raise Exception("Could not find final registered subject file")
mytype="fa"
symbolic_ref = os.path.join(out_dir,f"{subject}_Reg_LPCA_nii4D.nii.gz")
final_image_test = os.path.join(out_dir,f"{subject}_{mytype}_to_MDT_test.nii.gz")
overwrite = False
if not os.path.exists(final_image_test) or overwrite:
if verbose:
print(f"Porting subject to MDT for subject: {subject} to {final_image_test}")
img_transposed_test = os.path.join(dirty_dir,f"{subject}_{type}_transposed_test.nii.gz")
img_transposed_test_2 = os.path.join(dirty_dir,f"{subject}_{type}_transposed_test_2.nii.gz")
if os.path.exists(subject_img_path) and preprocess_ref:
header_superpose(preprocess_ref, subject_img_path, outpath=img_transposed_test)
cmd = f"antsApplyTransforms -v 1 -d 3 -i {subject_img_path} -o {img_transposed_test_2} -r {preprocess_ref} -n MultiLabel -t [{trans}] [{rigid},1] [{affine},1]"
if verbose:
print(f"Runnings the Ants apply transforms to {subject_img_path}")
print(cmd)
os.system(cmd)
if not os.path.exists(preprocess_labels) or overwrite:
#Note, it used to be MultiLabel[1.0x1.0x1.0,2] but it seems like the default parameters tend to be based on the image themselves and work fine,
#so be careful but I removed it for now so it would work on different image sets
cmd = f"antsApplyTransforms -v 1 -d 3 -i {atlas_labels} -o {preprocess_labels} -r {preprocess_ref} -n MultiLabel -t [{trans},1] [{rigid},1] [{affine},1] {MDT_to_subject} [{MDT_to_atlas_affine},1] {atlas_to_MDT}"
if verbose:
print(f"Runnings the Ants apply transforms to {atlas_labels}")
print(cmd)
os.system(cmd)
if os.path.exists(preprocess_labels) and not ((os.path.exists(fixed_preprocess_labels)) or overwrite):
header_superpose(final_ref, preprocess_labels, outpath=fixed_preprocess_labels)
if os.path.exists(fixed_preprocess_labels) and not (os.path.exists(coreg_labels) or overwrite):
if orientation_out != orientation_in:
if verbose:
print(f"Reorientation from {orientation_out} back to {orientation_in}")
img_transform_exec(fixed_preprocess_labels, orientation_out, orientation_in, coreg_labels, recenter=recenter)
else:
if verbose:
print(f"Orientations are the same, skipping reorientation")
shutil.copy(fixed_preprocess_labels, coreg_labels)
if os.path.exists(coreg_labels):
header_superpose(final_ref, coreg_labels, outpath=final_labels)
#cmd = f"{os.path.join(gunniespath, 'nifti_header_splicer.bash')} {final_ref} {coreg_labels} {final_labels}"
#os.system(cmd)
if os.path.exists(final_labels):
if verbose:
print(f"Applying fsl maths to {final_labels}")
cmd = f"fslmaths {final_labels} -add 0 {final_labels} -odt short"
os.system(cmd)
else:
print(f"Already calculated the label file for subject {subject}")
skip_making_data_package_for_tractography = True
if not skip_making_data_package_for_tractography:
print('if you see this, make the bval fixes')
if not os.path.exists9(symbolic_ref):
cmd=f"ln - s {final_ref} {symbolic_ref}"
bvals = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvals.txt")
bvecs = os.path.join(mainpath, f"diffusion_prep_{subject}", f"{subject}_bvecs.txt")
bval_copy = os.path.join(out_dir, f"{subject}_bvals.txt")
bvec_copy = os.path.join(out_dir, f"{subject}_bvecs.txt")
if not os.path.exists(bval_copy):
shutil.copy(bvals, bval_copy)
if not os.path.exists(bvec_copy):
shutil.copy(bvecs, bvec_copy)