From fa2ad8d2f280d523f723ce63fd44220f802a9c45 Mon Sep 17 00:00:00 2001 From: Stefan Appelhoff Date: Mon, 13 Jul 2026 19:04:12 +0200 Subject: [PATCH] [FIX] ASR: make nonlinear_eigenspace reproducible nonlinear_eigenspace drew its optimization starting point from an unseeded module-level Generator, so the riemann ASR path produced slightly different results on every call. The starting point only affects convergence, not the resulting eigenspace, so seed it with a fixed value. This makes the riemann ASR output reproducible run-to-run and stabilizes the incremental-vs-bulk equivalence check in test_asr_class. Adds a regression test asserting repeated calls return identical results. --- meegkit/utils/covariances.py | 8 +++----- tests/test_cov.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/meegkit/utils/covariances.py b/meegkit/utils/covariances.py index 28e10a61..71ca5b1f 100644 --- a/meegkit/utils/covariances.py +++ b/meegkit/utils/covariances.py @@ -13,8 +13,6 @@ unsqueeze, ) -rng = np.random.default_rng() - def block_covariance(data, window=128, overlap=0.5, padding=True, estimator="cov"): """Compute blockwise covariance. @@ -486,9 +484,9 @@ def ehess(X, U): alpha * np.diagflat(mldivide(L, rhoX)) @ U return h - # Initialization as suggested in above referenced paper. - # randomly generate starting point for svd - x = rng.standard_normal((n, k)) + # Initialization as suggested in above referenced paper. Use a fixed seed + # so results are reproducible (the starting point only affects convergence). + x = np.random.default_rng(1468432035).standard_normal((n, k)) [U, S, V] = linalg.svd(x, full_matrices=False) x = U.dot(V.T) S0, U0 = linalg.eig( diff --git a/tests/test_cov.py b/tests/test_cov.py index ff24e496..0ca46108 100644 --- a/tests/test_cov.py +++ b/tests/test_cov.py @@ -2,9 +2,20 @@ from numpy.testing import assert_almost_equal from meegkit.utils import convmtx, tscov, tsxcov +from meegkit.utils.covariances import nonlinear_eigenspace rng = np.random.default_rng(10) + +def test_nonlinear_eigenspace_reproducible(): + """nonlinear_eigenspace returns identical results across calls.""" + A = rng.standard_normal((6, 6)) + L = A @ A.T + np.eye(6) + d1, v1 = nonlinear_eigenspace(L, 6) + d2, v2 = nonlinear_eigenspace(L, 6) + np.testing.assert_array_equal(np.real(d1), np.real(d2)) + np.testing.assert_array_equal(v1, v2) + def test_tscov(): """Test time-shift covariance.""" x = 2 * np.eye(3) + 0.1 * rng.random(3)