-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDICOMDir.py
executable file
·170 lines (74 loc) · 3.81 KB
/
DICOMDir.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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 09 21:09:48 2013
@author: stephen russek
"""
# show_dicomdir.py
"""Example file to show use of read_dicomdir()
"""
# Copyright (c) 2013 Darcy Mason
# This file is part of pydicom, relased under an MIT-style license.
# See the file license.txt included with this distribution, also
# available at http://pydicom.googlecode.com
#
import sys
import dicom
import os.path
from pprint import pprint
# dicom.debug()
if __name__ == "__main__":
print "------------------------------------------------------------"
print "Example program showing DICOMDIR contents, assuming standard"
print "Patient -> Study -> Series -> Images hierarchy"
print "------------------------------------------------------------"
print
if len(sys.argv) > 1:
filepath = sys.argv[1]
if os.path.isdir(filepath): # only gave directory, add standard name
filepath = os.path.join(filepath, "DICOMDIR")
dcmdir = dicom.read_dicomdir(filepath)
base_dir = os.path.dirname(filepath)
else:
# Read standard "DICOMDIR" filename from current directory
dcmdir = dicom.read_dicomdir()
base_dir = "."
for patrec in dcmdir.patient_records:
print "Patient: {0.PatientID}: {0.PatientsName}".format(patrec)
studies = patrec.children
for study in studies:
print (" Study {0.StudyID}: {0.StudyDate}:"
" {0.StudyDescription}".format(study))
all_series = study.children
for series in all_series:
image_count = len(series.children)
plural = ('', 's')[image_count > 1]
# Write basic series info and image count
# Put N/A in if no Series Description
if not 'SeriesDescription' in series:
series.SeriesDescription = "N/A"
print (" " * 8 + "Series {0.SeriesNumber}: {0.Modality}: {0.SeriesDescription}"
" ({1} image{2})".format(series, image_count, plural))
# Open and read something from each image, for demonstration purposes
# For simple quick overview of DICOMDIR, leave the following out
print " " * 12 + "Reading images..."
image_records = series.children
image_filenames = [os.path.join(base_dir, *image_rec.ReferencedFileID)
for image_rec in image_records]
# slice_locations = [dicom.read_file(image_filename).SliceLocation
# for image_filename in image_filenames]
datasets = [dicom.read_file(image_filename)
for image_filename in image_filenames]
patient_names = set(ds.PatientName for ds in datasets)
patient_IDs = set(ds.PatientID for ds in datasets)
# List the image filenames
print "\n" + " " * 12 + "Image filenames:"
print " " * 12,
pprint(image_filenames, indent=12)
# Expect all images to have same patient name, id
# Show the set of all names, IDs found (should each have one)
print (" " * 12 + "Patient Names in images..: "
"{0:s}".format(patient_names))
print (" " * 12 + "Patient IDs in images..:"
"{0:s}".format(patient_IDs))
# print (" " * 12 + "Slice Locations from "
# "{0} to {1}".format(min(slice_locations), max(slice_locations)))