-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.py
91 lines (76 loc) · 2.16 KB
/
metrics.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
import numpy as np
import SimpleITK as sitk
def hausdorff_distance(in1, in2, label="all"):
"""
Compute hausdorff distance
Parameters
----------
in1 : sitkImage
in2 : sitkImage
label : str, optional
label to be considered foreground, by default "all"
Returns
-------
float
hausdorff distance value
"""
hausdorff_distance_filter = sitk.HausdorffDistanceImageFilter()
if label == "all":
hausdorff_distance_filter.Execute(in1, in2)
else:
in1_array = sitk.GetArrayFromImage(in1)
in1_array = (in1_array == label) * 1
in1_array = in1_array.astype("uint16")
img1 = sitk.GetImageFromArray(in1_array)
in2_array = sitk.GetArrayFromImage(in2)
in2_array = (in2_array == label) * 1
in2_array = in2_array.astype("uint16")
img2 = sitk.GetImageFromArray(in2_array)
hausdorff_distance_filter.Execute(img1, img2)
return hausdorff_distance_filter.GetHausdorffDistance()
def volumetric_difference(in1, in2, label="all"):
"""
Compute volumetric difference
Parameters
----------
in1 : ndarray
in2 : ndarray
label : str, optional
label to be considered foreground, by default "all"
Returns
-------
float
volumetric distance
"""
if label == "all":
return np.sum((in1 != in2)) / ((np.sum(in1 > 0) + np.sum(in2 > 0)))
else:
in1 = (in1 == label) * 1
in2 = (in2 == label) * 1
return np.sum((in1 != in2)) / ((np.sum(in1 > 0) + np.sum(in2 > 0)))
def dice_score(in1, in2, label="all"):
"""
Compute dice score
Parameters
----------
in1 : ndarray
in2 : ndarray
label : str, optional
label to be considered foreground, by default "all"
Returns
-------
float
dice score
"""
if label == "all":
return (
2
* np.sum((in1 > 0) & (in2 > 0) & (in1 == in2))
/ (np.sum(in1 > 0) + np.sum(in2 > 0))
)
else:
return (
2
* np.sum((in1 == label) & (in2 == label))
/ (np.sum(in1 == label) + np.sum(in2 == label))
)