forked from pieper/nrrdify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenameNRRDs.py
More file actions
62 lines (53 loc) · 1.95 KB
/
renameNRRDs.py
File metadata and controls
62 lines (53 loc) · 1.95 KB
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
import json
import os
import shutil
import subprocess
sourceDirectory = '/Volumes/encrypted/babybrains/converted'
targetDirectory = '/Volumes/encrypted/babybrains/converted-renamed'
patientMap = {}
studyMap = {}
seriesMap = {}
def rename(subdirectory):
lsProcess = subprocess.Popen(['ls', os.path.join(subdirectory)], stdout=subprocess.PIPE)
nrrds = []
for line in lsProcess.stdout.readlines():
if line.strip().endswith('.nrrd'):
nrrds.append(line.strip())
for nrrd in nrrds:
patient = subdirectory.split('/')[-2]
study = subdirectory.split('/')[-1]
if not patient in patientMap:
patientMap[patient] = 'patient-%05d' % len(patientMap.keys())
studyMap[patient] = {}
if not study in studyMap[patient]:
studyMap[patient][study] = 'study-%05d' % len(studyMap[patient].keys())
outputPath = os.path.join(targetDirectory, patientMap[patient], studyMap[patient][study])
if not os.path.exists(outputPath):
os.makedirs(outputPath)
nrrdPath = os.path.join(outputPath, patientMap[patient] + "_" + studyMap[patient][study] + "_" + "series-" + str(nrrds.index(nrrd)) + ".nrrd")
print('-- copy --')
oldPath = os.path.join(subdirectory, nrrd)
print(oldPath)
print(nrrdPath)
shutil.copyfile(oldPath, nrrdPath)
findProcess = subprocess.Popen(['find', sourceDirectory, '-type', 'd'], stdout=subprocess.PIPE)
while True:
try:
subdirectory = findProcess.stdout.readline().strip()
if subdirectory == "":
break
print(subdirectory)
rename(subdirectory)
#if len(patientMap.keys()) > 20:
#break
except:
print ('-'*40)
import traceback
traceback.print_exc() # XXX But this goes to stderr!
print ('-'*40)
fp = open(os.path.join(sourceDirectory, "patientMap.json"), 'w')
fp.write(json.dumps(patientMap))
fp.close()
fp = open(os.path.join(sourceDirectory, "studyMap.json"), 'w')
fp.write(json.dumps(studyMap))
fp.close()