Skip to content

Commit

Permalink
Handle invalid proportions
Browse files Browse the repository at this point in the history
* update demes-sped
  • Loading branch information
molpopgen committed Nov 25, 2024
1 parent 4eb5273 commit b579855
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions demes/demes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def unit_interval(self, attribute, value):
raise ValueError(f"must have 0 <= {attribute.name} <= 1")


def unit_interval_exclusive_lo(self, attribute, value):
if not (0 < value <= 1):
raise ValueError(f"must have 0 < {attribute.name} <= 1")


def sum_less_than_one(self, attribute, value):
if sum(value) > 1:
raise ValueError(f"{attribute.name} must sum to less than one")
Expand Down Expand Up @@ -480,7 +485,9 @@ class Pulse:
time: Time = attr.ib(validator=[int_or_float, positive, finite])
proportions: List[Proportion] = attr.ib(
validator=attr.validators.deep_iterable(
member_validator=attr.validators.and_(int_or_float, unit_interval),
member_validator=attr.validators.and_(
int_or_float, unit_interval_exclusive_lo
),
iterable_validator=attr.validators.instance_of(list),
)
)
Expand Down Expand Up @@ -2095,7 +2102,7 @@ def fromdict(cls, data: MutableMapping[str, Any]) -> Graph:
proportions=(
list,
attr.validators.deep_iterable(
member_validator=int_or_float,
member_validator=[int_or_float, unit_interval_exclusive_lo],
iterable_validator=attr.validators.instance_of(list),
),
),
Expand Down Expand Up @@ -2156,7 +2163,7 @@ def fromdict(cls, data: MutableMapping[str, Any]) -> Graph:
list,
attr.validators.deep_iterable(
member_validator=attr.validators.and_(
int_or_float, unit_interval
int_or_float, unit_interval_exclusive_lo
),
iterable_validator=attr.validators.and_(
attr.validators.instance_of(list),
Expand Down

0 comments on commit b579855

Please sign in to comment.