-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.py
265 lines (203 loc) · 8.01 KB
/
util.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
from scipy import signal
import scipy.io as sio
import os
import odl
def load_data():
"""Get the data from disk.
Returns
-------
mat1_sino, mat2_sino : numpy.ndarray
projection of material 1 and 2
geometry : odl.tomo.Geometry
Geometry of the data
"""
current_path = os.path.dirname(os.path.realpath(__file__))
data_path = os.path.join(current_path,
'data',
'aux_corr_in_real_ct_image.mat')
try:
data_mat = sio.loadmat(data_path)
except IOError:
raise IOError('data/aux_corr_in_real_ct_image.mat missing, contact '
'developers for a copy of the data or use another data '
'source.')
data = data_mat['decomposedBasisProjectionsmmObj']
data = data.swapaxes(0, 2)
angle_partition = odl.uniform_partition(0, np.pi, 180)
detector_partition = odl.uniform_partition(-150 * np.sqrt(2),
150 * np.sqrt(2),
853)
geometry = odl.tomo.Parallel2dGeometry(angle_partition, detector_partition)
return data, geometry
def fan_to_fan_flat(geometry, data):
tmp_space = odl.uniform_discr_frompartition(geometry.partition,
interp='linear')
rot_angles = tmp_space.meshgrid[0]
fan_angles = tmp_space.meshgrid[1]
data = tmp_space.element(data)
source_to_detector = geometry.src_radius + geometry.det_radius
fan_dist = source_to_detector * np.arctan(fan_angles / source_to_detector)
data = data.interpolation((rot_angles, fan_dist),
bounds_check=False)
data = data[::-1]
return data
def swap_axes(array):
array = np.swapaxes(array, 0, -2)
array = np.swapaxes(array, 1, -1)
return array
def load_fan_data(return_crlb=False, data_name='supersampled'):
if data_name == 'fan':
file_name = 'runs_2017_01_07_lineardet'
elif data_name == 'supersampled':
file_name = '10_Jan_2017_17_51_44_simulation_forbild_head'
elif data_name == 'fan_circ':
file_name = 'simulated_images_2017_01_06'
else:
assert False
current_path = os.path.dirname(os.path.realpath(__file__))
data_path = os.path.join(current_path,
'data', file_name,
'head_image.mat')
try:
data_mat = sio.loadmat(data_path)
except IOError:
raise IOError('{} missing, '
'contact '
'developers for a copy of the data or use another data '
'source.'.format(data_path))
# print(sorted(data_mat.keys()))
data = data_mat['decomposedbasisProjectionsmm']
data = data.swapaxes(0, 2)
if data_name == 'fan':
det_size = 853
angle_partition = odl.uniform_partition(0.5 * np.pi, 2.5 * np.pi, 360,
nodes_on_bdry=[[True, False]])
detector_partition = odl.uniform_partition(-det_size / 2.0,
det_size / 2.0,
853)
geometry = odl.tomo.FanFlatGeometry(angle_partition, detector_partition,
src_radius=500,
det_radius=500)
data[:] = data[:, ::-1]
if not return_crlb:
return data, geometry
else:
crlb = data_mat['CRLB']
crlb = crlb.swapaxes(0, 1)
crlb[:] = crlb[::-1]
crlb = swap_axes(crlb)
# Negative correlation
#crlb[0, 1] *= -1
#crlb[1, 0] *= -1
return data, geometry, crlb
if data_name == 'supersampled':
det_size = 853
angle_partition = odl.uniform_partition(0 * np.pi, 2 * np.pi, 360)
detector_partition = odl.uniform_partition(-det_size / 2.0,
det_size / 2.0,
853)
geometry = odl.tomo.FanFlatGeometry(angle_partition, detector_partition,
src_radius=500,
det_radius=500)
#data[:] = data[:, ::-1]
if not return_crlb:
return data, geometry
else:
crlb = data_mat['CRLB']
crlb = crlb.swapaxes(0, 1)
#crlb[:] = crlb[::-1]
crlb = swap_axes(crlb)
# Negative correlation
#crlb[0, 1] *= -1
#crlb[1, 0] *= -1
return data, geometry, crlb
elif data_name == 'fan_circ':
# Create approximate fan flat geometry.
det_size = 883 * (500 + 500)
angle_partition = odl.uniform_partition(0.5 * np.pi, 2.5 * np.pi, 360,
nodes_on_bdry=[[True, False]])
detector_partition = odl.uniform_partition(-det_size / 2.0,
det_size / 2.0,
883)
geometry = odl.tomo.FanFlatGeometry(angle_partition, detector_partition,
src_radius=500,
det_radius=500)
# Convert to true fan flat geometry
data[0][:] = fan_to_fan_flat(geometry, data[0])
data[1][:] = fan_to_fan_flat(geometry, data[1])
if not return_crlb:
return data, geometry
else:
crlb = data_mat['CRLB']
crlb = crlb.swapaxes(0, 1)
crlb = np.moveaxis(crlb, [-2, -1], [0, 1])
crlb[0, 0][:] = fan_to_fan_flat(geometry, crlb[0, 0])
crlb[0, 1][:] = fan_to_fan_flat(geometry, crlb[0, 1])
crlb[1, 0][:] = fan_to_fan_flat(geometry, crlb[1, 0])
crlb[1, 1][:] = fan_to_fan_flat(geometry, crlb[1, 1])
# Negative correlation
# crlb[0, 1] *= -1
# crlb[1, 0] *= -1
return data, geometry, crlb
def estimate_cov(I1, I2):
"""Estiamte the covariance of I1 and I2."""
assert I1.shape == I2.shape
H, W = I1.shape
M = np.array([[1, -2, 1],
[-2, 4., -2],
[1, -2, 1]])
sigma = np.sum(signal.convolve2d(I1, M) * signal.convolve2d(I2, M))
sigma /= (W * H - 1)
return sigma / 36.0 # unknown factor, too lazy to solve
def inverse_sqrt_matrix(mat):
"""Compute pointwise inverse square root of matri(ces).
See formula from wikipedia:
https://en.wikipedia.org/wiki/Square_root_of_a_2_by_2_matrix
"""
mat = np.asarray(mat)
a = mat[0, 0]
b = mat[0, 1]
c = mat[1, 1]
tau = a + c
delta = a * c - b * b
s = np.sqrt(delta)
t = np.sqrt(tau + 2 * s)
mat_sqrt = np.zeros(mat.shape)
mat_sqrt[0, 0] = a + s
mat_sqrt[0, 1] = b
mat_sqrt[1, 0] = b
mat_sqrt[1, 1] = c + s
mat_sqrt /= t[None, None]
# compute inverse, need to move 2x2 matrix to last
mat_sqrt = swap_axes(mat_sqrt)
mat_sqrt_inv = np.linalg.inv(mat_sqrt)
mat_sqrt_inv = swap_axes(mat_sqrt_inv)
return mat_sqrt_inv
def cov_matrix(data):
"""Estimate the covariance matrix from data.
Parameters
----------
data : kxnxm `numpy.ndarray`
Estimates the covariance along the first dimension.
Returns
-------
cov_mat : kxk `numpy.ndarray`
Covariance matrix.
"""
n = len(data)
cov_mat = np.zeros([n, n])
for i in range(n):
for j in range(n):
cov_mat[i, j] = estimate_cov(data[i], data[j])
return cov_mat
if __name__ == '__main__':
# Example
I1 = np.random.randn(50, 50)
I2 = 3 * np.random.randn(50, 50)
corr_variable = (I1 + I2)
print(estimate_cov(I1, I1)) # should be 1
print(estimate_cov(I1, I2)) # should be 0
print(estimate_cov(I2, I1)) # should be 0
print(estimate_cov(I2, I2)) # should be 9
print(estimate_cov(I1, corr_variable)) # should be 1