-
Notifications
You must be signed in to change notification settings - Fork 0
/
slicescore.py
executable file
·138 lines (122 loc) · 3.55 KB
/
slicescore.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import nrrd
import sys
import warnings
from numpy import (
int,
round,
linspace,
newaxis,
shape,
array,
uint32,
uint8,
max,
sqrt,
abs,
mean,
dtype,
int32,
add,
divide,
subtract,
sum,
square,
multiply,
asarray,
squeeze,
float128,
average,
ones,
fmin,
)
def zsampleslice(data):
"""Returns four sample Z slices through a 3d image array."""
data = array(data, ndmin=3)
l = shape(data)[2]
a = data[:, :, 0]
s = int(l / 3)
b = data[:, :, s]
c = data[:, :, -s]
d = data[:, :, l - 1]
return array([a, b, c, d])
def ysampleslice(data):
"""Returns four sample Z slices through a 3d image array."""
data = array(data, ndmin=3)
l = shape(data)[1]
a = data[:, 0, :]
s = int(l / 4)
b = data[:, s, :]
c = data[:, -s, :]
d = data[:, l - 1, :]
return array([a, b, c, d])
def xsampleslice(data):
"""Returns four sample Z slices through a 3d image array."""
data = array(data, ndmin=3)
l = shape(data)[0]
a = data[0, :, :]
s = int(l / 4)
b = data[s, :, :]
c = data[-s, :, :]
d = data[l - 1, :, :]
return array([a, b, c, d])
def RMSdiff(data1, data2):
"""Returns the RMS difference between two images."""
return sqrt(mean(abs(data1 - (data2 + 0.0)) ** 2.0))
def OverlapCoeff(data1, data2):
"""Returns the Overlap Coefficent between two images."""
nd1 = squeeze(asarray(data1, dtype=float128))
nd2 = squeeze(asarray(data2, dtype=float128))
r = sum(multiply(nd1, nd2)) / sqrt(multiply(sum(square(nd1)), sum(square(nd2))))
print(r)
return r
def minOverlapCoeff(data1, data2):
"""Returns the min Overlap Coefficent between image slices."""
r = []
print(shape(data1))
for i in range(0, min(shape(data1))):
nd1 = squeeze(asarray(data1[i], dtype=float128))
if sum(nd1) < 1:
nd1[0, 0] = 1.0
print(shape(nd1))
print(sum(nd1))
nd2 = squeeze(asarray(data2[i], dtype=float128))
if sum(nd2) < 1:
nd2[0, 0] = 1.0
print(shape(nd2))
print(sum(nd2))
if (sum(nd1) + sum(nd2)) > 0:
r.append(sum(multiply(nd1, nd2)) / sqrt(multiply(sum(square(nd1)), sum(square(nd2)))))
else:
print('Note: both equal only as blank')
r.append(1.0)
print(r)
return fmin(r)
def avgOverlapCoeff(data1, data2):
"""Returns the min Overlap Coefficent between image slices."""
r = []
weights = ones(min(shape(data1)), dtype=float)
weights[0] = 0.1
weights[-1] = 0.1
for i in range(0, min(shape(data1))):
nd1 = squeeze(asarray(data1[i], dtype=float128))
if sum(nd1) < 1:
nd1[0, 0] = 1.0
nd2 = squeeze(asarray(data2[i], dtype=float128))
if sum(nd2) < 1:
nd2[0, 0] = 1.0
if (sum(nd1) + sum(nd2)) > 0:
r.append(sum(multiply(nd1, nd2)) / sqrt(multiply(sum(square(nd1)), sum(square(nd2)))))
else:
print('Note: both equal only as blank')
r.append(1.0)
print(r)
print(weights)
print(average(r, weights=weights))
return average(r, weights=weights)
def symTest(function, data):
"""Applies the given function to the diagonal slices output from xslice. Can be used to assess the symmetry of a 3D image using a comparison function such as OverlapCoeff."""
if data.ndim < 3:
warnings.warn("must be used with data output from xslice", SyntaxWarning)
return False
else:
return function(data[0], data[1])