[5d12a0]: / tests / test_ops.py

Download this file

168 lines (124 with data), 5.8 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
 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
import os
import unittest
from common import run_tests
import ants
import numpy as np
import numpy.testing as nptest
class Test_iMath(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_functions(self):
image = ants.image_read(ants.get_data('r16'))
ants.iMath_canny(image, sigma=2, lower=0, upper=1)
ants.iMath_fill_holes(image, hole_type=2)
ants.iMath_GC(image, radius=1)
ants.iMath_GD(image, radius=1)
ants.iMath_GE(image, radius=1)
ants.iMath_GO(image, radius=1)
ants.iMath_get_largest_component(image, min_size=50)
ants.iMath_grad(image, sigma=0.5, normalize=False)
ants.iMath_histogram_equalization(image, alpha=0.5, beta=0.5)
ants.iMath_laplacian(image, sigma=0.5, normalize=False)
ants.iMath_MC(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False)
ants.iMath_MD(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False)
ants.iMath_ME(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False)
ants.iMath_MO(image, radius=1, value=1, shape=1, parametric=False, lines=3, thickness=1, include_center=False)
ants.iMath_maurer_distance(image, foreground=1)
ants.iMath_normalize(image)
ants.iMath_pad(image, padding=2)
ants.iMath_perona_malik(image, conductance=0.25, n_iterations=1)
ants.iMath_sharpen(image)
ants.iMath_truncate_intensity(image, lower_q=0.05, upper_q=0.95, n_bins=64)
class Test_slice_image(unittest.TestCase):
"""
Test ants.ANTsImage class
"""
def setUp(self):
pass
def tearDown(self):
pass
def test_slice_image_2d(self):
"""
Test that resampling an image doesnt cause the resampled
image to have NaNs - previously caused by resampling an
image of type DOUBLE
"""
img = ants.image_read(ants.get_ants_data('r16'))
img2 = ants.slice_image(img, 0, 100)
self.assertTrue(isinstance(img2, np.ndarray))
img2 = ants.slice_image(img, 1, 100)
self.assertTrue(isinstance(img2, np.ndarray))
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 2, 100)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, -3, 100)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 4, 100)
img2 = ants.slice_image(img, -1, 100)
img3 = ants.slice_image(img, 1, 100)
self.assertTrue(np.allclose(img2, img3))
def test_slice_image_2d_vector(self):
img0 = ants.image_read(ants.get_ants_data('r16'))
img = ants.merge_channels([img0,img0,img0])
img2 = ants.slice_image(img, 0, 100)
self.assertTrue(isinstance(img2, np.ndarray))
img2 = ants.slice_image(img, 1, 100)
self.assertTrue(isinstance(img2, np.ndarray))
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 2, 100)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, -3, 100)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 4, 100)
img2 = ants.slice_image(img, -1, 100)
img3 = ants.slice_image(img, 1, 100)
self.assertTrue(np.allclose(img2, img3))
def test_slice_image_3d(self):
"""
Test that resampling an image doesnt cause the resampled
image to have NaNs - previously caused by resampling an
image of type DOUBLE
"""
img = ants.image_read(ants.get_ants_data('mni'))
img2 = ants.slice_image(img, 0, 100)
self.assertEqual(img2.dimension, 2)
img2 = ants.slice_image(img, 1, 100)
self.assertEqual(img2.dimension, 2)
img2 = ants.slice_image(img, 2, 100)
self.assertEqual(img2.dimension, 2)
img2 = ants.slice_image(img.clone('unsigned int'), 2, 100)
self.assertEqual(img2.dimension, 2)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 3, 100)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 2, 100, collapse_strategy=23)
img2 = ants.slice_image(img, -1, 100)
img3 = ants.slice_image(img, 2, 100)
self.assertTrue(ants.allclose(img2, img3))
def test_slice_image_3d_vector(self):
"""
Test that resampling an image doesnt cause the resampled
image to have NaNs - previously caused by resampling an
image of type DOUBLE
"""
img0 = ants.image_read(ants.get_ants_data('mni'))
img = ants.merge_channels([img0,img0,img0])
img2 = ants.slice_image(img, 0, 100)
self.assertEqual(img2.dimension, 2)
img2 = ants.slice_image(img, 1, 100)
self.assertEqual(img2.dimension, 2)
img2 = ants.slice_image(img, 2, 100)
self.assertEqual(img2.dimension, 2)
img2 = ants.slice_image(img.clone('unsigned int'), 2, 100)
self.assertEqual(img2.dimension, 2)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 3, 100)
with self.assertRaises(Exception):
img2 = ants.slice_image(img, 2, 100, collapse_strategy=23)
img2 = ants.slice_image(img, -1, 100)
img3 = ants.slice_image(img, 2, 100)
self.assertTrue(ants.allclose(img2, img3))
if __name__ == '__main__':
run_tests()