From 11bb20d51eeddb4caadc89cdf16d6bd07c8443d6 Mon Sep 17 00:00:00 2001 From: Mohammad Adil Date: Thu, 27 Feb 2020 13:28:33 -0800 Subject: [PATCH 1/3] Add spatial flip to transforms. --- monai/transforms/transforms.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/monai/transforms/transforms.py b/monai/transforms/transforms.py index 602baf48d1..f919556db8 100644 --- a/monai/transforms/transforms.py +++ b/monai/transforms/transforms.py @@ -62,6 +62,24 @@ def __call__(self, img): return rescale_array(img, self.minv, self.maxv, self.dtype) +@export +class Flip: + """Reverses the order of elements along the given axis. Preserves shape. + Uses np.flip in practice. See numpy.flip for additional details. + + Args: + axes (None, int or tuple of ints): Axes along which to flip over. Default is None. + """ + + def __init__(self, axes=None): + assert isinstance(axes, int) or isinstance(axes, tuple) and all(isinstance(n, int) for n in axes), \ + "axes must be None, int or tuple of ints." + self.axes = axes + + def __call__(self, img): + return np.flip(img, axes) + + @export class ToTensor: """ From 2b3ce010431d36ed2c573472c71da6e9d5a33af2 Mon Sep 17 00:00:00 2001 From: Mohammad Adil Date: Mon, 2 Mar 2020 21:38:57 -0800 Subject: [PATCH 2/3] Add tests and fix errors. --- monai/transforms/transforms.py | 4 ++-- tests/test_flip.py | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_flip.py diff --git a/monai/transforms/transforms.py b/monai/transforms/transforms.py index f919556db8..86e18de020 100644 --- a/monai/transforms/transforms.py +++ b/monai/transforms/transforms.py @@ -72,12 +72,12 @@ class Flip: """ def __init__(self, axes=None): - assert isinstance(axes, int) or isinstance(axes, tuple) and all(isinstance(n, int) for n in axes), \ + assert isinstance(axes, (int, list, tuple)) and all(isinstance(n, int) for n in axes), \ "axes must be None, int or tuple of ints." self.axes = axes def __call__(self, img): - return np.flip(img, axes) + return np.flip(img, self.axes) @export diff --git a/tests/test_flip.py b/tests/test_flip.py new file mode 100644 index 0000000000..480d2f1ba3 --- /dev/null +++ b/tests/test_flip.py @@ -0,0 +1,39 @@ +# Copyright 2020 MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np +from parameterized import parameterized + +from monai.transforms import Flip +from tests.utils import NumpyImageTestCase2D + + +class FlipTest(NumpyImageTestCase2D): + + @parameterized.expand([ + ("no_axis", None, AssertionError), + ("wrong_axis", ['s', 1], AssertionError) + ]) + def test_invalid_inputs(self, _, axis, raises): + with self.assertRaises(raises): + flip = Flip(axis) + flip(self.imt) + + def test_correct_results(self): + flip = Flip(axes=[0, 1]) + expected = np.flip(self.imt, [0, 1]) + self.assertTrue(np.allclose(expected, flip(self.imt))) + + +if __name__ == '__main__': + unittest.main() From 8bd766c7bbc7978b200c58adba40f6be39729880 Mon Sep 17 00:00:00 2001 From: Mohammad Adil Date: Tue, 3 Mar 2020 08:54:31 -0800 Subject: [PATCH 3/3] Fix unit tests. --- monai/transforms/transforms.py | 10 +++++----- tests/test_flip.py | 15 ++++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/monai/transforms/transforms.py b/monai/transforms/transforms.py index 86e18de020..db4eafb9ca 100644 --- a/monai/transforms/transforms.py +++ b/monai/transforms/transforms.py @@ -71,13 +71,13 @@ class Flip: axes (None, int or tuple of ints): Axes along which to flip over. Default is None. """ - def __init__(self, axes=None): - assert isinstance(axes, (int, list, tuple)) and all(isinstance(n, int) for n in axes), \ - "axes must be None, int or tuple of ints." - self.axes = axes + def __init__(self, axis=None): + assert axis is None or isinstance(axis, (int, list, tuple)), \ + "axis must be None, int or tuple of ints." + self.axis = axis def __call__(self, img): - return np.flip(img, self.axes) + return np.flip(img, self.axis) @export diff --git a/tests/test_flip.py b/tests/test_flip.py index 480d2f1ba3..a70b9c92c5 100644 --- a/tests/test_flip.py +++ b/tests/test_flip.py @@ -21,17 +21,22 @@ class FlipTest(NumpyImageTestCase2D): @parameterized.expand([ - ("no_axis", None, AssertionError), - ("wrong_axis", ['s', 1], AssertionError) + ("wrong_axis", ['s', 1], TypeError), + ("not_numbers", 's', AssertionError) ]) def test_invalid_inputs(self, _, axis, raises): with self.assertRaises(raises): flip = Flip(axis) flip(self.imt) - def test_correct_results(self): - flip = Flip(axes=[0, 1]) - expected = np.flip(self.imt, [0, 1]) + @parameterized.expand([ + ("no_axis", None), + ("one_axis", 1), + ("many_axis", [0, 1, 2]) + ]) + def test_correct_results(self, _, axis): + flip = Flip(axis=axis) + expected = np.flip(self.imt, axis) self.assertTrue(np.allclose(expected, flip(self.imt)))