Skip to content

use deserialize in Censored.from_dict #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pymc_extras/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ def from_dict(cls, data: dict[str, Any]) -> Censored:
"""Create a censored distribution from a dictionary."""
data = data["data"]
return cls( # type: ignore
distribution=Prior.from_dict(data["dist"]),
distribution=deserialize(data["dist"]),
lower=data["lower"],
upper=data["upper"],
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,3 +1168,38 @@ def test_import_incorrect_directly() -> None:
match = "PyMC doesn't have a distribution of name 'SomeIncorrectDistribution'"
with pytest.raises(UnsupportedDistributionError, match=match):
from pymc_extras.prior import SomeIncorrectDistribution # noqa: F401


@pytest.fixture
def alternative_prior_deserialize():
def is_type(data):
return isinstance(data, dict) and "distribution" in data

def deserialize(data):
return Prior(**data)

register_deserialization(is_type=is_type, deserialize=deserialize)

yield

DESERIALIZERS.pop()


def test_censored_with_alternative(alternative_prior_deserialize) -> None:
data = {
"class": "Censored",
"data": {
"dist": {
"distribution": "Normal",
},
"lower": 0,
"upper": 10,
},
}

instance = deserialize(data)

assert isinstance(instance, Censored)
assert instance.lower == 0
assert instance.upper == 10
assert instance.distribution == Prior("Normal")