From 229c659df11cdfaba9c98930039b3a3651bf6ae4 Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Fri, 17 Jun 2022 07:12:24 +0200 Subject: [PATCH] TST: fix test issues with casting-related warnings with numpy >1.23 Closes gh-16403 Gbp-Pq: Name 0015-TST-fix-test-issues-with-casting-related-warnings-wi.patch --- scipy/io/_mmio.py | 7 +++++-- scipy/sparse/tests/test_sparsetools.py | 5 ++++- scipy/spatial/tests/test_distance.py | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/scipy/io/_mmio.py b/scipy/io/_mmio.py index f0260654..f3c28f85 100644 --- a/scipy/io/_mmio.py +++ b/scipy/io/_mmio.py @@ -13,6 +13,7 @@ import os import sys +import numpy as np from numpy import (asarray, real, imag, conj, zeros, ndarray, concatenate, ones, can_cast) @@ -382,8 +383,10 @@ class MMFile: else: if issymm and aij != aji: issymm = False - if isskew and aij != -aji: - isskew = False + with np.errstate(over="ignore"): + # This can give a warning for uint dtypes, so silence that + if isskew and aij != -aji: + isskew = False if isherm and aij != conj(aji): isherm = False if not (issymm or isskew or isherm): diff --git a/scipy/sparse/tests/test_sparsetools.py b/scipy/sparse/tests/test_sparsetools.py index 06d3fcf6..dcada59f 100644 --- a/scipy/sparse/tests/test_sparsetools.py +++ b/scipy/sparse/tests/test_sparsetools.py @@ -302,7 +302,10 @@ def test_upcast(): if np.issubdtype(b_dtype, np.complexfloating): b = b0.copy().astype(b_dtype) else: - b = b0.real.copy().astype(b_dtype) + with np.errstate(invalid="ignore"): + # Casting a large value (2**32) to int8 causes a warning in + # numpy >1.23 + b = b0.real.copy().astype(b_dtype) if not (a_dtype == np.bool_ and b_dtype == np.bool_): c = np.zeros((2,), dtype=np.bool_) diff --git a/scipy/spatial/tests/test_distance.py b/scipy/spatial/tests/test_distance.py index ab382191..0ebbe6a1 100644 --- a/scipy/spatial/tests/test_distance.py +++ b/scipy/spatial/tests/test_distance.py @@ -219,6 +219,10 @@ def _weight_masked(arrays, weights, axis): def _rand_split(arrays, weights, axis, split_per, seed=None): + # Coerce `arrays` to float64 if integer, to avoid nan-to-integer issues + arrays = [arr.astype(np.float64) if np.issubdtype(arr.dtype, np.integer) + else arr for arr in arrays] + # inverse operation for stats.collapse_weights weights = np.array(weights, dtype=np.float64) # modified inplace; need a copy seeded_rand = np.random.RandomState(seed) -- 2.30.2