Skip to content

ENH: pad: implement mode="edge" #811

@ev-br

Description

@ev-br

Currently, xpx.pad only implements mode="constant". However, mode="edge" is used, for example, in SciPy FFT and ndimage,

$ git grep -n mode=\'edge\'
scipy/fft/tests/test_real_transforms.py:40:    y2 = xp.asarray(np.pad(np.asarray(y), pad, mode='edge'))
scipy/fft/tests/test_real_transforms.py:111:    y2 = xp.asarray(np.pad(np.asarray(y), pad, mode='edge'))
scipy/ndimage/_interpolation.py:219:            padded = np.pad(input, npad, mode='edge')
scipy/signal/_short_time_fft.py:1096:            'edge': dict(mode='edge'),

also scikit-image:

$ git grep -n mode=\'edge\'
doc/examples/registration/plot_opticalflow.py:42:image1_warp = warp(image1, np.array([row_coords + v, col_coords + u]), mode='edge')
src/skimage/exposure/_adapthist.py:172:    map_array = np.pad(hist, [[1, 1] for _ in range(ndim)] + [[0, 0]], mode='edge')
src/skimage/filters/_fft_based.py:92:        ``mode='edge'`` extension.
src/skimage/filters/_fft_based.py:155:        image = np.pad(image, npad, mode='edge')
src/skimage/registration/_optical_flow.py:104:            moving_image, _get_warp_points(grid, flow_current), mode='edge'
src/skimage/registration/_optical_flow.py:324:            moving_image, _get_warp_points(grid, flow), mode='edge'
src/skimage/segmentation/_chan_vese.py:26:    P = np.pad(phi, 1, mode='edge')
src/skimage/segmentation/_chan_vese.py:95:    P = np.pad(phi, 1, mode='edge')
src/skimage/segmentation/boundaries.py:37:    windows = view_as_windows(np.pad(label_img_expanded, 1, mode='edge'), (3,) * ndim)
tests/skimage/registration/test_ilk.py:34:    image1 = warp(image0, grid - gt_flow, mode='edge')
tests/skimage/registration/test_tvl1.py:34:    image1 = warp(image0, grid - gt_flow, mode='edge')
tests/skimage/transform/test_warps.py:568:        x, out_size, order=1, mode='edge', anti_aliasing=True, anti_aliasing_sigma=sigma

A generic, array-agnostic edge mode seems doable, since the numpy's version is pretty much all slice assignments,
https://github.com/numpy/numpy/blob/main/numpy/lib/_arraypad_impl.py#L870-L874

Many array libraries implement it natively: jax.numpy.pad, cupy.pad, even dask. An odd one out is pytorch, which only has an equivalent functionality in torchvision IIUC, so a pytorch version could probably share the padding code with a "generic" version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions