-
Notifications
You must be signed in to change notification settings - Fork 56
/
dicom_dir.py
executable file
·107 lines (94 loc) · 3.34 KB
/
dicom_dir.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
#!/usr/bin/env python
"""
This example will read a directory of dicom files and parse them into
a list of Patients with Studies, Series, and Instances for each.
run with
./dicom_dir -d directory --recursive
This example just prints out the patient/study/series/instance hierarchy
"""
# Copyright (c) 2017 Robert Haxton
# This file is part of pydicom, released under a modified MIT license.
# See the file LICENSE included with this distribution, also
# available at https://github.com/pydicom/pydicom
from __future__ import print_function
import os
import argparse
import sys
import fnmatch
from pprint import pformat
import pydicom
from patient import Patient
def find_dicom_files(directory, pattern="*", directory_exclude_pattern='', recursive=True):
"""
search a root directory for all files matching a given pattern (in Glob format - *.dcm etc)
and that have the "DICM" magic number
returns a full path name
"""
for root, dirs, files in os.walk(directory):
if not recursive:
dirs = []
for x in dirs:
if fnmatch.fnmatch(x, directory_exclude_pattern):
try:
dirs.remove(x)
except Exception:
pass
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
x = "Not A Dicom File"
with open(filename, 'rb') as f:
x = f.read(132)
if x[128:] == "DICM":
yield filename
def parse_args(argv=None):
"""Argument parser for Dicom Tools"""
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
description="Scan a directory of dicom files "
"and assemble a list of patient, "
"study, series, image sets")
parser.add_argument("-d", "--dicom-dir",
dest='dicom_dir',
type=str,
help="Directory of dicom files ",
default=".")
parser.add_argument("-r", "--recursive",
dest='recursive',
action='store_true',
help="Process recursively")
parser.add_argument("--no-recursive",
dest='recursive',
action='store_false',
help="Do not process recursively")
parser.set_defaults(recursive=True)
return parser.parse_args()
def main(argv=None):
"""main for dicom_dir"""
if argv is None:
argv = sys.argv
args = parse_args(argv=argv[1:])
print(pformat(args))
patients = list()
for x in find_dicom_files(directory=args.dicom_dir,
pattern="*.dcm",
directory_exclude_pattern=".*",
recursive=args.recursive):
f = pydicom.dcmread(x)
for p in patients:
try:
p.add_dataset(f)
except Exception as e:
pass
else:
break
else:
print("New patient!")
patients.append(Patient(dicom_dataset=f))
print("Found", len(patients), "patients")
for x in patients:
print(repr(x))
print("\n")
if __name__ == "__main__":
main()