forked from imatge-upc/liverseg-2017-nipsws
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEvulation_script.py
77 lines (60 loc) · 2.36 KB
/
Evulation_script.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
from medpy import metric
from surface import Surface
import glob
import nibabel as nb
import numpy as np
import os
## we have to convert the png results
## all 75 pngs back into 1-segmentation.nii
###########
###########
def get_scores(pred,label,vxlspacing):
volscores = {}
volscores['dice'] = metric.dc(pred,label)
volscores['jaccard'] = metric.binary.jc(pred,label)
volscores['voe'] = 1. - volscores['jaccard']
volscores['rvd'] = metric.ravd(label,pred)
if np.count_nonzero(pred) ==0 or np.count_nonzero(label)==0:
volscores['assd'] = 0
volscores['msd'] = 0
else:
evalsurf = Surface(pred,label,physical_voxel_spacing = vxlspacing,mask_offset = [0.,0.,0.], reference_offset = [0.,0.,0.])
volscores['assd'] = evalsurf.get_average_symmetric_surface_distance()
volscores['msd'] = metric.hd(label,pred,voxelspacing=vxlspacing)
return volscores
label_path = 'D:\L_pipe\liver_open\liverseg-2017-nipsws\comparsion'
prob_path = 'D:\L_pipe\liver_open\liverseg-2017-nipsws\data_output'
labels = sorted(glob.glob(label_path+'/*.nii'))
probs = sorted(glob.glob(prob_path+'/*.nii'))
# print(labels)
# print(probs)
results = []
outpath = 'D:\L_pipe\liver_open\liverseg-2017-nipsws\data/results.csv'
# D:\L_pipe\liver_open\liverseg-2017-nipsws\data
for label, prob in zip(labels,probs):
loaded_label = nb.load(label)
loaded_prob = nb.load(prob)
print("working")
liver_scores = get_scores(loaded_prob.get_data()>=1,loaded_label.get_data()>=1,loaded_label.header.get_zooms()[:3])
lesion_scores = get_scores(loaded_prob.get_data()==2,loaded_label.get_data()==2,loaded_label.header.get_zooms()[:3])
print("Liver dice",liver_scores['dice'], "Lesion dice", lesion_scores['dice'])
results.append([label, liver_scores, lesion_scores])
#create line for csv file
outstr = str(label) + ','
for l in [liver_scores, lesion_scores]:
for k,v in l.iteritems():
outstr += str(v) + ','
outstr += '\n'
#create header for csv file if necessary
if not os.path.isfile(outpath):
headerstr = 'Volume,'
for k,v in liver_scores.iteritems():
headerstr += 'Liver_' + k + ','
for k,v in liver_scores.iteritems():
headerstr += 'Lesion_' + k + ','
headerstr += '\n'
outstr = headerstr + outstr
#write to file
f = open(outpath, 'a+')
f.write(outstr)
f.close()