From 0a215094f657fdc68b834f7363473c8b74691abc Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 10 Jul 2026 20:11:24 +0100 Subject: [PATCH] fix(graphical): seed inherently-stochastic test_full_hierachical test_full_hierachical fits a marginal hierarchical EP model whose Laplace refinement (n_refine=3) draws samples from the global np.random state via NormalMessage.sample, so its recovered mu_logt hyperparameter converges to a good value or a sigma-collapsed one depending on the ambient RNG state left by whatever ran before it. The test seeded np.random only before data generation, not before the fit. When #1351 added test_ep_statistics_fixes.py, its Monte-Carlo KL-direction tests consumed np.random and shifted the ambient state into a failing region, turning the Tests workflow red on main (green at #1347 -> red at #1351/#1354). The fit's math is unchanged; this is a test-determinism fix. Seed np.random immediately before the fit so the test is reproducible. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01DgCFQoGUbVkspbCjRuKnHJ --- test_autofit/graphical/hierarchical/test_hierarchical.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test_autofit/graphical/hierarchical/test_hierarchical.py b/test_autofit/graphical/hierarchical/test_hierarchical.py index fcb0b23a1..90fe3db20 100644 --- a/test_autofit/graphical/hierarchical/test_hierarchical.py +++ b/test_autofit/graphical/hierarchical/test_hierarchical.py @@ -298,6 +298,14 @@ def test_full_hierachical(data): }, ) + # This fits a *marginal* hierarchical EP model whose Laplace refinement draws + # stochastic samples from the global np.random state (n_refine=3 via + # NormalMessage.sample). The fit sits near a convergence boundary, so its + # recovered fixed point (good vs sigma-collapse) depends on the ambient RNG + # state left by whatever ran before — an inherently stochastic test. Seed here + # so the fit is reproducible; this is the pragmatic fix (see PyAutoFit #1352). + np.random.seed(0) + laplace = graph.LaplaceOptimiser() ep_opt = graph.EPOptimiser(model, default_optimiser=laplace) new_approx = ep_opt.run(model_approx, max_steps=10)