Skip to content

lib.distances.minimize_vectors rejects read-only input arrays (buffer source array is read-only) #5555

Open

Description

@nulltracer-maker

Expected behavior

MDAnalysis.lib.distances.minimize_vectors should accept read-only NumPy arrays as input, since the function only needs to read the input vectors and writes the transformed results to a separate output array.

Identical writable and read-only arrays should produce the same results. The function's documentation does not require writable input.

Actual behavior

minimize_vectors raises a Cython buffer error when the input array is read-only, while the identical writable array works:

ValueError: buffer source array is read-only

Root cause

The issue originates from the Cython implementations _minimize_vectors_ortho and _minimize_vectors_triclinic in lib/c_distances.pyx.

Both kernels declare vectors as a non-const typed memoryview:

cython.floating[:, :] vectors

However, both kernels only read vectors. Input values are copied into a separately allocated output array, and the minimum-image transformation is performed in-place on output, not on the caller's input.

The Python wrapper already allocates the output using np.empty_like(vectors), but does not ensure that the input buffer is writable before passing it to Cython.

Suggested fix

Declare vectors as a const memoryview in both kernels, or create a writable copy only when necessary.

A regression test could compare results for identical writable and read-only inputs, covering both orthogonal and triclinic simulation boxes.

Read-only NumPy arrays commonly arise from memory-mapped files (np.memmap), joblib, and shared data buffers.

For precedent, SciPy performed a project-wide audit of similar writable-buffer requirements: [scipy/scipy#20196](scipy/scipy#20196).

Code to reproduce the behavior

import numpy as np
from MDAnalysis.lib.distances import minimize_vectors

rs = np.random.RandomState(0)
vecs = rs.rand(10, 3) - 0.5

vecs_ro = vecs.copy()
vecs_ro.setflags(write=False)

box = np.array([
    10.0, 10.0, 10.0,
    90.0, 90.0, 90.0
])

# Writable input: works
result = minimize_vectors(vecs, box)
print(result.shape)  # (10, 3)

# Identical read-only input: fails
minimize_vectors(vecs_ro, box)
# ValueError: buffer source array is read-only

Current version of MDAnalysis

  • MDAnalysis: 2.11.0
  • NumPy: 2.5.5
  • Python: 3.12
  • Operating system: macOS arm64

Found while scanning Cython buffer preconditions against Python-side input handling. The reproducer was manually verified.

Activity

  1. commit-msuyog commented on Nov 19, 2026

    @commit-msuyog

    Thanks for the clear report. Since both kernels only read vectors and write into the separately allocated output, declaring it as const cython.floating[:, :] should fix this without an extra copy.

    I'd like to take this one. I'll open a PR with the const change and a regression test covering writable vs. read-only inputs for orthogonal and triclinic boxes, and check the other kernels in c_distances.pyx for the same pattern.

  2. added
    AI-assisted
    on Nov 19, 2026
  3. AkshitBoora commented on Nov 27, 2026

    @AkshitBoora
    Contributor

    Could you please assign this PR to me? I'd like to work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AI-assistedComponent-lib

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions