nipy / nibabel

Python package to access a cacophony of neuro-imaging file formats
http://nipy.org/nibabel/
Other
654 stars 258 forks source link

Update affines.py to be compatible with current release of numpy #1348

Closed neurolabusc closed 1 month ago

neurolabusc commented 2 months ago

My colleague noted that conform no longer works. I replicated this on my system using nibabel Version 5.2.1 and the current stable release of numpy Version 2.1.0 on my MacOS M2 with Python 3.11.2. My colleague resolved this by making an environment and specifying numpy<2. However, it would be good to have a solution to encourage people to enjoy all the benefits of modern numpy.

To replicate, run this script:

import nibabel as nib
import numpy as np
from nibabel.processing import conform

input_image_path = './T1w.nii'
output_image_path = './cT1w.nii'
img = nib.load(input_image_path)
conformed_img = conform(img)
nib.save(conformed_img, output_image_path)

print(f"Conformed image saved as {output_image_path}")

I get this error:

Traceback (most recent call last):
  File "/Users/chris/Downloads/ARC_reslice/conformer.py", line 8, in <module>
    conformed_img = conform(img)
                    ^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/nibabel/processing.py", line 388, in conform
    out_aff = rescale_affine(reoriented.affine, reoriented.shape, voxel_size, out_shape)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/nibabel/affines.py", line 368, in rescale_affine
    shape = np.array(shape, copy=False)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unable to avoid copy while creating an array as requested.
If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x).
For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
effigies commented 2 months ago

Ah, we definitely need a release. This has been fixed in master. In the short term, pip install git+https://github.com/nipy/nibabel.git@master will do the job. I will aim to have a release out by the end of the month. I will probably do a 5.3 to avoid the complications of a major release. I think 6.0 will probably be numpy 2+ only, because there are some things that are quite hard to support properly on both sides of the major release (#1318)...

effigies commented 1 month ago

5.3.0 is released.