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
29 changes: 25 additions & 4 deletions autofit/mapper/prior_model/prior_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing
from typing import *

from autonerves import conf
from autonerves.class_path import get_class_path
from autonerves.exc import ConfigException
from autofit.mapper.model import ModelInstance, assert_not_frozen, frozen_cache
Expand Down Expand Up @@ -408,7 +409,24 @@ def make_prior(self, attribute_name):
return cls
if not inspect.isclass(cls):
# noinspection PyProtectedMember
cls = inspect._findclass(cls)
owner = inspect._findclass(cls)
if owner is None:
# A plain function has its own config path, but no class MRO.
# The class lookup walks __bases__, so do not send it a function
# (or None). Keep the actual model callable unchanged.
path = f"{cls.__module__}.{cls.__name__}".split(".")
for config in conf.instance.prior_config.prior_configs:
try:
prior_config = config(path + [attribute_name])
except KeyError:
continue
return Prior.from_dict(prior_config)
raise ConfigException(
f"No prior config found for function {cls.__module__}."
f"{cls.__name__}, parameter {attribute_name!r}. "
"Add prior configuration or pass an explicit prior to Model."
)
cls = owner
try:
return Prior.for_class_and_attribute_name(cls, attribute_name)
except ConfigException as e:
Expand Down Expand Up @@ -559,9 +577,12 @@ def _instance_for_arguments(
return ModelInstance(constructor_arguments)

if not inspect.isclass(self.cls):
result = object.__new__(inspect._findclass(self.cls))
cls = self.cls
cls(result, **constructor_arguments)
owner = inspect._findclass(self.cls)
if owner is None:
result = self.cls(**constructor_arguments)
else:
result = object.__new__(owner)
self.cls(result, **constructor_arguments)
else:
result = self.cls(**constructor_arguments)

Expand Down
50 changes: 50 additions & 0 deletions test_autofit/mapper/model/test_function_priors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import autofit as af
import pytest
from autonerves import conf
from autonerves.exc import ConfigException


def configured_function(value=0.0):
return value


def test_function_config_prior(tmp_path):
priors = tmp_path / "priors"
priors.mkdir()
(priors / "test_function_priors.yaml").write_text(
"configured_function:\n value:\n type: Uniform\n lower_limit: 2.0\n upper_limit: 4.0\n"
)
original_configs = conf.instance.configs
conf.instance.push(new_path=tmp_path)
try:
model = af.Model(configured_function)
assert model.cls is configured_function
assert model.value.lower_limit == 2.0
assert model.value.upper_limit == 4.0
assert model.instance_from_prior_medians() == 3.0
finally:
conf.instance.configs = original_configs


@pytest.mark.parametrize("frozen", [False, True])
def test_function_explicit_prior(frozen):
prior = af.UniformPrior(lower_limit=2.0, upper_limit=4.0)
model = af.Model(configured_function, value=prior)
assert model.value is prior
if frozen:
model.freeze()
assert model.instance_from_prior_medians() == 3.0


def test_function_missing_config_has_actionable_error():
with pytest.raises(
ConfigException, match="configured_function.*value.*explicit prior"
):
af.Model(configured_function)


def test_class_initializer_keeps_owner_config():
model = af.Model(af.ex.Gaussian.__init__)
result = model.instance_from_prior_medians()
assert isinstance(result, af.ex.Gaussian)
assert model.prior_count == 3
Loading