Skip to content

Commit

Permalink
Add Python check that regions don't extend past genome length. (#987)
Browse files Browse the repository at this point in the history
See issues #910 and #908.
This is not a fix for those issues,
which would require that the validation
happen on the C++ side.
  • Loading branch information
molpopgen committed Sep 23, 2022
1 parent e6a46b1 commit cf8a59a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions fwdpy11/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
make_data_matrix,
simplify,
simplify_tables,
_validate_regions,
) # NOQA


Expand Down
4 changes: 4 additions & 0 deletions fwdpy11/_evolvets.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def evolvets(
evolve_with_tree_sequences,
)

fwdpy11._validate_regions(params.sregions, pop.tables.genome_length)
fwdpy11._validate_regions(params.nregions, pop.tables.genome_length)
fwdpy11._validate_regions(params.recregions, pop.tables.genome_length)

try:
# DemographicModelDetails ?
demographic_model = params.demography.model
Expand Down
14 changes: 14 additions & 0 deletions fwdpy11/_functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@

def infinite_sites(rng: GSLrng, pop: DiploidPopulation, mu: float) -> int:
return _infinite_sites(rng, pop, mu)

def _validate_regions(regions, sequence_length):
for r in regions:
try:
if r.beg >= sequence_length or r.end > sequence_length:
raise ValueError(f"Region {r} extends beyond the sequence length {sequence_length}")
except AttributeError:
try:
for des in r.des:
if des.beg >= sequence_length or des.end > sequence_length:
raise ValueError(f"Region {des} in {r} extends beyond the sequence length {sequence_length}")
except TypeError: # r.des not Iterable
if r.des.beg >= sequence_length or r.des.end > sequence_length:
raise ValueError(f"Region {des} in {r} extends beyond the sequence length {sequence_length}")
1 change: 1 addition & 0 deletions tests/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest

import numpy as np
import pytest

import fwdpy11

Expand Down

0 comments on commit cf8a59a

Please sign in to comment.