Skip to content

Allow read-only input arrays in 8 public APIs (gh-8423) - #8427

Closed
eliem08 wants to merge 1 commit into
scikit-image:mainfrom
eliem08:fix/readonly-inputs
Closed

eliem08 wants to merge 1 commit into
scikit-image:mainfrom
eliem08:fix/readonly-inputs

Conversation

@eliem08

@eliem08 eliem08 commented Nov 20, 2026

Copy link
Copy Markdown

Summary

Nine public APIs reject read-only NumPy inputs with
ValueError: buffer source array is read-only, because their Cython entry
points demand writable buffers even though the inputs are only read.
This PR makes those inputs const typed memoryviews, so read-only arrays
(from memory-mapped data, read-only shared data, or zero-copy pipelines)
are accepted and give results identical to the same writable data. No
copies are introduced and writable inputs behave exactly as before.

The ninth API, util.map_array, needs no change: _remap.pyx already
uses const memoryviews for input_arr, input_vals and output_vals,
and test_map_array_read_only (gh-6378) already covers it.

What changed

  • restoration — unwrap_phase (1D/2D/3D): image/mask are now
    const in _unwrap_1d/2d/3d.pyx; the C unwrap2D/unwrap3D entry
    points and their helpers are const-qualified in
    unwrap_2d_ljmu.{h,c} / unwrap_3d_ljmu.{h,c} (audited: the wrapped
    image and mask are only read; writes go to internal pixel/voxel structs
    and the output array). unwrap_phase also no longer tries to zero NaN
    entries in-place on the caller's array: it copies first, only when NaNs
    are present.
  • restoration — denoise_tv_bregman, denoise_bilateral: image
    (and the lookup tables in the bilateral case) are now const in
    _denoise_cy.pyx.
  • restoration — inpaint_biharmonic: mask_flat is now
    const npy_bool[::1] in _inpaint.pyx (the wrapper always coerces the
    mask to bool). Note: const char[::1] does not accept a bool array
    under Cython 3.3.1, which is why the type is npy_bool rather than
    char.
  • transform — iradon_sart: image and projection are now const
    in sart_projection_update and in bilinear_ray_sum /
    bilinear_ray_update (_radon_transform.pyx).
  • morphology — area_opening, diameter_opening (and max_tree,
    max_tree_local_maxima): the image and auxiliary inputs are now const
    in _max_tree_cy.pyx; parent / sorted_indices / output stay
    writable.
  • feature — ORB.detect_and_extract: image is now const in
    _corner_fast, _corner_moravec, _corner_orientations (corner_cy.pyx)
    and in _orb_loop (orb_cy.pyx), which the descriptor extraction path
    also goes through.

Tests

New regression tests in both tests/skimage2/ and tests/skimage/
(the trees are mirrors) — each compares read-only vs identical writable
input for exact equality:

  • test_unwrap_phase_read_only, test_unwrap_phase_read_only_nan
  • test_denoise_tv_bregman_read_only, test_denoise_bilateral_read_only
  • test_inpaint_biharmonic_read_only (both split_into_regions values)
  • test_iradon_sart_read_only
  • test_area_opening_read_only, test_diameter_opening_read_only
  • test_corner_read_only, test_orb_read_only

The reproducer from the issue (12 differential checks) goes from 8
failures with ValueError: buffer source array is read-only before this
change to 12/12 passing after it. Neighbouring suites pass:
test_unwrap.py, test_inpaint.py, test_radon_transform.py,
test_max_tree.py, test_corner.py, test_orb.py,
test_map_array.py (512 passed) and test_denoise.py (82 passed,
89 xfailed — all pre-existing PyWavelets-missing xfails). ruff check
and ruff format --check are clean.

Fixes #8423.

Signed-off-by: Ilyass Emssaad [email protected]

@stefanv

stefanv commented Nov 21, 2026

Copy link
Copy Markdown
Member

I'm going to ask @darskkaa to review to see whether there are additions here that are still needed. We can drop the tests.

@eliem08

eliem08 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Superseded by #8434. Rebased onto current main after #8424 and #8426 landed part of this fix; #8434 keeps the remaining changes (unwrap_phase, ORB loop, extra const params, and the regression tests). Closing this one in favour of the rebased PR.

@eliem08 eliem08 closed this Nov 24, 2026
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.

Multiple public APIs reject read-only NumPy arrays due to Cython buffer requirements ##

2 participants