TST: fix test issues with casting-related warnings with numpy >1.23
authorRalf Gommers <ralf.gommers@gmail.com>
Fri, 17 Jun 2022 05:12:24 +0000 (07:12 +0200)
committerDrew Parsons <dparsons@debian.org>
Wed, 25 Jan 2023 14:32:10 +0000 (14:32 +0000)
Closes gh-16403

Gbp-Pq: Name 0015-TST-fix-test-issues-with-casting-related-warnings-wi.patch

scipy/io/_mmio.py
scipy/sparse/tests/test_sparsetools.py
scipy/spatial/tests/test_distance.py

index f0260654992e405b8995da174c51716c1539126c..f3c28f85a652a50a1a57d9d1961ee2313bc8d23d 100644 (file)
@@ -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):
index 06d3fcf6199a5bc39a1ab5c45ae5b39e29504905..dcada59f5b75999616b2ce2f739ea97acca83d39 100644 (file)
@@ -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_)
index ab3821919895e00e7a4ab835d45216eceaf0caa1..0ebbe6a17236753db58c41f7f7aaec777aa2edcb 100644 (file)
@@ -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)