From 59d08fbba6af7d199c457fe98be86ba8b667e4f3 Mon Sep 17 00:00:00 2001 From: Nick Johnson <24689722+ntjohnson1@users.noreply.github.com> Date: Wed, 26 Apr 2023 08:10:20 -0400 Subject: [PATCH] Remove warning for nvecs: * Make debug level log for now * Remove test enforcement --- pyttb/ktensor.py | 3 ++- pyttb/sptensor.py | 3 ++- pyttb/tensor.py | 4 +-- pyttb/ttensor.py | 4 +-- tests/test_cp_als.py | 14 ++-------- tests/test_ktensor.py | 60 ++++++++++++++---------------------------- tests/test_sptensor.py | 15 ++++------- tests/test_tensor.py | 7 +---- tests/test_ttensor.py | 7 +---- 9 files changed, 37 insertions(+), 80 deletions(-) diff --git a/pyttb/ktensor.py b/pyttb/ktensor.py index 7860158f..72c6b0bc 100644 --- a/pyttb/ktensor.py +++ b/pyttb/ktensor.py @@ -5,6 +5,7 @@ """Classes and functions for working with Kruskal tensors.""" from __future__ import annotations +import logging import warnings import numpy as np @@ -1295,7 +1296,7 @@ def nvecs(self, n, r, flipsign=True): v = v[:, (-np.abs(w)).argsort()] v = v[:, :r] else: - warnings.warn( + logging.debug( "Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve" ) w, v = scipy.linalg.eigh(y) diff --git a/pyttb/sptensor.py b/pyttb/sptensor.py index fda2e5a5..53946138 100644 --- a/pyttb/sptensor.py +++ b/pyttb/sptensor.py @@ -4,6 +4,7 @@ """Sparse Tensor Implementation""" from __future__ import annotations +import logging import warnings from collections.abc import Sequence from typing import Any, Callable, List, Optional, Tuple, Union, cast, overload @@ -934,7 +935,7 @@ def nvecs(self, n: int, r: int, flipsign: bool = True) -> np.ndarray: if r < y.shape[0] - 1: _, v = scipy.sparse.linalg.eigs(y, r) else: - warnings.warn( + logging.debug( "Greater than or equal to sptensor.shape[n] - 1 eigenvectors requires" " cast to dense to solve" ) diff --git a/pyttb/tensor.py b/pyttb/tensor.py index 5c4c6d8b..50a245dd 100644 --- a/pyttb/tensor.py +++ b/pyttb/tensor.py @@ -4,7 +4,7 @@ """Dense Tensor Implementation""" from __future__ import annotations -import warnings +import logging from itertools import permutations from math import factorial from typing import Any, Callable, List, Optional, Tuple, Union @@ -790,7 +790,7 @@ def nvecs(self, n: int, r: int, flipsign: bool = True) -> np.ndarray: v = v[:, (-np.abs(w)).argsort()] v = v[:, :r] else: - warnings.warn( + logging.debug( "Greater than or equal to tensor.shape[n] - 1 eigenvectors" " requires cast to dense to solve" ) diff --git a/pyttb/ttensor.py b/pyttb/ttensor.py index 238351ce..50b080de 100644 --- a/pyttb/ttensor.py +++ b/pyttb/ttensor.py @@ -2,8 +2,8 @@ # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. +import logging import textwrap -import warnings import numpy as np import scipy @@ -574,7 +574,7 @@ def nvecs(self, n, r, flipsign=True): v = v[:, (-np.abs(w)).argsort()] v = v[:, :r] else: - warnings.warn( + logging.debug( "Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve" ) w, v = scipy.linalg.eigh(Y) diff --git a/tests/test_cp_als.py b/tests/test_cp_als.py index 44a8679d..a7b141c2 100644 --- a/tests/test_cp_als.py +++ b/tests/test_cp_als.py @@ -38,12 +38,7 @@ def test_cp_als_tensor_default_init(capsys, sample_tensor): @pytest.mark.indevelopment def test_cp_als_tensor_nvecs_init(capsys, sample_tensor): (data, T) = sample_tensor - with pytest.warns(Warning) as record: - (M, Minit, output) = ttb.cp_als(T, 1, init="nvecs") - assert ( - "Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) - ) + (M, Minit, output) = ttb.cp_als(T, 1, init="nvecs") capsys.readouterr() assert pytest.approx(output["fit"], 1) == 0 @@ -87,12 +82,7 @@ def test_cp_als_sptensor_default_init(capsys, sample_sptensor): @pytest.mark.indevelopment def test_cp_als_sptensor_nvecs_init(capsys, sample_sptensor): (data, T) = sample_sptensor - with pytest.warns(Warning) as record: - (M, Minit, output) = ttb.cp_als(T, 1, init="nvecs") - assert ( - "Greater than or equal to sptensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) - ) + (M, Minit, output) = ttb.cp_als(T, 1, init="nvecs") capsys.readouterr() assert pytest.approx(output["fit"], 1) == 0 diff --git a/tests/test_ktensor.py b/tests/test_ktensor.py index e4f490ce..861a16fc 100644 --- a/tests/test_ktensor.py +++ b/tests/test_ktensor.py @@ -710,47 +710,32 @@ def test_ktensor_normalize(sample_ktensor_2way, sample_ktensor_3way): def test_ktensor_nvecs(sample_ktensor_3way): (data, K) = sample_ktensor_3way - with pytest.warns(Warning) as record: - assert np.allclose( - K.nvecs(0, 1), np.array([[0.5731077440321353], [0.8194800264377384]]) - ) - assert ( - "Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) + assert np.allclose( + K.nvecs(0, 1), np.array([[0.5731077440321353], [0.8194800264377384]]) ) - with pytest.warns(Warning) as record: - assert np.allclose( - K.nvecs(0, 2), - np.array( - [ - [0.5731077440321353, 0.8194800264377384], - [0.8194800264377384, -0.5731077440321353], - ] - ), - ) - assert ( - "Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) + assert np.allclose( + K.nvecs(0, 2), + np.array( + [ + [0.5731077440321353, 0.8194800264377384], + [0.8194800264377384, -0.5731077440321353], + ] + ), ) assert np.allclose( K.nvecs(1, 1), np.array([[0.5048631426517823], [0.5745404391632514], [0.6442177356747206]]), ) - with pytest.warns(Warning) as record: - assert np.allclose( - K.nvecs(1, 2), - np.array( - [ - [0.5048631426517821, 0.7605567306550753], - [0.5745404391632517, 0.0568912743440822], - [0.6442177356747206, -0.6467741818894517], - ] - ), - ) - assert ( - "Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) + assert np.allclose( + K.nvecs(1, 2), + np.array( + [ + [0.5048631426517821, 0.7605567306550753], + [0.5745404391632517, 0.0568912743440822], + [0.6442177356747206, -0.6467741818894517], + ] + ), ) assert np.allclose( @@ -777,12 +762,7 @@ def test_ktensor_nvecs(sample_ktensor_3way): ) # Test for r >= N-1, requires cast to dense - with pytest.warns(Warning) as record: - K.nvecs(1, 3) - assert ( - "Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) - ) + K.nvecs(1, 3) @pytest.mark.indevelopment diff --git a/tests/test_sptensor.py b/tests/test_sptensor.py index 57c8b2f6..17c90385 100644 --- a/tests/test_sptensor.py +++ b/tests/test_sptensor.py @@ -1625,16 +1625,11 @@ def test_sptensor_nvecs(sample_sptensor): ) # Test for r >= N-1, requires cast to dense - with pytest.warns(Warning) as record: - ans = np.zeros((4, 3)) - ans[3, 0] = 1 - ans[2, 1] = 1 - ans[1, 2] = 1 - assert np.allclose((sptensorInstance.nvecs(1, 3)), ans) - assert ( - "Greater than or equal to sptensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) - ) + ans = np.zeros((4, 3)) + ans[3, 0] = 1 + ans[2, 1] = 1 + ans[1, 2] = 1 + assert np.allclose((sptensorInstance.nvecs(1, 3)), ans) # Negative test, check for only singleton dims with pytest.raises(ValueError): diff --git a/tests/test_tensor.py b/tests/test_tensor.py index 459a1ad5..e034cc70 100644 --- a/tests/test_tensor.py +++ b/tests/test_tensor.py @@ -1626,12 +1626,7 @@ def test_tensor_nvecs(sample_tensor_2way): assert np.allclose((tensorInstance.nvecs(1, 1)), nv1) # Test for r >= N-1, requires cast to dense - with pytest.warns(Warning) as record: - assert np.allclose((tensorInstance.nvecs(1, 2)), nv2) - assert ( - "Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) - ) + assert np.allclose((tensorInstance.nvecs(1, 2)), nv2) def test_tenones(): diff --git a/tests/test_ttensor.py b/tests/test_ttensor.py index 090f3400..e1e7dd9e 100644 --- a/tests/test_ttensor.py +++ b/tests/test_ttensor.py @@ -367,12 +367,7 @@ def test_ttensor_nvecs(random_ttensor): n = 1 r = 2 full_eigvals = ttensorInstance.full().nvecs(n, r) - with pytest.warns(Warning) as record: - ttensor_eigvals = ttensorInstance.nvecs(n, r) - assert ( - "Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve" - in str(record[0].message) - ) + ttensor_eigvals = ttensorInstance.nvecs(n, r) assert np.allclose(ttensor_eigvals, full_eigvals) # Negative Tests