This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdicom-mr-classifier.py
executable file
·659 lines (569 loc) · 22.5 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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/env python
import os
import re
import json
import pytz
import pydicom
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")
DEFAULT_TIME = '120000.00'
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.25,
"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.
For study date/time Dicom tag used by order of priority goes like a:
- StudyDate/StudyTime
- SeriesDate/SeriesTime
- AcquisitionDate/AcquisitionTime
- AcquisitionDateTime
- StudyDate and Time defaults to DEFAULT_TIME
- SeriesDates and Time defaults to DEFAULT_TIME
- AcquisitionDate and Time defaults to DEFAULT_TIME
For acquisition date/time Dicom tag used by order of priority goes like a:
- SeriesDate/SeriesTime
- AcquisitionDate/AcquisitionTime
- AcquisitionDateTime
- ContentDate/ContentTime
- StudyDate/StudyTime
- SeriesDate and Time defaults to DEFAULT_TIME
- AcquisitionDate and Time defaults to DEFAULT_TIME
- StudyDate and Time defaults to DEFAULT_TIME
"""
# Study Date and Time, with precedence as below
if getattr(dcm, 'StudyDate', None) and getattr(dcm, 'StudyTime', None):
study_date = dcm.StudyDate
study_time = dcm.StudyTime
elif getattr(dcm, 'SeriesDate', None) and getattr(dcm, 'SeriesTime', None):
study_date = dcm.SeriesDate
study_time = dcm.SeriesTime
elif getattr(dcm, 'AcquisitionDate', None) and getattr(dcm, 'AcquisitionTime', None):
study_date = dcm.AcquisitionDate
study_time = dcm.AcquisitionTime
elif getattr(dcm, 'AcquisitionDateTime', None):
study_date = dcm.AcquisitionDateTime[0:8]
study_time = dcm.AcquisitionDateTime[8:]
elif getattr(dcm, 'StudyTime', None) and getattr(dcm, 'InstanceCreationDate', None):
study_date = dcm.InstanceCreationDate
study_time = dcm.StudyTime
# If only Dates are available setting time to 00:00
elif getattr(dcm, 'StudyDate', None):
study_date = dcm.StudyDate
study_time = DEFAULT_TIME
elif getattr(dcm, 'SeriesDate', None):
study_date = dcm.SeriesDate
study_time = DEFAULT_TIME
elif getattr(dcm, 'AcquisitionDate', None):
study_date = dcm.AcquisitionDate
study_time = DEFAULT_TIME
elif getattr(dcm, 'InstanceCreationDate', None):
study_date = dcm.InstanceCreationDate
study_time = DEFAULT_TIME
else:
study_date = None
study_time = None
# Acquisition Date and Time, with precedence as below
if getattr(dcm, 'SeriesDate', None) and getattr(dcm, 'SeriesTime', None):
acquisition_date = dcm.SeriesDate
acquisition_time = dcm.SeriesTime
elif getattr(dcm, 'AcquisitionDate', None) and getattr(dcm, 'AcquisitionTime', None):
acquisition_date = dcm.AcquisitionDate
acquisition_time = dcm.AcquisitionTime
elif getattr(dcm, 'AcquisitionDateTime', None):
acquisition_date = dcm.AcquisitionDateTime[0:8]
acquisition_time = dcm.AcquisitionDateTime[8:]
# The following allows the timestamps to be set for ScreenSaves
elif getattr(dcm, 'ContentDate', None) and getattr(dcm, 'ContentTime', None):
acquisition_date = dcm.ContentDate
acquisition_time = dcm.ContentTime
# Looking deeper if nothing found so far
elif getattr(dcm, 'StudyDate', None) and getattr(dcm, 'StudyTime', None):
acquisition_date = dcm.StudyDate
acquisition_time = dcm.StudyTime
elif getattr(dcm, 'InstanceCreationDate', None) and getattr(dcm, 'InstanceCreationTime', None):
acquisition_date = dcm.InstanceCreationDate
acquisition_time = dcm.InstanceCreationTime
# If only Dates are available setting time to 00:00
elif getattr(dcm, 'SeriesDate', None):
acquisition_date = dcm.SeriesDate
acquisition_time = DEFAULT_TIME
elif getattr(dcm, 'AcquisitionDate', None):
acquisition_date = dcm.AcquisitionDate
acquisition_time = DEFAULT_TIME
elif getattr(dcm, 'StudyDate', None):
acquisition_date = dcm.StudyDate
acquisition_time = DEFAULT_TIME
elif getattr(dcm, 'InstanceCreationDate', None):
acquisition_date = dcm.InstanceCreationDate
acquisition_time = DEFAULT_TIME
else:
acquisition_date = None
acquisition_time = None
session_timestamp = timestamp(study_date, study_time, timezone)
acquisition_timestamp = timestamp(acquisition_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 (
isinstance(s, pydicom.valuerep.PersonName)
):
return format_string(s)
if type(s) == list or type(s) == pydicom.multival.MultiValue:
try:
return [int(x) if type(x) == int else float(x) for x in s]
except ValueError:
return [format_string(x) for x in s if len(x) > 0]
elif type(s) == float or type(s) == int:
return s
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 = "".join(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 pydicom.uid.UID or s_key in ignore_keys:
continue
if type(s_val) == pydicom.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)) != pydicom.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)) == pydicom.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 pydicom
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=None):
if config is None:
return None
# 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)
return None
def dicom_classify(zip_file_path, outbase, timezone, config=None):
"""
Extracts metadata from dicom file header within a zip file and writes to .metadata.json.
"""
import pydicom
# Parse config for options
if config:
config_force = config["config"].get("force")
if config_force:
log.warning("Attempting to force DICOM read. Input DICOM may not be valid.")
else:
config_force = False
# 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 = pydicom.dcmread(dcm_path, force=config_force)
# 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 = pydicom.dcmread(zip_file_path)
if not dcm:
log.warning(
'DICOM could not be read! Is this a valid DICOM file? To force parsing the file, run again setting "force" configuration option to "true"'
)
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
if hasattr(dcm, "PatientWeight") and dcm.get("PatientWeight"):
weight = assign_type(dcm.get("PatientWeight"))
try:
weight = float(weight)
metadata["session"]["weight"] = weight
except:
log.warning('Could not parse PatientWeight, droppping.')
pass
# 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:
age = int(age)
metadata["session"]["age"] = age
except:
log.warning('Could not parse PatientAge, droppping.')
pass
if hasattr(dcm, "PatientName") and isinstance(dcm.get('PatientName'),pydicom.valuerep.PersonName):
if 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"] = str(
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"] = str(last)
metadata["session"]["subject"]["firstname"] = str(first)
if dcm.get("PatientName").family_name:
metadata["session"]["subject"]["lastname"] = str(
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"] = str(last)
metadata["session"]["subject"]["firstname"] = str(first)
# File classification
dicom_file = {}
dicom_file["name"] = os.path.basename(zip_file_path)
if dcm.get('Modality'):
dicom_file["modality"] = format_string(dcm.get("Modality"))
else:
log.warning('No modality found.')
dicom_file["modality"] = None
dicom_file["classification"] = {}
# Acquisition metadata
metadata["acquisition"] = {}
if acquisition_timestamp:
metadata["acquisition"]["timestamp"] = acquisition_timestamp
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)
log.info("Custom classification from config: %s", classification)
if not classification and dcm.get("Modality") == "MR":
classification = classification_from_label.infer_classification(series_desc)
log.info("Inferred classification from label: %s", classification)
# GEAR-1084, keep any custom classification already set.
if not classification:
classification = {'Custom': ['N/A']}
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
# File info from dicom header
dicom_file["info"] = get_dicom_header(dcm)
# Grab CSA header for Siemens data
if dcm.get("Manufacturer") == "SIEMENS":
csa_header = get_csa_header(dcm)
if csa_header:
dicom_file["info"]["CSAHeader"] = csa_header
# Append the dicom_file to the files array
metadata["acquisition"]["files"] = [dicom_file]
# 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",
default="/flywheel/v0/config.json",
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())
# Load config from file
if args.config_file and os.path.isfile(args.config_file):
with open(args.config_file) as json_file:
config = json.load(json_file)
else:
config = None
metadatafile = dicom_classify(args.dcmzip, args.outbase, args.timezone, config)
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())