Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion autofit/mapper/model_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,16 @@ def dict(self) -> dict:

if isinstance(self, Collection):
type_ = "collection"
elif isinstance(self, AbstractPriorModel) and self.prior_count == 0:
elif (
isinstance(self, Model)
and self.__dict__.get("_serialize_as_instance", False)
and self.prior_count == 0
):
type_ = "instance"
elif isinstance(self, Model):
type_ = "model"
elif isinstance(self, AbstractPriorModel) and self.prior_count == 0:
type_ = "instance"
elif isinstance(self, TuplePrior):
type_ = "tuple_prior"
elif isinstance(self, Array):
Expand Down
4 changes: 4 additions & 0 deletions autofit/mapper/prior_model/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,10 @@ def from_instance(
)
except AttributeError:
return instance
# Preserve the instance round-trip contract for converted objects.
# Explicit Model(...) components remain models even when all fixed.
# Private provenance is excluded from identifiers and serialized args.
result._serialize_as_instance = True
if isinstance(instance, model_classes):
return result.as_model()
return result
Expand Down
57 changes: 57 additions & 0 deletions test_autofit/graph_spec/test_json_roundtrip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json

import autofit as af
from autofit.graph_spec import GraphSpec
from autofit.mapper.identifier import Identifier


class Parameterless:
def __init__(self):
pass


def semantic_graph(value):
# GraphSpec exposes process-local object/prior counters. JSON has never
# preserved these; compare every semantic field, separately pin folder IDs.
if isinstance(value, dict):
return {
k: semantic_graph(v)
for k, v in value.items()
if k not in {"obj_id", "prior_id"}
}
if isinstance(value, list):
return [semantic_graph(v) for v in value]
return value


def test_fixed_models_survive_json(tmp_path):
model = af.Collection(
fixed=af.Model(af.ex.Gaussian, centre=0.0, normalization=0.1, sigma=0.01),
empty=af.Model(Parameterless),
free=af.Model(af.ex.Gaussian),
)
original_id = str(Identifier(model))
original_graph = GraphSpec.from_model(model).to_dict()
path = tmp_path / "model.json"
path.write_text(json.dumps(model.dict()))
reloaded = af.Model.from_json(path)
assert isinstance(reloaded.fixed, af.Model)
assert isinstance(reloaded.empty, af.Model)
assert semantic_graph(GraphSpec.from_model(reloaded).to_dict()) == semantic_graph(
original_graph
)
assert str(Identifier(model)) == original_id
assert str(Identifier(reloaded)) == original_id
before = af.Nautilus(name="roundtrip")
before.paths.model = model
after = af.Nautilus(name="roundtrip")
after.paths.model = reloaded
assert after.paths.identifier == before.paths.identifier
assert after.paths.output_path == before.paths.output_path


def test_fixed_model_identifier_compatibility():
model = af.Model(af.ex.Gaussian, centre=0.0, normalization=0.1, sigma=0.01)
assert model.dict()["type"] == "model"
# Pinned from unmodified main, before the serialization repair.
assert str(Identifier(model)) == "47764ddfa16f140e6596b381544285c4"
2 changes: 1 addition & 1 deletion test_autofit/mapper/model/serialise/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_model_priors(self, model, model_dict, remove_ids):
def test_model_floats(self, instance_dict):
model = af.Model(af.ex.Gaussian, centre=0.0, normalization=0.1, sigma=0.01)

assert model.dict() == instance_dict
assert model.dict() == {**instance_dict, "type": "model"}

def test_collection(self, model, collection_dict, remove_ids):
collection = af.Collection(gaussian=model)
Expand Down
Loading