Skip to content

Commit

Permalink
Improve recombination interval validation in _evolvets
Browse files Browse the repository at this point in the history
Fixes #1153
  • Loading branch information
molpopgen committed Aug 1, 2023
1 parent d0ed5d1 commit 8311f78
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
31 changes: 24 additions & 7 deletions fwdpy11/_evolvets.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,30 @@ def evolvets(
isinstance(
r, fwdpy11._fwdpy11.NonPoissonCrossoverGenerator) or \
isinstance(r, fwdpy11.Region), f"{type(r)}"
if r.beg < 0:
raise ValueError(f"{r} has begin value < 0.0")
if r.end > pop.tables.genome_length:
raise ValueError(
f"{r} has end value >= genome length of {pop.tables.genome_length}"
)
if hasattr(r, "beg") and hasattr(r, "end"):
if r.beg < 0:
raise ValueError(f"{r} has begin value < 0.0")
if r.end > pop.tables.genome_length:
raise ValueError(
f"{r} has end value >= genome length of {pop.tables.genome_length}"
)
elif hasattr(r, "position"):
if r.position < 0:
raise ValueError(f"{r} has position value < 0.0")
if r.position >= pop.tables.genome_length:
raise ValueError(
f"{r} has position value >= genome length of {pop.tables.genome_length}"
)
elif hasattr(r, "regions"):
for i in r.regions:
if i.beg < 0:
raise ValueError(f"{r} has begin value < 0.0")
if i.end > pop.tables.genome_length:
raise ValueError(
f"{r} has end value >= genome length of {pop.tables.genome_length}"
)
else:
raise TypeError(f"unexpceted type: {type(r)}")

if recorder is None:
from ._fwdpy11 import NoAncientSamples
Expand Down Expand Up @@ -225,7 +243,6 @@ def evolvets(

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)

pneutral = 0.0
if params.rates.neutral_mutation_rate + params.rates.selected_mutation_rate > 0.0:
Expand Down
34 changes: 26 additions & 8 deletions tests/test_ModelParams_with_recregions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with fwdpy11. If not, see <http://www.gnu.org/licenses/>.
#

import demes
import pytest

import fwdpy11
Expand All @@ -30,6 +31,30 @@ def _validate_expected(regions, numbers):
i, fwdpy11.NonPoissonCrossoverGenerator)]) == numbers[1]


def roundtrip(regions):
yaml = """
time_units: generations
demes:
- name: A
epochs:
- {end_time: 0, start_size: 10}
"""
graph = demes.loads(yaml)
demog = fwdpy11.discrete_demography.from_demes(graph, burnin=1)
pdict = {"recregions": regions,
"nregions": [],
"sregions": [],
"rates": (0, 0, None),
"gvalue": fwdpy11.Multiplicative(2.),
"simlen": 10,
"demography": demog,
}
params = fwdpy11.ModelParams(**pdict)
pop = fwdpy11.DiploidPopulation(10, 2)
rng = fwdpy11.GSLrng(123)
fwdpy11.evolvets(rng, pop, params, 100)


# The setup is ([regions], (number poisson, number non-poisson))
# The latter is used to mimic the internal idiom that we
# use to set up the genetic map to send to C++
Expand All @@ -52,11 +77,4 @@ def _validate_expected(regions, numbers):
def test_model_params_with_recregions(data):
regions, expected = data
_validate_expected(regions, expected)
pdict = {"recregions": regions,
"nregions": [],
"sregions": [],
"rates": (0, 0, None),
"gvalue": fwdpy11.Multiplicative(2.),
"simlen": 10,
}
_ = fwdpy11.ModelParams(**pdict)
roundtrip(regions)

0 comments on commit 8311f78

Please sign in to comment.