Skip to content

Commit 0433ac5

Browse files
committed
[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.
1 parent a1b1464 commit 0433ac5

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

‎meegkit/utils/covariances.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
unsqueeze,
1414
)
1515

16-
rng = np.random.default_rng()
17-
1816

1917
def block_covariance(data, window=128, overlap=0.5, padding=True, estimator="cov"):
2018
"""Compute blockwise covariance.
@@ -486,9 +484,9 @@ def ehess(X, U):
486484
alpha * np.diagflat(mldivide(L, rhoX)) @ U
487485
return h
488486

489-
# Initialization as suggested in above referenced paper.
490-
# randomly generate starting point for svd
491-
x = rng.standard_normal((n, k))
487+
# Initialization as suggested in above referenced paper. Use a fixed seed
488+
# so results are reproducible (the starting point only affects convergence).
489+
x = np.random.default_rng(1468432035).standard_normal((n, k))
492490
[U, S, V] = linalg.svd(x, full_matrices=False)
493491
x = U.dot(V.T)
494492
S0, U0 = linalg.eig(

‎tests/test_cov.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
from numpy.testing import assert_almost_equal
33

44
from meegkit.utils import convmtx, tscov, tsxcov
5+
from meegkit.utils.covariances import nonlinear_eigenspace
56

67
rng = np.random.default_rng(10)
78

9+
10+
def test_nonlinear_eigenspace_reproducible():
11+
"""nonlinear_eigenspace returns identical results across calls."""
12+
A = rng.standard_normal((6, 6))
13+
L = A @ A.T + np.eye(6)
14+
d1, v1 = nonlinear_eigenspace(L, 6)
15+
d2, v2 = nonlinear_eigenspace(L, 6)
16+
np.testing.assert_array_equal(np.real(d1), np.real(d2))
17+
np.testing.assert_array_equal(v1, v2)
18+
819
def test_tscov():
920
"""Test time-shift covariance."""
1021
x = 2 * np.eye(3) + 0.1 * rng.random(3)

0 commit comments

Comments
 (0)