|
| 1 | +""" |
| 2 | +Tests for EPAnalysisFactor cavity-message injection. |
| 3 | +
|
| 4 | +EPAnalysisFactor is a thin AnalysisFactor subclass that exposes the EP |
| 5 | +cavity distribution to its Analysis on each fit, via the hook in |
| 6 | +``autofit.graphical.expectation_propagation.optimiser.factor_step``. |
| 7 | +The Analysis can then read per-variable cavity messages inside |
| 8 | +``log_likelihood_function`` — the canonical use case is a "global" |
| 9 | +Analysis that compares predictions to per-dataset posterior summaries |
| 10 | +produced by upstream local fits. |
| 11 | +""" |
| 12 | +from unittest.mock import MagicMock |
| 13 | + |
| 14 | +import autofit as af |
| 15 | +from autofit.graphical.expectation_propagation.optimiser import factor_step |
| 16 | +from autofit.graphical.utils import Status, StatusFlag |
| 17 | + |
| 18 | + |
| 19 | +class _RecordingAnalysis(af.Analysis): |
| 20 | + """Records every log_likelihood_function call's cavity state.""" |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + super().__init__() |
| 24 | + self.observed_cavities = [] |
| 25 | + |
| 26 | + def log_likelihood_function(self, instance): |
| 27 | + self.observed_cavities.append(getattr(self, "_cavity_mean_field", None)) |
| 28 | + return 0.0 |
| 29 | + |
| 30 | + |
| 31 | +def test_set_cavity_dist_attaches_to_analysis(): |
| 32 | + """``set_cavity_dist`` should populate ``analysis._cavity_mean_field``.""" |
| 33 | + model = af.Model(af.ex.Gaussian) |
| 34 | + analysis = _RecordingAnalysis() |
| 35 | + factor = af.EPAnalysisFactor(prior_model=model, analysis=analysis) |
| 36 | + |
| 37 | + sentinel = object() |
| 38 | + factor.set_cavity_dist(sentinel) |
| 39 | + |
| 40 | + assert analysis._cavity_mean_field is sentinel |
| 41 | + |
| 42 | + |
| 43 | +def test_plain_analysis_factor_has_no_set_cavity_dist(): |
| 44 | + """Plain AnalysisFactor must remain untouched by the hook.""" |
| 45 | + model = af.Model(af.ex.Gaussian) |
| 46 | + analysis = _RecordingAnalysis() |
| 47 | + factor = af.AnalysisFactor(prior_model=model, analysis=analysis) |
| 48 | + |
| 49 | + assert not hasattr(factor, "set_cavity_dist") |
| 50 | + |
| 51 | + |
| 52 | +def test_factor_step_invokes_set_cavity_dist(): |
| 53 | + """ |
| 54 | + ``factor_step`` should call ``set_cavity_dist`` before optimisation |
| 55 | + so the Analysis sees the cavity during every likelihood evaluation. |
| 56 | + """ |
| 57 | + model = af.Model(af.ex.Gaussian) |
| 58 | + analysis = _RecordingAnalysis() |
| 59 | + factor = af.EPAnalysisFactor(prior_model=model, analysis=analysis) |
| 60 | + |
| 61 | + cavity_sentinel = object() |
| 62 | + |
| 63 | + factor_approx = MagicMock() |
| 64 | + factor_approx.factor = factor |
| 65 | + factor_approx.cavity_dist = cavity_sentinel |
| 66 | + factor_approx.model_dist = MagicMock() |
| 67 | + |
| 68 | + optimiser = MagicMock() |
| 69 | + optimiser.optimise.return_value = ( |
| 70 | + MagicMock(), |
| 71 | + Status(success=True, messages=(), flag=StatusFlag.SUCCESS), |
| 72 | + ) |
| 73 | + |
| 74 | + factor_step(factor_approx, optimiser) |
| 75 | + |
| 76 | + assert analysis._cavity_mean_field is cavity_sentinel |
| 77 | + optimiser.optimise.assert_called_once_with(factor_approx) |
| 78 | + |
| 79 | + |
| 80 | +def test_factor_step_no_op_for_plain_analysis_factor(): |
| 81 | + """No exception should be raised for plain ``AnalysisFactor``.""" |
| 82 | + model = af.Model(af.ex.Gaussian) |
| 83 | + analysis = _RecordingAnalysis() |
| 84 | + factor = af.AnalysisFactor(prior_model=model, analysis=analysis) |
| 85 | + |
| 86 | + factor_approx = MagicMock() |
| 87 | + factor_approx.factor = factor |
| 88 | + factor_approx.cavity_dist = object() |
| 89 | + factor_approx.model_dist = MagicMock() |
| 90 | + |
| 91 | + optimiser = MagicMock() |
| 92 | + optimiser.optimise.return_value = ( |
| 93 | + MagicMock(), |
| 94 | + Status(success=True, messages=(), flag=StatusFlag.SUCCESS), |
| 95 | + ) |
| 96 | + |
| 97 | + factor_step(factor_approx, optimiser) |
| 98 | + |
| 99 | + assert not hasattr(analysis, "_cavity_mean_field") |
0 commit comments