InsightSoftwareConsortium / itk_cucim

ITK Python filters accelerated with cuCIM
Apache License 2.0
3 stars 3 forks source link

Implement convolution_image_filter #7

Open thewtex opened 2 years ago

grlee77 commented 2 years ago

For this filter, is there an easier way to set the boundary condition or output region mode via the Python interface than the following verbose way?

import itk
import numpy as np

out = itk.convolution_image_filter(
    np.random.randn(64, 64),
    kernel_image=np.ones((3, 3)),
    normalize=False,
    boundary_condition=itk.itkImageBoundaryConditionPython.itkPeriodicBoundaryConditionID2(),
    output_region_mode=itk.ConvolutionImageFilterBaseEnums.ConvolutionImageFilterOutputRegion_SAME,
)

Once properties of a reference filter are set, it should be possible to convert to appropriate arguments with something like:

output_mode = ref_filt.GetOutputRegionMode()
conv_enums = itk.ConvolutionImageFilterBaseEnums
if output_mode == conv_enums.ConvolutionImageFilterOutputRegion_SAME
    out_mode = 'same'
elif output_mode == conv_enums.ConvolutionImageFilterOutputRegion_VALID
    out_mode = 'valid'
else:
    raise ValueError("unrecognized mode enum: {output_mode}")

boundary_cond = ref_filt.GetBoundaryCondition().GetNameOfClass()
if boundary_cond == 'itkZeroFluxNeumannBoundaryCondition':
    mode = 'nearest'
elif  boundary_cond == 'itkConstantBoundaryCondition':
    mode = 'constant'
elif boundary_cond == 'itkPeriodicBoundaryCondition':
    mode = 'wrap'
else:
    raise ValueError(f"unsupported boundary condition: {boundary_cond}")

We don't have a "valid" mode in cupyx.scipy.ndimage.convolve, but can just use "same" mode and crop the output to the valid region.

thewtex commented 2 years ago

@grlee77 yes, the enum's are quite verbose, but the logic looks good.