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
10 changes: 10 additions & 0 deletions autofit/mapper/prior/arithmetic/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def __init__(self, lower, greater, name=""):
super().__init__(lower, greater)
self._name = name

@property
def name(self):
"""The optional label supplied when this assertion was added."""
return self._name

def __gt__(self, other):
return CompoundAssertion(self, self._left > other)

Expand Down Expand Up @@ -92,6 +97,11 @@ def __init__(self, assertion_1, assertion_2, name=""):
self.assertion_2 = assertion_2
self._name = name

@property
def name(self):
"""The optional label supplied when this assertion was added."""
return self._name

def _instance_for_arguments(
self,
arguments,
Expand Down
2 changes: 1 addition & 1 deletion autofit/mapper/prior_model/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def add_assertion(self, assertion, name=""):
if assertion is True:
return
try:
assertion.name = name
assertion._name = name
except AttributeError:
pass
self._assertions.append(assertion)
Expand Down
5 changes: 2 additions & 3 deletions test_autofit/graph_spec/test_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ def test_06_assertion():
assert edge.op == "<"
assert edge.left == "0.5"
assert edge.right == "sigma"
# `add_assertion(name=)` is silently dropped by `AbstractPriorModel.name`
# having no setter (a separately filed bug), so the name is absent.
assert edge.name is None
# The graph carries the user-supplied assertion label.
assert edge.name == "sigma is large"

# Assertions are edges: not in the tree, and not in `model.info`.
assert [row.name for row in spec.root.rows] == ["centre", "normalization", "sigma"]
Expand Down
35 changes: 35 additions & 0 deletions test_autofit/mapper/prior/test_assertion_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import autofit as af
import pytest


@pytest.mark.parametrize("chained", [False, True])
@pytest.mark.parametrize("name", ["", "sigma_floor"])
def test_added_assertion_name_and_violation(chained, name):
model = af.Model(af.ex.Gaussian)
assertion = (5.0 < model.sigma) < 10.0 if chained else model.sigma > 5.0
model.add_assertion(assertion, name=name)
assert model.gathered_assertions()[0].name == name
with pytest.raises(af.exc.FitException) as error:
model.check_assertions({model.sigma: 1.0})
assert name in str(error.value)


def test_name_is_presentation_only():
model = af.Model(af.ex.Gaussian)
assertion = model.sigma > 5.0
model.add_assertion(assertion)
identifier = model.identifier
assertion._name = "sigma_floor"
assert assertion.name == "sigma_floor"
assert model.identifier == identifier
assert model.name == "Gaussian"
assert af.Collection().name == "Collection"


def test_boolean_assertions_keep_their_behaviour():
model = af.Model(af.ex.Gaussian)
model.add_assertion(True, name="true")
assert model.gathered_assertions() == []
model.add_assertion(False, name="false")
with pytest.raises(af.exc.FitException):
model.check_assertions({})
Loading