|
| 1 | +import itertools |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +import autofit as af |
| 7 | +import autofit.graphical as g |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(autouse=True) |
| 11 | +def reset_ids(): |
| 12 | + af.Prior._ids = itertools.count() |
| 13 | + |
| 14 | + |
| 15 | +class CountingAnalysis(af.ex.Analysis): |
| 16 | + """ |
| 17 | + An example `Analysis` that counts how many times the (notionally expensive) model |
| 18 | + data computation runs, so a test can prove the `FactorGraphModel` shared-state |
| 19 | + mechanism computes it once per evaluation rather than once per factor. |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, data, noise_map, share_model_data=True): |
| 23 | + super().__init__( |
| 24 | + data=data, noise_map=noise_map, share_model_data=share_model_data |
| 25 | + ) |
| 26 | + self.model_data_calls = 0 |
| 27 | + |
| 28 | + def model_data_1d_from(self, instance): |
| 29 | + self.model_data_calls += 1 |
| 30 | + return super().model_data_1d_from(instance=instance) |
| 31 | + |
| 32 | + |
| 33 | +def _shared_gaussian_graph(analyses): |
| 34 | + """ |
| 35 | + Build a `FactorGraphModel` whose factors share the *entire* Gaussian model via |
| 36 | + shared prior objects, so the model data is identical for every factor. |
| 37 | + """ |
| 38 | + centre = af.UniformPrior(lower_limit=0.0, upper_limit=10.0) |
| 39 | + normalization = af.UniformPrior(lower_limit=0.0, upper_limit=10.0) |
| 40 | + sigma = af.UniformPrior(lower_limit=0.0, upper_limit=10.0) |
| 41 | + |
| 42 | + factors = [] |
| 43 | + for analysis in analyses: |
| 44 | + gaussian = af.Model(af.ex.Gaussian) |
| 45 | + gaussian.centre = centre |
| 46 | + gaussian.normalization = normalization |
| 47 | + gaussian.sigma = sigma |
| 48 | + factors.append(af.AnalysisFactor(gaussian, analysis)) |
| 49 | + |
| 50 | + return g.FactorGraphModel(*factors) |
| 51 | + |
| 52 | + |
| 53 | +def _datasets(n=3, size=10): |
| 54 | + """`n` distinct 1D datasets sharing a common noise map of ones.""" |
| 55 | + return [ |
| 56 | + (np.arange(size, dtype=float) + float(i), np.ones(size)) |
| 57 | + for i in range(n) |
| 58 | + ] |
| 59 | + |
| 60 | + |
| 61 | +def _instance(collection): |
| 62 | + prior_count = collection.global_prior_model.prior_count |
| 63 | + return collection.global_prior_model.instance_from_unit_vector( |
| 64 | + [0.5] * prior_count |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def _reference_log_likelihood(collection, instance): |
| 69 | + """Sum each factor's likelihood with no sharing (each computes its own model data).""" |
| 70 | + return sum( |
| 71 | + factor.analysis.log_likelihood_function(instance_) |
| 72 | + for factor, instance_ in zip(collection.model_factors, instance) |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def test_shared_state_computed_once_per_evaluation(): |
| 77 | + analyses = [ |
| 78 | + CountingAnalysis(data, noise_map) for data, noise_map in _datasets(n=3) |
| 79 | + ] |
| 80 | + collection = _shared_gaussian_graph(analyses) |
| 81 | + instance = _instance(collection) |
| 82 | + |
| 83 | + collection.log_likelihood_function(instance) |
| 84 | + |
| 85 | + total_calls = sum(analysis.model_data_calls for analysis in analyses) |
| 86 | + assert total_calls == 1 |
| 87 | + |
| 88 | + |
| 89 | +def test_shared_likelihood_equals_unshared_sum(): |
| 90 | + analyses = [ |
| 91 | + CountingAnalysis(data, noise_map) for data, noise_map in _datasets(n=3) |
| 92 | + ] |
| 93 | + collection = _shared_gaussian_graph(analyses) |
| 94 | + instance = _instance(collection) |
| 95 | + |
| 96 | + shared_log_likelihood = collection.log_likelihood_function(instance) |
| 97 | + reference_log_likelihood = _reference_log_likelihood(collection, instance) |
| 98 | + |
| 99 | + assert shared_log_likelihood == pytest.approx(reference_log_likelihood) |
| 100 | + |
| 101 | + |
| 102 | +def test_no_provider_graph_is_unchanged(): |
| 103 | + """ |
| 104 | + With `share_model_data=False` no factor opts in, so no state is shared: each factor |
| 105 | + computes its own model data (N calls) and the summed likelihood is unchanged. |
| 106 | + """ |
| 107 | + analyses = [ |
| 108 | + CountingAnalysis(data, noise_map, share_model_data=False) |
| 109 | + for data, noise_map in _datasets(n=3) |
| 110 | + ] |
| 111 | + collection = _shared_gaussian_graph(analyses) |
| 112 | + instance = _instance(collection) |
| 113 | + |
| 114 | + log_likelihood = collection.log_likelihood_function(instance) |
| 115 | + reference_log_likelihood = _reference_log_likelihood(collection, instance) |
| 116 | + |
| 117 | + total_calls = sum(analysis.model_data_calls for analysis in analyses) |
| 118 | + # one call per factor from the graph evaluation, plus one per factor from the |
| 119 | + # reference sum — the graph did not share, so it computed all three itself. |
| 120 | + assert total_calls == 2 * len(analyses) |
| 121 | + assert log_likelihood == pytest.approx(reference_log_likelihood) |
| 122 | + |
| 123 | + |
| 124 | +def test_shared_state_from_default_returns_none(): |
| 125 | + analysis = af.ex.Analysis( |
| 126 | + data=np.arange(10, dtype=float), noise_map=np.ones(10) |
| 127 | + ) |
| 128 | + model = af.Model(af.ex.Gaussian) |
| 129 | + instance = model.instance_from_unit_vector([0.5] * model.prior_count) |
| 130 | + |
| 131 | + assert analysis.shared_state_from(instance) is None |
0 commit comments