From 7e2c728f672199688bc8a6496ff022e02e2a90da Mon Sep 17 00:00:00 2001 From: Adam Lugowski Date: Wed, 6 Sep 2023 21:05:27 -0700 Subject: [PATCH] Fix test warnings --- matrepr/adapters/sparse_impl.py | 7 ++++++- tests/test_sparse.py | 33 +++++++++++++++++++-------------- tests/test_str.py | 2 +- tests/test_tensorflow.py | 3 +++ tests/test_torch.py | 4 ++++ 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/matrepr/adapters/sparse_impl.py b/matrepr/adapters/sparse_impl.py index 8aad611..c5fe4f4 100644 --- a/matrepr/adapters/sparse_impl.py +++ b/matrepr/adapters/sparse_impl.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: BSD-2-Clause from typing import Any, Iterable, Tuple +import warnings import sparse from sparse import COO @@ -64,7 +65,11 @@ def __init__(self, mat): PyDataSparseBase.__init__(self, mat) def get_coo(self, row_range: Tuple[int, int], col_range: Tuple[int, int]) -> Iterable[Tuple[int, int, Any]]: - ret = COO(self.mat[slice(*row_range), slice(*col_range)]) + with warnings.catch_warnings(): + # COO will complain about a structure it itself created + warnings.simplefilter("ignore", category=DeprecationWarning, lineno=261) + + ret = COO(self.mat[slice(*row_range), slice(*col_range)]) ret.coords[0] += row_range[0] ret.coords[1] += col_range[0] return zip(ret.coords[0], ret.coords[1], ret.data) diff --git a/tests/test_sparse.py b/tests/test_sparse.py index 1d44e05..35b6859 100644 --- a/tests/test_sparse.py +++ b/tests/test_sparse.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: BSD-2-Clause import unittest +import warnings try: import sparse @@ -11,12 +12,12 @@ from matrepr import to_html, to_latex, to_str -import numpy.random -numpy.random.seed(123) +import scipy +import numpy as np +np.random.seed(123) def generate_fixed_value(m, n): - import scipy row_factor = 10**(1+len(str(n))) nnz = m*n rows, cols, data = [1] * nnz, [1] * nnz, [1] * nnz @@ -34,16 +35,20 @@ def generate_fixed_value(m, n): class PyDataSparseTests(unittest.TestCase): def setUp(self): self.mats = [ - sparse.COO([], shape=(0,)), - sparse.COO(coords=[1, 4], data=[11, 44], shape=(10,)), - sparse.COO([], shape=(0, 0)), - sparse.COO([], shape=(10, 10)), + sparse.COO(np.array([1])), + sparse.COO(coords=np.array([1, 4]), data=np.array([11, 44]), shape=(10,)), + sparse.COO(np.empty(shape=(10, 10))), sparse.random((10, 10), density=0.4), sparse.COO.from_scipy_sparse(generate_fixed_value(10, 10)), - sparse.COO(coords=[[0, 0], [0, 0]], data=[111, 222], shape=(13, 13)), - sparse.COO(coords=[[0, 1], [3, 2], [1, 3]], data=[111, 222], shape=(5, 5, 5)), + sparse.COO(coords=np.array([[0, 0], [0, 0]]), data=np.array([111, 222]), shape=(13, 13)), # has dupes + sparse.COO(coords=np.array([[0, 1], [3, 2], [1, 3]]), data=np.array([111, 222]), shape=(5, 5, 5)), ] + with warnings.catch_warnings(): + # COO will incorrectly complain that the object is not ndarray when it is. + warnings.simplefilter("ignore", category=DeprecationWarning, lineno=261) + self.mats.append(sparse.COO(np.empty(shape=(0, 0)))) + self.types = [ sparse.COO, sparse.DOK, @@ -86,14 +91,14 @@ def test_formats(self): def test_contents_1d(self): values = [1000, 1001, 1002, 1003, 1004] - vec = sparse.COO([0, 1, 2, 3, 4], data=values, shape=(10,)) + vec = sparse.COO(np.array([0, 1, 2, 3, 4]), data=np.array(values), shape=(10,)) res = to_html(vec, notebook=False, max_rows=20, max_cols=20, title=True, indices=True) for value in values: self.assertIn(f"{value}", res) def test_truncate_1d(self): values = [1000, 1001, 1002, 1003, 1009] - vec = sparse.COO([0, 1, 2, 3, 9], data=values, shape=(10,)) + vec = sparse.COO(np.array([0, 1, 2, 3, 9]), data=np.array(values), shape=(10,)) res = to_html(vec, notebook=False, max_rows=3, max_cols=3, num_after_dots=1, title=True, indices=True) for value in [1000, 1009]: self.assertIn(f"{value}", res) @@ -123,7 +128,7 @@ def test_truncate_2d(self): def test_contents_3d(self): values = [111, 222] - mat = sparse.COO(coords=[[0, 1], [3, 2], [1, 3]], data=values, shape=(5, 5, 5)) + mat = sparse.COO(coords=np.array([[0, 1], [3, 2], [1, 3]]), data=np.array(values), shape=(5, 5, 5)) res = to_html(mat, notebook=False, max_rows=20, max_cols=20, title=True, indices=True) res_str = to_str(mat) for value in values: @@ -137,7 +142,7 @@ def test_contents_3d(self): def test_truncate_3d(self): values = [111, 222] - mat = sparse.COO(coords=[[0, 10], [30, 2], [1, 30]], data=values, shape=(50, 50, 50)) + mat = sparse.COO(coords=np.array([[0, 10], [30, 2], [1, 30]]), data=np.array(values), shape=(50, 50, 50)) res = to_html(mat, notebook=False, max_rows=30, max_cols=3, num_after_dots=1) res_str = to_str(mat, max_rows=30, max_cols=3, num_after_dots=1) @@ -164,7 +169,7 @@ def test_no_comma_space(self): self.assertNotIn(" ,", res_str) def test_patch_sparse(self): - source_mat = sparse.COO(coords=[1, 4, 6], data=[11, 44, 222], shape=(10,)) + source_mat = sparse.COO(coords=np.array([1, 4, 6]), data=np.array([11, 44, 222]), shape=(10,)) # noinspection PyUnresolvedReferences import matrepr.patch.sparse diff --git a/tests/test_str.py b/tests/test_str.py index ee85118..ca99636 100644 --- a/tests/test_str.py +++ b/tests/test_str.py @@ -123,7 +123,7 @@ def test_tabulate_forward(self): mat = [[1, 2], [2000, 300000]] left = to_str(mat, colalign=["left", "left"]) right = to_str(mat, colalign=["right", "right"]) - self.assertNotEquals(left, right) + self.assertNotEqual(left, right) if __name__ == '__main__': diff --git a/tests/test_tensorflow.py b/tests/test_tensorflow.py index 1112850..281e2c9 100644 --- a/tests/test_tensorflow.py +++ b/tests/test_tensorflow.py @@ -3,8 +3,11 @@ # SPDX-License-Identifier: BSD-2-Clause import unittest +import warnings try: + # Suppress warning from inside tensorflow + warnings.filterwarnings("ignore", message="module 'sre_constants' is deprecated") import tensorflow as tf tf.random.set_seed(1234) diff --git a/tests/test_torch.py b/tests/test_torch.py index e381112..6972bb0 100644 --- a/tests/test_torch.py +++ b/tests/test_torch.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: BSD-2-Clause import unittest +import warnings import numpy as np @@ -30,6 +31,9 @@ def generate_fixed_value(m, n): @unittest.skipIf(torch is None, "PyTorch not installed") class PyTorchTests(unittest.TestCase): def setUp(self): + # filter beta state warning + warnings.filterwarnings("ignore", message="Sparse CSR tensor support is in beta state") + rand2d = torch.rand(50, 30) self.rand2d = rand2d[rand2d < 0.6] = 0