-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice_Score_nii_vs_nii.py
195 lines (138 loc) · 6.14 KB
/
Dice_Score_nii_vs_nii.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 27 22:55:10 2020
@author: agapi
Script to calculate the Dice score between the real label and the predicted segmentation.
In addition, the 20 best and worst predictions respectively are plotted and saved as .png.
"""
import os
import numpy as np
from imageio import imread
import matplotlib
from matplotlib import pyplot as plt
import SimpleITK as sitk
import glob
import matplotlib.gridspec as gridspec
plt.switch_backend('agg')
from skimage.measure import find_contours
import nibabel as nib
def make_2D_plots(X, y, y_pred, n_best=20, n_worst=20):
#PLotting the results'
img_rows = X.shape[1]
img_cols = img_rows
axis = tuple( range(1, X.ndim ) )
scores = numpy_dice(y, y_pred, axis=axis )
sort_ind = np.argsort( scores )[::-1]
indice = np.nonzero( y.sum(axis=axis) )[0]
#Add some best and worst predictions
img_list = []
count = 1
for ind in sort_ind:
if ind in indice:
img_list.append(ind)
count+=1
if count>n_best:
break
segm_pred = y_pred[img_list].reshape(-1,img_rows, img_cols)
img = X[img_list].reshape(-1,img_rows, img_cols)
segm = y[img_list].reshape(-1, img_rows, img_cols).astype('float32')
n_cols= 4
n_rows = int( np.ceil(len(img)/n_cols) )
fig = plt.figure(figsize=[ 4*n_cols, int(4*n_rows)] )
gs = gridspec.GridSpec( n_rows , n_cols )
for mm in range( len(img) ):
ax = fig.add_subplot(gs[mm])
ax.imshow(img[mm], cmap='gray')
contours = find_contours(segm[mm], 0.01, fully_connected='high')
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=1, color='r')
contours = find_contours(segm_pred[mm], 0.01, fully_connected='high')
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=1, color='b')
ax.axis('image')
ax.set_xticks([])
ax.set_yticks([])
ax.set_aspect(1) # aspect ratio of 1
fig.savefig('../Morphsnakes/2D_plots/best_predictions_train.png', bbox_inches='tight', dpi=300 )
img_list = []
count = 1
for ind in sort_ind[::-1]:
if ind in indice:
img_list.append(ind)
count+=1
if count>n_worst:
break
segm_pred = y_pred[img_list].reshape(-1,img_rows, img_cols)
img = X[img_list].reshape(-1,img_rows, img_cols)
segm = y[img_list].reshape(-1, img_rows, img_cols).astype('float32')
n_cols= 4
n_rows = int( np.ceil(len(img)/n_cols) )
fig = plt.figure(figsize=[ 4*n_cols, int(4*n_rows)] )
gs = gridspec.GridSpec( n_rows , n_cols )
for mm in range( len(img) ):
ax = fig.add_subplot(gs[mm])
ax.imshow(img[mm], cmap='gray')
contours = find_contours(segm[mm], 0.01, fully_connected='high')
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=1, color='r')
contours = find_contours(segm_pred[mm], 0.01, fully_connected='high')
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=1, color='b')
ax.axis('image')
ax.set_xticks([])
ax.set_yticks([])
ax.set_aspect(1) # aspect ratio of 1
fig.savefig('../Morphsnakes/2D_plots/worst_predictions_train.png', bbox_inches='tight', dpi=300 )
def make_3D_plots(background, filename, fig = None):
from mpl_toolkits.mplot3d import Axes3D
# PyMCubes package is required for `visual_callback_3d`
try:
import mcubes
except ImportError:
raise ImportError("PyMCubes is required for 3D `visual_callback_3d`")
# Prepare the visual environment.
if fig is None:
fig = plt.figure()
fig.clf()
ax = fig.add_subplot(111, projection='3d')
if ax.collections:
del ax.collections[0]
coords, triangles = mcubes.marching_cubes(background, 0.5)
ax.plot_trisurf(coords[:, 0], coords[:, 1], coords[:, 2],
triangles=triangles)
plt.pause(0.1)
fig.savefig(os.path.join('/home/agapi/Desktop/MasterThesis/Morphsnakes', filename + ".png"))
def numpy_dice(y_true, y_pred, axis=None, smooth=1.0):
intersection = y_true*y_pred
return ( 2. * intersection.sum(axis=axis) +smooth)/ (np.sum(y_true, axis=axis) + np.sum(y_pred, axis=axis) +smooth )
def check_predictions(data, true_label, prediction, plot):
if not os.path.isdir('../Morphsnakes/2D_plots'):
os.mkdir('../Morphsnakes/2D_plots')
print('Accuracy:', numpy_dice(true_label, prediction))
#Create 2D plots with best and worst predictions
make_2D_plots(data, true_label, prediction)
#Create the 3D plot of the real label and the prediction respectively.
# for i in range(3):
# true_label = np.transpose(true_label, (1, 2, 0))
# for angle in range(4):
# true_label = np.rot90(true_label)
# make_3D_plots(true_label, "true_label_3D_plot" + str(i) + str(angle))
# for i in range(3):
# prediction = np.transpose(prediction, (1, 2, 0))
# for angle in range(4):
# prediction = np.rot90(prediction)
# make_3D_plots(prediction, "predicted_label_3D_plot" + str(i) + str(angle))
# make_3D_plots(np.rot90(prediction, k=1), "predicted_label_3D_plot")
def read_cases(the_list=None, folder='../data/train/', masks=True):
filenames = glob.glob(folder+'/*0082*.nii.gz', recursive=True) #Add pancreas to exclude label files.
for filename in filenames:
itkimage = sitk.ReadImage(filename)
itkimage = sitk.Flip(itkimage, [False, True, False]) #Flip images, because they are shown reversed.
img = sitk.GetArrayFromImage(itkimage)
return img
if __name__=='__main__':
x_data = read_cases(folder = '/home/agapi/Desktop/MasterThesis/Datasets/NIH_nii')
y_true = read_cases(folder = '/home/agapi/Desktop/MasterThesis/Datasets/NIH_labels_edited')
y_pred = read_cases(folder = '/home/agapi/Desktop/MasterThesis/Morphsnakes/nii_test')
check_predictions(x_data, y_true, y_pred, True)