Skip to content

NumPy 2.4 compatibility: removed and deprecated array APIs - #88

Open
inhuszar wants to merge 1 commit into
freesurfer:masterfrom
inhuszar:fix/numpy-2.4
Open

inhuszar wants to merge 1 commit into
freesurfer:masterfrom
inhuszar:fix/numpy-2.4

Conversation

@inhuszar

Copy link
Copy Markdown
Contributor

NumPy 2.4 removed several APIs that surfa still uses, and NumPy 2.x warns about others:

  • io/framed.py: np.in1d was removed in NumPy 2.4, so saving an Overlay as a FreeSurfer annotation raised AttributeError. Use np.isin (available since NumPy 1.13).
  • transform/affine.py: the newshape keyword of np.reshape was removed in NumPy 2.4, so Affine.to_warp() raised TypeError. Pass the shape positionally.
  • core/framed.py, transform/affine.py: FramedArray.array and Affine.array did not accept the dtype/copy arguments that NumPy 2 passes, which triggers a DeprecationWarning on every np.array()/np.asarray() call with copy or dtype. Implement the NumPy 2 protocol: honour dtype, copy=True returns a copy, copy=False raises if a copy would be required, copy=None copies only when needed.
  • core/framed.py: FramedArray.onehot assigned to ndarray.shape, which is deprecated in NumPy 2.5 (DeprecationWarning); use np.reshape.

All changes are backwards compatible with NumPy 1.x.

Extends test/test_numpy_compat.py with regression tests for each item (run with -W error::DeprecationWarning).

NumPy 2.4 removed several APIs that surfa still uses, and NumPy 2.x warns
about others:

  - io/framed.py: np.in1d was removed in NumPy 2.4, so saving an
    Overlay as a FreeSurfer annotation raised AttributeError. Use
    np.isin (available since NumPy 1.13).
  - transform/affine.py: the `newshape` keyword of np.reshape was
    removed in NumPy 2.4, so Affine.to_warp() raised TypeError. Pass the
    shape positionally.
  - core/framed.py, transform/affine.py: FramedArray.__array__ and
    Affine.__array__ did not accept the `dtype`/`copy` arguments that
    NumPy 2 passes, which triggers a DeprecationWarning on every
    np.array()/np.asarray() call with copy or dtype. Implement the NumPy 2
    protocol: honour dtype, copy=True returns a copy, copy=False raises
    if a copy would be required, copy=None copies only when needed.
  - core/framed.py: FramedArray.onehot assigned to ndarray.shape, which
    is deprecated in NumPy 2.5 (DeprecationWarning); use
    np.reshape.

All changes are backwards compatible with NumPy 1.x.

Extends test/test_numpy_compat.py with regression tests for each item
(run with -W error::DeprecationWarning).

Originally committed in the inhuszar/surfa fork as 9d73f7f ("NumPy 2.4
deprecation fixes", meshmind prerequisites checkout).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit afbae5b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant