-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path08_run_qsiprep.py
104 lines (83 loc) · 2.77 KB
/
08_run_qsiprep.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
import click
import os
import shutil
from tqdm import tqdm
from pathlib import Path
# Paremeters are read from the config.py file
import config
# Main -------------------------------------------------------------------------
def _uniq(input):
"""Search for unique subject ID"""
output = []
for x in input:
if x not in output:
output.append(x)
return output
def _parse_range(str_):
"""
Parse a string like numeric index set to a list
INPUT : "0-2,5,9-11"
OUTPUT: [0, 1, 2, 5, 9, 10, 11]
"""
list_ = set()
for chunk in str_.split(','):
unit = chunk.split('-')
list_.update(range(int(unit[0]), int(unit[-1]) + 1))
return sorted(list_)
def _check_range(select, total):
if len(select) > len(total):
print("Error: Your selected range exceeds the total numbers of the dataset")
exit()
@click.command()
@click.argument("range")
@click.option("--nthreads", default=8, help="Number of Threads", show_default=True)
@click.option("--out_res", default=2, help="Output Resolution", show_default=True)
@click.option("--dryrun", is_flag=True, help="print cmd only")
def main(range, nthreads, out_res, dryrun):
"""Run QSIPrep"""
bids_dir = Path(config.outdir, 'BIDS_out')
output_dir = Path(config.outdir, "derivatives") # fullpath for output
work_dir = Path(config.outdir, "Work")
if not output_dir.is_dir():
output_dir.mkdir(parents=True, exist_ok=True)
subjlist = _parse_range(range)
# exit the program if selected subjects are more than the total number
_check_range(subjlist, config.datasets)
if not dryrun:
pbar = tqdm(total=len(subjlist), unit="subject", desc="Transforming",
colour="#BDC0BA")
for subj in subjlist:
subjid = config.datasets[subj][1]
session = config.datasets[subj][2]
pbar.set_description(f"Processing ID {subjid}-{session}")
pbar.refresh()
# import pdb; pdb.set_trace()
# container qsiprep
# --session_id
cmd = f"singularity run --cleanenv\
-B {bids_dir}:/data:ro \
-B {output_dir}:/output \
-B {work_dir}:/work \
{config.qsiprep} \
/data \
/output \
participant \
--participant_label {subjid} \
--skip_bids_validation \
--output-space T1w \
--template MNI152NLin2009cAsym \
--fs-license-file {config.scriptdir}/license.txt \
--output-resolution {out_res} \
--skip_bids_validation \
--nthreads {nthreads} \
--b0-motion-corr-to iterative \
-w /work"
if dryrun:
print(cmd)
else:
os.system(cmd)
pbar.update(1)
if not dryrun:
pbar.close()
if __name__ == "__main__":
main()