forked from scitran-apps/dicom-mr-classifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dicom-mr-classifier.py
executable file
·513 lines (435 loc) · 17.7 KB
/
dicom-mr-classifier.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/env python
import os
import re
import json
import pytz
import dicom
import string
import tzlocal
import logging
import zipfile
import datetime
import classification_from_label
from fnmatch import fnmatch
from pprint import pprint
logging.basicConfig()
log = logging.getLogger('dicom-mr-classifier')
def get_session_label(dcm):
"""
Switch on manufacturer and either pull out the StudyID or the StudyInstanceUID
"""
session_label = ''
if ( dcm.get('Manufacturer') and (dcm.get('Manufacturer').find('GE') != -1 or dcm.get('Manufacturer').find('Philips') != -1 ) and dcm.get('StudyID')):
session_label = dcm.get('StudyID')
else:
session_label = dcm.get('StudyInstanceUID')
return session_label
def validate_timezone(zone):
# pylint: disable=missing-docstring
if zone is None:
zone = tzlocal.get_localzone()
else:
try:
zone = pytz.timezone(zone.zone)
except pytz.UnknownTimeZoneError:
zone = None
return zone
def parse_patient_age(age):
"""
Parse patient age from string.
convert from 70d, 10w, 2m, 1y to datetime.timedelta object.
Returns age as duration in seconds.
"""
if age == 'None' or not age:
return None
conversion = { # conversion to days
'Y': 365,
'M': 30,
'W': 7,
'D': 1,
}
scale = age[-1:]
value = age[:-1]
if scale not in conversion.keys():
# Assume years
scale = 'Y'
value = age
age_in_seconds = datetime.timedelta(int(value) * conversion.get(scale)).total_seconds()
# Make sure that the age is reasonable
if not age_in_seconds or age_in_seconds <= 0:
age_in_seconds = None
return age_in_seconds
def timestamp(date, time, timezone):
"""
Return datetime formatted string
"""
if date and time and timezone:
# return datetime.datetime.strptime(date + time[:6], '%Y%m%d%H%M%S')
try:
return timezone.localize(datetime.datetime.strptime(date + time[:6], '%Y%m%d%H%M%S'), timezone)
except:
log.warning('Failed to create timestamp!')
log.info(date)
log.info(time)
log.info(timezone)
return None
return None
def get_timestamp(dcm, timezone):
"""
Parse Study Date and Time, return acquisition and session timestamps
"""
if hasattr(dcm, 'StudyDate') and hasattr(dcm, 'StudyTime'):
study_date = dcm.StudyDate
study_time = dcm.StudyTime
elif hasattr(dcm, 'StudyDateTime'):
study_date = dcm.StudyDateTime[0:8]
study_time = dcm.StudyDateTime[8:]
else:
study_date = None
study_time = None
if hasattr(dcm, 'AcquisitionDate') and hasattr(dcm, 'AcquisitionTime'):
acquitision_date = dcm.AcquisitionDate
acquisition_time = dcm.AcquisitionTime
elif hasattr(dcm, 'AcquisitionDateTime'):
acquitision_date = dcm.AcquisitionDateTime[0:8]
acquisition_time = dcm.AcquisitionDateTime[8:]
# The following allows the timestamps to be set for ScreenSaves
elif hasattr(dcm, 'ContentDate') and hasattr(dcm, 'ContentTime'):
acquitision_date = dcm.ContentDate
acquisition_time = dcm.ContentTime
else:
acquitision_date = None
acquisition_time = None
session_timestamp = timestamp(dcm.StudyDate, dcm.StudyTime, timezone)
acquisition_timestamp = timestamp(acquitision_date, acquisition_time, timezone)
if session_timestamp:
if session_timestamp.tzinfo is None:
log.info('no tzinfo found, using UTC...')
session_timestamp = pytz.timezone('UTC').localize(session_timestamp)
session_timestamp = session_timestamp.isoformat()
else:
session_timestamp = ''
if acquisition_timestamp:
if acquisition_timestamp.tzinfo is None:
log.info('no tzinfo found, using UTC')
acquisition_timestamp = pytz.timezone('UTC').localize(acquisition_timestamp)
acquisition_timestamp = acquisition_timestamp.isoformat()
else:
acquisition_timestamp = ''
return session_timestamp, acquisition_timestamp
def get_sex_string(sex_str):
"""
Return male or female string.
"""
if sex_str == 'M':
sex = 'male'
elif sex_str == 'F':
sex = 'female'
else:
sex = ''
return sex
def assign_type(s):
"""
Sets the type of a given input.
"""
if type(s) == list or type(s) == dicom.multival.MultiValue:
try:
return [ int(x) for x in s ]
except ValueError:
try:
return [ float(x) for x in s ]
except ValueError:
return [ format_string(x) for x in s if len(x) > 0 ]
else:
s = str(s)
try:
return int(s)
except ValueError:
try:
return float(s)
except ValueError:
return format_string(s)
def format_string(in_string):
formatted = re.sub(r'[^\x00-\x7f]',r'', str(in_string)) # Remove non-ascii characters
formatted = filter(lambda x: x in string.printable, formatted)
if len(formatted) == 1 and formatted == '?':
formatted = None
return formatted#.encode('utf-8').strip()
def get_seq_data(sequence, ignore_keys):
seq_dict = {}
for seq in sequence:
for s_key in seq.dir():
s_val = getattr(seq, s_key, '')
if type(s_val) is dicom.UID.UID or s_key in ignore_keys:
continue
if type(s_val) == dicom.sequence.Sequence:
_seq = get_seq_data(s_val, ignore_keys)
seq_dict[s_key] = _seq
continue
if type(s_val) == str:
s_val = format_string(s_val)
else:
s_val = assign_type(s_val)
if s_val:
seq_dict[s_key] = s_val
return seq_dict
def get_dicom_header(dcm):
# Extract the header values
header = {}
exclude_tags = ['[Unknown]', 'PixelData', 'Pixel Data', '[User defined data]', '[Protocol Data Block (compressed)]', '[Histogram tables]', '[Unique image iden]']
tags = dcm.dir()
for tag in tags:
try:
if (tag not in exclude_tags) and ( type(dcm.get(tag)) != dicom.sequence.Sequence ):
value = dcm.get(tag)
if value or value == 0: # Some values are zero
# Put the value in the header
if type(value) == str and len(value) < 10240: # Max dicom field length
header[tag] = format_string(value)
else:
header[tag] = assign_type(value)
else:
log.debug('No value found for tag: ' + tag)
if type(dcm.get(tag)) == dicom.sequence.Sequence:
seq_data = get_seq_data(dcm.get(tag), exclude_tags)
# Check that the sequence is not empty
if seq_data:
header[tag] = seq_data
except:
log.debug('Failed to get ' + tag)
pass
return header
def get_csa_header(dcm):
import dicom
import nibabel.nicom.dicomwrappers
exclude_tags = ['PhoenixZIP', 'SrMsgBuffer']
header = {}
try:
raw_csa_header = nibabel.nicom.dicomwrappers.SiemensWrapper(dcm).csa_header
tags = raw_csa_header['tags']
except:
log.warning('Failed to parse csa header!')
return header
for tag in tags:
if not raw_csa_header['tags'][tag]['items'] or tag in exclude_tags:
log.debug('Skipping : %s' % tag)
pass
else:
value = raw_csa_header['tags'][tag]['items']
if len(value) == 1:
value = value[0]
if type(value) == str and ( len(value) > 0 and len(value) < 1024 ):
header[format_string(tag)] = format_string(value)
else:
header[format_string(tag)] = assign_type(value)
else:
header[format_string(tag)] = assign_type(value)
return header
def get_classification_from_string(value):
result = {}
parts = re.split(r'\s*,\s*', value)
last_key = None
for part in parts:
key_value = re.split(r'\s*:\s*', part)
if len(key_value) == 2:
last_key = key = key_value[0]
value = key_value[1]
else:
if last_key:
key = last_key
else:
log.warn('Unknown classification format: {0}'.format(part))
key = 'Custom'
value = part
if key not in result:
result[key] = []
result[key].append(value)
return result
def get_custom_classification(label, config_file):
if config_file is None or not os.path.isfile(config_file):
return None
try:
with open(config_file, 'r') as f:
config = json.load(f)
# Check custom classifiers
classifications = config['inputs'].get('classifications', {}).get('value', {})
if not classifications:
log.debug('No custom classifications found in config')
return None
if not isinstance(classifications, dict):
log.warning('classifications must be an object!')
return None
for k in classifications.keys():
val = classifications[k]
if not isinstance(val, basestring):
log.warn('Expected string value for classification key %s', k)
continue
if len(k) > 2 and k[0] == '/' and k[-1] == '/':
# Regex
try:
if re.search(k[1:-1], label, re.I):
log.debug('Matched custom classification for key: %s', k)
return get_classification_from_string(val)
except re.error:
log.exception('Invalid regular expression: %s', k)
elif fnmatch(label.lower(), k.lower()):
log.debug('Matched custom classification for key: %s', k)
return get_classification_from_string(val)
except IOError:
log.exception('Unable to load config file: %s', config_file)
return None
def dicom_classify(zip_file_path, outbase, timezone, config_file=None):
"""
Extracts metadata from dicom file header within a zip file and writes to .metadata.json.
"""
import dicom
# Check for input file path
if not os.path.exists(zip_file_path):
log.debug('could not find %s' % zip_file_path)
log.debug('checking input directory ...')
if os.path.exists(os.path.join('/input', zip_file_path)):
zip_file_path = os.path.join('/input', zip_file_path)
log.debug('found %s' % zip_file_path)
if not outbase:
outbase = '/flywheel/v0/output'
log.info('setting outbase to %s' % outbase)
# Extract the last file in the zip to /tmp/ and read it
dcm = []
if zipfile.is_zipfile(zip_file_path):
zip = zipfile.ZipFile(zip_file_path)
num_files = len(zip.namelist())
for n in range((num_files -1), -1, -1):
dcm_path = zip.extract(zip.namelist()[n], '/tmp')
if os.path.isfile(dcm_path):
try:
log.info('reading %s' % dcm_path)
dcm = dicom.read_file(dcm_path)
# Here we check for the Raw Data Storage SOP Class, if there
# are other DICOM files in the zip then we read the next one,
# if this is the only class of DICOM in the file, we accept
# our fate and move on.
if dcm.get('SOPClassUID') == 'Raw Data Storage' and n != range((num_files -1), -1, -1)[-1]:
continue
else:
break
except:
pass
else:
log.warning('%s does not exist!' % dcm_path)
else:
log.info('Not a zip. Attempting to read %s directly' % os.path.basename(zip_file_path))
dcm = dicom.read_file(zip_file_path)
if not dcm:
log.warning('dcm is empty!!!')
os.sys.exit(1)
# Build metadata
metadata = {}
# Session metadata
metadata['session'] = {}
session_timestamp, acquisition_timestamp = get_timestamp(dcm, timezone);
if session_timestamp:
metadata['session']['timestamp'] = session_timestamp
if hasattr(dcm, 'OperatorsName') and dcm.get('OperatorsName'):
metadata['session']['operator'] = format_string(dcm.get('OperatorsName'))
session_label = get_session_label(dcm)
if session_label:
metadata['session']['label'] = session_label
# Subject Metadata
metadata['session']['subject'] = {}
if hasattr(dcm, 'PatientSex') and get_sex_string(dcm.get('PatientSex')):
metadata['session']['subject']['sex'] = get_sex_string(dcm.get('PatientSex'))
if hasattr(dcm, 'PatientAge') and dcm.get('PatientAge'):
try:
age = parse_patient_age(dcm.get('PatientAge'))
if age:
metadata['session']['subject']['age'] = int(age)
except:
pass
if hasattr(dcm, 'PatientName') and dcm.get('PatientName').given_name:
# If the first name or last name field has a space-separated string, and one or the other field is not
# present, then we assume that the operator put both first and last names in that one field. We then
# parse that field to populate first and last name.
metadata['session']['subject']['firstname'] = format_string(dcm.get('PatientName').given_name)
if not dcm.get('PatientName').family_name:
name = format_string(dcm.get('PatientName').given_name.split(' '))
if len(name) == 2:
first = name[0]
last = name[1]
metadata['session']['subject']['lastname'] = last
metadata['session']['subject']['firstname'] = first
if hasattr(dcm, 'PatientName') and dcm.get('PatientName').family_name:
metadata['session']['subject']['lastname'] = format_string(dcm.get('PatientName').family_name)
if not dcm.get('PatientName').given_name:
name = format_string(dcm.get('PatientName').family_name.split(' '))
if len(name) == 2:
first = name[0]
last = name[1]
metadata['session']['subject']['lastname'] = last
metadata['session']['subject']['firstname'] = first
# File classification
dicom_file = {}
dicom_file['name'] = os.path.basename(zip_file_path)
dicom_file['modality'] = format_string(dcm.get('Modality', 'MR'))
dicom_file['classification'] = {}
# Acquisition metadata
metadata['acquisition'] = {}
if hasattr(dcm, 'Modality') and dcm.get('Modality'):
metadata['acquisition']['instrument'] = format_string(dcm.get('Modality'))
series_desc = format_string(dcm.get('SeriesDescription', ''))
if series_desc:
metadata['acquisition']['label'] = series_desc
classification = get_custom_classification(series_desc, config_file)
log.info('Custom classification from config: %s', classification)
if not classification:
classification = classification_from_label.infer_classification(series_desc)
log.info('Inferred classification from label: %s', classification)
dicom_file['classification'] = classification
# If no pixel data present, make classification intent "Non-Image"
if not hasattr(dcm, 'PixelData'):
nonimage_intent = {'Intent': ['Non-Image']}
# If classification is a dict, update dict with intent
if isinstance(dicom_file['classification'], dict):
dicom_file['classification'].update(nonimage_intent)
# Else classification is a list, assign dict with intent
else:
dicom_file['classification'] = nonimage_intent
if acquisition_timestamp:
metadata['acquisition']['timestamp'] = acquisition_timestamp
# Acquisition metadata from dicom header
dicom_file['info'] = get_dicom_header(dcm)
# Append the dicom_file to the files array
metadata['acquisition']['files'] = [dicom_file]
# Acquisition metadata from dicom header
metadata['acquisition']['metadata'] = get_dicom_header(dcm)
if dcm.get('Manufacturer') == 'SIEMENS':
csa_header = get_csa_header(dcm)
if csa_header:
metadata['acquisition']['metadata']['CSAHeader'] = csa_header
# Write out the metadata to file (.metadata.json)
metafile_outname = os.path.join(os.path.dirname(outbase),'.metadata.json')
with open(metafile_outname, 'w') as metafile:
json.dump(metadata, metafile)
# Show the metadata
pprint(metadata)
return metafile_outname
if __name__ == '__main__':
"""
Generate session, subject, and acquisition metatada by parsing the dicom header, using pydicom.
"""
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('dcmzip', help='path to dicom zip')
ap.add_argument('outbase', nargs='?', help='outfile name prefix')
ap.add_argument('--log_level', help='logging level', default='info')
ap.add_argument('--config-file', help='Configuration file with custom classifications in context')
args = ap.parse_args()
log.setLevel(getattr(logging, args.log_level.upper()))
logging.getLogger('sctran.data').setLevel(logging.INFO)
log.info('start: %s' % datetime.datetime.utcnow())
args.timezone = validate_timezone(tzlocal.get_localzone())
metadatafile = dicom_classify(args.dcmzip, args.outbase, args.timezone, config_file=args.config_file)
if os.path.exists(metadatafile):
log.info('generated %s' % metadatafile)
else:
log.info('failure! %s was not generated!' % metadatafile)
log.info('stop: %s' % datetime.datetime.utcnow())