Skip to content

Commit

Permalink
Add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hyanwong committed Nov 3, 2023
1 parent f87e435 commit 467c77a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
12 changes: 12 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ def test_verbosity(self, tmp_path, caplog, flag, log_status):
self.run_tsdate_cli(tmp_path, input_ts, flag, cmd="preprocess")
assert log_status in caplog.text

@pytest.mark.parametrize(
"method", ["inside_outside", "maximization", "variational_gamma"]
)
def test_no_progress(self, method, tmp_path, capfd):
input_ts = msprime.simulate(4, random_seed=123)
params = f"-m 0.1 --method {method}"
self.run_tsdate_cli(tmp_path, input_ts, f"{self.popsize} {params}")
(out, err) = capfd.readouterr()
assert out == ""
# run_tsdate_cli print logging to stderr
assert err == ""

def test_progress(self, tmp_path, capfd):
input_ts = msprime.simulate(4, random_seed=123)
params = "--method inside_outside --progress"
Expand Down
10 changes: 7 additions & 3 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"""
Test cases for the python API for tsdate.
"""
import unittest

import msprime
import numpy as np
import pytest
Expand All @@ -37,7 +35,7 @@
from tsdate.demography import PopulationSizeHistory


class TestPrebuilt(unittest.TestCase):
class TestPrebuilt:
"""
Tests for tsdate on prebuilt tree sequences
"""
Expand All @@ -47,6 +45,12 @@ def test_no_population_size(self):
with pytest.raises(ValueError, match="Must specify population size"):
tsdate.date(ts, mutation_rate=None)

@pytest.mark.parametrize("method", ["maximization", "variational_gamma"])
def test_no_mutation(self, method):
ts = utility_functions.two_tree_mutation_ts()
with pytest.raises(ValueError, match="method requires mutation rate"):
tsdate.date(ts, method=method, population_size=1, mutation_rate=None)

def test_not_needed_population_size(self):
ts = utility_functions.two_tree_mutation_ts()
prior = tsdate.build_prior_grid(ts, population_size=1, timepoints=10)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ def test_date_params_recorded(self):
ts = utility_functions.single_tree_ts_n2()
mu = 0.123
Ne = 9
dated_ts = tsdate.date(ts, population_size=Ne, mutation_rate=mu)
dated_ts = tsdate.date(
ts, population_size=Ne, mutation_rate=mu, method="maximization"
)
rec = json.loads(dated_ts.provenance(-1).record)
assert np.isclose(rec["parameters"]["mutation_rate"], mu)
assert np.isclose(rec["parameters"]["population_size"], Ne)
assert rec["parameters"]["method"] == "maximization"

@pytest.mark.parametrize(
"popdict",
Expand Down
3 changes: 3 additions & 0 deletions tsdate/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,9 @@ def variational_dates(
if not max_iterations >= 1:
raise ValueError("Maximum number of iterations must be greater than 0")

if mutation_rate is None:
raise ValueError("Variational gamma method requires mutation rate")

# Parameters below are not used in variational dating, but are here
# to match the signature of get_dates(). We may be able to remove some
# if we move to specifying some params via a control dictionary
Expand Down

0 comments on commit 467c77a

Please sign in to comment.