-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
212 lines (174 loc) · 6.4 KB
/
Utils.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import numpy as np
import matplotlib.pyplot as plt
import os,glob
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from keras.utils.vis_utils import plot_model
def verbose_print(str="", verbose=True):
"""
prints only if verbose is true, useful to ignore printing based on one value
:param str: the string to be printed
:param verbose: whether to print or not
"""
if verbose:
print(str)
def plot_preprocessing_images(imgs, title):
"""
plots a 2x3 image plot
:param imgs: array of images to plot
:param title: the title of the plot
"""
columns = 2
rows = 4
# create figure
fig = plt.figure()
# turn off the box around the image
plt.box(False)
# sets the title of the plot
plt.title(title, {'fontweight':'bold','fontsize':11}, pad=10)
plt.axis("off")
plt.tight_layout()
for i in range(1, columns * rows + 1):
# adds a subplot
fig.add_subplot(rows, columns, i)
# turns off the axis and box and ensures padding
plt.axis("off")
plt.tight_layout(pad=10, w_pad=10, h_pad=1.0)
# sets the title based on where in the loop we are
if i == 1:
title = "original"
elif i == 2:
title = "threshold"
elif i == 3:
title = "flood fill"
elif i == 4:
title = "crop"
elif i == 5:
title = "tozero"
elif i == 6:
title = "morph"
elif i == 6:
title = "max_morph"
else:
title = ""
# sets the subplot title
plt.title(title, {'fontsize':8})
# plots the image onto the subplot
if i < 8:
plt.imshow(imgs[i-1], cmap=plt.cm.bone)
# shows the image
plt.show()
def plot_image(pixel_array, title):
"""
plots one image
:param pixel_array: the image to plot
:param title: the title of the image
:return:
"""
# reshapes the iamge if needed
try:
if pixel_array.shape[2] == 1:
pixel_array = np.reshape(pixel_array, (pixel_array.shape[0], pixel_array.shape[1]))
except:
None
#plots the image
plt.imshow(pixel_array, cmap=plt.cm.bone)
plt.xlabel('Height', fontsize=18)
plt.ylabel('Width', fontsize=16)
# changes the title to strings based the integers
if title == 0:
title = "No Nodule Present"
elif title == 1:
title = "Large Nodule"
elif title == 2:
title = "Small Nodule"
elif title == 3:
title = "Non-Nodule"
plt.title(title)
plt.show()
save_iterator = 0
def save_plot(pixel_array, title):
"""
Does the same as show_file except it saves the plot to file instead of showing it
:param pixel_array: the image
:param title: the title
"""
global save_iterator
plt.imshow(pixel_array, cmap=plt.cm.bone)
if title == 0:
title = "No Nodule Present"
elif title == 1:
title = "Large Nodule"
elif title == 2:
title = "Small Nodule"
elif title == 3:
title = "Non-Nodule"
plt.title(title)
plt.savefig('output/{}.png'.format(save_iterator))
save_iterator += 1
def get_files_in_folder(path, files=None):
"""
searches for files in sub directories of input folder
:param path: folder to search for folders in
:param files: the array to append the files to
:return:
"""
# fixes a problem of files not being reset as files=[] is a mutable,
# aka default, action and is ignored if files contains something
if files == None:
files = []
for i in glob.glob(os.path.join(path + "*")):
if os.path.isfile(i):
files.append(path + os.path.basename(i))
elif os.path.isdir(i):
dirname = os.path.basename(i)
get_files_in_folder(path + dirname + "/", files)
return(files)
def normalize(min, max, x):
"""
normallizes the x basedd on min and max
:param min: the minimum observed value
:param max: the maximum observed value
:param x: the value to normalize
:return: the normalized value
"""
return (x - min) / (max - min)
def one_hot_encode(data):
"""
one-hot encodes the input data
:param data: the data to one-hot encode
:return: a encoded version of the input array
"""
# create a label encoder
label_encoder = LabelEncoder()
onehot_encoder = OneHotEncoder(categories='auto', sparse=False)
# fit the label encoder to known data values
unique_values = [0, 11, 12, 13, 14, 15, 20, 30]
label_encoder = label_encoder.fit(unique_values)
# encode dataset
integer_encoded = label_encoder.transform(data)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
# fit one hot encoder to known labels
unique_values = np.asarray([[0], [1], [2], [3], [4], [5], [6], [7]])
onehot_encoder = onehot_encoder.fit(unique_values)
# encode the label encoded dataset as onehot encoeded
onehot_encoded = onehot_encoder.transform(integer_encoded)
return onehot_encoded
def save_parameters(pre_process_type, scalar, _size, batch_size, path, output, epochs, input_size, represent, model, zr, history):
'''
Saves the parameters zr model, keras model and history to a file
'''
if not os.path.exists('{}'.format(path)):
os.makedirs('{}'.format(path))
if os.path.exists("{}/parameters.py".format(path)):
os.remove("{}/parameters.py".format(path))
os.system("echo pre_process_type = \\'{}\\' >> {}parameters.py".format(pre_process_type, path))
os.system("echo scalar = \'{}\' >> '{}'parameters.py".format(scalar, path))
os.system("echo _size = \'{}\' >> '{}'parameters.py".format(_size, path))
os.system("echo batch_size = {} >> '{}'parameters.py".format(batch_size, path))
os.system('echo output = {} >> "{}"parameters.py'.format(output, path))
os.system('echo epochs = {} >> "{}"parameters.py'.format(epochs, path))
os.system('echo input_size = {} >> "{}"parameters.py'.format(input_size, path))
os.system('echo represent = {} >> "{}"parameters.py'.format(represent, path))
model.save("{}m.hdf5".format(path))
np.save('{}z.npy'.format(path), [zr])
np.save('{}history.npy'.format(path), [history])