forked from Remi-Gau/bidsNighres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
executable file
·157 lines (133 loc) · 4.27 KB
/
run.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
#!/usr/bin/env python3
import json
import os
import subprocess
from os.path import abspath
from os.path import dirname
from os.path import join
from os.path import realpath
import click
from rich import print
from bidsNighres.bidsutils import check_layout
from bidsNighres.bidsutils import get_bids_filter_config
from bidsNighres.bidsutils import get_dataset_layout
from bidsNighres.bidsutils import init_derivatives_layout
from bidsNighres.segment import segment
from bidsNighres.segment import skullstrip
__version__ = open(join(dirname(realpath(__file__)), "version")).read()
def run(command, env={}):
merged_env = os.environ
merged_env.update(env)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
env=merged_env,
)
while True:
line = process.stdout.readline()
line = str(line, "utf-8")[:-1]
print(line)
if line == "" and process.poll() != None:
break
if process.returncode != 0:
raise Exception("Non zero return code: %d" % process.returncode)
@click.command()
@click.option(
"--input-datasets",
help="""
The directory with the input dataset formatted according to the BIDS standard.
""",
type=click.Path(exists=True, dir_okay=True),
required=True,
)
@click.option(
"--output-location",
help="""
The directory where the output files should be stored.
If you are running group level analysis this folder should be prepopulated
with the results of the participant level analysis.
""",
type=click.Path(exists=False, dir_okay=True),
required=True,
)
@click.option(
"--analysis-level",
help="""
Level of the analysis that will be performed.
Multiple participant level analyses can be run independently
(in parallel) using the same output-location.
""",
default="subject",
type=click.Choice(["subject", "group"], case_sensitive=True),
required=True,
)
@click.option(
"--participant-label",
help="""
The label(s) of the participant(s) that should be analyzed. The label
corresponds to sub-<participant_label> from the BIDS spec
(so it does not include "sub-"). If this parameter is not
provided all subjects should be analyzed. Multiple
participants can be specified with a space separated list.
""", # nargs ?
required=True,
)
# TODO: implement having a list of participants
# https://stackoverflow.com/questions/48391777/nargs-equivalent-for-options-in-click
@click.option(
"--action",
help="""
What to do
""",
type=click.Choice(["genT1map", "skullstrip", "segment"], case_sensitive=False),
required=True,
)
@click.option(
"--bids-filter-file",
help="""
Path to a JSON file to filter input file
""",
default="",
show_default=True,
)
@click.option(
"--dry-run",
help="""
""",
default=False,
show_default=True,
)
def main(
input_datasets,
output_location,
analysis_level,
participant_label,
action,
bids_filter_file,
dry_run,
):
input_datasets = abspath(input_datasets)
print(f"Input dataset: {input_datasets}")
output_location = abspath(output_location)
print(f"Output location: {output_location}")
if bids_filter_file == "":
bids_filter = get_bids_filter_config()
else:
bids_filter = get_bids_filter_config(bids_filter_file)
if action == "skullstrip":
layout_in = get_dataset_layout(input_datasets)
check_layout(layout_in)
layout_out = init_derivatives_layout(output_location)
# print(layout.get_subjects())
# print(layout.get_sessions())
# TODO add loop for subjects
skullstrip(layout_in, layout_out, participant_label, bids_filter=bids_filter)
elif action == "segment":
layout_out = get_dataset_layout(output_location)
segment(layout_out, participant_label, bids_filter=bids_filter, dry_run=dry_run)
# parser.add_argument('-v', '--version', action='version',
# version='bidsNighRes {}'.format(__version__))
if __name__ == "__main__":
main()