-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsegment_pnh.py
661 lines (488 loc) · 21.8 KB
/
segment_pnh.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#!/usr/bin/env python3
"""
Non humain primates anatomical segmentation pipeline based ANTS
Adapted in Nipype from an original pipelin of Kepkee Loh wrapped.
Description
--------------
TODO :/
Arguments
-----------
-data:
Path to the BIDS directory that contain subjects' MRI data.
-out:
Nipype's processing directory.
It's where all the outputs will be saved.
-subjects:
IDs list of subjects to process.
-ses
session (leave blank if None)
-params
json parameter file; leave blank if None
Example
---------
python segment_pnh.py -data [PATH_TO_BIDS] -out ../local_tests/ -subjects Elouk
Requirements
--------------
This workflow use:
- ANTS
- AFNI
- FSL
"""
# Authors : David Meunier (david.meunier@univ-amu.fr)
# Bastien Cagna (bastien.cagna@univ-amu.fr)
# Kepkee Loh (kepkee.loh@univ-amu.fr)
# Julien Sein (julien.sein@univ-amu.fr)
import os
import os.path as op
import argparse
import json
import nipype.pipeline.engine as pe
import nipype.interfaces.fsl as fsl
from macapype.pipelines.full_pipelines import (
create_full_spm_subpipes,
create_full_ants_subpipes,
create_full_T1_ants_subpipes,
create_transfo_FLAIR_pipe,
create_transfo_MD_pipe)
from macapype.pipelines.rename import rename_all_derivatives
from macapype.utils.utils_bids import (create_datasource,
create_datasource_indiv_params,
create_datasink)
from macapype.utils.utils_tests import load_test_data, format_template
from macapype.utils.utils_params import update_params
from macapype.utils.misc import show_files, get_first_elem, parse_key
fsl.FSLCommand.set_default_output_type('NIFTI_GZ')
###############################################################################
def create_main_workflow(data_dir, process_dir, soft, species, subjects,
sessions, acquisitions, reconstructions, params_file,
indiv_params_file, mask_file, template_path,
template_files, nprocs, reorient, deriv, pad,
wf_name="macapype"):
# macapype_pipeline
""" Set up the segmentatiopn pipeline based on ANTS
Arguments
---------
data_path: pathlike str
Path to the BIDS directory that contains anatomical images
out_path: pathlike str
Path to the ouput directory (will be created if not alredy existing).
Previous outputs maybe overwritten.
soft: str
Indicate which analysis should be launched; so for, only spm and ants
are accepted; can be extended
subjects: list of str (optional)
Subject's IDs to match to BIDS specification (sub-[SUB1], sub-[SUB2]...)
sessions: list of str (optional)
Session's IDs to match to BIDS specification (ses-[SES1], ses-[SES2]...)
acquisitions: list of str (optional)
Acquisition name to match to BIDS specification (acq-[ACQ1]...)
reconstructions: list of str (optional)
Reconstructions name to match to BIDS specification (rec-[ACQ1]...)
indiv_params_file: path to a JSON file
JSON file that specify some parameters of the pipeline,
unique for the subjects/sessions.
params_file: path to a JSON file
JSON file that specify some parameters of the pipeline.
nprocs: integer
number of processes that will be launched by MultiProc
Returns
-------
workflow: nipype.pipeline.engine.Workflow
"""
soft = soft.lower()
ssoft = soft.split("_")
new_ssoft = ssoft.copy()
if 'test' in ssoft:
new_ssoft.remove('test')
if 'prep' in ssoft:
new_ssoft.remove('prep')
if 'noseg' in ssoft:
new_ssoft.remove('noseg')
if 't1' in ssoft:
new_ssoft.remove('t1')
if 'native' in ssoft:
new_ssoft.remove('native')
if 'template' in ssoft:
new_ssoft.remove('template')
soft = "_".join(new_ssoft)
# formating args
data_dir = op.abspath(data_dir)
process_dir = op.abspath(process_dir)
try:
os.makedirs(process_dir)
except OSError:
print("process_dir {} already exists".format(process_dir))
#params
if params_file is None:
# species
if species is not None:
species = species.lower()
rep_species = {"marmoset":"marmo", "marmouset":"marmo", "chimpanzee":"chimp"}
if species in list(rep_species.keys()):
species = rep_species[species]
list_species = ["macaque", "marmo", "baboon", "chimp"]
assert species in list_species, \
"Error, species {} should in the following list {}".format(
species, list_species)
package_directory = op.dirname(op.abspath(__file__))
params_file = "{}/params_segment_{}_{}.json".format(
package_directory, species, soft)
else:
print("Error, no -params or no -species was found (one or the \
other is mandatory)")
exit(-1)
print("Using default params file:", params_file)
params, indiv_params, extra_wf_name = update_params(
ssoft=ssoft, subjects=subjects, sessions=sessions,
params_file=params_file, indiv_params_file=indiv_params_file)
else:
# format for relative path
params_file = op.abspath(params_file)
# params
assert op.exists(params_file), "Error with file {}".format(
params_file)
print("Using orig params file:", params_file)
params = json.load(open(params_file))
extra_wf_name = "_orig"
indiv_params = {}
# indiv_params
if indiv_params_file is not None:
# format for relative path
indiv_params_file = op.abspath(indiv_params_file)
assert op.exists(indiv_params_file), "Error with file {}".format(
indiv_params_file)
indiv_params = json.load(open(indiv_params_file))
# modifying if reorient
if reorient is not None:
print("reorient: ", reorient)
if "short_preparation_pipe" in params.keys():
params["short_preparation_pipe"]["avg_reorient_pipe"] = {
"reorient":
{"origin": reorient, "deoblique": True}}
wf_name += extra_wf_name
# soft
wf_name += "_{}".format(soft)
if 't1' in ssoft:
wf_name += "_t1"
if mask_file is not None:
wf_name += "_mask"
assert "spm" in ssoft or "spm12" in ssoft or "ants" in ssoft, \
"error with {}, should be among [spm12, spm, ants]".format(ssoft)
# adding forced space
if "spm" in ssoft or "spm12" in ssoft:
if 'native' in ssoft:
wf_name += "_native"
elif "ants" in ssoft:
if "template" in ssoft:
wf_name += "_template"
# params_template
if template_path is not None:
# format for relative path
template_path = op.abspath(template_path)
assert os.path.exists(template_path), "Error, template_path {} do not exists".format(template_path)
print(template_files)
params_template = {}
assert len(template_files) > 1, "Error, template_files unspecified {}".format(template_files)
template_head = os.path.join(template_path,template_files[0])
assert os.path.exists(template_head), "Could not find template_head {}".format(template_head)
params_template["template_head"] = template_head
template_brain = os.path.join(template_path,template_files[1])
assert os.path.exists(template_brain), "Could not find template_brain {}".format(template_brain)
params_template["template_brain"] = template_brain
if len(template_files) == 3:
template_seg = os.path.join(template_path,template_files[2])
assert os.path.exists(template_seg), "Could not find template_seg {}".format(template_seg)
params_template["template_seg"] = template_seg
elif len(template_files) == 5:
template_gm = os.path.join(template_path,template_files[2])
assert os.path.exists(template_gm), "Could not find template_gm {}".format(template_gm)
params_template["template_gm"] = template_gm
template_wm = os.path.join(template_path,template_files[3])
assert os.path.exists(template_wm), "Could not find template_wm {}".format(template_wm)
params_template["template_wm"] = template_wm
template_csf = os.path.join(template_path,template_files[4])
assert os.path.exists(template_csf), "Could not find template_csf {}".format(template_csf)
params_template["template_csf"] = template_csf
else:
print("Unknown template_files format, should be 3 or 5 files")
exit(-1)
params_template_aladin = params_template
params_template_stereo = params_template
else:
### use template from params
assert ("general" in params.keys() and \
"template_name" in params["general"].keys()), \
"Error, the params.json should contains a general/template_name"
template_name = params["general"]["template_name"]
if "general" in params.keys() and "my_path" in params["general"].keys():
my_path = params["general"]["my_path"]
else:
my_path = ""
template_dir = load_test_data(template_name, path_to=my_path)
params_template = format_template(template_dir, template_name)
if "template_aladin_name" in params["general"].keys():
template_aladin_name = params["general"]["template_aladin_name"]
template_aladin_dir = load_test_data(template_aladin_name,
path_to=my_path)
params_template_aladin = format_template(template_aladin_dir,
template_aladin_name)
else:
params_template_aladin = params_template
if "template_stereo_name" in params["general"].keys():
template_stereo_name = params["general"]["template_stereo_name"]
print("template_stereo_name = {}".format(template_stereo_name))
template_stereo_dir = load_test_data(template_stereo_name,
path_to=my_path)
params_template_stereo = format_template(template_stereo_dir,
template_stereo_name)
else:
params_template_stereo = params_template
print(params_template)
# main_workflow
main_workflow = pe.Workflow(name=wf_name)
main_workflow.base_dir = process_dir
# which soft is used
if "spm" in ssoft or "spm12" in ssoft:
if 'native' in ssoft:
space = 'native'
else:
space = 'template'
segment_pnh_pipe = create_full_spm_subpipes(
params_template=params_template,
params_template_aladin=params_template_aladin,
params_template_stereo=params_template_stereo,
params=params, pad=pad, space=space)
elif "ants" in ssoft:
if "template" in ssoft:
space = "template"
else:
space = "native"
if "t1" in ssoft:
segment_pnh_pipe = create_full_T1_ants_subpipes(
params_template=params_template,
params_template_aladin=params_template_aladin,
params_template_stereo=params_template_stereo,
params=params, space=space, pad=pad)
else:
segment_pnh_pipe = create_full_ants_subpipes(
params_template=params_template,
params_template_aladin=params_template_aladin,
params_template_stereo=params_template_stereo,
params=params, mask_file=mask_file, space=space, pad=pad)
# list of all required outputs
output_query = {}
# T1 (mandatory, always added)
# T2 is optional, if "_T1" is added in the -soft arg
if 't1' in ssoft:
output_query['T1'] = {
"datatype": "anat", "suffix": "T1w",
"extension": ["nii", ".nii.gz"]}
else:
output_query['T1'] = {
"datatype": "anat", "suffix": "T1w",
"extension": ["nii", ".nii.gz"]}
output_query['T2'] = {
"datatype": "anat", "suffix": "T2w",
"extension": ["nii", ".nii.gz"]}
# FLAIR is optional, if "_FLAIR" is added in the -soft arg
if 'flair' in ssoft:
output_query['FLAIR'] = {
"datatype": "anat", "suffix": "FLAIR",
"extension": ["nii", ".nii.gz"]}
# MD and b0mean are optional, if "_MD" is added in the -soft arg
if 'md' in ssoft:
output_query['MD'] = {
"datatype": "dwi", "acquisition": "MD", "suffix": "dwi",
"extension": ["nii", ".nii.gz"]}
output_query['b0mean'] = {
"datatype": "dwi", "acquisition": "b0mean", "suffix": "dwi",
"extension": ["nii", ".nii.gz"]}
# indiv_params
if indiv_params:
datasource = create_datasource_indiv_params(
output_query, data_dir, indiv_params, subjects, sessions,
acquisitions, reconstructions)
main_workflow.connect(datasource, "indiv_params",
segment_pnh_pipe, 'inputnode.indiv_params')
else:
datasource = create_datasource(
output_query, data_dir, subjects, sessions, acquisitions,
reconstructions)
main_workflow.connect(datasource, 'T1',
segment_pnh_pipe, 'inputnode.list_T1')
if "t1" not in ssoft:
main_workflow.connect(datasource, 'T2',
segment_pnh_pipe, 'inputnode.list_T2')
elif "t1" in ssoft and "spm" in ssoft:
# cheating using T2 as T1
main_workflow.connect(datasource, 'T1',
segment_pnh_pipe, 'inputnode.list_T2')
if "flair" in ssoft:
if "transfo_FLAIR_pipe" in params.keys():
print("Found transfo_FLAIR_pipe")
transfo_FLAIR_pipe = create_transfo_FLAIR_pipe(
params=parse_key(params, "transfo_FLAIR_pipe"),
params_template=params_template)
if "t1" in ssoft:
main_workflow.connect(
segment_pnh_pipe,
"short_preparation_pipe.outputnode.preproc_T1",
transfo_FLAIR_pipe, 'inputnode.orig_T1')
else:
main_workflow.connect(
segment_pnh_pipe, "debias.t1_debiased_file",
transfo_FLAIR_pipe, 'inputnode.orig_T1')
main_workflow.connect(
segment_pnh_pipe, "reg.transfo_file",
transfo_FLAIR_pipe, 'inputnode.lin_transfo_file')
main_workflow.connect(datasource, ('FLAIR', get_first_elem),
transfo_FLAIR_pipe, 'inputnode.FLAIR')
if 'md' in ssoft:
if "transfo_MD_pipe" in params.keys():
print("Found transfo_MD_pipe")
transfo_MD_pipe = create_transfo_MD_pipe(
params=parse_key(params, "transfo_MD_pipe"),
params_template=params_template)
main_workflow.connect(
segment_pnh_pipe,
"old_segment_pipe.outputnode.threshold_wm",
transfo_MD_pipe, 'inputnode.threshold_wm')
main_workflow.connect(
datasource, ('MD', get_first_elem),
transfo_MD_pipe, 'inputnode.MD')
main_workflow.connect(
datasource, ('b0mean', get_first_elem),
transfo_MD_pipe, 'inputnode.b0mean')
main_workflow.connect(
segment_pnh_pipe, "debias.t1_debiased_file",
transfo_MD_pipe, 'inputnode.orig_T1')
main_workflow.connect(
segment_pnh_pipe, "debias.t2_debiased_brain_file",
transfo_MD_pipe, 'inputnode.SS_T2')
main_workflow.connect(
segment_pnh_pipe, "reg.transfo_file",
transfo_MD_pipe, 'inputnode.lin_transfo_file')
main_workflow.connect(
segment_pnh_pipe, "reg.inv_transfo_file",
transfo_MD_pipe, 'inputnode.inv_lin_transfo_file')
if deriv:
datasink_name = os.path.join("derivatives", wf_name)
if "regex_subs" in params.keys():
params_regex_subs = params["regex_subs"]
else:
params_regex_subs = {}
if "subs" in params.keys():
params_subs = params["rsubs"]
else:
params_subs = {}
print(datasource.iterables)
datasink = create_datasink(iterables=datasource.iterables,
name=datasink_name,
params_subs=params_subs,
params_regex_subs=params_regex_subs)
datasink.inputs.base_directory = process_dir
if len(datasource.iterables) == 1:
pref_deriv = "sub-%(sub)s"
parse_str = r"sub-(?P<sub>\w*)_.*"
elif len(datasource.iterables) > 1:
pref_deriv = "sub-%(sub)s_ses-%(ses)s"
parse_str = r"sub-(?P<sub>\w*)_ses-(?P<ses>\w*)_.*"
rename_all_derivatives(params, main_workflow, segment_pnh_pipe,
datasink, pref_deriv, parse_str, space, ssoft)
if 'flair' in ssoft:
main_workflow.connect(
transfo_FLAIR_pipe, 'outputnode.norm_FLAIR',
datasink, '@norm_flair')
# running main_workflow
main_workflow.write_graph(graph2use="colored")
main_workflow.config['execution'] = {'remove_unnecessary_outputs': 'false'}
# saving real params.json
real_params_file = op.join(process_dir, wf_name, "real_params.json")
with open(real_params_file, 'w+') as fp:
json.dump(params, fp)
if deriv:
try:
os.makedirs(op.join(process_dir, datasink_name))
except OSError:
print("process_dir {} already exists".format(process_dir))
real_params_file = op.join(process_dir, datasink_name,
"real_params.json")
with open(real_params_file, 'w+') as fp:
json.dump(params, fp)
if nprocs is None:
nprocs = 4
if "test" not in ssoft:
if "seq" in ssoft or nprocs == 0:
main_workflow.run()
else:
main_workflow.run(plugin='MultiProc',
plugin_args={'n_procs': nprocs})
def main():
# Command line parser
parser = argparse.ArgumentParser(
description="PNH segmentation pipeline")
parser.add_argument("-data", dest="data", type=str, required=True,
help="Directory containing MRI data (BIDS)")
parser.add_argument("-out", dest="out", type=str, # nargs='+',
help="Output dir", required=True)
parser.add_argument("-soft", dest="soft", type=str,
help="Sofware of analysis (SPM or ANTS are defined)",
required=True)
parser.add_argument("-species", dest="species", type=str,
help="Type of PNH to process",
required=False)
parser.add_argument("-subjects", "-sub", dest="sub",
type=str, nargs='+', help="Subjects", required=False)
parser.add_argument("-sessions", "-ses", dest="ses",
type=str, nargs='+', help="Sessions", required=False)
parser.add_argument("-acquisitions", "-acq", dest="acq", type=str,
nargs='+', default=None, help="Acquisitions")
parser.add_argument("-records", "-rec", dest="rec", type=str, nargs='+',
default=None, help="Records")
parser.add_argument("-params", dest="params_file", type=str,
help="Parameters json file", required=False)
parser.add_argument("-indiv_params", "-indiv", dest="indiv_params_file",
type=str, help="Individual parameters json file",
required=False)
parser.add_argument("-mask", dest="mask_file", type=str,
help="precomputed mask file", required=False)
parser.add_argument("-template_path", dest="template_path", type=str,
help="specifies user-based template path",
required=False)
parser.add_argument("-template_files", dest="template_files", type=str,
nargs="+", help="specifies user-based template files \
(3 or 5 are possible options)",
required=False)
parser.add_argument("-nprocs", dest="nprocs", type=int,
help="number of processes to allocate", required=False)
parser.add_argument("-reorient", dest="reorient", type=str,
help="reorient initial image", required=False)
parser.add_argument("-deriv", dest="deriv", action='store_true',
help="output derivatives in BIDS orig directory",
required=False)
parser.add_argument("-pad", dest="pad", action='store_true',
help="padding mask and seg_mask",
required=False)
args = parser.parse_args()
# main_workflow
print("Initialising the pipeline...")
create_main_workflow(
data_dir=args.data,
soft=args.soft,
process_dir=args.out,
species=args.species,
subjects=args.sub,
sessions=args.ses,
acquisitions=args.acq,
reconstructions=args.rec,
params_file=args.params_file,
indiv_params_file=args.indiv_params_file,
mask_file=args.mask_file,
template_path=args.template_path,
template_files=args.template_files,
nprocs=args.nprocs,
reorient=args.reorient,
deriv=args.deriv,
pad=args.pad)
if __name__ == '__main__':
main()