-
Notifications
You must be signed in to change notification settings - Fork 0
/
xnat_PUP_downloader.py
306 lines (273 loc) · 13.5 KB
/
xnat_PUP_downloader.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
import nibabel as nib
import sys
sys.path.insert(0, "xnat_downloader")
import xnat_downloader.cli.run as run
import os
import argparse
import json
from pyxnat import Interface
def get_PUP_timecourse_object(testSub, ses_of_interest):
assessors_obj = testSub.ses_dict[ses_of_interest].assessors().get('')
for assessor_obj in assessors_obj:
if assessor_obj.id() is not None:
if "PUPTIMECOURSE" in assessor_obj.id():
puptimecourse_obj = assessor_obj
return puptimecourse_obj
else:
return None
def convert_analyze_to_nii(img_path, hdr_path):
img = nib.load(img_path)
nii_path = img_path.replace('.img', '.nii.gz')
nib.save(img, nii_path)
os.remove(img_path)
os.remove(hdr_path)
def get_image_and_header(assessor_puptimecourse_obj,
orig_img_path,
orig_hdr_path,
new_img_path,
new_hdr_path):
try:
# if header is missing, it is because the other file is an info file or some other type
# that doesn't need to be converted to nifti
assessor_puptimecourse_obj.resource(
'DATA').file(orig_img_path).get(new_img_path)
if orig_hdr_path is not None and new_hdr_path is not None:
assessor_puptimecourse_obj.resource(
'DATA').file(orig_hdr_path).get(new_hdr_path)
convert_analyze_to_nii(new_img_path, new_hdr_path)
except:
print("Could not find the requested file")
def get_t1w_image(puptimecourse_obj,
ses_dir,
sub,
ses):
orig_img_path = os.path.join("pet_proc", "T1001.4dfp.img")
orig_hdr_path = os.path.join("pet_proc", "T1001.4dfp.hdr")
new_img_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_T1w.img")
new_hdr_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_T1w.hdr")
if not os.path.exists(new_img_path.replace('.img', '.nii.gz')):
get_image_and_header(puptimecourse_obj, orig_img_path,
orig_hdr_path, new_img_path, new_hdr_path)
else:
print("Found a T1w file for " + sub + " at " + ses)
def get_orig_pet_image(puptimecourse_obj,
ses_dir,
sub,
ses):
filename_prefix = sub + "_" + ses + "_pib.4dfp"
orig_img_path = os.path.join("pet_proc", filename_prefix + ".img")
orig_hdr_path = os.path.join("pet_proc", filename_prefix + ".hdr")
new_img_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_task-rest_acq-pib_pet.img")
new_hdr_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_task-rest_acq-pib_pet.hdr")
if not os.path.exists(new_img_path.replace('.img', '.nii.gz')):
get_image_and_header(puptimecourse_obj, orig_img_path,
orig_hdr_path, new_img_path, new_hdr_path)
else:
print("Found a raw PET image for " + sub + " at " + ses)
def get_pup_params_file(puptimecourse_obj,
ses_dir,
sub,
ses):
filename_prefix = sub + "_PIB_" + ses
orig_params_path = os.path.join("pet_proc", filename_prefix + ".params")
new_params_path = os.path.join(ses_dir, filename_prefix + ".params")
new_params_json_path = os.path.join(ses_dir, "sub-" + sub + "_ses-" + ses + "_PUP_params.json")
if not os.path.exists(new_params_json_path):
print("Downloading PUP params file to: " + new_params_path)
get_image_and_header(
puptimecourse_obj, orig_params_path, None, new_params_path, None)
with open(new_params_path, "r") as f:
params = f.read()
params = params.split("\n")
params = params[0:len(params)-1]
params_dict = {}
for i in range(0, len(params)):
entry = params[i]
key = entry.split("=")[0]
value = entry.split("=")[1]
params_dict[key] = value
with open(new_params_json_path, "w") as f:
json.dump(params_dict, f)
else:
print("Found a PUP .params file for" + sub + " at " + ses)
def get_orig_pet_info_file(puptimecourse_obj,
ses_dir,
sub,
ses):
orig_img_path = os.path.join("pet_proc", sub + "_" + ses + "_pib.info")
new_img_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_task-rest_acq-pib_pet.info")
if not os.path.exists(new_img_path):
get_image_and_header(
puptimecourse_obj, orig_img_path, None, new_img_path, None)
else:
print("Found a raw PET info file for" + sub + " at " + ses)
def get_dkt_t1w_space_image(puptimecourse_obj,
ses_dir,
sub,
ses):
orig_img_path = os.path.join("pet_proc", "wmparc001.4dfp.img")
orig_hdr_path = os.path.join("pet_proc", "wmparc001.4dfp.hdr")
new_img_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_parcellation-DKT_space-T1w.img")
new_hdr_path = os.path.join(
ses_dir, "sub-" + sub + "_ses-" + ses + "_parcellation-DKT_space-T1w.hdr")
if not os.path.exists(new_img_path.replace('.img', '.nii.gz')):
get_image_and_header(puptimecourse_obj, orig_img_path,
orig_hdr_path, new_img_path, new_hdr_path)
else:
print("Found an atlas in native space file for " + sub + " at " + ses)
def get_suvr_t1w_space_image(puptimecourse_obj,
ses_dir,
sub,
ses,
radiotracer):
filename_prefix = sub + "_" + ses + "_pibn_msum_on_roi.4dfp"
orig_img_path = os.path.join("pet_proc", filename_prefix + ".img")
orig_hdr_path = os.path.join("pet_proc", filename_prefix + ".hdr")
new_img_path = os.path.join(ses_dir,
"sub-" + sub + "_ses-" + ses + "_acq-" + radiotracer + "_space-T1w_coregistered_pet.img")
new_hdr_path = os.path.join(ses_dir,
"sub-" + sub + "_ses-" + ses + "_acq-" + radiotracer + "_space-T1w_coregistered_pet.hdr")
if not os.path.exists(new_img_path.replace('.img', '.nii.gz')):
get_image_and_header(puptimecourse_obj, orig_img_path,
orig_hdr_path, new_img_path, new_hdr_path)
else:
print("Found an SUVR file in native space for " + sub + " at " + ses)
def get_suvr_t1w_space_image(puptimecourse_obj,
ses_dir,
sub,
ses,
radiotracer):
filename_prefix = sub + "_" + ses + "_pibn_msum_on_roi.4dfp"
orig_img_path = os.path.join("pet_proc", filename_prefix + ".img")
orig_hdr_path = os.path.join("pet_proc", filename_prefix + ".hdr")
new_img_path = os.path.join(ses_dir,
"sub-" + sub + "_ses-" + ses + "_acq-" + radiotracer + "_space-T1w_coregistered_pet.img")
new_hdr_path = os.path.join(ses_dir,
"sub-" + sub + "_ses-" + ses + "_acq-" + radiotracer + "_space-T1w_coregistered_pet.hdr")
if not os.path.exists(new_img_path.replace('.img', '.nii.gz')):
get_image_and_header(puptimecourse_obj, orig_img_path,
orig_hdr_path, new_img_path, new_hdr_path)
else:
print("Found an SUVR file in native space for " + sub + " at " + ses)
def get_moco_image(puptimecourse_obj,
ses_dir,
sub,
ses,
radiotracer):
filename_prefix = sub + "_" + radiotracer + "_" + ses + "n_moco.4dfp"
orig_img_path = os.path.join("pet_proc", filename_prefix + ".img")
orig_hdr_path = os.path.join("pet_proc", filename_prefix + ".hdr")
new_img_path = os.path.join(ses_dir,
"sub-" + sub + "_ses-" + ses + "_acq-" + radiotracer + "_moco_pet.img")
new_hdr_path = os.path.join(ses_dir,
"sub-" + sub + "_ses-" + ses + "_acq-" + radiotracer + "_moco_pet.hdr")
if not os.path.exists(new_img_path.replace('.img', '.nii.gz')):
get_image_and_header(puptimecourse_obj, orig_img_path,
orig_hdr_path, new_img_path, new_hdr_path)
else:
print("Found a MOCO file in PET space for " + sub + " at " + ses)
def main():
parser = argparse.ArgumentParser(
description='Script to download PET Unified Pipeline derivatives from XNAT server.')
parser.add_argument("--server",
help="Specify the XNAT server of interest")
parser.add_argument("--user",
help="Specify the XNAT server username")
parser.add_argument("--password",
help="Specify the associated password")
parser.add_argument("--project",
help="Specify the project/data collection, e.g. DIANDF11")
parser.add_argument("--ses", nargs="+",
help="Specify the visit labels or sessions to download, e.g. v00")
parser.add_argument("--subjects", action="append",
help="Specify a subject or list of subjects for which to download images")
parser.add_argument("--radiotracer",
help="Specify the radiotracer, e.g. pib or fdg")
parser.add_argument("--output_dir",
help="Specify the directory where to write the outputs")
args = parser.parse_args()
sessions = args.ses
radiotracer = args.radiotracer
output_dir = args.output_dir
server = args.server
user = args.user
password = args.password
project = args.project
subjects = args.subjects
central = Interface(server=server, user=user, password=password)
if not central.select.projects().get():
msg = "You have no access to any projects in the server, " \
"please check your url, username, and password."
raise RuntimeError(msg)
proj_obj = central.select.project(project)
if subjects is None:
sub_objs = proj_obj.subjects()
sub_objs._id_header = 'label'
subjects = sub_objs.get()
for sub in subjects:
print(sub)
testSub = run.Subject(proj_obj, sub)
testSub.get_sessions()
if project == "DIANDF12":
for ses in sessions:
ses_of_interest = sub + "_" + ses + "_" + radiotracer
if ses_of_interest in testSub.ses_dict.keys():
puptimecourse_obj = get_PUP_timecourse_object(
testSub, ses_of_interest)
if puptimecourse_obj is not None:
sub_dir = os.path.join(output_dir, "sub-" + sub)
ses_dir = os.path.join(sub_dir, "ses-" + ses)
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
os.mkdir(ses_dir)
else:
if not os.path.exists(ses_dir):
os.mkdir(ses_dir)
get_t1w_image(puptimecourse_obj, ses_dir, sub, ses)
get_dkt_t1w_space_image(
puptimecourse_obj, ses_dir, sub, ses)
get_suvr_t1w_space_image(
puptimecourse_obj, ses_dir, sub, ses, radiotracer)
get_orig_pet_image(puptimecourse_obj, ses_dir, sub, ses)
get_orig_pet_info_file(
puptimecourse_obj, ses_dir, sub, ses)
print("Finished downloading images for: " + ses_of_interest)
else:
print("No PUP timecourse available for: " + ses_of_interest)
else:
print(sub + " does not have a " + ses + " session")
elif project == "OASIS3":
for ses in testSub.ses_dict.keys():
if radiotracer == ses.split("_")[1]:
puptimecourse_obj = get_PUP_timecourse_object(
testSub, ses)
if puptimecourse_obj is not None:
sessionID = ses.split("_")[2]
sub_dir = os.path.join(output_dir, "sub-" + sub)
ses_dir = os.path.join(sub_dir, "ses-" + sessionID)
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
os.mkdir(ses_dir)
else:
if not os.path.exists(ses_dir):
os.mkdir(ses_dir)
get_t1w_image(puptimecourse_obj, ses_dir, sub, sessionID)
get_dkt_t1w_space_image(
puptimecourse_obj, ses_dir, sub, sessionID)
get_moco_image(
puptimecourse_obj, ses_dir, sub, sessionID, radiotracer)
get_pup_params_file(puptimecourse_obj,
ses_dir, sub, sessionID)
print("Finished downloading images for: " + ses)
else:
print("No PUP timecourse available for: " + ses)
else:
print(ses + " does not match the requested session")
if __name__ == "__main__":
main()