/usr/lib/python2.7/dist-packages/dipy/align/tests/test_expectmax.py is in python-dipy 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | import numpy as np
from numpy.testing import (assert_equal,
assert_array_equal,
assert_array_almost_equal,
assert_raises)
from .. import floating
from .. import expectmax as em
def test_compute_em_demons_step_2d():
r"""
Compares the output of the demons step in 2d against an analytical
step. The fixed image is given by $F(x) = \frac{1}{2}||x - c_f||^2$, the
moving image is given by $G(x) = \frac{1}{2}||x - c_g||^2$,
$x, c_f, c_g \in R^{2}$
References
----------
[Vercauteren09] Vercauteren, T., Pennec, X., Perchant, A., & Ayache, N.
(2009). Diffeomorphic demons: efficient non-parametric
image registration. NeuroImage, 45(1 Suppl), S61-72.
doi:10.1016/j.neuroimage.2008.10.040
"""
#Select arbitrary images' shape (same shape for both images)
sh = (30, 20)
#Select arbitrary centers
c_f = np.asarray(sh)/2
c_g = c_f + 0.5
#Compute the identity vector field I(x) = x in R^2
x_0 = np.asarray(range(sh[0]))
x_1 = np.asarray(range(sh[1]))
X = np.ndarray(sh + (2,), dtype = np.float64)
O = np.ones(sh)
X[...,0]= x_0[:, None] * O
X[...,1]= x_1[None, :] * O
#Compute the gradient fields of F and G
grad_F = X - c_f
grad_G = X - c_g
#The squared norm of grad_G to be used later
sq_norm_grad_G = np.sum(grad_G**2,-1)
#Compute F and G
F = 0.5*np.sum(grad_F**2,-1)
G = 0.5*sq_norm_grad_G
delta_field = G - F
#Now select an arbitrary parameter for $\sigma_x$ (eq 4 in [Vercauteren09])
sigma_x_sq = 1.5
#Set arbitrary values for $\sigma_i$ (eq. 4 in [Vercauteren09])
#The original Demons algorithm used simply |F(x) - G(x)| as an
#estimator, so let's use it as well
sigma_i_sq = (F - G)**2
#Select some pixels to have special values
np.random.seed(1346491)
random_labels = np.random.randint(0, 5, sh[0]*sh[1])
random_labels = random_labels.reshape(sh)
random_labels[sigma_i_sq==0] = 2 #this label is used to set sigma_i_sq == 0 below
random_labels[sq_norm_grad_G==0] = 2 #this label is used to set gradient == 0 below
expected = np.zeros_like(grad_G)
#Pixels with sigma_i_sq = inf
sigma_i_sq[random_labels == 0] = np.inf
expected[random_labels == 0, ...] = 0
#Pixels with gradient!=0 and sigma_i_sq=0
sqnrm = sq_norm_grad_G[random_labels == 1]
sigma_i_sq[random_labels == 1] = 0
expected[random_labels == 1, 0] = delta_field[random_labels == 1]*grad_G[random_labels == 1, 0]/sqnrm
expected[random_labels == 1, 1] = delta_field[random_labels == 1]*grad_G[random_labels == 1, 1]/sqnrm
#Pixels with gradient=0 and sigma_i_sq=0
sigma_i_sq[random_labels == 2] = 0
grad_G[random_labels == 2, ...] = 0
expected[random_labels == 2, ...] = 0
#Pixels with gradient=0 and sigma_i_sq!=0
grad_G[random_labels == 3, ...] = 0
#Directly compute the demons step according to eq. 4 in [Vercauteren09]
num = (sigma_x_sq * (F - G))[random_labels >= 3]
den = (sigma_x_sq * sq_norm_grad_G + sigma_i_sq)[random_labels >= 3]
expected[random_labels >= 3] = -1 * np.array(grad_G[random_labels >= 3]) #This is $J^{P}$ in eq. 4 [Vercauteren09]
expected[random_labels >= 3,...] *= (num / den)[..., None]
#Now compute it using the implementation under test
actual = np.empty_like(expected, dtype=floating)
em.compute_em_demons_step_2d(np.array(delta_field, dtype=floating),
np.array(sigma_i_sq, dtype=floating),
np.array(grad_G, dtype=floating),
sigma_x_sq,
actual)
#Test sigma_i_sq == inf
try:
assert_array_almost_equal(actual[random_labels==0], expected[random_labels==0])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq == inf")
#Test sigma_i_sq == 0 and gradient != 0
try:
assert_array_almost_equal(actual[random_labels==1], expected[random_labels==1])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq == 0 and gradient != 0")
#Test sigma_i_sq == 0 and gradient == 0
try:
assert_array_almost_equal(actual[random_labels==2], expected[random_labels==2])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq == 0 and gradient == 0")
#Test sigma_i_sq != 0 and gradient == 0
try:
assert_array_almost_equal(actual[random_labels==3], expected[random_labels==3])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq != 0 and gradient == 0 ")
#Test sigma_i_sq != 0 and gradient != 0
try:
assert_array_almost_equal(actual[random_labels==4], expected[random_labels==4])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq != 0 and gradient != 0")
def test_compute_em_demons_step_3d():
r"""
Compares the output of the demons step in 3d against an analytical
step. The fixed image is given by $F(x) = \frac{1}{2}||x - c_f||^2$, the
moving image is given by $G(x) = \frac{1}{2}||x - c_g||^2$,
$x, c_f, c_g \in R^{3}$
References
----------
[Vercauteren09] Vercauteren, T., Pennec, X., Perchant, A., & Ayache, N.
(2009). Diffeomorphic demons: efficient non-parametric
image registration. NeuroImage, 45(1 Suppl), S61-72.
doi:10.1016/j.neuroimage.2008.10.040
"""
#Select arbitrary images' shape (same shape for both images)
sh = (20, 15, 10)
#Select arbitrary centers
c_f = np.asarray(sh)/2
c_g = c_f + 0.5
#Compute the identity vector field I(x) = x in R^2
x_0 = np.asarray(range(sh[0]))
x_1 = np.asarray(range(sh[1]))
x_2 = np.asarray(range(sh[2]))
X = np.ndarray(sh + (3,), dtype = np.float64)
O = np.ones(sh)
X[...,0]= x_0[:, None, None] * O
X[...,1]= x_1[None, :, None] * O
X[...,2]= x_2[None, None, :] * O
#Compute the gradient fields of F and G
grad_F = X - c_f
grad_G = X - c_g
#The squared norm of grad_G to be used later
sq_norm_grad_G = np.sum(grad_G**2,-1)
#Compute F and G
F = 0.5*np.sum(grad_F**2,-1)
G = 0.5*sq_norm_grad_G
delta_field = G - F
#Now select an arbitrary parameter for $\sigma_x$ (eq 4 in [Vercauteren09])
sigma_x_sq = 1.5
#Set arbitrary values for $\sigma_i$ (eq. 4 in [Vercauteren09])
#The original Demons algorithm used simply |F(x) - G(x)| as an
#estimator, so let's use it as well
sigma_i_sq = (F - G)**2
#Select some pixels to have special values
np.random.seed(1346491)
random_labels = np.random.randint(0, 5, sh[0]*sh[1]*sh[2])
random_labels = random_labels.reshape(sh)
random_labels[sigma_i_sq==0] = 2 #this label is used to set sigma_i_sq == 0 below
random_labels[sq_norm_grad_G==0] = 2 #this label is used to set gradient == 0 below
expected = np.zeros_like(grad_G)
#Pixels with sigma_i_sq = inf
sigma_i_sq[random_labels == 0] = np.inf
expected[random_labels == 0, ...] = 0
#Pixels with gradient!=0 and sigma_i_sq=0
sqnrm = sq_norm_grad_G[random_labels == 1]
sigma_i_sq[random_labels == 1] = 0
expected[random_labels == 1, 0] = delta_field[random_labels == 1]*grad_G[random_labels == 1, 0]/sqnrm
expected[random_labels == 1, 1] = delta_field[random_labels == 1]*grad_G[random_labels == 1, 1]/sqnrm
expected[random_labels == 1, 2] = delta_field[random_labels == 1]*grad_G[random_labels == 1, 2]/sqnrm
#Pixels with gradient=0 and sigma_i_sq=0
sigma_i_sq[random_labels == 2] = 0
grad_G[random_labels == 2, ...] = 0
expected[random_labels == 2, ...] = 0
#Pixels with gradient=0 and sigma_i_sq!=0
grad_G[random_labels == 3, ...] = 0
#Directly compute the demons step according to eq. 4 in [Vercauteren09]
num = (sigma_x_sq * (F - G))[random_labels >= 3]
den = (sigma_x_sq * sq_norm_grad_G + sigma_i_sq)[random_labels >= 3]
expected[random_labels >= 3] = -1 * np.array(grad_G[random_labels >= 3]) #This is $J^{P}$ in eq. 4 [Vercauteren09]
expected[random_labels >= 3,...] *= (num / den)[...,None]
#Now compute it using the implementation under test
actual = np.empty_like(expected, dtype=floating)
em.compute_em_demons_step_3d(np.array(delta_field, dtype=floating),
np.array(sigma_i_sq, dtype=floating),
np.array(grad_G, dtype=floating),
sigma_x_sq,
actual)
#Test sigma_i_sq == inf
try:
assert_array_almost_equal(actual[random_labels==0], expected[random_labels==0])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq == inf")
#Test sigma_i_sq == 0 and gradient != 0
try:
assert_array_almost_equal(actual[random_labels==1], expected[random_labels==1])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq == 0 and gradient != 0")
#Test sigma_i_sq == 0 and gradient == 0
try:
assert_array_almost_equal(actual[random_labels==2], expected[random_labels==2])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq == 0 and gradient == 0")
#Test sigma_i_sq != 0 and gradient == 0
try:
assert_array_almost_equal(actual[random_labels==3], expected[random_labels==3])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq != 0 and gradient == 0 ")
#Test sigma_i_sq != 0 and gradient != 0
try:
assert_array_almost_equal(actual[random_labels==4], expected[random_labels==4])
except AssertionError:
raise AssertionError("Failed for sigma_i_sq != 0 and gradient != 0")
def test_quantize_positive_2d():
np.random.seed(1246592)
num_levels = 11 # an arbitrary number of quantization levels
img_shape = (15, 20) # arbitrary test image shape (must contain at least 3 elements)
min_positive = 0.1
max_positive = 1.0
epsilon = 1e-8
delta = (max_positive - min_positive + epsilon) / (num_levels - 1)
true_levels = np.zeros((num_levels,), dtype = np.float32)
# put the intensities at the centers of the bins
true_levels[1:] = np.linspace(min_positive+delta*0.5, max_positive-delta*0.5, num_levels-1)
true_quantization = np.empty(img_shape, dtype = np.int32) # generate a target quantization image
random_labels = np.random.randint(0, num_levels, np.size(true_quantization))
# make sure there is at least one element equal to 0, 1 and num_levels-1
random_labels[0] = 0
random_labels[1] = 1
random_labels[2] = num_levels-1
true_quantization[...] = random_labels.reshape(img_shape)
noise_amplitude = np.min([delta / 4.0, min_positive / 4.0]) # make sure additive noise doesn't change the quantization result
noise = np.random.ranf(np.size(true_quantization)).reshape(img_shape) * noise_amplitude
noise = noise.astype(floating)
input_image = np.ndarray(img_shape, dtype = floating)
input_image[...] = true_levels[true_quantization] + noise # assign intensities plus noise
input_image[true_quantization == 0] = 0 # preserve original zeros
input_image[true_quantization == 1] = min_positive # preserve min positive value
input_image[true_quantization == num_levels-1] = max_positive # preserve max positive value
out, levels, hist = em.quantize_positive_2d(input_image, num_levels)
levels = np.asarray(levels)
assert_array_equal(out, true_quantization)
assert_array_almost_equal(levels, true_levels)
for i in range(num_levels):
current_bin = np.asarray(true_quantization == i).sum()
assert_equal(hist[i], current_bin)
#test num_levels<2 and input image with zeros and non-zeros everywhere
assert_raises(ValueError, em.quantize_positive_2d, input_image, 0)
assert_raises(ValueError, em.quantize_positive_2d, input_image, 1)
out, levels, hist = em.quantize_positive_2d(np.zeros(img_shape, dtype=floating), 2)
assert_equal(out, np.zeros(img_shape, dtype=np.int32))
out, levels, hist = em.quantize_positive_2d(np.ones(img_shape, dtype=floating), 2)
assert_equal(out, np.ones(img_shape, dtype=np.int32))
def test_quantize_positive_3d():
np.random.seed(1246592)
num_levels = 11 # an arbitrary number of quantization levels
img_shape = (5, 10, 15) # arbitrary test image shape (must contain at least 3 elements)
min_positive = 0.1
max_positive = 1.0
epsilon = 1e-8
delta = (max_positive - min_positive + epsilon) / (num_levels - 1)
true_levels = np.zeros((num_levels,), dtype = np.float32)
# put the intensities at the centers of the bins
true_levels[1:] = np.linspace(min_positive+delta*0.5, max_positive-delta*0.5, num_levels-1)
true_quantization = np.empty(img_shape, dtype = np.int32) # generate a target quantization image
random_labels = np.random.randint(0, num_levels, np.size(true_quantization))
# make sure there is at least one element equal to 0, 1 and num_levels-1
random_labels[0] = 0
random_labels[1] = 1
random_labels[2] = num_levels-1
true_quantization[...] = random_labels.reshape(img_shape)
noise_amplitude = np.min([delta / 4.0, min_positive / 4.0]) # make sure additive noise doesn't change the quantization result
noise = np.random.ranf(np.size(true_quantization)).reshape(img_shape) * noise_amplitude
noise = noise.astype(floating)
input_image = np.ndarray(img_shape, dtype = floating)
input_image[...] = true_levels[true_quantization] + noise # assign intensities plus noise
input_image[true_quantization == 0] = 0 # preserve original zeros
input_image[true_quantization == 1] = min_positive # preserve min positive value
input_image[true_quantization == num_levels-1] = max_positive # preserve max positive value
out, levels, hist = em.quantize_positive_3d(input_image, num_levels)
levels = np.asarray(levels)
assert_array_equal(out, true_quantization)
assert_array_almost_equal(levels, true_levels)
for i in range(num_levels):
current_bin = np.asarray(true_quantization == i).sum()
assert_equal(hist[i], current_bin)
#test num_levels<2 and input image with zeros and non-zeros everywhere
assert_raises(ValueError, em.quantize_positive_3d, input_image, 0)
assert_raises(ValueError, em.quantize_positive_3d, input_image, 1)
out, levels, hist = em.quantize_positive_3d(np.zeros(img_shape, dtype=floating), 2)
assert_equal(out, np.zeros(img_shape, dtype=np.int32))
out, levels, hist = em.quantize_positive_3d(np.ones(img_shape, dtype=floating), 2)
assert_equal(out, np.ones(img_shape, dtype=np.int32))
def test_compute_masked_class_stats_2d():
np.random.seed(1246592)
shape = (32, 32)
#Create random labels
labels = np.ndarray(shape, dtype=np.int32)
labels[...] = np.random.randint(2, 10, np.size(labels)).reshape(shape)
labels[0, 0] = 1 # now label 0 is not present and label 1 occurs once
#Create random values
values = np.random.randn(shape[0], shape[1]).astype(floating)
values *= labels
values += labels
expected_means = [0, values[0, 0]] + [values[labels == i].mean() for i in range(2, 10)]
expected_vars = [np.inf, np.inf] + [values[labels == i].var() for i in range(2, 10)]
mask = np.ones(shape, dtype = np.int32)
means, vars = em.compute_masked_class_stats_2d(mask, values, 10, labels)
assert_array_almost_equal(means, expected_means, decimal = 4)
assert_array_almost_equal(vars, expected_vars, decimal = 4)
def test_compute_masked_class_stats_3d():
np.random.seed(1246592)
shape = (32, 32, 32)
#Create random labels
labels = np.ndarray(shape, dtype=np.int32)
labels[...] = np.random.randint(2, 10, np.size(labels)).reshape(shape)
labels[0, 0, 0] = 1 # now label 0 is not present and label 1 occurs once
#Create random values
values = np.random.randn(shape[0], shape[1], shape[2]).astype(floating)
values *= labels
values += labels
expected_means = [0, values[0, 0, 0]] + [values[labels == i].mean() for i in range(2, 10)]
expected_vars = [np.inf, np.inf] + [values[labels == i].var() for i in range(2, 10)]
mask = np.ones(shape, dtype = np.int32)
means, vars = em.compute_masked_class_stats_3d(mask, values, 10, labels)
assert_array_almost_equal(means, expected_means, decimal = 4)
assert_array_almost_equal(vars, expected_vars, decimal = 4)
if __name__=='__main__':
test_compute_em_demons_step_2d()
test_compute_em_demons_step_3d()
test_quantize_positive_2d()
test_quantize_positive_3d()
test_compute_masked_class_stats_2d()
test_compute_masked_class_stats_3d()
|