-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluateTotalSegmentator.py
94 lines (69 loc) · 2.7 KB
/
evaluateTotalSegmentator.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
import os
import numpy as np
import nibabel as nib
import pickle as pkl
from display import plot3Dmesh
import matplotlib.pyplot as plt
SYSTEM = 'local'
if SYSTEM == 'local':
root_dir = '/Users/katecevora/Documents/PhD'
os.chdir(root_dir)
data_dir = 'data/MSDPancreas/ImagesTr'
labels_dir = 'data/MSDPancreas/LabelsTr'
preds_dir = 'data/MSDPancreas/MSDPancreas/TotalSegmentator'
else:
root_dir = '/vol/biomedic3/kc2322/'
data_dir = os.path.join(root_dir, 'data/MSDPancreas/MSDPancreas/imagesTr')
labels_dir = os.path.join(root_dir, 'data/MSDPancreas/MSDPancreas/labelsTr')
preds_dir = os.path.join(root_dir, 'data/MSDPancreas/MSDPancreas/TotalSegmentator')
images_dir = os.path.join(root_dir, 'images/TotalSegmentator/3D')
results_dir = os.path.join(root_dir, 'results/TotalSegmentator')
def plotResults():
f = open(os.path.join(results_dir, "dice_scores.pkl"), "rb")
scores_dict = pkl.load(f)
f.close()
# convert scores into numpy array
scores = np.array(list(scores_dict.values()))
y = np.zeros(scores.shape)
y.fill(1)
plt.scatter(scores, y, marker='o', facecolors='none', edgecolors="#ffa07a", s=100)
plt.axvline(np.mean(scores), label="Mean: {0:.2f}, Std.: {1:.2f}".format(np.mean(scores), np.std(scores)))
#plt.scatter(np.mean(res), j + 1, marker='x', color='k', s=80)
plt.xlabel("Dice Score")
plt.yticks([1], ["TotalSegmentator"])
plt.ylim([0, 2])
plt.legend()
plt.tight_layout()
plt.show()
def getDiceScores():
# list the files in the training folder
files_list = os.listdir(data_dir)
scores = {}
for f in files_list:
if f.endswith(".gz"):
name = f.split(".")[0]
img = nib.load(os.path.join(data_dir, f))
gt = nib.load(os.path.join(labels_dir, f))
pred = nib.load(os.path.join(preds_dir, name, "pancreas.nii.gz"))
header = img.header
vox_size = header.get_zooms()
img_raw = img.get_fdata()
lab_raw = pred.get_fdata()
gt_raw = gt.get_fdata()
# Calculate Dice score
gt_raw[gt_raw == 2] = 0
dice = np.sum(lab_raw[gt_raw == 1]) * 2.0 / (np.sum(lab_raw) + np.sum(gt_raw))
# Add Dice score to a dictionary
scores[name] = dice
# Plot
#plot3Dmesh(gt_raw, lab_raw, dice, save_path=os.path.join(images_dir, name + ".png"))
f = open(os.path.join(results_dir, "dice_scores.pkl"), "wb")
pkl.dump(scores, f)
f.close()
return scores
def main():
#scores = getDiceScores()
#print("Average Dice score = {0:.2f}".format(sum(scores.values()) / len(scores)))
plotResults()
if __name__ == "__main__":
main()