-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_dicom_v2301.py
executable file
·172 lines (144 loc) · 5.62 KB
/
rename_dicom_v2301.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
#!/usr/bin/python
import pydicom
import argparse
import json
import os.path
import shutil
import os
import re
import logging
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
find_non_alpha = re.compile('[^0-9a-zA-Z_\-]+')
dest_permissions = 0o775
class MyHandler(FileSystemEventHandler):
def __init__(self, dest_dir) -> None:
self.dest_dir = dest_dir
super().__init__()
def on_closed(self, event):
logging.info('File closed: {}'.format(event.src_path))
try:
run_one_file(event.src_path, self.dest_dir)
except Exception as ex:
logging.error('Error on file {}'.format(event.src_path),
exc_info=ex)
def make_and_chmodown_dir_if_not_exist(dir_name):
if not os.path.exists(dir_name):
logging.info('Creating directory: {}'.format(dir_name))
os.makedirs(dir_name)
os.chmod(dir_name, dest_permissions)
shutil.chown(dir_name, user='dcmserver', group='gmidata_dicom')
def get_acquisition_date(ds: pydicom.dataset.FileDataset) -> str:
if 'AcquisitionDate' in ds:
return ds.AcquisitionDate
elif 'AcquisitionDateTime' in ds:
return ds.AcquisitionDateTime[:8]
elif 'SeriesDate' in ds:
return ds.SeriesDate
elif 'StudyDate' in ds:
return ds.StudyDate
else:
return '00000000'
def run_one_file(path: str, dest_dir: str):
# read dicom header
ds = pydicom.dcmread(path, stop_before_pixels=True)
# get and format needed fields
alphanum = lambda v: find_non_alpha.sub('_', str(v).strip())
fields = {
'id':
alphanum(ds.PatientID) if hasattr(ds, 'PatientID') else 'NOID',
'name':
alphanum(ds.PatientName) if hasattr(ds, 'PatientName') else 'NONAME',
'snum':
int(ds.SeriesNumber) if hasattr(ds, 'SeriesNumber') and
(ds.SeriesNumber is not None) else 0,
'sdesc':
alphanum(ds.SeriesDescription)
if hasattr(ds, 'SeriesDescription') else 'UNKNOWN',
'inum':
int(ds.InstanceNumber) if hasattr(ds, 'InstanceNumber') and
(ds.InstanceNumber is not None) else 0,
'adate':
alphanum(get_acquisition_date(ds)),
}
# construct destination directory
subj_dir = os.path.join(dest_dir, '{}+{}'.format(fields['id'],
fields['name']))
make_and_chmodown_dir_if_not_exist(subj_dir)
series_dir = os.path.join(
subj_dir, '{}+{:03d}-{}'.format(fields['adate'], fields['snum'],
fields['sdesc']))
make_and_chmodown_dir_if_not_exist(series_dir)
# construct destination file name
dest_name = os.path.join(series_dir, '{:05d}.dcm'.format(fields['inum']))
# ensure file doesn't already exist, or else append a number
inc_num = 0
while os.path.exists(dest_name) and inc_num < 9999:
inc_num += 1
dest_name = os.path.join(
series_dir, '{:05d}_{:04d}.dcm'.format(fields['inum'], inc_num))
# move file
logging.info('Moving file: {} -> {}'.format(path, dest_name))
# shutil.move(path, dest_name)
os.system('/bin/mv "{}" "{}"'.format(path, dest_name))
os.chmod(dest_name, dest_permissions)
return dest_name
# main
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=
'Read DICOM header and move files accordingly to the correct destination.'
)
parser.add_argument('--watch',
action='store_true',
help='Watch for new files in source directory')
parser.add_argument('--verbose',
action='store_true',
help='Be verbose with filenames and actions')
parser.add_argument('source_dir',
type=str,
help='Path to source directory')
parser.add_argument('dest_dir',
type=str,
help='Path to destination directory')
parser.add_argument('files',
type=str,
nargs='*',
help='List of files to process')
args = parser.parse_args()
if args.verbose:
logging.basicConfig(stream=sys.stdout,
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s')
if args.watch:
# watch for new files
logging.info('Watching for new files in: {}'.format(args.source_dir))
observer = Observer()
observer.schedule(MyHandler(dest_dir=args.dest_dir),
path=args.source_dir,
recursive=False)
observer.start()
try:
# process all files in the directory right now
files = [
os.path.join(args.source_dir, f)
for f in os.listdir(args.source_dir)
if os.path.isfile(os.path.join(args.source_dir, f))
]
for f in files:
try:
run_one_file(f, args.dest_dir)
except Exception as ex:
logging.error('Error on file {}'.format(f), exc_info=ex)
# keep running
while True:
time.sleep(5)
finally:
observer.stop()
observer.join()
else:
# process files
for f in args.files:
run_one_file(f, args.dest_dir)