[5d12a0]: / tests / test_plotting.py

Download this file

281 lines (215 with data), 10.2 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
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
"""
Test ants.learn module
nptest.assert_allclose
self.assertEqual
self.assertTrue
"""
import os
import unittest
from common import run_tests
from tempfile import mktemp
# Prevent displaying figures
import matplotlib as mpl
backend_ = mpl.get_backend()
mpl.use("Agg")
import numpy as np
import numpy.testing as nptest
import ants
class TestModule_plot(unittest.TestCase):
def setUp(self):
img2d = ants.image_read(ants.get_ants_data('r16'))
img3d = ants.image_read(ants.get_ants_data('mni'))
self.imgs = [img2d, img3d]
def tearDown(self):
pass
def test_plot_example(self):
filename = mktemp(suffix='.png')
for img in self.imgs:
ants.plot(img)
ants.plot(img, overlay=img*2)
ants.plot(img, overlay=img*2)
ants.plot(img, filename=filename)
def test_extra_plot(self):
img = ants.image_read(ants.get_ants_data('r16'))
ants.plot(img, overlay=img*2, domain_image_map=ants.image_read(ants.get_data('r64')))
img = ants.image_read(ants.get_ants_data('r16'))
ants.plot(img, crop=True)
img = ants.image_read(ants.get_ants_data('mni'))
ants.plot(img, overlay=img*2,
domain_image_map=ants.image_read(ants.get_data('mni')).resample_image((4,4,4)))
img = ants.image_read(ants.get_ants_data('mni'))
ants.plot(img, overlay=img*2, reorient=True, crop=True)
def test_random(self):
img = ants.image_read(ants.get_ants_data('r16'))
img3 = ants.image_read(ants.get_data('r64'))
img2 = ants.image_read(ants.get_ants_data('mni'))
imgv = ants.merge_channels([img2])
ants.plot(img2, axis='x', scale=True, ncol=1)
ants.plot(img2, axis='y', scale=(0.05, 0.95))
ants.plot(img2, axis='z', slices=[10,20,30], title='Test', cbar=True,
cbar_vertical=True)
ants.plot(img2, cbar=True, cbar_vertical=False)
ants.plot(img, black_bg=False, title='Test', cbar=True)
imgx = img2.clone()
imgx.set_spacing((10,1,1))
ants.plot(imgx)
ants.plot(ants.get_ants_data('r16'), overlay=ants.get_data('r64'), blend=True)
with self.assertRaises(Exception):
ants.plot(ants.get_ants_data('r16'), overlay=123)
with self.assertRaises(Exception):
ants.plot(ants.get_ants_data('r16'), overlay=ants.merge_channels([img,img]))
ants.plot(ants.from_numpy(np.zeros((100,100))))
ants.plot(img.clone('unsigned int'))
ants.plot(img, domain_image_map=img3)
with self.assertRaises(Exception):
ants.plot(123)
class TestModule_plot_ortho(unittest.TestCase):
def setUp(self):
img3d = ants.image_read(ants.get_ants_data('mni'))
self.imgs = [img3d]
def tearDown(self):
pass
def test_plot_example(self):
filename = mktemp(suffix='.png')
for img in self.imgs:
ants.plot_ortho(img)
ants.plot_ortho(img, filename=filename)
def test_plot_extra(self):
img = ants.image_read(ants.get_ants_data('mni'))
ants.plot_ortho(img, overlay=img*2,
domain_image_map=ants.image_read(ants.get_data('mni')))
img = ants.image_read(ants.get_ants_data('mni'))
ants.plot_ortho(img, overlay=img*2, reorient=True, crop=True)
def test_random_params(self):
img = ants.image_read(ants.get_ants_data('mni')).resample_image((4,4,4))
img2 = ants.image_read(ants.get_data('r16'))
ants.plot_ortho(ants.get_data('mni'), overlay=ants.get_data('mni'))
with self.assertRaises(Exception):
ants.plot_ortho(123)
with self.assertRaises(Exception):
ants.plot_ortho(img2)
with self.assertRaises(Exception):
ants.plot_ortho(img, overlay=img2)
imgx = img.clone()
imgx.set_spacing((3,3,3))
ants.plot_ortho(img,overlay=imgx)
ants.plot_ortho(img.clone('unsigned int'),overlay=img, blend=True)
imgx = img.clone()
imgx.set_spacing((10,1,1))
ants.plot_ortho(imgx)
ants.plot_ortho(img, flat=True, title='Test', text='This is a test')
ants.plot_ortho(img, title='Test', text='This is a test', cbar=True)
with self.assertRaises(Exception):
ants.plot_ortho(img, domain_image_map=123)
with self.assertRaises(Exception):
ants.plot_orto(ants.merge_channels([img,img]))
class TestModule_plot_ortho_stack(unittest.TestCase):
def setUp(self):
self.img = ants.image_read(ants.get_ants_data('mni'))
def tearDown(self):
pass
def test_plot_example(self):
filename = mktemp(suffix='.png')
ants.plot_ortho_stack([self.img, self.img])
ants.plot_ortho_stack([self.img, self.img], filename=filename)
def test_extra_ortho_stack(self):
img = ants.image_read(ants.get_ants_data('mni'))
ants.plot_ortho_stack([img, img], overlays=[img*2, img*2],
domain_image_map=ants.image_read(ants.get_data('mni')))
img = ants.image_read(ants.get_ants_data('mni'))
ants.plot_ortho_stack([img, img], overlays=[img*2, img*2], reorient=True, crop=True)
class TestModule_plot_hist(unittest.TestCase):
def setUp(self):
img2d = ants.image_read(ants.get_ants_data('r16'))
img3d = ants.image_read(ants.get_ants_data('mni'))
self.imgs = [img2d, img3d]
def tearDown(self):
pass
def test_plot_example(self):
filename = mktemp(suffix='.png')
for img in self.imgs:
ants.plot_hist(img)
class TestModule_plot_grid(unittest.TestCase):
def setUp(self):
mni1 = ants.image_read(ants.get_data('mni'))
mni2 = mni1.smooth_image(1.)
mni3 = mni1.smooth_image(2.)
mni4 = mni1.smooth_image(3.)
self.images3d = np.asarray([[mni1, mni2],
[mni3, mni4]])
self.images2d = np.asarray([[mni1.slice_image(2,100), mni2.slice_image(2,100)],
[mni3.slice_image(2,100), mni4.slice_image(2,100)]])
def tearDown(self):
pass
def test_plot_example(self):
ants.plot_grid(self.images3d, slices=100)
# should take middle slices if none are given
ants.plot_grid(self.images3d)
# should work with 2d images
ants.plot_grid(self.images2d)
def test_examples(self):
mni1 = ants.image_read(ants.get_data('mni'))
mni2 = mni1.smooth_image(1.)
mni3 = mni1.smooth_image(2.)
mni4 = mni1.smooth_image(3.)
images = np.asarray([[mni1, mni2],
[mni3, mni4]])
slices = np.asarray([[100, 100],
[100, 100]])
ants.plot_grid(images=images, slices=slices, title='2x2 Grid')
images2d = np.asarray([[mni1.slice_image(2,100), mni2.slice_image(2,100)],
[mni3.slice_image(2,100), mni4.slice_image(2,100)]])
ants.plot_grid(images=images2d, title='2x2 Grid Pre-Sliced')
ants.plot_grid(images.reshape(1,4), slices.reshape(1,4), title='1x4 Grid')
ants.plot_grid(images.reshape(4,1), slices.reshape(4,1), title='4x1 Grid')
# Padding between rows and/or columns
ants.plot_grid(images, slices, cpad=0.02, title='Col Padding')
ants.plot_grid(images, slices, rpad=0.02, title='Row Padding')
ants.plot_grid(images, slices, rpad=0.02, cpad=0.02, title='Row and Col Padding')
# Adding plain row and/or column labels
ants.plot_grid(images, slices, title='Adding Row Labels', rlabels=['Row #1', 'Row #2'])
ants.plot_grid(images, slices, title='Adding Col Labels', clabels=['Col #1', 'Col #2'])
ants.plot_grid(images, slices, title='Row and Col Labels',
rlabels=['Row 1', 'Row 2'], clabels=['Col 1', 'Col 2'])
# Making a publication-quality image
images = np.asarray([[mni1, mni2, mni2],
[mni3, mni4, mni4]])
slices = np.asarray([[100, 100, 100],
[100, 100, 100]])
axes = np.asarray([[0, 1, 2],
[0, 1, 2]])
ants.plot_grid(images, slices, axes, title='Publication Figures with ANTsPy',
tfontsize=20, title_dy=0.03, title_dx=-0.04,
rlabels=['Row 1', 'Row 2'],
clabels=['Col 1', 'Col 2', 'Col 3'],
rfontsize=16, cfontsize=16)
class TestModule_plot_ortho_stack(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_random_ortho_stack_params(self):
img = ants.image_read(ants.get_data('mni')).resample_image((4,4,4))
img2 = ants.image_read(ants.get_data('r16')).resample_image((4,4))
ants.plot_ortho_stack([ants.get_data('mni'), ants.get_data('mni')])
ants.plot_ortho_stack([ants.get_data('mni'), ants.get_data('mni')],
overlays=[ants.get_data('mni'), ants.get_data('mni')])
with self.assertRaises(Exception):
ants.plot_ortho_stack([1,2,3])
with self.assertRaises(Exception):
ants.plot_ortho_stack([img2,img2])
with self.assertRaises(Exception):
ants.plot_ortho_stack([img,img], overlays=[img2,img2])
with self.assertRaises(Exception):
ants.plot_ortho_stack([img,img], overlays=[1,2])
imgx = img.clone()
imgx.set_spacing((2,2,2))
ants.plot_ortho_stack([img,imgx])
imgx.set_spacing((2,1,1))
ants.plot_ortho_stack([imgx,img])
ants.plot_ortho_stack([img,img], scale=True, transpose=True,
title='Test', colpad=1, rowpad=1,
xyz_lines=True)
ants.plot_ortho_stack([img,img], scale=(0.05,0.95))
if __name__ == '__main__':
run_tests()