Skip to content

More numpy 2.0 related fixes #6974

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

Merged
merged 3 commits into from
Jun 13, 2024
Merged

Conversation

aganders3
Copy link
Contributor

References and relevant issues

I don't see a tracking issue for numpy 2.0 support, but here are some previous PRs I've found:
#6932
#6776

And a zulip thread:
https://napari.zulipchat.com/#narrow/stream/212875-general/topic/handling.20the.20numpy.202.2E0.20release/near/381330412

Description

This fixes a few more numpy-2.0 related issues. I will comment on some of them inline.

I tested locally using a wheel from my vispy PR. I also uninstalled tensorstore to skip related tests (see google/tensorstore#165).

There are still a few test failures in napari/layers/image/_tests/test_image.py that look possibly related to Xarray:

napari/layers/image/_tests/test_image.py:649 test_image_scale[scale5] - DeprecationWarning: __array__ implementation doesn't accept a copy keyword, so passing copy=False failed. __array__ must implement 'dtype' and… [1012/3861]

I also get a failure on napari/_qt/widgets/_tests/test_qt_tooltip.py::test_qt_tooltip_label, but I think this may be a macOS thing (it passes if I mouse over it).

Other than that, local tests with pytest napari are all green.

@aganders3 aganders3 requested a review from jni June 10, 2024 16:36
@github-actions github-actions bot added the tests Something related to our tests label Jun 10, 2024
@@ -11,7 +11,6 @@
import pytest
import xarray as xr
import zarr
from numpy.core.numerictypes import issubdtype
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple fix to import from the public API (already used once in this file):

__________________________________________________________________________________ ERROR collecting napari/layers/labels/_tests/test_labels.py ___________________________________________________________________________________
napari/layers/labels/_tests/test_labels.py:14: in <module>
    from numpy.core.numerictypes import issubdtype
numpy2-venv/lib/python3.11/site-packages/numpy/core/numerictypes.py:8: in __getattr__
    _raise_warning(attr_name, "numerictypes")
numpy2-venv/lib/python3.11/site-packages/numpy/core/_utils.py:10: in _raise_warning
    warnings.warn(
E   DeprecationWarning: numpy.core.numerictypes is deprecated and has been renamed to numpy._core.numerictypes. The numpy._core namespace contains private NumPy internals and its use is discouraged, as NumPy internals can change without warning in any release. In practice, most real-world usage of numpy.core is to access functionality in the public NumPy API. If that is the case, use the public NumPy API. If not, you are using NumPy internals. If you would still like to access an internal attribute, use numpy._core.numerictypes.issubdtype.

Comment on lines +304 to +308
# calculate distance to line (2D cross-product)
line_dist = abs(
unit_lines[..., 0] * point_vectors[..., 1]
- unit_lines[..., 1] * point_vectors[..., 0]
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.cross is deprecated for 2D inputs. Here is some discussion about alternatives:
numpy/numpy#26620

I think this fix is okay as it seems this function is already restricted to 2D-only.

Below in this file this function is used again, but for 3D input. We might want to use np.linalg.cross instead, but it's only available in numpy 2.0 so ¯_(ツ)_/¯.

@@ -1451,7 +1451,7 @@ def _map_color(self, attribute: str, update_color_mapping: bool = False):
# and update_color_mapping==False
color_cycle_map = getattr(self, f'{attribute}_color_cycle_map')
color_cycle_keys = [*color_cycle_map]
props_in_map = np.in1d(color_properties, color_cycle_keys)
props_in_map = np.isin(color_properties, color_cycle_keys)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in1d is deprecated and isin is the suggested replacement. I'm pretty sure for this use it is a drop-in replacement. I believe isin can be slower for certain array dtypes (object), but I'm not sure that's an issue here.

Copy link

codecov bot commented Jun 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.51%. Comparing base (3aac4b7) to head (9e91a24).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6974      +/-   ##
==========================================
- Coverage   92.56%   92.51%   -0.06%     
==========================================
  Files         611      611              
  Lines       55295    55296       +1     
==========================================
- Hits        51182    51155      -27     
- Misses       4113     4141      +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@andy-sweet
Copy link
Member

I assume that running the tests locally with numpy 2 also requires having the following vispy changes in that same env? vispy/vispy#2599

@aganders3
Copy link
Contributor Author

Yep - there are wheels in the CI artifacts for that branch you can download, or build it locally.

Copy link
Member

@jni jni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for all the individual explanations @aganders3! Made this very easy to go through. 😊

@jni jni added this to the 0.5.0 milestone Jun 11, 2024
@jni jni added maintenance PR with maintance changes, ready to merge Last chance for comments! Will be merged in ~24h labels Jun 11, 2024
@jni jni merged commit 85f19ec into napari:main Jun 13, 2024
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label Jun 13, 2024
@aganders3 aganders3 deleted the fix-numpy-2.0-issues branch June 13, 2024 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance PR with maintance changes, tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy