Skip to content

Commit 77d4068

Browse files
authored
Merge pull request #248 from nspope/ne-rescale-time
Priors with piecewise demographic histories
2 parents b9bf7d8 + 9e5fc0b commit 77d4068

13 files changed

+567
-173
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ tsdate.egg-info
99
data/prior_1000df.txt
1010
*.ipynb
1111
.ipynb*
12+
build/
13+
.*.swp

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ repos:
2121
hooks:
2222
- id: black
2323
language_version: python3
24-
- repo: https://gitlab.com/pycqa/flake8
24+
- repo: https://github.com/pycqa/flake8
2525
rev: 3.9.2
2626
hooks:
2727
- id: flake8

CHANGELOG.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@
1212
in ``get_dates`` and the internal ``normalize`` terminology is changed to
1313
``standardize`` to better reflect the fact that the maximum (not sum) is one.
1414

15+
- The ``Ne`` argument to ``date`` has been deprecated (although it is
16+
still in the API for backward compatibility). The equivalent argument
17+
``population_size`` should be used instead.
18+
19+
**Features**
20+
21+
- Priors may be calculated using a piecewise-constant effective population trajectory,
22+
which is implemented in the ``demography.PopulationSizeHistory`` class. The
23+
``population_size`` argument to ``date`` accepts either a single scalar effective
24+
population size, or a ``PopulationSizeHistory`` instance.
25+
1526
**Bugfixes**
1627

1728
- The returned posteriors when ``return_posteriors=True`` now return actual
1829
probabilities (scaled so that they sum to one) rather than standardized
19-
"probabilites" whose maximum value is one.
30+
"probabilities" whose maximum value is one.
2031

2132
--------------------
2233
[0.1.5] - 2022-06-07

tests/test_accuracy.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_basic(
104104
mu = sim_mutations_parameters["rate"]
105105

106106
dts, posteriors = tsdate.date(
107-
ts, Ne=Ne, mutation_rate=mu, return_posteriors=True
107+
ts, population_size=Ne, mutation_rate=mu, return_posteriors=True
108108
)
109109
# make sure we can read node metadata - old tsdate versions didn't set a schema
110110
if dts.table_metadata_schemas.node.schema is None:
@@ -139,6 +139,38 @@ def test_scaling(self, Ne):
139139
Test that we are in the right theoretical ballpark given known Ne
140140
"""
141141
ts = tskit.Tree.generate_comb(2).tree_sequence
142-
dts = tsdate.date(ts, Ne=Ne, mutation_rate=None)
142+
dts = tsdate.date(ts, population_size=Ne, mutation_rate=None)
143143
# Check the date is within 10% of the expected
144144
assert 0.9 < dts.node(dts.first().root).time / (2 * Ne) < 1.1
145+
146+
@pytest.mark.parametrize(
147+
"bkwd_rate, trio_tmrca",
148+
[ # calculated from simulations
149+
(-1.0, 0.76),
150+
(-0.9, 0.79),
151+
(-0.8, 0.82),
152+
(-0.7, 0.85),
153+
(-0.6, 0.89),
154+
(-0.5, 0.94),
155+
(-0.4, 0.99),
156+
(-0.3, 1.05),
157+
(-0.2, 1.12),
158+
(-0.1, 1.21),
159+
(0.0, 1.32),
160+
],
161+
)
162+
def test_piecewise_scaling(self, bkwd_rate, trio_tmrca):
163+
"""
164+
Test that we are in the right theoretical ballpark given known Ne,
165+
under exponential growth.
166+
167+
Check coalescence time of a trio instead of a pair, because of
168+
https://github.com/tskit-dev/tsdate/issues/230
169+
"""
170+
time = np.linspace(0, 10, 100)
171+
ne = 0.5 * np.exp(bkwd_rate * time)
172+
ts = tskit.Tree.generate_comb(3).tree_sequence
173+
demo = tsdate.demography.PopulationSizeHistory(ne, time[1:])
174+
dts = tsdate.date(ts, population_size=demo, mutation_rate=None)
175+
# Check the date is within 10% of the expected
176+
assert 0.9 < dts.node(dts.first().root).time / trio_tmrca < 1.1

tests/test_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ def test_cached_prior(self):
2727
if os.path.isfile(fn):
2828
raise AssertionError(f"The file {fn} already exists. Delete before testing")
2929
with self.assertLogs(level="WARNING") as log:
30-
priors_approx10 = ConditionalCoalescentTimes(10, Ne=1)
30+
priors_approx10 = ConditionalCoalescentTimes(10)
3131
assert len(log.output) == 1
3232
assert "user cache" in log.output[0]
3333
priors_approx10.add(10)
3434
# Check we have created the prior file
3535
assert os.path.isfile(fn)
36-
priors_approxNone = ConditionalCoalescentTimes(None, Ne=1)
36+
priors_approxNone = ConditionalCoalescentTimes(None)
3737
priors_approxNone.add(10)
3838
assert np.allclose(priors_approx10[10], priors_approxNone[10], equal_nan=True)
3939
# Test when using a bigger n that we're using the precalculated version

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_default_values(self):
5050
args = parser.parse_args(["date", self.infile, self.output, "1"])
5151
assert args.tree_sequence == self.infile
5252
assert args.output == self.output
53-
assert args.Ne == 1
53+
assert args.population_size == 1
5454
assert args.mutation_rate is None
5555
assert args.recombination_rate is None
5656
assert args.epsilon == 1e-6
@@ -216,7 +216,7 @@ def compare_python_api(self, input_ts, cmd, Ne, mutation_rate, method):
216216
cli.tsdate_main(full_cmd.split())
217217
output_ts = tskit.load(output_filename)
218218
dated_ts = tsdate.date(
219-
input_ts, Ne=Ne, mutation_rate=mutation_rate, method=method
219+
input_ts, population_size=Ne, mutation_rate=mutation_rate, method=method
220220
)
221221
print(dated_ts.tables.nodes.time, output_ts.tables.nodes.time)
222222
assert np.array_equal(dated_ts.tables.nodes.time, output_ts.tables.nodes.time)

0 commit comments

Comments
 (0)