[9f71e2]: / Section 3 Simulate DIMSE / src / utils / volume_stats.py

Download this file

74 lines (60 with data), 2.3 kB

 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
"""
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}")
# TASK: Write implementation of Dice3D. If you completed exercises in the lessons
# you should already have it.
a = a > 0
b = b > 0
a[a>0] = 1
b[b>0] = 1
#a, b = (a > 0).astype(int), (b > 0).astype(int)
intersection = np.sum(a*b)
volumes = np.sum(a) + np.sum(b)
if volumes == 0:
return -1
else:
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}")
# TASK: Write implementation of Jaccard similarity coefficient. Please do not use
# the Dice3D function from above to do the computation ;)
a = a > 0
b = b > 0
a[a>0] = 1
b[b>0] = 1
#a, b = (a > 0).astype(int), (b > 0).astype(int)
intersection = np.sum(a*b)
volumes = np.sum(a) + np.sum(b)
if volumes == 0:
return -1
else:
return float(intersection)/float((volumes - intersection))