From b8042eed7a106fb1a5bec645f6a830d9f4615c4b Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 25 Jun 2024 17:48:32 -0400 Subject: [PATCH] Backport PR #28458: Correct numpy dtype comparisons in image_resample --- lib/matplotlib/tests/test_image.py | 17 +++++++++++++++++ src/_image_wrapper.cpp | 24 ++++++++++++------------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 1602f86716cb..a043d3aec983 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1585,3 +1585,20 @@ def test_non_transdata_image_does_not_touch_aspect(): assert ax.get_aspect() == 1 ax.imshow(im, transform=ax.transAxes, aspect=2) assert ax.get_aspect() == 2 + + +@pytest.mark.parametrize( + 'dtype', + ('float64', 'float32', 'int16', 'uint16', 'int8', 'uint8'), +) +@pytest.mark.parametrize('ndim', (2, 3)) +def test_resample_dtypes(dtype, ndim): + # Issue 28448, incorrect dtype comparisons in C++ image_resample can raise + # ValueError: arrays must be of dtype byte, short, float32 or float64 + rng = np.random.default_rng(4181) + shape = (2, 2) if ndim == 2 else (2, 2, 3) + data = rng.uniform(size=shape).astype(np.dtype(dtype, copy=True)) + fig, ax = plt.subplots() + axes_image = ax.imshow(data) + # Before fix the following raises ValueError for some dtypes. + axes_image.make_image(None)[0] diff --git a/src/_image_wrapper.cpp b/src/_image_wrapper.cpp index 65c8c8324ebc..856dcf4ea3ce 100644 --- a/src/_image_wrapper.cpp +++ b/src/_image_wrapper.cpp @@ -173,20 +173,20 @@ image_resample(py::array input_array, if (auto resampler = (ndim == 2) ? ( - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : nullptr) : ( // ndim == 3 - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : - (dtype.is(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : + (dtype.equal(py::dtype::of())) ? resample : nullptr)) { Py_BEGIN_ALLOW_THREADS resampler( 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