Skip to content

Allow read-only input arrays in unwrap_phase and ORB (gh-8423) - #8434

Open
eliem08 wants to merge 2 commits into
scikit-image:mainfrom
eliem08:fix/readonly-inputs-v2
Open

eliem08 wants to merge 2 commits into
scikit-image:mainfrom
eliem08:fix/readonly-inputs-v2

Conversation

@eliem08

@eliem08 eliem08 commented Nov 24, 2026 •

Copy link
Copy Markdown

Supersedes #8427, rebased onto current main. Since #8427 opened, upstream landed most of this fix: #8424 (corner_fast/moravec/orientations) and #8426 (denoise/inpaint/radon/max_tree consts, part of #8423). On current main only 2 of the 8 APIs still reject read-only input: unwrap_phase and ORB.detect_and_extract. This PR fixes those two, and keeps the extra const params in denoise/radon/max_tree/inpaint (harmless, no effect for callers) plus the regression tests that still fail on main.

Summary

Eight public APIs rejected read-only NumPy inputs with ValueError: buffer source array is read-only, because their Cython entry points demanded writable buffers even though the inputs are only read. Six of them were fixed upstream by #8424 and #8426. The two that still fail on main, restoration.unwrap_phase and feature.ORB.detect_and_extract, are fixed here by making their 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.

One copy is introduced, and only in one place: unwrap_phase copies the input when NaNs are present, because the NaN entries are zeroed before unwrapping and np.asarray may return the caller's read-only array unchanged. Without the copy, read-only input with NaNs would still fail. No other 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: extra const params in _denoise_cy.pyx beyond what Accept read-only input in more Cython kernels #8426 covered.
  • restoration — inpaint_biharmonic: extra const params in _inpaint.pyx beyond Accept read-only input in more Cython kernels #8426 (upstream's const unsigned char mask kept as their deliberate choice).
  • transform — iradon_sart: extra const params in _radon_transform.pyx beyond Accept read-only input in more Cython kernels #8426.
  • morphology — area_opening, diameter_opening (and max_tree, max_tree_local_maxima): extra const params in _max_tree_cy.pyx beyond Accept read-only input in more Cython kernels #8426; parent / sorted_indices / output stay writable.
  • feature — ORB.detect_and_extract: image is now const in _orb_loop (orb_cy.pyx), which the descriptor extraction path goes through. (_corner_fast, _corner_moravec, _corner_orientations were covered by Accept read-only input in corner_fast, corner_moravec and corner_orientations #8424.)

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. Only the tests that fail on main are kept; the other 7 were dropped in this revision because they already pass on main (review feedback):

  • test_unwrap_phase_read_only, test_unwrap_phase_read_only_nan
  • test_orb_read_only

Trimmed tree, CPU-only, all 14 affected test files in both trees: 712 passed, 178 xfailed (all pre-existing, mostly missing PyWavelets), 0 failures. ruff check and ruff format --check are clean.

Fixes #8423.

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

@darskkaa

Copy link
Copy Markdown
Contributor

@stefanv reviewed as asked in #8427.

OK: the unwrap_phase and ORB fixes (_unwrap_*.pyx, the C files, unwrap.py, const image in _orb_loop). I did not find any bugs. With just those built locally, everything in #8423 passes and results for normal arrays are unchanged.

Fine but not needed: the consts in max_tree, denoise, inpaint and radon. Harmless, no effect for callers.

Issues:

  • 7 of the 10 new tests already pass on main, so they don't test this PR. Agree with dropping them.
  • The description is out of date. On main only 2 of the 8 APIs still fail, and unwrap.py does add a copy when there are NaNs.

… pass on main

Drop the 7 new read-only tests that already pass on upstream main
(scikit-image#8424/scikit-image#8426 landed the underlying fixes); only unwrap_phase and ORB
still fail there. Keeps test_unwrap_phase_read_only,
test_unwrap_phase_read_only_nan and test_orb_read_only.

Signed-off-by: Ilyass Emssaad <[email protected]>
@eliem08 eliem08 changed the title Allow read-only input arrays in 8 public APIs (gh-8423) Allow read-only input arrays in unwrap_phase and ORB (gh-8423) Nov 24, 2026

This branch has not been deployed

No deployments
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