Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: NumPy 1.24 deprecation warnings #24865

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

Closed
sergiud opened this issue Jan 1, 2023 · 7 comments · Fixed by #24970
Closed

[Bug]: NumPy 1.24 deprecation warnings #24865

sergiud opened this issue Jan 1, 2023 · 7 comments · Fixed by #24970
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: confirmed bug topic: color/color & colormaps
Milestone

Comments

@sergiud
Copy link

sergiud commented Jan 1, 2023

Bug summary

Starting NumPy 1.24 I observe several deprecation warnings.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

plt.get_cmap()(np.empty((0, ), dtype=np.uint8))

Actual outcome

/usr/lib/python3.10/site-packages/matplotlib/colors.py:730: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 257 to uint8 will fail in the future.
For the old behavior, usually:
    np.array(value).astype(dtype)`
will give the desired result (the cast overflows).
  xa[xa > self.N - 1] = self._i_over
/usr/lib/python3.10/site-packages/matplotlib/colors.py:731: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 256 to uint8 will fail in the future.
For the old behavior, usually:
    np.array(value).astype(dtype)`
will give the desired result (the cast overflows).
  xa[xa < 0] = self._i_under
/usr/lib/python3.10/site-packages/matplotlib/colors.py:732: DeprecationWarning: NumPy will stop allowing conversion of out-of-bound Python integers to integer arrays.  The conversion of 258 to uint8 will fail in the future.
For the old behavior, usually:
    np.array(value).astype(dtype)`
will give the desired result (the cast overflows).
  xa[mask_bad] = self._i_bad

Expected outcome

No warnings.

Additional information

No response

Operating system

ArchLinux

Matplotlib Version

3.6.2

Matplotlib Backend

QtAgg

Python version

Python 3.10.9

Jupyter version

No response

Installation

Linux package manager

@dstansby
Copy link
Member

dstansby commented Jan 1, 2023

Thanks for the report! Unfortunately I can't reproduce this. What version of numpy are you using when the warning appears?

@sergiud
Copy link
Author

sergiud commented Jan 1, 2023

Sorry, forgot to mention that you need to enable the warnings during normal execution, e.g., python -W always <file>.py. In my case, the warnings are issued during pytest run which seems to activate these warnings by default.

As for the NumPy version, I'm running

$ python -c 'import numpy; print(numpy.__version__)'
1.24.0

@dstansby
Copy link
Member

dstansby commented Jan 1, 2023

Thanks, I can now reproduce 😄

@oscargus
Copy link
Member

oscargus commented Jan 10, 2023

The problem is that there are three more values, that are by default out of range in this case, to note specific cases:

self._i_under = self.N
self._i_over = self.N + 1
self._i_bad = self.N + 2

(N = 256 by default)

These are then assigned to out-of-range and masked/bad values here:

xa[xa > self.N - 1] = self._i_over
xa[xa < 0] = self._i_under
xa[mask_bad] = self._i_bad

which now raises a deprecation warning.

I think that one way forward would be to check the type of xa for int/uint and in that case take modulo the maximum value for self._i_over etc. This is basically what happens now anyway, but we need to do it explicitly rather than relying on numpy doing it. (I cannot really see how this makes sense from a color perspective, but we will at least keep the current behavior.)

@tacaswell
Copy link
Member

I think this is exposing a real bug that we need to promote the input data to be bigger than uint8. What we are doing here is buildin a lookup up table, the first N entries are for for values into the actually color map and then the next 3 entries are the special cases for over/under/bad so xa needs to be big enough to hold self.N + 2 as values.

@QuLogic QuLogic added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jan 13, 2023
@QuLogic
Copy link
Member

QuLogic commented Jan 13, 2023

I don't know if this is a bigger bug or not, but I would like us to have fixed any deprecation warnings from dependencies before 3.7 is out (or if necessary a quick 3.7.1.)

@dangersniru
Copy link

Change it to (np.array()).astype(np.int8) helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: confirmed bug topic: color/color & colormaps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants