pywt icon indicating copy to clipboard operation
pywt copied to clipboard

`downcoef` / `upcoef` reject read-only input arrays (`buffer source array is read-only`)

Open nulltracer-maker opened this issue 2 weeks ago • 1 comments

pywt.downcoef and pywt.upcoef raise a Cython buffer error for read-only input arrays; the identical writable array works:

import numpy as np
import pywt

rs = np.random.RandomState(0)
data = rs.randn(256)
data_ro = data.copy()
data_ro.setflags(write=False)

pywt.downcoef('a', data, 'db4', level=3)      # OK
pywt.downcoef('a', data_ro, 'db4', level=3)   # ValueError: buffer source array is read-only
pywt.upcoef('a', data_ro, 'db4', level=3)     # same error

The other public entry points in the same module — pywt.dwt, pywt.wavedec, pywt.swt, and pywt.waverec with read-only coefficient arrays — all accept read-only input (verified on 1.10.0), so this looks like an oversight in the downcoef/upcoef wrappers rather than an intended contract (the docstrings do not require writable input).

Full traceback (1.10.0)

  File ".../pywt/_dwt.py", line 322, in downcoef
    return np.asarray(_downcoef(part == 'a', data, wavelet, mode, level))
  File "pywt/_extensions/_dwt.pyx", line 427, in pywt._extensions._dwt.downcoef
  File "__pyx_ff_map_fused_...numpy_float32_t...", line 707, in View.MemoryView.memoryview_cwrapper
  File "__pyx_ff_map_fused_...numpy_float32_t...", line 371, in View.MemoryView.memoryview.__cinit__
ValueError: buffer source array is read-only

Root cause

In pywt/_dwt.py, both downcoef (lines 314-315) and upcoef (lines 164-165) normalize their inputs using:

dt = _check_dtype(data)
data = np.asarray(data, dtype=dt, order='C')

However, np.asarray does not guarantee a writable buffer: when the input is already C-contiguous with the expected dtype, the read-only array is reused without copying.

The resulting array is then passed to the Cython implementations (_downcoef / _upcoef in _extensions/_dwt.pyx), whose memoryview parameters are not declared const and therefore require a writable buffer. This raises ValueError: buffer source array is read-only even though the same numerical input works when writable — and although downcoef/upcoef do not need to modify the user's data.

Unlike downcoef/upcoef, the other public wavelet-transform APIs (dwt, wavedec, swt) accept the same read-only input, indicating inconsistent handling across entry points.

Suggested fix

If the Cython kernels only read the input (the math of partial wavelet reconstruction/deconstruction does not modify user data), declaring the corresponding memoryview parameters const would be the zero-copy fix.

Alternatively, handle read-only arrays in the Python wrappers, copying only when necessary (in downcoef the variable is data; in upcoef it is coeffs):

if not data.flags.writeable:
    data = data.copy()

A regression test could compare results for identical writable and read-only inputs across downcoef/upcoef/dwt/wavedec/swt.

Read-only arrays are common inputs (np.memmap, joblib mmap, h5py/PIL/dask exports). For precedent on the const approach, SciPy audited the whole code base for this pattern (https://github.com/scipy/scipy/issues/20196).

Environment

  • PyWavelets 1.10.0 (latest stable on PyPI, released 2026-09-07), verified from the installed distribution metadata
  • numpy 2.5.5, Python 3.12, macOS arm64

Side note (packaging): the 1.10.0 wheel's generated pywt/version.py still reports version = '1.8.0' (git_revision 4bd3a5f), so pywt.__version__ misidentifies the release. This initially made our report mislabel the version; maintainers may want to regenerate that file.

Found while scanning Cython buffer preconditions vs. Python-side input handling; the repro and traceback above are self-contained and hand-verified on the wheel installed from PyPI.

nulltracer-maker avatar Nov 18 '26 13:11 nulltracer-maker

Thanks for the report @nulltracer-maker. Your suggested fix looks right to me.

rgommers avatar Nov 18 '26 16:11 rgommers