Skip to content

Commit

Permalink
Merge pull request #547 from ANTsX/rightside-ops
Browse files Browse the repository at this point in the history
rightside ops
  • Loading branch information
Nick Cullen authored Feb 19, 2024
2 parents 784506c + 5302bdf commit 2fbeeb1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ants/core/ants_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,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()

Expand All @@ -457,6 +468,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()

Expand All @@ -468,6 +490,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()

Expand Down
57 changes: 57 additions & 0 deletions tests/test_core_ants_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 2fbeeb1

Please sign in to comment.