-
Notifications
You must be signed in to change notification settings - Fork 1
/
bids_form.py
186 lines (164 loc) · 4.92 KB
/
bids_form.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
"""Flask entry point with extra CLI commands."""
from autobidsportal.app import create_app
from autobidsportal.dcm4cheutils import Dcm4cheError, gen_utils
from autobidsportal.models import (
Cfmm2tarOutput,
DataladDataset,
DatasetType,
ExplicitPatient,
Notification,
Principal,
Study,
Tar2bidsOutput,
Task,
User,
db,
)
from autobidsportal.tasks import update_heuristics
app = create_app()
@app.shell_context_processor
def make_shell_context():
"""Add useful variables into the shell context."""
return {
"db": db,
"User": User,
"Study": Study,
"Principal": Principal,
"Notification": Notification,
"Task": Task,
"Cfmm2tarOutput": Cfmm2tarOutput,
"Tar2bidsOutput": Tar2bidsOutput,
"ExplicitPatient": ExplicitPatient,
}
@app.cli.command()
def check_pis():
"""Add a list of pi names from dicom server to the Principal table."""
try:
principal_names = gen_utils().get_all_pi_names()
db.session.query(Principal).delete()
for principal_name in principal_names:
principal = Principal(principal_name=principal_name)
db.session.add(principal)
db.session.commit()
except Dcm4cheError as err:
print(err)
return "Success"
@app.cli.command()
def run_update_heuristics():
"""Clone the heuristic repo if it doesn't exist, then pull from it.
The point of this wrapper function is to expose the task to the CLI.
"""
update_heuristics()
@app.cli.command()
def run_all_cfmm2tar():
"""Run cfmm2tar on all active studies.
This won't run cfmm2tar on studies that currently have cfmm2tar runs in
progress.
"""
for study in Study.query.all():
if (
len(
Task.query.filter_by(
study_id=study.id,
name="run_cfmm2tar",
complete=False,
).all(),
)
> 0
) or (not study.active):
print(f"Skipping study {study.id}. Active: {study.active}")
continue
app.task_queue.enqueue(
"autobidsportal.tasks.check_tar_files",
study.id,
)
@app.cli.command()
def run_all_tar2bids():
"""Run tar2bids on all active studies."""
for study in Study.query.all():
if (
len(
Task.query.filter_by(
study_id=study.id,
name="run_tar2bids",
complete=False,
).all(),
)
> 0
) or not study.active:
print(f"Skipping study {study.id}. Active: {study.active}")
continue
app.task_queue.enqueue(
"autobidsportal.tasks.find_unprocessed_tar_files",
study.id,
)
@app.cli.command()
def run_all_archive():
"""Archive all active studies' raw datasets.
This won't archive studies that currently have tar2bids runs in
progress.
"""
for study in Study.query.all():
if (
len(
Task.query.filter_by(
study_id=study.id,
name="get_info_from_tar2bids",
complete=False,
).all(),
)
> 0
) or (not study.active):
continue
Task.launch_task(
"archive_raw_data",
"automatic archive task",
study.id,
study_id=study.id,
timeout=app.config["ARCHIVE_TIMEOUT"],
)
@app.cli.command()
def run_all_archive_derivative():
"""Archive all active studies' derivative datasets.
This won't archive studies that currently have tar2bids runs in
progress.
"""
for study in Study.query.all():
if (
not DataladDataset.query.filter_by(
study_id=study.id,
dataset_type=DatasetType.DERIVED_DATA,
).one_or_none()
) or (not study.active):
continue
Task.launch_task(
"archive_derivative_data",
"automatic archive task",
study.id,
study_id=study.id,
timeout=app.config["ARCHIVE_TIMEOUT"],
)
@app.cli.command()
def run_all_gradcorrect():
"""Run gradcorrect on all active studies."""
for study in Study.query.all():
if (
(study.scanner != "type2")
or (
len(
Task.query.filter_by(
study_id=study.id,
name="gradcorrect_study",
complete=False,
).all(),
)
> 0
)
or not study.active
):
print(f"Skipping study {study.id}. Active: {study.active}")
continue
app.task_queue.enqueue(
"autobidsportal.tasks.find_uncorrected_images",
study.id,
)