-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·247 lines (228 loc) · 14.6 KB
/
run.py
File metadata and controls
executable file
·247 lines (228 loc) · 14.6 KB
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
#!/usr/bin/python
from glob import glob
import os
import shutil
import argparse
from IntendedFor import setup
import sys
import re
import fnmatch
import pdb
#book keeping and global variables
parser = argparse.ArgumentParser(description='Script that controls BIDS conversion for individual studies')
parser.add_argument('--output_dir', help="The directory that the BIDS data will be outputted to",required=True)
parser.add_argument('--tmp_dir', help="The directory that DICOM data will be temporarily stored in.", required=True)
parser.add_argument('--dicom_dir', help='The directory that houses dicom directories/files.',required=True)
parser.add_argument('--ses_id', help="scanning session id")
parser.add_argument('--subj_id', help="subject id")
parser.add_argument('--heuristic', help="Path to heuristic file, if the file is already within the container (i.e. within heuristics folder)"
" you do not have to specify a path. ")
parser.add_argument('--dry_run', action='store_true', help="Dry run. A dicominfo_*.tsv file will generate within .heudiconv/'subj_id'/info directory which can be used to create heuristic script")
parser.add_argument('--datalad',help='Store the entire collection as DataLad dataset(s).'
' Small files will be committed directly to git, while'
' large to annex. New version (6) of annex repositories'
' will be used in a "thin" mode so it would look to'
' mortals as just any other regular directory (i.e. no'
' symlinks to under .git/annex). For now just for BIDS'
' mode.')
args = parser.parse_args()
dicom_dir = args.dicom_dir
tmp_dir = args.tmp_dir
ses_id = args.ses_id
subj_id = args.subj_id
heuristic = args.heuristic
dry_run = args.dry_run
output_dir = args.output_dir
datalad = args.datalad
# change temporary dir to different location based on --tmp_dir flag
os.system('export TMPDIR='+tmp_dir)
# make bids directory if it does not exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if os.path.exists(os.path.join(output_dir,".heudiconv",subj_id,'ses-'+ses_id)):
shutil.rmtree(os.path.join(output_dir,".heudiconv",subj_id,'ses-'+ses_id))
# determine conversion type (i.e. dry run or heuristic)
if dry_run:
convert_type = ' -c none -f convertall'
else:
if not heuristic:
raise Exception("If --dry_run is not being a conducted a heuristic script must be used. Re-run and place path within --heuristic argument.")
else:
heuristics_script = heuristic
convert_type = ' -c dcm2niix -f %s -b' % heuristics_script
if datalad:
convert_type = convert_type + ' --datalad'
# try to determine how data is organized within dicom directory, place within /tmp directory, and determine what heudiconv format (cross-sectional, longitudinal should look like)
# just subj_id
if subj_id and not ses_id:
if len(glob(dicom_dir + "/*" + subj_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + subj_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + subj_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{subject}*/%s -s %s --overwrite -o %s' % (dicom_dir, recursion_pattern, subj_id, output_dir)
elif len(glob(dicom_dir + "/*" + subj_id + "*")) > 1:
raise Exception("There are multiple directories within dicom directory: " + dicom_dir + " with the same subject id: " + subj_id + ". Either change the subject id or enter a session id. Must exit.")
else:
raise Exception("Cannot find a directory within dicom directory: " + dicom_dir + " with the subject id: " + subj_id + ". Must exit.")
#just ses id
elif ses_id and not subj_id:
if len(glob(dicom_dir + "/*" + ses_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + ses_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + ses_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{subject}*/%s -s %s --overwrite -o %s' % (dicom_dir, recursion_pattern, ses_id, output_dir)
elif len(glob(dicom_dir + "/*" + ses_id + "*")) > 1:
raise Exception("There are multiple directories within dicom directory: " + dicom_dir + " with the same subject id: " + ses_id + ". Either change the subject id or enter a session id. Must exit.")
else:
raise Exception("Cannot find a directory within dicom directory: " + dicom_dir + " with the subject id: " + ses_id + ". Must exit.")
# subject id and session id
elif ses_id and subj_id:
if len(glob(dicom_dir + "/*" + subj_id + "*/*" + ses_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + subj_id + "*/*" + ses_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + subj_id + "*/*" + ses_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{subject}*/*{session}*/%s -s %s -ss %s --overwrite -o %s' % (dicom_dir, recursion_pattern, subj_id, ses_id, output_dir)
elif len(glob(dicom_dir + "/*" + ses_id + "*/*" + subj_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + ses_id + "*/*" + subj_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + ses_id + "*/*" + subj_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{session}*/*{subject}*/%s -s %s -ss %s --overwrite -o %s' % (dicom_dir, recursion_pattern, ses_id, subj_id, output_dir)
elif len(glob(dicom_dir + "/*" + subj_id + "*" + ses_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + subj_id + "*" + ses_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + subj_id + "*" + ses_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{subject}*{session}*/%s -s %s -ss %s --overwrite -o %s' % (dicom_dir, recursion_pattern, subj_id, ses_id, output_dir)
elif len(glob(dicom_dir + "/*" + ses_id + "*" + subj_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + ses_id + "*" + subj_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + ses_id + "*" + subj_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{session}*{subject}*/%s -s %s -ss %s --overwrite -o %s' % (dicom_dir, recursion_pattern, subj_id, ses_id, output_dir)
elif len(glob(dicom_dir + "/*" + subj_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + subj_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + subj_id + "*")[0]
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + "/")[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + "/")[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + "/")[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + "/")[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + "/")[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/*{subject}*/%s -s %s -ss %s --overwrite -o %s' % (dicom_dir, recursion_pattern, subj_id, ses_id, output_dir)
elif len(glob(dicom_dir + "/*" + ses_id + "*")) == 1:
if os.path.isdir(glob(dicom_dir + "/*" + ses_id + "*")[0]):
dicom_session_folder = glob(dicom_dir + "/*" + ses_id + "*")[0]
if os.path.isdir(os.path.join(tmp_dir,subj_id,ses_id)):
shutil.rmtree(os.path.join(tmp_dir,subj_id,ses_id))
shutil.copytree(dicom_session_folder,os.path.join(tmp_dir,subj_id,ses_id))
dicom_session_folder = os.path.join(tmp_dir,subj_id,ses_id)
matches = []
for root, dirnames, filenames in os.walk(dicom_session_folder):
for filename in fnmatch.filter(filenames, '*.dcm'):
matches.append(os.path.join(root, filename))
if len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 1:
recursion_pattern = '*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 2:
recursion_pattern = '*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 3:
recursion_pattern = '*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 4:
recursion_pattern = '*/*/*/*'
elif len(matches[0].split(dicom_session_folder + '/')[1].split('/')) == 5:
recursion_pattern = '*/*/*/*/*'
convert_format = 'heudiconv -d %s/{subject}/{session}/%s -s %s -ss %s --overwrite -o %s' % (tmp_dir, recursion_pattern, subj_id, ses_id, output_dir)
else:
raise Exception("Cannot find a directory within dicom directory: " + dicom_dir + " with a combination of subject id: " + subj_id + ", or session id: " + ses_id + ". Must exit.")
# neither subject id or session id
else:
raise Exception("Neither subject id nor session id arguments were entered. Must exit.")
os.system(convert_format + convert_type)
# Now change IntendedFor field within fmaps
if not dry_run:
# Remove alphanumerics within subj_id
subj_id = re.sub(r'[^a-zA-Z0-9]', "", subj_id)
setup(os.path.join(output_dir, "sub-"+subj_id))