forked from silverlight6/Ultrasound_Modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFilePlayGround_2.py
More file actions
348 lines (287 loc) · 13.5 KB
/
DataFilePlayGround_2.py
File metadata and controls
348 lines (287 loc) · 13.5 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import numpy as np
import os # reading files
import cv2 # resize
import math
import cmath
import multiprocessing
import random
import scipy.ndimage as ndimage # rotate, zoom
from sklearn.utils import shuffle # shuffle seed
from matplotlib import pyplot as plt
from scipy.io import loadmat
def FetchTimeData(datapath):
Harmonics = loadmat(datapath)
harmonic = np.array(list(Harmonics['harmonics']))
harmShape = harmonic.shape
period = 50 # number of time-ticks along the waveform,
tt = np.linspace(1, period, period)
form = np.zeros([harmShape[0], harmShape[1], period]) # allocate memory to waveform.
mag = np.sqrt(np.square(harmonic.real) + np.square(harmonic.imag))
for j in range(1, harmShape[1]):
for i in range(1, harmShape[0]):
for k in range(1, 7):
phase = cmath.phase(harmonic[i, j, k])
# the harmonics here are the magnitude and phase, not the real and imaginary parts.
form[i, j, :] = form[i, j, :] + mag[i, j, k] * np.sin(2 * k * tt * math.pi / period + phase)
normalMask = np.array(list(Harmonics['normalMask']))
bloodMask = np.array(list(Harmonics['bloodMask']))
brainMask = np.array(list(Harmonics['brainMask']))
bloodMask = np.nan_to_num(bloodMask)
normalMask = np.nan_to_num(normalMask)
label = np.where(bloodMask > normalMask, 2, 1)
label = np.where(brainMask == 0, 0, label)
real = harmonic.real
imag = harmonic.imag
M1 = np.sqrt(np.square(real[:, :, 0]) + np.square(imag[:, :, 0]))
MO = np.sqrt(np.square(real[:, :, 0]) + np.square(imag[:, :, 0]))
for i in range(1, 7):
MO += np.sqrt(np.square(real[:, :, i]) + np.square(imag[:, :, i]))
M1 = M1 / MO
tOutput = np.zeros([form.shape[0], form.shape[1], 3])
tOutput[:, :, 0] = form[:, :, 0]
tOutput[:, :, 1] = form[:, :, 17]
tOutput[:, :, 2] = M1
tOutput = tOutput - tOutput.mean(axis=0).mean(axis=0)
safe_max = np.abs(tOutput).max(axis=0).max(axis=0)
safe_max[safe_max == 0] = 1
tOutput = tOutput / safe_max
for i in range(0, 3):
tOutput[:, :, i] = np.where(brainMask == 0, 0, tOutput[:, :, i])
tOutput = cv2.resize(tOutput, (64, 256), interpolation=cv2.INTER_CUBIC)
label = label.astype('float32')
label = cv2.resize(label, (64, 256), interpolation=cv2.INTER_CUBIC)
label = label.reshape(256, 64, 1)
tOutput = tOutput.reshape(256, 64, 3)
combinedData = np.concatenate((label, tOutput), axis=-1)
return combinedData
def FetchPolarAxis(datapath):
Harmonics = loadmat(datapath)
print(Harmonics.keys())
xaxis = np.array(list(Harmonics['xAxis']))
yaxis = np.array(list(Harmonics['zAxis']))
xaxis = cv2.resize(xaxis, (64, 256), interpolation=cv2.INTER_AREA)
yaxis = cv2.resize(yaxis, (64, 256), interpolation=cv2.INTER_AREA)
xaxis += 100
yaxis -= 4
home = os.path.expanduser("~")
savePath = "/data/TBI/Datasets/NPFiles/"
print("saved in : {}".format(savePath))
np.save(savePath + "xAxis.npy", xaxis)
np.save(savePath + "yAxis.npy", yaxis)
def noisy(image, r):
row, col, ch = image.shape
mean = 0
var = 0.001 + (r % 20 / 1000)
sigma = var**0.5
gauss = np.random.normal(mean, sigma, (row, col, ch))
gauss = gauss.reshape([row, col, ch])
noisy = image + gauss
return noisy
def dataAug(image):
r = random.randint(0, 100000)
if r % 3:
image = np.fliplr(image)
# if r % 5:
# image = ndimage.zoom(input=image, zoom=.9 + ((r % 6) / 25), output=image, mode='nearest')
if r % 7:
image = ndimage.rotate(image, ((r % 6) / 50 - .05), reshape=False)
if r % 11:
image = noisy(image, r)
return image
def imageReduc(image):
output = image
si = image.shape
# print("si = {}".format(si))
mask = np.where(image[:, :, 0] == 0, 1, 0)
mask2 = np.zeros((si[0], si[1]), dtype=np.int32)
for _ in range(0, 2): # shrinks input by 2 pixels
for i in range(1, si[0] - 1):
for j in range(1, si[1] - 1):
if mask[i, j] == 1:
mask2[i - 1, j] = 1
mask2[i, j - 1] = 1
mask2[i + 1, j] = 1
mask2[i, j + 1] = 1
mask2[i - 1, j - 1] = 1
mask2[i - 1, j + 1] = 1
mask2[i + 1, j + 1] = 1
mask2[i + 1, j - 1] = 1
mask2[i, j] = 1
mask = mask2
mask2 = np.zeros((si[0], si[1]), dtype=np.int32)
output[:, :, 0] = np.where(mask == 1, 0, output[:, :, 0])
for k in range(1, si[2]):
output[:, :, k] = np.where(output[:, :, 0] == 0, 0, output[:, :, k])
return output
def output2DImages():
# Make arrays for dividing data between training, testing, and validation.
manager = multiprocessing.Manager()
trainingData = manager.list()
testingData = manager.list()
validationData = manager.list()
trainingPaths = manager.list()
testingPaths = manager.list()
validationPaths = manager.list()
trainingLabels = manager.list()
testingLabels = manager.list()
validationLabels = manager.list()
trainingCycles = manager.list()
testingCycles = manager.list()
validationCycles = manager.list()
IPH_patients = [8, 9, 10, 47, 53, 62, 66, 67, 69, 74, 75, 78, 85, 89, 101]
# counting files
count = 0
def fileLoop(path, patient_num):
for harmonic in os.listdir(path):
if ".mat" in harmonic:
# print("Checkpoint 1")
hPath = os.path.join(path, harmonic) # put file name on path
pathName = harmonic[0:17] # get path to print
Harmonics = loadmat(hPath)
# print(Harmonics.keys())
# load each subsection needed
harmonic = np.array(list(Harmonics['harmonics']))
normalMask = np.array(list(Harmonics['normalMask']))
bloodMask = np.array(list(Harmonics['bloodMask']))
brainMask = np.array(list(Harmonics['brainMask']))
bMode = np.array(list(Harmonics['bModeNorm']))
# separate real and imaginary.
real = harmonic.real
imag = harmonic.imag
# turn nan to zero
bloodMask = np.nan_to_num(bloodMask)
normalMask = np.nan_to_num(normalMask)
bMode = np.nan_to_num(bMode)
# bMode = bMode.mean(axis=-1)
bMode = np.log2(bMode)
label = np.where(bloodMask > normalMask, 2, 1)
label = np.where(brainMask == 0, 0, label)
label = label.astype('float32')
label = cv2.resize(label, (64, 256), interpolation=cv2.INTER_CUBIC)
label = label.reshape(256, 64, 1)
cycles = real.shape[-1]
real = real.astype('float64')
imag = imag.astype('float64')
bMode = bMode.astype('float64')
iLabel_num = np.count_nonzero(label[:, :, 0] == 2) / (256 * 64)
iLabel = iLabel_num > .01
for k in range(0, cycles):
realO = real[:, :, :, k]
imagO = imag[:, :, :, k]
bModeO = bMode[:, :, 0, k]
# delete non-brain from input data
for i in range(0, 7):
realO[:, :, i] = np.where(brainMask == 0, 0, realO[:, :, i])
imagO[:, :, i] = np.where(brainMask == 0, 0, imagO[:, :, i])
# bModeO = np.where(brainMask == 0, 0, bModeO)
realO = cv2.resize(realO, (64, 256), interpolation=cv2.INTER_CUBIC)
imagO = cv2.resize(imagO, (64, 256), interpolation=cv2.INTER_CUBIC)
bModeO = cv2.resize(bModeO, (64, 256), interpolation=cv2.INTER_CUBIC)
# tOutput = cv2.resize(tOutput, (64, 256), interpolation=cv2.INTER_CUBIC)
bModeO = bModeO.reshape([256, 64, 1])
# concatenate the columns into one structure
image = np.concatenate((label, realO, imagO, bModeO), axis=2)
# print("appendData.shape = {}".format(appendData.shape))
lock = multiprocessing.Lock()
lock.acquire()
if count < 10: # 44 patients in training
trainingData.append([image])
trainingPaths.append([pathName])
trainingLabels.append([iLabel])
trainingCycles.append([cycles])
# for _ in range(0, math.floor(iLabel_num / .2)):
# trainingData.append([image])
# trainingPaths.append([pathName])
# trainingLabels.append([iLabel])
# trainingCycles.append([cycles])
# if patient_num in IPH_patients:
# for i in range(0, 4):
# lock.release()
# image = imageReduc(image)
# lock.acquire()
# trainingData.append([image])
# trainingPaths.append([pathName])
# trainingLabels.append([iLabel])
# trainingCycles.append([cycles])
# for _ in range(0, 2):
# lock.release()
# image = dataAug(image)
# lock.acquire()
# trainingData.append([image])
# trainingPaths.append([pathName])
# trainingLabels.append([iLabel])
# trainingCycles.append([cycles])
elif count < 12: # 4 patients in testing
testingData.append([image])
testingPaths.append([pathName])
testingLabels.append([iLabel])
testingCycles.append([cycles])
else: # everything else in validation
validationData.append([image])
validationPaths.append([pathName])
validationLabels.append([iLabel])
validationCycles.append([cycles])
lock.release()
# for testing purposes only
print(count)
# print("in fileloop - {}".format(trainingData))
return
# data paths; it is just what it sounds like
# watch out for polar versus non-polar
dataPaths = '/data/TBI/Datasets/CardiacData/'
# make sure that data gets looked at even
processes = []
pathlist = os.listdir(dataPaths)
pathlist = np.sort(pathlist)
pathlist = shuffle(pathlist, random_state=20)
for path in pathlist:
num = path[3:]
num = int(num)
fpath = os.path.join(dataPaths, path)
if num in IPH_patients:
p = multiprocessing.Process(target=fileLoop, args=(fpath, num))
p.start()
processes.append(p)
count += 1
for process in processes:
# print("in process loop - {}".format(trainingData))
process.join()
# convert to numpy arrays, because the data is 4D
trainingData = np.array(trainingData)
testingData = np.array(testingData)
validationData = np.array(validationData)
trainingPaths = np.array(trainingPaths)
testingPaths = np.array(testingPaths)
validationPaths = np.array(validationPaths)
trainingLabels = np.array(trainingLabels)
testingLabels = np.array(testingLabels)
validationLabels = np.array(validationLabels)
trainingCycles = np.array(trainingCycles)
testingCycles = np.array(testingCycles)
validationCycles = np.array(validationCycles)
# let us see what we got
print(testingPaths)
print("training {}".format(trainingData.shape))
print("testing {}".format(testingData.shape))
print("validation {}".format(validationData.shape))
print("Cycle count train {}".format(trainingCycles.shape))
print("Cycle count test {}".format(testingCycles.shape))
print("Cycle count validation {}".format(validationCycles.shape))
print("Positive # label {}".format(np.count_nonzero(trainingLabels)))
savePath = "/data/TBI/Datasets/NPFiles/IPH/"
print("saved in : {}".format(savePath))
np.save(savePath + "TrainingData.npy", trainingData)
np.save(savePath + "TestingData.npy", testingData)
np.save(savePath + "ValidationData.npy", validationData)
np.save(savePath + "TrainingPaths.npy", trainingPaths)
np.save(savePath + "TestingPaths.npy", testingPaths)
np.save(savePath + "ValidationPaths.npy", validationPaths)
np.save(savePath + "TrainingLabels.npy", trainingLabels)
np.save(savePath + "TestingLabels.npy", testingLabels)
np.save(savePath + "ValidationLabels.npy", validationLabels)
np.save(savePath + "TrainingCycles.npy", trainingCycles)
np.save(savePath + "TestingCycles.npy", testingCycles)
np.save(savePath + "ValidationCycles.npy", validationCycles)
output2DImages()
# FetchPolarAxis('/data/TBI/Datasets/PolarData/DoD001/DoD001_Ter001_RC1_Harmonics_Polar.mat')
# FetchTimeData('/data/TBI/Datasets/PolarData/DoD001/DoD001_Ter001_RC1_Harmonics_Polar.mat')