Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
neurolabusc opened this issue Sep 9, 2024 · 2 comments
Closed

Comments

@neurolabusc
Copy link

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
Copy link
Member

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
Copy link
Member

effigies commented Oct 8, 2024

5.3.0 is released.

@effigies effigies closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants