From 95f46ad4a7463a9e6de6cab2f98553ee726acece Mon Sep 17 00:00:00 2001 From: ncullen93 Date: Thu, 30 May 2024 13:21:40 +0200 Subject: [PATCH] ENH: add components decorator --- ants/decorators.py | 12 ++++++++++++ ants/ops/crop_image.py | 17 ++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/ants/decorators.py b/ants/decorators.py index c553b14b..dc4955e4 100644 --- a/ants/decorators.py +++ b/ants/decorators.py @@ -1,3 +1,4 @@ +import ants from functools import wraps from ants.core.ants_image import ANTsImage @@ -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 diff --git a/ants/ops/crop_image.py b/ants/ops/crop_image.py index b945ec2f..25b54459 100644 --- a/ants/ops/crop_image.py +++ b/ants/ops/crop_image.py @@ -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 @@ -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': @@ -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. @@ -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 @@ -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`