You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
Activity
commit-msuyog commentedon Nov 19, 2026
Thanks for the clear report. Since both kernels only read
vectorsand write into the separately allocatedoutput, declaring it asconst 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.pyxfor the same pattern.AkshitBoora commentedon Nov 27, 2026
Could you please assign this PR to me? I'd like to work on it.