From a0f9cf4ffe7366ba46d5fe9b7ebe633a1dbbf997 Mon Sep 17 00:00:00 2001 From: Peter Ralph Date: Tue, 20 Jun 2023 14:41:45 -0700 Subject: [PATCH 1/2] version bump (#310) --- CHANGELOG.rst | 4 ++-- pyslim/_version.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7d9c441e..4e3d9628 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,5 +1,5 @@ *************************** -[UPCOMING.X.X] - XXXX-XX-XX +[1.0.2] - 2023-06-20 *************************** **Bugfixes**: @@ -13,7 +13,7 @@ correctly starts the msprime population with effective size `ancestral_Ne` at the time of the roots, which might be at the value of `ts.metadata['SLiM']['tick']`, this value minus 1, or this value minus 2. - Furthermore, `recapitate` now throws a warning if any roots of any trees + Furthermore, `recapitate` now throws an error if any roots of any trees are not at the same time as the others. (:user:`petrelharp`, :pr:`308`) diff --git a/pyslim/_version.py b/pyslim/_version.py index 217212c4..52c937f5 100644 --- a/pyslim/_version.py +++ b/pyslim/_version.py @@ -1,5 +1,5 @@ # coding: utf-8 -pyslim_version = '1.0.1' +pyslim_version = '1.0.2' slim_file_version = '0.8' # other file versions that require no modification compatible_slim_file_versions = ['0.8'] From 135a99d2f93d7d391b9842a69cbb3ea97b658ba3 Mon Sep 17 00:00:00 2001 From: Peter Ralph Date: Wed, 21 Jun 2023 21:22:54 -0700 Subject: [PATCH 2/2] This *actually* fixes the bug. (#311) * bugfix for reals * test recap happens at the right time --- CHANGELOG.rst | 8 +++++++- pyslim/methods.py | 5 +---- tests/test_tree_sequence.py | 27 ++++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4e3d9628..5eab61ee 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,5 +1,5 @@ *************************** -[1.0.2] - 2023-06-20 +[1.0.3] - 2023-06-21 *************************** **Bugfixes**: @@ -17,6 +17,12 @@ are not at the same time as the others. (:user:`petrelharp`, :pr:`308`) +*************************** +[1.0.2] - 2023-06-20 +*************************** + +This was a bugfix release that was pushed out without the actual bug fix. +Please don't use this one. *************************** [1.0.1] - 2022-09-23 diff --git a/pyslim/methods.py b/pyslim/methods.py index 68e50dbb..3294bf4f 100644 --- a/pyslim/methods.py +++ b/pyslim/methods.py @@ -92,10 +92,7 @@ def recapitate(ts, # since that's when all the linages are at, and otherwise the event # won't apply to them demography.add_population_split( - np.nextafter( - ts.metadata['SLiM']['tick'], - 2 * ts.metadata['SLiM']['tick'], - ), + np.nextafter( recap_time, 2 * recap_time), derived=derived_names, ancestral=ancestral_name, ) diff --git a/tests/test_tree_sequence.py b/tests/test_tree_sequence.py index 9e3eefcf..49a6fca2 100644 --- a/tests/test_tree_sequence.py +++ b/tests/test_tree_sequence.py @@ -4,6 +4,7 @@ import random import numpy as np import os +import json import pytest import tskit @@ -88,7 +89,7 @@ class TestRecapitate(tests.PyslimTestCase): Tests for recapitation. ''' - def check_recap_consistency(self, ts, recap): + def check_recap_consistency(self, ts, recap, with_ancestral_Ne=True): assert ts.metadata['SLiM']['tick'] == recap.metadata['SLiM']['tick'] assert ts.metadata['SLiM']['cycle'] == recap.metadata['SLiM']['cycle'] assert ts.metadata['SLiM']['stage'] == recap.metadata['SLiM']['stage'] @@ -98,6 +99,26 @@ def check_recap_consistency(self, ts, recap): if ts.has_reference_sequence(): assert ts.reference_sequence.data == recap.reference_sequence.data + root_times = list(set([ts.node(n).time for t in ts.trees() for n in t.roots])) + assert len(root_times) == 1 + if with_ancestral_Ne: + # check that time recorded in provenance is correct + assert recap.num_provenances == 2 + recap_prov = json.loads(recap.provenance(1).record) + recap_events = recap_prov['parameters']['demography']['events'] + assert len(recap_events) == 1 + recap_time = recap_events[0]['time'] + assert np.allclose(recap_time, root_times[0]) + # the oldest nodes in all trees with SLiM provenance should be at that time + if recap.num_nodes <= 500: # takes a long time otherwise + for t in recap.trees(): + for n in t.nodes(): + rn = recap.node(n) + if rn.metadata is not None: + p = t.parent(n) + if p == tskit.NULL or recap.node(p).metadata is None: + assert rn.time == root_times[0] + ts_samples = list(ts.samples()) for u in recap.samples(): n1 = recap.node(u) @@ -218,7 +239,7 @@ def test_with_demography(self, recipe): extra_metadata={"slim_id": ts.num_populations}, ) demography.add_population_split( - time=ts.metadata["SLiM"]["tick"] + 1.0, + time=ts.metadata["SLiM"]["tick"] + 20.0, derived=[p.name for p in demography.populations if p.name != "ancestral"], ancestral="ancestral", ) @@ -228,7 +249,7 @@ def test_with_demography(self, recipe): recombination_rate=recomb_rate, random_seed=333, ) - self.check_recap_consistency(ts, recap) + self.check_recap_consistency(ts, recap, with_ancestral_Ne=False) @pytest.mark.parametrize('recipe', recipe_eq(exclude="long"), indirect=True) def test_first_gen_nodes(self, recipe):