From 94c707dcd33b94bcdea9f05712e7fde1b338004d Mon Sep 17 00:00:00 2001 From: ncullen93 Date: Mon, 19 Feb 2024 17:35:39 +0100 Subject: [PATCH 1/2] rightside ops --- ants/core/ants_image.py | 34 +++++++++++++++++++++ tests/test_core_ants_image.py | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/ants/core/ants_image.py b/ants/core/ants_image.py index 610e28c7..0d38dc03 100644 --- a/ants/core/ants_image.py +++ b/ants/core/ants_image.py @@ -436,6 +436,7 @@ def unique(self, sort=False): ## OVERLOADED OPERATORS ## def __add__(self, other): + print(other) this_array = self.numpy() if isinstance(other, ANTsImage): @@ -446,6 +447,17 @@ def __add__(self, other): new_array = this_array + other return self.new_image_like(new_array) + def __radd__(self, other): + this_array = self.numpy() + + if isinstance(other, ANTsImage): + if not image_physical_space_consistency(self, other): + raise ValueError('images do not occupy same physical space') + other = other.numpy() + + new_array = other + this_array + return self.new_image_like(new_array) + def __sub__(self, other): this_array = self.numpy() @@ -457,6 +469,17 @@ def __sub__(self, other): new_array = this_array - other return self.new_image_like(new_array) + def __rsub__(self, other): + this_array = self.numpy() + + if isinstance(other, ANTsImage): + if not image_physical_space_consistency(self, other): + raise ValueError('images do not occupy same physical space') + other = other.numpy() + + new_array = other - this_array + return self.new_image_like(new_array) + def __mul__(self, other): this_array = self.numpy() @@ -468,6 +491,17 @@ def __mul__(self, other): new_array = this_array * other return self.new_image_like(new_array) + def __rmul__(self, other): + this_array = self.numpy() + + if isinstance(other, ANTsImage): + if not image_physical_space_consistency(self, other): + raise ValueError('images do not occupy same physical space') + other = other.numpy() + + new_array = other * this_array + return self.new_image_like(new_array) + def __truediv__(self, other): this_array = self.numpy() diff --git a/tests/test_core_ants_image.py b/tests/test_core_ants_image.py index c85e12a1..6fc77535 100644 --- a/tests/test_core_ants_image.py +++ b/tests/test_core_ants_image.py @@ -232,6 +232,25 @@ def test__add__(self): img2.set_spacing([2.31]*img.dimension) img3 = img + img2 + def test__radd__(self): + #self.setUp() + for img in self.imgs: + # op on constant + img2 = 6.9 + img + self.assertTrue(ants.image_physical_space_consistency(img, img2)) + nptest.assert_allclose(img2.numpy(), img.numpy() + 6.9) + + # op on another image + img2 = img + img.clone() + self.assertTrue(ants.image_physical_space_consistency(img, img2)) + nptest.assert_allclose(img2.numpy(), img.numpy()+img.numpy()) + + with self.assertRaises(Exception): + # different physical space + img2 = img.clone() + img2.set_spacing([2.31]*img.dimension) + img3 = img + img2 + def test__sub__(self): #self.setUp() for img in self.imgs: @@ -251,6 +270,25 @@ def test__sub__(self): img2.set_spacing([2.31]*img.dimension) img3 = img - img2 + def test__rsub__(self): + #self.setUp() + for img in self.imgs: + # op on constant + img2 = 6.9 - img + self.assertTrue(ants.image_physical_space_consistency(img, img2)) + nptest.assert_allclose(img2.numpy(), 6.9 - img.numpy()) + + # op on another image + img2 = img - img.clone() + self.assertTrue(ants.image_physical_space_consistency(img, img2)) + nptest.assert_allclose(img2.numpy(), img.numpy()-img.numpy()) + + with self.assertRaises(Exception): + # different physical space + img2 = img.clone() + img2.set_spacing([2.31]*img.dimension) + img3 = img - img2 + def test__mul__(self): #self.setUp() for img in self.imgs: @@ -270,6 +308,25 @@ def test__mul__(self): img2.set_spacing([2.31]*img.dimension) img3 = img * img2 + def test__rmul__(self): + #self.setUp() + for img in self.imgs: + # op on constant + img2 = 6.9 * img + self.assertTrue(ants.image_physical_space_consistency(img, img2)) + nptest.assert_allclose(img2.numpy(), 6.9*img.numpy()) + + # op on another image + img2 = img * img.clone() + self.assertTrue(ants.image_physical_space_consistency(img, img2)) + nptest.assert_allclose(img2.numpy(), img.numpy()*img.numpy()) + + with self.assertRaises(Exception): + # different physical space + img2 = img.clone() + img2.set_spacing([2.31]*img.dimension) + img3 = img * img2 + def test__div__(self): #self.setUp() for img in self.imgs: From 5302bdf7dd6069eddb29ff0859aabd69cd4f1084 Mon Sep 17 00:00:00 2001 From: ncullen93 Date: Mon, 19 Feb 2024 17:38:12 +0100 Subject: [PATCH 2/2] remove print --- ants/core/ants_image.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ants/core/ants_image.py b/ants/core/ants_image.py index 0d38dc03..03a1630d 100644 --- a/ants/core/ants_image.py +++ b/ants/core/ants_image.py @@ -436,7 +436,6 @@ def unique(self, sort=False): ## OVERLOADED OPERATORS ## def __add__(self, other): - print(other) this_array = self.numpy() if isinstance(other, ANTsImage):