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)