Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove warning for nvecs: #99

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyttb/ktensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""Classes and functions for working with Kruskal tensors."""
from __future__ import annotations

import logging
import warnings

import numpy as np
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pyttb/sptensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions pyttb/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
)
Expand Down
4 changes: 2 additions & 2 deletions pyttb/ttensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 2 additions & 12 deletions tests/test_cp_als.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
60 changes: 20 additions & 40 deletions tests/test_ktensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
15 changes: 5 additions & 10 deletions tests/test_sptensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 1 addition & 6 deletions tests/test_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
7 changes: 1 addition & 6 deletions tests/test_ttensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down