-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_bids_ccna_cimaq.py
executable file
·341 lines (287 loc) · 11.8 KB
/
convert_bids_ccna_cimaq.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python
# encoding: utf-8
import argparse
import glob
import os
import sys
import tarfile
import time
SIEMENS = ['Prisma_fit', 'TrioTim', 'Prisma', 'Skyra']
GE = ['DISCOVERY_MR750', 'SIGNA_Pioneer', 'DISCOVERY_MR750w', 'Signa_HDxt']
PHILIPS = ['Intera', 'Achieva', 'Ingenia', 'Achieva_dStream']
CONFIG_FOLDER = '/home/bore/p/unf/s/bids_conversion/configs'
MAIN_CONFIG = os.path.join(CONFIG_FOLDER, 'config.json')
GE_CONFIG = os.path.join(CONFIG_FOLDER, 'config_ge_cimaq.json')
SIEMENS_CONFIG = os.path.join(CONFIG_FOLDER, 'config_siemens_cimaq.json')
PHILIPS_CONFIG = os.path.join(CONFIG_FOLDER, 'config_philips_cimaq.json')
QUEBEC_CONFIG = os.path.join(CONFIG_FOLDER, 'config_philips_cimaq_QC.json')
CIMAQ_SIEMENS_CONFIG = ['IUGM', 'hospital_douglas',
'Mc_Connell_Brain_Imaging_Centre',
'THE_OTTAWA_HOSPITAL_CIVIC',
'the_ottawa_hospital_civic_campus']
TIME_SLEEP = 1
def get_arguments():
p = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""
Convert CIMAQ or CCNA release to bids format.
Input: Folder with tar files.
""")
p.add_argument('iFolder',
help="Folder to be sorted")
p.add_argument('oFolder',
help='Output folder - if doesn\'t exist it will be '
'created.')
p.add_argument('mode', default='ccna',
help="Mode CCNA or CIMAQ")
p.add_argument('--run_command', action='store_true',
help='Run the command line otherwise it will print'
' the command line')
return p
class IRMSession:
def __init__(self, filename, patient_name, patient_id, patient_date,
scan_date, patient_sex, scanner_model, scanner_version,
institution, mode, run_command):
# lineer
# (0, '<STUDY>\n')
# (L1, '<STUDY_INFO>\n')
# (L2, '* Unique Study ID
# (L3, '* Patient Name
# (L4, '* Patient ID
# (L5, '* Patient date of birth
# (L6, '* Scan Date
# (L7, '* Patient Sex
# (L8, '* Scanner Model Name
# (L9, '* Scanner Software Version
# (L10, '* Institution Name
# (L11, '* Modality
# (L12, '</STUDY_INFO>\n')
self.filename = filename.split('.meta')[0]
self.mode = mode
self.patient_name = patient_name
self.patient_id = patient_id
self.patient_birthdate = patient_date
self.patient_sex = patient_sex
self.scan_date = scan_date
self.scanner_model = scanner_model
self.scanner_version = scanner_version
self.institution = institution
# participant ID 7 chiffres
self.pscid = patient_name.split('_')[0]
# participant ID loris 6 chiffres
self.candid = patient_name.split('_')[1]
# visit label
self.session = self.get_session(patient_name.split('_')[2])
self.scanner_manufacturer = self.getManufacturer()
self.config = os.path.join(CONFIG_FOLDER, self.get_config())
self.run_command = run_command
def get_session(self, session):
if session == 'Initial':
return 1
else:
return 2
def getManufacturer(self):
if self.scanner_model in SIEMENS:
return 'siemens'
elif self.scanner_model in PHILIPS:
return 'philips'
elif self.scanner_model in GE:
return 'ge'
else:
print(self.scanner_model)
def get_config(self):
if self.mode == 'ccna':
return 'config_{}_{}.json'.format(self.scanner_manufacturer,
self.mode)
elif self.mode == 'cimaq':
return 'config_{}_{}_{}.json'.format(self.scanner_manufacturer,
self.mode,
self.institution)
def show(self):
print('#############')
print('# Subject: {}'.format(self.patient_name))
print('# Input file: {}'.format(self.filename))
print('# Scanner Model: {}'.format(self.scanner_model))
print('# Institution: {}'.format(self.institution))
print('#############')
def showOneLine(self, all=False):
if all:
print('{},{},{},{},{},{},{},{},{},'
'{},{},{},{},{}'.format(self.filename,
self.patient_name,
self.patient_id,
self.patient_birthdate,
self.patient_sex,
self.scan_date,
self.candid,
self.pscid,
self.session,
self.institution,
self.scanner_manufacturer,
self.scanner_model,
self.mode,
self.config))
else:
print('{},{},{},{},{},{}'.format(self.pscid,
self.session,
self.institution,
self.scanner_manufacturer,
self.scanner_model,
self.filename))
def extract(self):
tarname = self.filename + '.tar.gz'
if not os.path.exists(self.filename):
print('# -> Extraction {}'.format(tarname))
iTar = tarfile.open(name=tarname, mode='r|gz')
iTar.extractall(self.filename)
else:
print('# -> Already extracted {}'.format(tarname))
def delete_filename(self):
cmd = 'rm -rf {}'.format(self.filename)
os.system(cmd)
time.sleep(TIME_SLEEP)
def convert(self, bidsOutput):
cmd = 'dcm2bids -d {} -p {} -s {} -c {} -o {}'.format(self.filename,
self.pscid,
self.session,
MAIN_CONFIG,
bidsOutput)
if self.run_command:
os.system(cmd)
time.sleep(TIME_SLEEP)
else:
print(cmd)
if self.mode == 'ccna':
cmd = 'dcm2bids -d {} -p {} -s {} '\
'-c {} -o {}'.format(self.filename,
self.pscid,
self.session,
self.config,
bidsOutput)
elif self.mode == 'cimaq':
if self.institution in CIMAQ_SIEMENS_CONFIG:
curr_config = self.config
elif self.institution != 'CINQ' and\
'Quebec' not in self.institution and\
self.scanner_manufacturer == 'philips':
curr_config = PHILIPS_CONFIG
elif self.scanner_manufacturer == 'philips':
curr_config = QUEBEC_CONFIG
elif self.scanner_manufacturer == 'ge':
curr_config = GE_CONFIG
else:
print('# --> {} not converted, {} '
'- institution'.format(self.filename,
self.institution))
pass
cmd = 'dcm2bids -d {} -p {} -s {} '\
'-c {} -o {}'.format(self.filename,
self.candid,
self.session,
curr_config,
bidsOutput)
if self.run_command:
os.system(cmd)
time.sleep(TIME_SLEEP)
else:
print(cmd)
def read_metas(oFolder, mode, run_command):
metas = []
allFiles = glob.glob(os.path.join(oFolder, '**/*.meta'), recursive=True)
allFiles.sort()
for iFile in allFiles:
patient_name = None
patient_id = None
patient_date = None
scan_date = None
patient_sex = None
scanner_model = None
scanner_version = None
institution = None
fp = open(iFile)
for i, line in enumerate(fp):
if '*' == line[0]:
answer = line.split(':')[1].strip()
answer = answer.replace(' ', '_')
if answer.isspace() or answer == '':
continue
if 'Patient Name' in line:
patient_name = answer
elif 'Patient ID' in line:
patient_id = answer
elif 'Patient date of birth' in line:
patient_date = answer
elif 'Scan Date' in line:
scan_date = answer
elif 'Patient Sex' in line:
patient_sex = answer
elif 'Scanner Model Name' in line:
scanner_model = answer
elif 'Scanner Software Version' in line:
scanner_version = answer
elif 'Institution Name' in line:
institution = str(answer).lower()
elif '<FILES>' in line:
break
fp.close()
metas.append(IRMSession(iFile,
patient_name,
patient_id,
patient_date,
scan_date,
patient_sex,
scanner_model,
scanner_version,
institution,
mode,
run_command))
return metas
def extract_main_tar(i_tar_filename, oFolder):
i_basename, i_extension = os.path.splitext(os.path.basename(i_tar_filename))
o_folder_name = os.path.join(oFolder, i_basename)
if not os.path.exists(o_folder_name):
print('# -> Extraction {}'.format(i_tar_filename))
iTar = tarfile.open(name=i_tar_filename, mode='r')
iTar.extractall(o_folder_name)
else:
print('# {} already exists' .format(o_folder_name))
def extract_unique_dataset(metas):
"""
Input:
metas: List
"""
list_of_metas = []
for nMeta in metas:
list_of_metas.append((nMeta.institution,
nMeta.scanner_model,
nMeta.scanner_manufacturer,
nMeta.scanner_version))
indexes = []
uniqueMetas = list(set(list_of_metas))
for uniqueMeta in uniqueMetas:
for curr_index, meta in enumerate(list_of_metas):
if uniqueMeta == meta:
indexes.append(curr_index)
break
for curr_index in indexes:
metas[curr_index].showOneLine()
def check_mode(parser, mode):
if mode not in ['ccna', 'cimaq']:
parser.error('{} mode is not valid (ccna or cimaq).'.format(mode))
def main():
parser = get_arguments()
args = parser.parse_args()
check_mode(parser, args.mode)
all_tar_files = glob.glob(os.path.join(args.iFolder, '*tar'))
all_tar_files.sort()
for curr_tar_file in all_tar_files:
extract_main_tar(curr_tar_file, args.oFolder)
subjects = read_metas(args.oFolder, args.mode, args.run_command)
for sub in subjects:
# sub.showOneLine()
# print(sub.candid, sub.pscid)
sub.extract()
sub.convert(args.oFolder)
sub.delete_filename()
if __name__ == '__main__':
sys.exit(main())