Skip to content

Commit

Permalink
ENH: add components decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
ncullen93 committed May 30, 2024
1 parent c4f0e32 commit 95f46ad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
12 changes: 12 additions & 0 deletions ants/decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ants
from functools import wraps
from ants.core.ants_image import ANTsImage

Expand All @@ -7,3 +8,14 @@ def wrapper(self, *args, **kwargs):
return func(self, *args, **kwargs)
setattr(ANTsImage, func.__name__, wrapper)
return func


def components_method(func):
@wraps(func)
def wrapper(image, *args, **kwargs):
if image.has_components:
return ants.merge_channels([func(img, *args, **kwargs) for img in ants.split_channels(image)],
channels_first=image.channels_first)
else:
return func(image, *args, **kwargs)
return wrapper
17 changes: 6 additions & 11 deletions ants/ops/crop_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@


import ants
from ants.decorators import image_method
from ants.decorators import image_method, components_method
from ants.internal import get_lib_fn

@image_method
@components_method
def crop_image(image, label_image=None, label=1):
"""
Use a label image to crop a smaller ANTsImage from within a larger ANTsImage
Expand Down Expand Up @@ -40,11 +41,7 @@ def crop_image(image, label_image=None, label=1):
>>> fi2 = ants.merge_channels([fi,fi])
>>> cropped2 = ants.crop_image(fi2)
>>> cropped = ants.crop_image(fi, fi, 100 )
"""
if image.has_components:
return ants.merge_channels([crop_image(img, label_image, label) for img in ants.split_channels(image)],
channels_first=image.channels_first)

"""
inpixeltype = image.pixeltype
ndim = image.dimension
if image.pixeltype != 'float':
Expand All @@ -62,6 +59,7 @@ def crop_image(image, label_image=None, label=1):


@image_method
@components_method
def crop_indices(image, lowerind, upperind):
"""
Create a proper ANTsImage sub-image by indexing the image with indices.
Expand Down Expand Up @@ -93,11 +91,7 @@ def crop_indices(image, lowerind, upperind):
>>> cropped = ants.crop_indices( fi, (10,10), (100,100) )
>>> cropped = ants.smooth_image( cropped, 5 )
>>> decropped = ants.decrop_image( cropped, fi )
"""
if image.has_components:
return ants.merge_channels([crop_indices(img, lowerind, upperind) for img in ants.split_channels(image)],
channels_first=image.channels_first)

"""
inpixeltype = 'float'
if image.pixeltype != 'float':
inpixeltype = image.pixeltype
Expand All @@ -114,6 +108,7 @@ def crop_indices(image, lowerind, upperind):
return ants_image

@image_method
@components_method
def decrop_image(cropped_image, full_image):
"""
The inverse function for `ants.crop_image`
Expand Down

0 comments on commit 95f46ad

Please sign in to comment.