|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import autofit as af |
| 6 | +from autofit.mapper.prior.arithmetic.compound import NegativePrior |
| 7 | +from autonerves.dictable import to_dict, from_dict |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture(name="model") |
| 11 | +def make_model(): |
| 12 | + """ |
| 13 | + A model where two priors are shared by two components through arithmetic, |
| 14 | + like a stellar mass ``(1 - f) * m`` and a dark mass ``f * m``. |
| 15 | + """ |
| 16 | + m = af.UniformPrior(lower_limit=0.0, upper_limit=10.0) |
| 17 | + f = af.UniformPrior(lower_limit=0.0, upper_limit=1.0) |
| 18 | + |
| 19 | + stars = af.Model(af.ex.Gaussian) |
| 20 | + halo = af.Model(af.ex.Gaussian) |
| 21 | + |
| 22 | + stars.normalization = (1.0 - f) * m |
| 23 | + halo.normalization = f * m |
| 24 | + |
| 25 | + for component in (stars, halo): |
| 26 | + component.centre = 50.0 |
| 27 | + component.sigma = 10.0 |
| 28 | + |
| 29 | + return af.Collection(stars=stars, halo=halo) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(name="loaded") |
| 33 | +def make_loaded(model): |
| 34 | + return from_dict(json.loads(json.dumps(to_dict(model)))) |
| 35 | + |
| 36 | + |
| 37 | +def test_dict_is_json_serialisable(model): |
| 38 | + json.dumps(to_dict(model)) |
| 39 | + |
| 40 | + |
| 41 | +def test_round_trip_keeps_shared_priors(model, loaded): |
| 42 | + assert loaded.prior_count == model.prior_count == 2 |
| 43 | + |
| 44 | + |
| 45 | +def test_round_trip_keeps_identifier(model, loaded): |
| 46 | + assert loaded.identifier == model.identifier |
| 47 | + |
| 48 | + |
| 49 | +def test_round_trip_gives_same_instance(model, loaded): |
| 50 | + values = {"m": 7.0, "f": 0.3} |
| 51 | + |
| 52 | + def instance(m): |
| 53 | + arguments = { |
| 54 | + prior: values[prior_name(m, prior)] for prior in m.priors_ordered_by_id |
| 55 | + } |
| 56 | + return m.instance_for_arguments(arguments) |
| 57 | + |
| 58 | + def prior_name(m, prior): |
| 59 | + (name,) = {path[-1] for path, p in m.path_priors_tuples if p is prior} |
| 60 | + return name |
| 61 | + |
| 62 | + original = instance(model) |
| 63 | + reloaded = instance(loaded) |
| 64 | + |
| 65 | + assert reloaded.stars.normalization == pytest.approx(original.stars.normalization) == 4.9 |
| 66 | + assert reloaded.halo.normalization == pytest.approx(original.halo.normalization) == 2.1 |
| 67 | + |
| 68 | + |
| 69 | +def test_modified_prior_of_a_prior_serialises(): |
| 70 | + f = af.UniformPrior(lower_limit=0.0, upper_limit=1.0) |
| 71 | + negative = -f |
| 72 | + |
| 73 | + assert isinstance(negative, NegativePrior) |
| 74 | + assert negative.dict()["prior"]["type"] == "Uniform" |
| 75 | + assert negative.cls is float |
| 76 | + |
| 77 | + loaded = af.ModelObject.from_dict(json.loads(json.dumps(negative.dict()))) |
| 78 | + assert isinstance(loaded, NegativePrior) |
| 79 | + assert loaded.instance_from_prior_medians() == -0.5 |
| 80 | + |
| 81 | + |
| 82 | +def test_info(model): |
| 83 | + assert "MultiplePrior" in model.info |
| 84 | + |
| 85 | + |
| 86 | +def test_dict_without_operand_names_loads(): |
| 87 | + d = { |
| 88 | + "type": "compound", |
| 89 | + "compound_type": "SumPrior", |
| 90 | + "left": 1.0, |
| 91 | + "right": {"type": "Uniform", "id": 0, "lower_limit": 0.0, "upper_limit": 1.0}, |
| 92 | + } |
| 93 | + loaded = af.ModelObject.from_dict(d) |
| 94 | + assert loaded.instance_from_prior_medians() == 1.5 |
0 commit comments