|
| 1 | +# Copyright 2020 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import unittest |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +from scipy.ndimage import zoom as zoom_scipy |
| 16 | +from parameterized import parameterized |
| 17 | + |
| 18 | +from monai.transforms import Zoom |
| 19 | +from tests.utils import NumpyImageTestCase2D |
| 20 | + |
| 21 | + |
| 22 | +class ZoomTest(NumpyImageTestCase2D): |
| 23 | + |
| 24 | + @parameterized.expand([ |
| 25 | + (1.1, 3, 'constant', 0, True, False, False), |
| 26 | + (0.9, 3, 'constant', 0, True, False, False), |
| 27 | + (0.8, 1, 'reflect', 0, False, False, False) |
| 28 | + ]) |
| 29 | + def test_correct_results(self, zoom, order, mode, cval, prefilter, use_gpu, keep_size): |
| 30 | + zoom_fn = Zoom(zoom=zoom, order=order, mode=mode, cval=cval, |
| 31 | + prefilter=prefilter, use_gpu=use_gpu, keep_size=keep_size) |
| 32 | + zoomed = zoom_fn(self.imt) |
| 33 | + expected = zoom_scipy(self.imt, zoom=zoom, mode=mode, order=order, |
| 34 | + cval=cval, prefilter=prefilter) |
| 35 | + self.assertTrue(np.allclose(expected, zoomed)) |
| 36 | + |
| 37 | + @parameterized.expand([ |
| 38 | + ("gpu_zoom", 0.6, 3, 'constant', 0, True) |
| 39 | + ]) |
| 40 | + def test_gpu_zoom(self, _, zoom, order, mode, cval, prefilter): |
| 41 | + zoom_fn = Zoom(zoom=zoom, order=order, mode=mode, cval=cval, |
| 42 | + prefilter=prefilter, use_gpu=True, keep_size=False) |
| 43 | + zoomed = zoom_fn(self.imt) |
| 44 | + expected = zoom_scipy(self.imt, zoom=zoom, mode=mode, order=order, |
| 45 | + cval=cval, prefilter=prefilter) |
| 46 | + self.assertTrue(np.allclose(expected, zoomed)) |
| 47 | + |
| 48 | + def test_keep_size(self): |
| 49 | + zoom_fn = Zoom(zoom=0.6, keep_size=True) |
| 50 | + zoomed = zoom_fn(self.imt) |
| 51 | + self.assertTrue(np.array_equal(zoomed.shape, self.imt.shape)) |
| 52 | + |
| 53 | + @parameterized.expand([ |
| 54 | + ("no_zoom", None, 1, TypeError), |
| 55 | + ("invalid_order", 0.9, 's', AssertionError) |
| 56 | + ]) |
| 57 | + def test_invalid_inputs(self, _, zoom, order, raises): |
| 58 | + with self.assertRaises(raises): |
| 59 | + zoom_fn = Zoom(zoom=zoom, order=order) |
| 60 | + zoomed = zoom_fn(self.imt) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + unittest.main() |
0 commit comments