-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume_stats.py
61 lines (46 loc) · 1.8 KB
/
volume_stats.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
"""
Contains various functions for computing statistics over 3D volumes
"""
import numpy as np
def Dice3d(a, b):
"""
This will compute the Dice Similarity coefficient for two 3-dimensional volumes
Volumes are expected to be of the same size. We are expecting binary masks -
0's are treated as background and anything else is counted as data
Arguments:
a {Numpy array} -- 3D array with first volume
b {Numpy array} -- 3D array with second volume
Returns:
float
"""
if len(a.shape) != 3 or len(b.shape) != 3:
raise Exception(f"Expecting 3 dimensional inputs, got {a.shape} and {b.shape}")
if a.shape != b.shape:
raise Exception(f"Expecting inputs of the same shape, got {a.shape} and {b.shape}")
a1 = a>0
b1 = b>0
intersection = np.sum(a1*b1)
volumes = np.sum(a1) + np.sum(b1)
if volumes == 0:
return -1
return 2.*float(intersection)/ float(volumes)
def Jaccard3d(a, b):
"""
This will compute the Jaccard Similarity coefficient for two 3-dimensional volumes
Volumes are expected to be of the same size. We are expecting binary masks -
0's are treated as background and anything else is counted as data
Arguments:
a {Numpy array} -- 3D array with first volume
b {Numpy array} -- 3D array with second volume
Returns:
float
"""
if len(a.shape) != 3 or len(b.shape) != 3:
raise Exception(f"Expecting 3 dimensional inputs, got {a.shape} and {b.shape}")
if a.shape != b.shape:
raise Exception(f"Expecting inputs of the same shape, got {a.shape} and {b.shape}")
a1 = a>0
b1 = b>0
intersection = abs(np.sum(a1*b1))
volumes = abs(np.sum(a1)) + abs(np.sum(b1)) - intersection
return float(intersection)/float(volumes)