forked from vuptran/cardiac-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename_sunnybrook.py
More file actions
executable file
·24 lines (21 loc) · 974 Bytes
/
Copy pathrename_sunnybrook.py
File metadata and controls
executable file
·24 lines (21 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python2.7
'''renames Sunnybrook submission files to match IM-0001-XXXX,
where XXXX is 4 digit index corresponding to the number of dicom images
'''
import os, re, sys
root_folder = sys.argv[1]
for case in os.listdir(root_folder):
case_path = os.path.join(root_folder, case)
if os.path.isdir(case_path):
d = {}
dcm_files = [f for dirpath, dirnames, files in os.walk(case_path)
for f in sorted(files) if '.dcm' in f]
for idx, dcm in enumerate(dcm_files):
match = re.search(r'IM-0001-(\d{4})', dcm)
d[match.group(1)] = '{:04d}'.format(idx+1)
for dirpath, dirnames, files in os.walk(case_path):
for f in sorted(files):
match = re.search(r'IM-0001-(\d{4})', f)
if match:
f_new = f.replace(match.group(1), d[match.group(1)])
os.rename(os.path.join(dirpath, f), os.path.join(dirpath, f_new))