Skip to content
Draft
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
15 changes: 7 additions & 8 deletions tests/core/grf/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import glass.grf

if TYPE_CHECKING:
from collections.abc import Callable
from types import ModuleType

from pytest_mock import MockerFixture


def test_corr_unknown(xp: ModuleType) -> None:
@pytest.mark.parametrize(
"corr_func",
[glass.grf.corr, glass.grf.icorr, glass.grf.dcorr],
)
def test_corr_unknown(xp: ModuleType, corr_func: Callable) -> None:
class Unknown:
def corr(self, _other, _x): # type: ignore[no-untyped-def]
return NotImplemented
Expand All @@ -28,13 +33,7 @@ def dcorr(self, _other, _x): # type: ignore[no-untyped-def]
x = xp.zeros(10)

with pytest.raises(NotImplementedError, match="Unknown"):
glass.grf.corr(t1, t2, x)

with pytest.raises(NotImplementedError, match="Unknown"):
glass.grf.icorr(t1, t2, x)

with pytest.raises(NotImplementedError, match="Unknown"):
glass.grf.dcorr(t1, t2, x)
corr_func(t1, t2, x)


def test_compute(mocker: MockerFixture, xp: ModuleType) -> None:
Expand Down
50 changes: 30 additions & 20 deletions tests/core/test_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def test_cov_clip(
compare.assert_allclose(xp.linalg.eigvalsh(cov), h)


def test_nearcorr(compare: type[Compare], xp: ModuleType) -> None:
@pytest.mark.parametrize("tol", [None, 1e-10])
def test_nearcorr_correctness(
compare: type[Compare], xp: ModuleType, tol: float | None
) -> None:
"""Test glass.algorithm.nearcorr against the known result from Higham (2002)."""
# from Higham (2002)
a = xp.asarray(
[
Expand All @@ -98,32 +102,38 @@ def test_nearcorr(compare: type[Compare], xp: ModuleType) -> None:
],
)

x = glass.algorithm.nearcorr(a)
x = glass.algorithm.nearcorr(a, tol=tol)
compare.assert_allclose(x, b, atol=1e-4)

# explicit tolerance
x = glass.algorithm.nearcorr(a, tol=1e-10)
compare.assert_allclose(x, b, atol=1e-4)

# no iterations
with pytest.warns(
UserWarning,
match="Nearest correlation matrix not found in 0 iterations",
):
x = glass.algorithm.nearcorr(a, niter=0)
compare.assert_allclose(x, a)
@pytest.mark.parametrize("niter", [0, 1])
def test_nearcorr_no_convergence(
compare: type[Compare], xp: ModuleType, niter: int
) -> None:
"""Test that a UserWarning is raised when the solution does not converge."""
a = xp.asarray(
[
[1.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
],
)

match_msg = f"Nearest correlation matrix not found in {niter} iterations"
with pytest.warns(UserWarning, match=match_msg):
x = glass.algorithm.nearcorr(a, niter=niter)

# non-square matrix should raise
# should be unchanged
if niter == 0:
compare.assert_allclose(x, a)


def test_nearcorr_non_square(xp: ModuleType) -> None:
"""Test that a ValueError is raised for non-square input matrices."""
# Assert: Check for the specific ValueError
with pytest.raises(ValueError, match="non-square matrix"):
glass.algorithm.nearcorr(xp.zeros((4, 3)))

# no convergence
with pytest.warns(
UserWarning,
match="Nearest correlation matrix not found in 1 iterations",
):
x = glass.algorithm.nearcorr(a, niter=1)


def test_cov_nearest(
compare: type[Compare],
Expand Down