|
a |
|
b/tests/test_bugs.py |
|
|
1 |
""" |
|
|
2 |
Test that any previously reported bugs are fixed |
|
|
3 |
|
|
|
4 |
nptest.assert_allclose |
|
|
5 |
self.assertEqual |
|
|
6 |
self.assertTrue |
|
|
7 |
|
|
|
8 |
""" |
|
|
9 |
|
|
|
10 |
import os |
|
|
11 |
import unittest |
|
|
12 |
from common import run_tests |
|
|
13 |
|
|
|
14 |
import ants |
|
|
15 |
|
|
|
16 |
import numpy as np |
|
|
17 |
import numpy.testing as nptest |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
class Test_bugs(unittest.TestCase): |
|
|
21 |
""" |
|
|
22 |
Test ants.ANTsImage class |
|
|
23 |
""" |
|
|
24 |
def setUp(self): |
|
|
25 |
pass |
|
|
26 |
|
|
|
27 |
def tearDown(self): |
|
|
28 |
pass |
|
|
29 |
|
|
|
30 |
def test_resample_returns_NaNs(self): |
|
|
31 |
""" |
|
|
32 |
Test that resampling an image doesnt cause the resampled |
|
|
33 |
image to have NaNs - previously caused by resampling an |
|
|
34 |
image of type DOUBLE |
|
|
35 |
""" |
|
|
36 |
img2d = ants.image_read(ants.get_ants_data('r16')) |
|
|
37 |
img2dr = ants.resample_image(img2d, (2,2), 0, 0) |
|
|
38 |
|
|
|
39 |
self.assertTrue(np.sum(np.isnan(img2dr.numpy())) == 0) |
|
|
40 |
|
|
|
41 |
img3d = ants.image_read(ants.get_ants_data('mni')) |
|
|
42 |
img3dr = ants.resample_image(img3d, (2,2,2), 0, 0) |
|
|
43 |
|
|
|
44 |
self.assertTrue(np.sum(np.isnan(img3dr.numpy())) == 0) |
|
|
45 |
|
|
|
46 |
def test_compose_multi_type_transforms(self): |
|
|
47 |
image = ants.image_read(ants.get_ants_data("r16")) |
|
|
48 |
|
|
|
49 |
linear_transform = ants.create_ants_transform(transform_type= |
|
|
50 |
"AffineTransform", precision='float', dimension=image.dimension) |
|
|
51 |
|
|
|
52 |
displacement_field = ants.simulate_displacement_field(image, |
|
|
53 |
field_type="bspline", number_of_random_points=1000, |
|
|
54 |
sd_noise=10.0, enforce_stationary_boundary=True, |
|
|
55 |
number_of_fitting_levels=4, mesh_size=1, |
|
|
56 |
sd_smoothing=4.0) |
|
|
57 |
displacement_field_xfrm = ants.transform_from_displacement_field(displacement_field) |
|
|
58 |
|
|
|
59 |
xfrm = ants.compose_ants_transforms([linear_transform, displacement_field_xfrm]) |
|
|
60 |
xfrm = ants.compose_ants_transforms([linear_transform, linear_transform]) |
|
|
61 |
xfrm = ants.compose_ants_transforms([displacement_field_xfrm, linear_transform]) |
|
|
62 |
|
|
|
63 |
def test_bspline_image_with_2d_weights(self): |
|
|
64 |
# see https://github.com/ANTsX/ANTsPy/issues/655 |
|
|
65 |
import ants |
|
|
66 |
import numpy as np |
|
|
67 |
|
|
|
68 |
output_size = (256, 256) |
|
|
69 |
bspline_epsilon = 1e-4 |
|
|
70 |
number_of_fitting_levels = 4 |
|
|
71 |
|
|
|
72 |
image = ants.image_read(ants.get_ants_data("r16")) |
|
|
73 |
image = ants.resample_image(image, (100, 100), use_voxels=True) |
|
|
74 |
|
|
|
75 |
indices = np.meshgrid(list(range(image.shape[0])), |
|
|
76 |
list(range(image.shape[1]))) |
|
|
77 |
indices_array = np.stack((indices[1].flatten(), |
|
|
78 |
indices[0].flatten()), axis=0) |
|
|
79 |
|
|
|
80 |
image_parametric_values = indices_array.transpose() |
|
|
81 |
|
|
|
82 |
weight_array = np.ones(image.shape) |
|
|
83 |
parametric_values = image_parametric_values |
|
|
84 |
scattered_data = np.atleast_2d(image.numpy().flatten()).transpose() |
|
|
85 |
weight_values = np.atleast_2d(weight_array.flatten()).transpose() |
|
|
86 |
|
|
|
87 |
min_parametric_values = np.min(parametric_values, axis=0) |
|
|
88 |
max_parametric_values = np.max(parametric_values, axis=0) |
|
|
89 |
|
|
|
90 |
spacing = np.zeros((2,)) |
|
|
91 |
for d in range(2): |
|
|
92 |
spacing[d] = (max_parametric_values[d] - min_parametric_values[d]) / (output_size[d] - 1) + bspline_epsilon |
|
|
93 |
|
|
|
94 |
bspline_image = ants.fit_bspline_object_to_scattered_data(scattered_data, parametric_values, |
|
|
95 |
parametric_domain_origin=min_parametric_values - bspline_epsilon, |
|
|
96 |
parametric_domain_spacing=spacing, |
|
|
97 |
parametric_domain_size=output_size, |
|
|
98 |
data_weights=weight_values, |
|
|
99 |
number_of_fitting_levels=number_of_fitting_levels, |
|
|
100 |
mesh_size=1) |
|
|
101 |
|
|
|
102 |
|
|
|
103 |
def test_scalar_rgb_missing(self): |
|
|
104 |
import ants |
|
|
105 |
img = ants.image_read(ants.get_data('r16')) |
|
|
106 |
with self.assertRaises(Exception): |
|
|
107 |
img_color = ants.scalar_to_rgb(img, cmap='jet') |
|
|
108 |
|
|
|
109 |
def test_bspline_zeros(self): |
|
|
110 |
import ants |
|
|
111 |
import numpy as np |
|
|
112 |
x = np.linspace(-4, 4, num=100) + np.random.uniform(-0.1, 0.1, 100) |
|
|
113 |
u = np.linspace(0, 1.0, num=len(x)) |
|
|
114 |
scattered_data = np.expand_dims(u, axis=-1) |
|
|
115 |
parametric_data = np.expand_dims(u, axis=-1) |
|
|
116 |
spacing = 1/(len(x)-1) * 1.0 |
|
|
117 |
bspline_curve = ants.fit_bspline_object_to_scattered_data( |
|
|
118 |
scattered_data, parametric_data, |
|
|
119 |
parametric_domain_origin=[0.0], parametric_domain_spacing=[spacing], |
|
|
120 |
parametric_domain_size=[len(x)], is_parametric_dimension_closed=None, |
|
|
121 |
number_of_fitting_levels=5, mesh_size=1) |
|
|
122 |
|
|
|
123 |
# Erroneously returns all zeros. |
|
|
124 |
self.assertNotEqual(bspline_curve.sum(), 0) |
|
|
125 |
|
|
|
126 |
def test_from_numpy_different_dtypes(self): |
|
|
127 |
all_dtypes = ('bool', |
|
|
128 |
'int8', |
|
|
129 |
'int16', |
|
|
130 |
'int32', |
|
|
131 |
'int64', |
|
|
132 |
'uint16', |
|
|
133 |
'uint64', |
|
|
134 |
'float16') |
|
|
135 |
arr = np.random.randn(100,100) |
|
|
136 |
|
|
|
137 |
for dtype in all_dtypes: |
|
|
138 |
arr2 = arr.astype(dtype) |
|
|
139 |
img = ants.from_numpy(arr2) |
|
|
140 |
self.assertTrue(ants.is_image(img)) |
|
|
141 |
|
|
|
142 |
if __name__ == '__main__': |
|
|
143 |
run_tests() |