Skip to content

Commit 05336d1

Browse files
authored
Remove warning for nvecs: (#99)
* Make debug level log for now * Remove test enforcement
1 parent bc24b67 commit 05336d1

9 files changed

+37
-80
lines changed

pyttb/ktensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Classes and functions for working with Kruskal tensors."""
66
from __future__ import annotations
77

8+
import logging
89
import warnings
910

1011
import numpy as np
@@ -1295,7 +1296,7 @@ def nvecs(self, n, r, flipsign=True):
12951296
v = v[:, (-np.abs(w)).argsort()]
12961297
v = v[:, :r]
12971298
else:
1298-
warnings.warn(
1299+
logging.debug(
12991300
"Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
13001301
)
13011302
w, v = scipy.linalg.eigh(y)

pyttb/sptensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Sparse Tensor Implementation"""
55
from __future__ import annotations
66

7+
import logging
78
import warnings
89
from collections.abc import Sequence
910
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:
934935
if r < y.shape[0] - 1:
935936
_, v = scipy.sparse.linalg.eigs(y, r)
936937
else:
937-
warnings.warn(
938+
logging.debug(
938939
"Greater than or equal to sptensor.shape[n] - 1 eigenvectors requires"
939940
" cast to dense to solve"
940941
)

pyttb/tensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""Dense Tensor Implementation"""
55
from __future__ import annotations
66

7-
import warnings
7+
import logging
88
from itertools import permutations
99
from math import factorial
1010
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:
790790
v = v[:, (-np.abs(w)).argsort()]
791791
v = v[:, :r]
792792
else:
793-
warnings.warn(
793+
logging.debug(
794794
"Greater than or equal to tensor.shape[n] - 1 eigenvectors"
795795
" requires cast to dense to solve"
796796
)

pyttb/ttensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the
33
# U.S. Government retains certain rights in this software.
44

5+
import logging
56
import textwrap
6-
import warnings
77

88
import numpy as np
99
import scipy
@@ -574,7 +574,7 @@ def nvecs(self, n, r, flipsign=True):
574574
v = v[:, (-np.abs(w)).argsort()]
575575
v = v[:, :r]
576576
else:
577-
warnings.warn(
577+
logging.debug(
578578
"Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
579579
)
580580
w, v = scipy.linalg.eigh(Y)

tests/test_cp_als.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ def test_cp_als_tensor_default_init(capsys, sample_tensor):
3838
@pytest.mark.indevelopment
3939
def test_cp_als_tensor_nvecs_init(capsys, sample_tensor):
4040
(data, T) = sample_tensor
41-
with pytest.warns(Warning) as record:
42-
(M, Minit, output) = ttb.cp_als(T, 1, init="nvecs")
43-
assert (
44-
"Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
45-
in str(record[0].message)
46-
)
41+
(M, Minit, output) = ttb.cp_als(T, 1, init="nvecs")
4742
capsys.readouterr()
4843
assert pytest.approx(output["fit"], 1) == 0
4944

@@ -87,12 +82,7 @@ def test_cp_als_sptensor_default_init(capsys, sample_sptensor):
8782
@pytest.mark.indevelopment
8883
def test_cp_als_sptensor_nvecs_init(capsys, sample_sptensor):
8984
(data, T) = sample_sptensor
90-
with pytest.warns(Warning) as record:
91-
(M, Minit, output) = ttb.cp_als(T, 1, init="nvecs")
92-
assert (
93-
"Greater than or equal to sptensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
94-
in str(record[0].message)
95-
)
85+
(M, Minit, output) = ttb.cp_als(T, 1, init="nvecs")
9686
capsys.readouterr()
9787
assert pytest.approx(output["fit"], 1) == 0
9888

tests/test_ktensor.py

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -710,47 +710,32 @@ def test_ktensor_normalize(sample_ktensor_2way, sample_ktensor_3way):
710710
def test_ktensor_nvecs(sample_ktensor_3way):
711711
(data, K) = sample_ktensor_3way
712712

713-
with pytest.warns(Warning) as record:
714-
assert np.allclose(
715-
K.nvecs(0, 1), np.array([[0.5731077440321353], [0.8194800264377384]])
716-
)
717-
assert (
718-
"Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
719-
in str(record[0].message)
713+
assert np.allclose(
714+
K.nvecs(0, 1), np.array([[0.5731077440321353], [0.8194800264377384]])
720715
)
721-
with pytest.warns(Warning) as record:
722-
assert np.allclose(
723-
K.nvecs(0, 2),
724-
np.array(
725-
[
726-
[0.5731077440321353, 0.8194800264377384],
727-
[0.8194800264377384, -0.5731077440321353],
728-
]
729-
),
730-
)
731-
assert (
732-
"Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
733-
in str(record[0].message)
716+
assert np.allclose(
717+
K.nvecs(0, 2),
718+
np.array(
719+
[
720+
[0.5731077440321353, 0.8194800264377384],
721+
[0.8194800264377384, -0.5731077440321353],
722+
]
723+
),
734724
)
735725

736726
assert np.allclose(
737727
K.nvecs(1, 1),
738728
np.array([[0.5048631426517823], [0.5745404391632514], [0.6442177356747206]]),
739729
)
740-
with pytest.warns(Warning) as record:
741-
assert np.allclose(
742-
K.nvecs(1, 2),
743-
np.array(
744-
[
745-
[0.5048631426517821, 0.7605567306550753],
746-
[0.5745404391632517, 0.0568912743440822],
747-
[0.6442177356747206, -0.6467741818894517],
748-
]
749-
),
750-
)
751-
assert (
752-
"Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
753-
in str(record[0].message)
730+
assert np.allclose(
731+
K.nvecs(1, 2),
732+
np.array(
733+
[
734+
[0.5048631426517821, 0.7605567306550753],
735+
[0.5745404391632517, 0.0568912743440822],
736+
[0.6442177356747206, -0.6467741818894517],
737+
]
738+
),
754739
)
755740

756741
assert np.allclose(
@@ -777,12 +762,7 @@ def test_ktensor_nvecs(sample_ktensor_3way):
777762
)
778763

779764
# Test for r >= N-1, requires cast to dense
780-
with pytest.warns(Warning) as record:
781-
K.nvecs(1, 3)
782-
assert (
783-
"Greater than or equal to ktensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
784-
in str(record[0].message)
785-
)
765+
K.nvecs(1, 3)
786766

787767

788768
@pytest.mark.indevelopment

tests/test_sptensor.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,16 +1625,11 @@ def test_sptensor_nvecs(sample_sptensor):
16251625
)
16261626

16271627
# Test for r >= N-1, requires cast to dense
1628-
with pytest.warns(Warning) as record:
1629-
ans = np.zeros((4, 3))
1630-
ans[3, 0] = 1
1631-
ans[2, 1] = 1
1632-
ans[1, 2] = 1
1633-
assert np.allclose((sptensorInstance.nvecs(1, 3)), ans)
1634-
assert (
1635-
"Greater than or equal to sptensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
1636-
in str(record[0].message)
1637-
)
1628+
ans = np.zeros((4, 3))
1629+
ans[3, 0] = 1
1630+
ans[2, 1] = 1
1631+
ans[1, 2] = 1
1632+
assert np.allclose((sptensorInstance.nvecs(1, 3)), ans)
16381633

16391634
# Negative test, check for only singleton dims
16401635
with pytest.raises(ValueError):

tests/test_tensor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,12 +1626,7 @@ def test_tensor_nvecs(sample_tensor_2way):
16261626
assert np.allclose((tensorInstance.nvecs(1, 1)), nv1)
16271627

16281628
# Test for r >= N-1, requires cast to dense
1629-
with pytest.warns(Warning) as record:
1630-
assert np.allclose((tensorInstance.nvecs(1, 2)), nv2)
1631-
assert (
1632-
"Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
1633-
in str(record[0].message)
1634-
)
1629+
assert np.allclose((tensorInstance.nvecs(1, 2)), nv2)
16351630

16361631

16371632
def test_tenones():

tests/test_ttensor.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,7 @@ def test_ttensor_nvecs(random_ttensor):
367367
n = 1
368368
r = 2
369369
full_eigvals = ttensorInstance.full().nvecs(n, r)
370-
with pytest.warns(Warning) as record:
371-
ttensor_eigvals = ttensorInstance.nvecs(n, r)
372-
assert (
373-
"Greater than or equal to tensor.shape[n] - 1 eigenvectors requires cast to dense to solve"
374-
in str(record[0].message)
375-
)
370+
ttensor_eigvals = ttensorInstance.nvecs(n, r)
376371
assert np.allclose(ttensor_eigvals, full_eigvals)
377372

378373
# Negative Tests

0 commit comments

Comments
 (0)