Skip to content

Conversation

@ncullen93
Copy link
Member

@ncullen93 ncullen93 commented May 30, 2024

There are many functions that do not natively support vector images in the C++ but can work in the Python code by splitting the image, applying the function to each component, and then merging the image back. Such functions look like this:

@image_method
def crop_image(image):
   if image.has_components:
      return ants.merge_channels([crop_image(img) for img in ants.split_channels(image)])
   libfn = get_lib_fn('cropImage')
   return ants.from_pointer(libfn(image.pointer))

This PR removes that first if-statement by adding a decorator called @components_method. This decorator will let you add such split-apply-merge support to any function. It saves code and is more robust, so the function will now look like this:

@image_method
@components_method
def crop_image(image):
   libfn = get_lib_fn('cropImage')
   return ants.from_pointer(libfn(image.pointer))

And now the function knows to do the split-apply-merge thing if the image has components.

NOTE that this should not be applied to functions that natively support component images in the C++ code (although it wont really hurt I think), but I think that is very few of them.

@coveralls
Copy link

Coverage Status

coverage: 84.728% (+0.04%) from 84.688%
when pulling 95f46ad on components-decorator
into c4f0e32 on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants