-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCRFUtils.py
executable file
·506 lines (368 loc) · 16.1 KB
/
CRFUtils.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 17 11:43:20 2016
@author: rsk
"""
import cPickle
import gzip
import os
import sys
import time
import numpy as np
import itertools
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import nibabel as nib
def plotImg(data):
plt.imshow(data,cmap=cm.Greys)
def reshape(data):
return np.reshape(data,(28,28))
def sp_noise(image,prob):
output = np.zeros(image.shape)
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = np.random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 1
else:
output[i][j] = image[i][j]
return output
def viewImg(img):
plt.imshow( np.reshape(img,(28,28)) ,cmap=cm.Greys )
def checkPred(index,train=0):
if train==0:
plt.subplot(121)
plt.imshow( np.reshape(trainLabels[index],(28,28)) , cmap=cm.Greys )
plt.subplot(122)
plt.imshow( np.reshape(predTrain[index],(28,28)) , cmap=cm.Greys )
else:
plt.subplot(121)
plt.imshow( np.reshape(testLabels[index],(28,28)) , cmap=cm.Greys )
plt.subplot(122)
plt.imshow( np.reshape(predTest[index],(28,28)) , cmap=cm.Greys )
def edges(shape=(28,28),dist=4,diag=0):
length = shape[0]*shape[1]
mat = np.reshape( list(range(length)), shape )
mat = np.array(mat)
edgeList=[]
if diag==1:
for i in range(shape[0]):
for j in range(shape[1]):
for x in range(i,i+dist+1):
for y in range(j-dist,j+dist+1):
if x==i and y==j: #Avoid edge to self
continue
if x<shape[0] and y>=0 and y<shape[1]: #Avoid going out of the matri
edgeList.append(np.sort([mat[i,j] , mat[x,y] ]))
else:
for i in range(shape[0]):
for j in range(shape[1]):
x=i
for y in range(j-dist,j+dist+1):
if x<shape[0] and y>=0 and y<shape[1] and y!=j: #Avoid going out of the matri
edgeList.append(np.sort([mat[i,j] , mat[x,y] ]))
for x in range(i+1,i+dist+1):
y=j
if x<shape[0] and y>=0 and y<shape[1]: #Avoid going out of the matri
edgeList.append(np.sort([mat[i,j] , mat[x,y] ]))
edgeList =np.array( sorted( np.vstack({tuple(row) for row in edgeList}), key=lambda x : x[0] ) )
return np.array(edgeList)
def gaussianNoise(shape):
return np.random.normal(loc=0.5,scale=1,size=shape)
def error(y, truth):
corr=0.0
total=0.0
for i in range(len(y)):
corr += np.sum(y[i]==truth[i])
total += len(y[i])
return (corr/total)*100
def confusion(y,truth):
a = zip(y,truth)
tp = np.sum(np.array([int(i==(1,1)) for i in a]))
tn = np.sum(np.array([int(i==(0,0)) for i in a]))
fp = np.sum(np.array([int(i==(1,0)) for i in a]))
fn = np.sum(np.array([int(i==(0,1)) for i in a]))
print "tp : "+str(tp)+" fp : "+str(fp)
print "tp : "+str(fn)+" tn : "+str(tn)
return float((tp+tn))/float(len(y))*100.0
def addEdges(shape,diag=0,dist=1):
edges=[]
#diag_edges=[]
for distance in range(1,dist+1):
##Adding horizontal edges
for i in range( shape[0]):
for j in range(shape[1]-distance):
edges.append([i*shape[0] + j, i*shape[0] +j +distance ])
## Adding vertical edges
for i in range( shape[0]-distance):
for j in range(shape[1]):
edges.append([i*shape[0] + j, (i+distance)*shape[0] + j])
if diag==1:
for distance in range(1,dist+1):
#Adding up-right edges:
for i in range(shape[0]-distance):
for j in range(shape[1] - distance):
edges.append([i*shape[0]+j , (i+distance)*shape[0] + j+distance])
# Adding up-left edges:
for i in range(shape[0]-distance):
for j in range(distance,shape[1]):
edges.append([i*shape[0]+j , (i+distance)*shape[0] + j- distance])
return np.array(edges)
def gen3Dedges(shape,diag=0,dist=1):
edges=[]
#adding horizontal edges
for z in range(shape[2]):
for y in range(shape[1]):
for x in range(shape[0]-1):
edges.append([z*shape[1]*shape[0] + y*shape[0] + x,z*shape[1]*shape[0] + y*shape[0] + x + 1])
#adding vertical edges
for z in range(shape[2]):
for y in range(shape[1]-1):
for x in range(shape[0]):
edges.append([z*shape[1]*shape[0] + y*shape[0] + x,z*shape[1]*shape[0] + (y+1)*shape[0] + x ])
#adding top edges
for z in range(shape[2]-1):
for y in range(shape[1]):
for x in range(shape[0]):
edges.append([z*shape[1]*shape[0] + y*shape[0] + x,(z+1)*shape[1]*shape[0] + y*shape[0] + x ])
return edges
#Function to add noise to the entire dataset
def addNoise(x,noise):
dtrain=[]
for i in x:
dtrain.append(np.hstack(sp_noise(reshape(i), noise)))
return dtrain
def getDigitData(digit=0):
path = os.getcwd() + "/mnist.pkl.gz"
f = gzip.open(path,'rb')
train,valid,test = cPickle.load(f)
f.close()
trainx,trainy = train
validx,validy = valid
testx,testy= test
train= []
valid=[]
test=[]
for i in range(len(trainx)):
if trainy[i]==digit:
train.append(trainx[i])
for i in range(len(validx)):
if validy[i]==digit:
valid.append(validx[i])
for i in range(len(testx)):
if testy[i]==digit:
test.append(testx[i])
return (train,valid,test)
def checkUp(x,y,shape=(28,28)):
if x>=0 and x<shape[0] and y>=0 and y<shape[1]:
return 1
else:
return 0
def getFeatures(img,shape,dist=1):
features=[]
for i in range(shape[0]):
for j in range(shape[1]):
data=[]
data.append(img[i*shape[0]+j])
#adding right
for distance in range(1,dist+1):
if checkUp(i,j+distance,shape):
#print('y1')
data.append(img[i*shape[0]+j+distance])
else:
data.append(0)
#adding left
for distance in range(1,dist+1):
if checkUp(i,j-distance,shape):
#print('y2')
data.append(img[i*shape[0]+j-distance])
else:
data.append(0)
#adding up
for distance in range(1,dist+1):
if checkUp(i-distance,j,shape):
#print('y3')
data.append(img[(i-distance)*shape[0]+j])
else:
data.append(0)
#adding bottom
for distance in range(1,dist+1):
if checkUp(i+distance,j,shape):
#print('y4')
data.append(img[(i+distance)*shape[0]+j])
else:
data.append(0)
#print data
features.append(data)
return np.array(features)
def extractSlices(folderPath,numImages,padding=(0,0,0,0)):
post=[]
truth=[]
flair=[]
t1=[]
t1c=[]
t2=[]
############## Loading the volumes #################################
counter=0
for folder in os.listdir(folderPath):
path = folderPath + "/" + folder
print path
names=os.listdir(path)
for i in range(len(names)):
if "new_n4dropout_" in names[i] and ".npy" in names[i]:
data = np.load(path+"/"+names[i])[padding[0]:(240-padding[1]) , padding[2]:(240-padding[3] ),: ]
post.append(data)
if "binary_truth_whole_tumor" in names[i]:
data = np.array(nib.load(path+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
truth.append(data)
if "Flair" in names[i] and ".nii" in names[i]:
data = np.array(nib.load(path+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
flair.append(data)
if "T2" in names[i]:
data = np.array(nib.load(path+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
t2.append(data)
if "T1c" in names[i]:
data = np.array(nib.load(path+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
t1c.append(data)
if "T1" in names[i] and not "T1c" in names[i]:
data = np.array(nib.load(path+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
t1.append(data)
counter+=1
if counter==numImages and numImages!=-1:
break
######### Extracting the slices ###############################
postLayers=[]
truthLayers=[]
flairLayers=[]
t1Layers=[]
t1cLayers=[]
t2Layers=[]
sliceShape = truth[0][:,:,0].shape
for i in range(len(post)):
for j in range(155):
a = truth[i][:,:,j].reshape((sliceShape[0]*sliceShape[1],) )
if a.sum()>0:
start=j+4
break
for j in range(154,-1,-1):
a = truth[i][:,:,j].reshape((sliceShape[0]*sliceShape[1],) )
if a.sum()>0:
end = j-4
break
if np.mean( post[i][:,:,start][:,:,0] )<0.5:
continue
for j in range(start,end):
postLayers.append( post[i][:,:,j])
truthLayers.append(np.flipud(np.rot90(truth[i][:,:,j],1)) )
flairLayers.append( np.flipud(np.rot90(flair[i][:,:,j],1)))
t1Layers.append( np.flipud(np.rot90(t1[i][:,:,j],1)))
t1cLayers.append( np.flipud(np.rot90(t1c[i][:,:,j],1)))
t2Layers.append( np.flipud(np.rot90(t2[i][:,:,j],1)))
####### Cleaning truth and post slices ###################
print "Making max prob predictions"
postLayersPred=[]
for i in range( len(truthLayers) ):
postLayersPred.append(np.zeros(sliceShape))
for j in range(sliceShape[0]):
for k in range( sliceShape[1]):
if postLayers[i][j,k][0]>np.max(postLayers[i][j,k][1:5]):
postLayersPred[i][j,k]=0.0
else:
postLayersPred[i][j,k]=1.0
print "cleaning post data and appending image data"
for i in range( len(truthLayers) ):
for j in range(sliceShape[0]):
for k in range( sliceShape[1]):
if np.array_equal(postLayers[i][j,k], np.array([0.0,0.0,0.0,0.0,0.0])):
postLayers[i][j,k]=np.array([1.0,0.0,0.0,0.0,0.0])
postLayers[i] = np.dstack((postLayers[i],flairLayers[i],t1Layers[i],t1cLayers[i],t2Layers[i],postLayersPred[i]))
for i in range( len(truthLayers)):
for j in range(sliceShape[0]):
for k in range(sliceShape[1]):
if truthLayers[i][j,k]>0.0:
truthLayers[i][j,k]=1.0
print "truthLayers length is %f" %(len(truthLayers))
# Layers=[]
# print "appending image layers"
# for index in range(len(truthLayers)):
# output=[]
#
# for i in range(sliceShape[0]):
# output.append([])
# for j in range(sliceShape[1]):
# output[i].append([ flairLayers[index][i,j] , t1Layers[index][i,j] , t1cLayers[index][i,j] , t2Layers[index][i,j] ])
#
# Layers.append(output)
# print "Layers length : %f " % (len(Layers))
return [np.array(postLayers),truthLayers,sliceShape]
def extractTestSlices(folderPath,padding=(0,0,0,0)):
names=os.listdir(folderPath)
for i in range(len(names)):
if "new_n4dropout_" in names[i] and ".npy" in names[i]:
post = np.load(folderPath+"/"+names[i])[padding[0]:(240-padding[1]) , padding[2]:(240-padding[3] ),: ]
if "binary_truth_whole_tumor" in names[i]:
truth = np.array(nib.load(folderPath+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
if "Flair" in names[i] and ".nii" in names[i]:
flair = np.array(nib.load(folderPath+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
if "T2" in names[i]:
t2 = np.array(nib.load(folderPath+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
if "T1c" in names[i]:
t1c = np.array(nib.load(folderPath+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
if "T1" in names[i] and not "T1c" in names[i]:
t1 = np.array(nib.load(folderPath+"/"+ names[i]).get_data())[padding[0]:(240-padding[1]), padding[2]:(240-padding[3]),:]
postLayers=[]
truthLayers=[]
flairLayers=[]
t1Layers=[]
t1cLayers=[]
t2Layers=[]
sliceShape = truth[:,:,0].shape
for j in range(155):
a = truth[:,:,j].reshape((sliceShape[0]*sliceShape[1],) )
if a.sum()>0:
start=j+4
break
for j in range(154,-1,-1):
a = truth[:,:,j].reshape((sliceShape[0]*sliceShape[1],) )
if a.sum()>0:
end = j-4
break
if np.mean( post[:,:,start][:,:,0] )<0.5:
return 0
for j in range(start,end):
postLayers.append( post[:,:,j])
truthLayers.append(np.flipud(np.rot90(truth[:,:,j],1)) )
flairLayers.append( np.flipud(np.rot90(flair[:,:,j],1)))
t1Layers.append( np.flipud(np.rot90(t1[:,:,j],1)))
t1cLayers.append( np.flipud(np.rot90(t1c[:,:,j],1)))
t2Layers.append( np.flipud(np.rot90(t2[:,:,j],1)))
########### Making max prob predictions from posterior prob data #############
postLayersPred=[]
for i in range( len(truthLayers) ):
postLayersPred.append(np.zeros(sliceShape))
for j in range(sliceShape[0]):
for k in range( sliceShape[1]):
if postLayers[i][j,k][0]>np.max(postLayers[i][j,k][1:5]):
postLayersPred[i][j,k]=0.0
else:
postLayersPred[i][j,k]=1.0
######## Cleaning post data and appending image data##############
for i in range( len(truthLayers) ):
for j in range(sliceShape[0]):
for k in range( sliceShape[1]):
if np.array_equal(postLayers[i][j,k], np.array([0.0,0.0,0.0,0.0,0.0])):
postLayers[i][j,k]=np.array([1.0,0.0,0.0,0.0,0.0])
postLayers[i] = np.dstack((postLayers[i],flairLayers[i],t1Layers[i],t1cLayers[i],t2Layers[i],postLayersPred[i]))
########### Cleaning truth data #######################
for i in range( len(truthLayers)):
for j in range(sliceShape[0]):
for k in range(sliceShape[1]):
if truthLayers[i][j,k]>0.0:
truthLayers[i][j,k]=1.0
# print "truthLayers length is %f" %(len(truthLayers))
return[np.array(postLayers),truthLayers,sliceShape]
def accuracy(x,y):
return np.sum(x[y==1])*2.0/(np.sum(x) + np.sum(y))