-
Notifications
You must be signed in to change notification settings - Fork 4
/
Sunny_Data_Conditioner.py
executable file
·174 lines (151 loc) · 5.37 KB
/
Sunny_Data_Conditioner.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
173
174
#!/usr/bin/env python
#########################################################################################
# Copyright (c) 2017 Liset Vazquez Romaguera, Francisco Perdigon Romero
# Authors: Francisco Perdigon Romero
# Liset Vazquez Romaguera
# Adapted from: https://gist.github.com/ajsander/b65061d12f50de3cef5d#file-fcn_tutorial-ipynb
#
# About the license: see the file LICENSE.TXT
#########################################################################################
import dicom, re, sys
import os, fnmatch, shutil, subprocess
import numpy as np
import matplotlib.pyplot as plt
import cv2
SAX_SERIES = {
# challenge training
# challenge training
"SC-HF-I-1": "0004",
"SC-HF-I-2": "0106",
"SC-HF-I-4": "0116",
"SC-HF-I-40": "0134",
"SC-HF-NI-3": "0379",
"SC-HF-NI-4": "0501",
"SC-HF-NI-34": "0446",
"SC-HF-NI-36": "0474",
"SC-HYP-1": "0550",
"SC-HYP-3": "0650",
"SC-HYP-38": "0734",
"SC-HYP-40": "0755",
"SC-N-2": "0898",
"SC-N-3": "0915",
"SC-N-40": "0944",
# challenge online
"SC-HF-I-9": "0241",
"SC-HF-I-10": "0024",
"SC-HF-I-11": "0043",
"SC-HF-I-12": "0062",
"SC-HF-NI-12": "0286",
"SC-HF-NI-13": "0304",
"SC-HF-NI-14": "0331",
"SC-HF-NI-15": "0359",
"SC-HYP-9": "0003",
"SC-HYP-10": "0579",
"SC-HYP-11": "0601",
"SC-HYP-12": "0629",
"SC-N-9": "1031",
"SC-N-10": "0851",
"SC-N-11": "0878",
# challenge validation
"SC-HF-I-5": "0156",
"SC-HF-I-6": "0180",
"SC-HF-I-7": "0209",
"SC-HF-I-8": "0226",
"SC-HF-NI-7": "0523",
"SC-HF-NI-11": "0270",
"SC-HF-NI-31": "0401",
"SC-HF-NI-33": "0424",
"SC-HYP-6": "0767",
"SC-HYP-7": "0007",
"SC-HYP-8": "0796",
"SC-HYP-37": "0702",
"SC-N-5": "0963",
"SC-N-6": "0984",
"SC-N-7": "1009",
}
SUNNYBROOK_ROOT_PATH = "/media/DATA/Deep DataBases/Sunnybrooks/"
TRAIN_CONTOUR_PATH = os.path.join(SUNNYBROOK_ROOT_PATH,
"Sunnybrook Cardiac MR Database ContoursPart3",
"TrainingDataContours")
TRAIN_IMG_PATH = os.path.join(SUNNYBROOK_ROOT_PATH,
"challenge_training")
OUT_FOLDER = "./data"
def shrink_case(case):
toks = case.split("-")
def shrink_if_number(x):
try:
cvt = int(x)
return str(cvt)
except ValueError:
return x
return "-".join([shrink_if_number(t) for t in toks])
class Contour(object):
def __init__(self, ctr_path):
self.ctr_path = ctr_path
match = re.search(r"/([^/]*)/contours-manual/IRCCI-expert/IM-0001-(\d{4})-icontour-manual.txt", ctr_path)
self.case = shrink_case(match.group(1))
self.img_no = int(match.group(2))
def __str__(self):
return "<Contour for case %s, image %d>" % (self.case, self.img_no)
__repr__ = __str__
def load_contour(contour, img_path):
filename = "IM-%s-%04d.dcm" % (SAX_SERIES[contour.case], contour.img_no)
full_path = os.path.join(img_path, contour.case, filename)
f = dicom.read_file(full_path)
img = f.pixel_array.astype(np.int)
ctrs = np.loadtxt(contour.ctr_path, delimiter=" ").astype(np.int)
label = np.zeros_like(img, dtype="uint8")
cv2.fillPoly(label, [ctrs], 1)
return img, label
def get_all_contours(contour_path):
contours = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(contour_path)
for f in fnmatch.filter(files, 'IM-0001-*-icontour-manual.txt')]
print("Shuffle data")
np.random.shuffle(contours)
print("Number of examples: {:d}".format(len(contours)))
extracted = map(Contour, contours)
return extracted
def export_all_contours(contours, img_path):
counter_img = 0
counter_label = 0
batchsz = 100
print("Processing {:d} images and labels...".format(len(contours)))
for i in xrange(int(np.ceil(len(contours) / float(batchsz)))):
batch = contours[(batchsz * i):(batchsz * (i + 1))]
if len(batch) == 0:
break
imgs, labels = [], []
for idx, ctr in enumerate(batch):
try:
img, label = load_contour(ctr, img_path)
imgs.append(img)
labels.append(label)
# if idx % 20 == 0:
# print ctr
# plt.imshow(img)
# plt.show()
# plt.imshow(label*255)
# plt.show()
except IOError:
continue
for img in imgs:
cv2.imwrite(OUT_FOLDER + "/mri/" + counter_img.__str__() + ".png", img)
counter_img += 1
print("Processed {:d} images".format(counter_img))
for lbl in labels:
tmp = lbl*255
cv2.imwrite(OUT_FOLDER + "/labels/" + counter_label.__str__()+".png", tmp)
counter_label += 1
print("Processed {:d} labels".format(counter_label))
if __name__ == "__main__":
SPLIT_RATIO = 0.1
print("Mapping ground truth contours to images...")
ctrs = get_all_contours(TRAIN_CONTOUR_PATH)
val_ctrs = ctrs[0:int(SPLIT_RATIO * len(ctrs))]
train_ctrs = ctrs[int(SPLIT_RATIO * len(ctrs)):]
print("Done mapping ground truth contours to images")
print("\nBuilding data for train...")
export_all_contours(ctrs, TRAIN_IMG_PATH)
#print("\nBuilding data for val...")
#export_all_contours(val_ctrs, TRAIN_IMG_PATH)