Repository: tskit-dev/tsdate

Issue:
variational_gamma method fails on tree seq with no mutations
```python
import msprime
ts = msprime.simulate(10, mutation_rate=0, random_seed=1)
tsdate.variational_gamma(ts, mutation_rate=1, return_posteriors=True)
# fails with --> 233 assert np.all(np.diff(rescaled_breaks) > 0)
```

I don't think this is a problem, but maybe we should detect it and error out early with a warning to use a different method?



Discussion:

Patch:
diff --git a/tsdate/core.py b/tsdate/core.py
index af02607..1d600d5 100644
--- a/tsdate/core.py
+++ b/tsdate/core.py
@@ -1600,8 +1600,8 @@ def variational_gamma(
     :param int max_iterations: The number of iterations used in the expectation
         propagation algorithm. Default: None, treated as 10.
     :param float rescaling_intervals: For time rescaling, the number of time
-        intervals within which to estimate a rescaling parameter. Default None,
-        treated as 1000.
+        intervals within which to estimate a rescaling parameter. Setting this to zero
+        means that rescaling is not performed. Default ``None``, treated as 1000.
     :param \\**kwargs: Other keyword arguments as described in the :func:`date` wrapper
         function, including ``time_units``, ``progress``, and ``record_provenance``.
         The arguments ``return_posteriors`` and ``return_likelihood`` can be
@@ -1640,7 +1640,10 @@ def variational_gamma(
         match_segregating_sites = False
     if regularise_roots is None:
         regularise_roots = True
-
+    if tree_sequence.num_mutations == 0:
+        raise ValueError(
+            "No mutations present: these are required for the variational_gamma method"
+        )
     dating_method = VariationalGammaMethod(
         tree_sequence, mutation_rate=mutation_rate, **kwargs
     )


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 53eb644..a1b2369 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -247,8 +247,8 @@ class TestOutput(RunCLI):
         "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} --rescaling-intervals 0"
+        input_ts = msprime.simulate(4, mutation_rate=10, random_seed=123)
+        params = f"-m 10 --method {method} --rescaling-intervals 0"
         self.run_tsdate_cli(tmp_path, input_ts, f"{self.popsize} {params}")
         (out, err) = capfd.readouterr()
         assert out == ""
@@ -275,8 +275,8 @@ class TestOutput(RunCLI):
         assert err.count("it/s") >= len(desc)
 
     def test_iterative_progress(self, tmp_path, capfd):
-        input_ts = msprime.simulate(4, random_seed=123)
-        params = "--method variational_gamma --mutation-rate 1e-8 "
+        input_ts = msprime.simulate(4, mutation_rate=10, random_seed=123)
+        params = "--method variational_gamma --mutation-rate 10 "
         params += "--progress --rescaling-intervals 0"
         self.run_tsdate_cli(tmp_path, input_ts, f"{self.popsize} {params}")
         (out, err) = capfd.readouterr()
diff --git a/tests/test_inference.py b/tests/test_inference.py
index ba0f377..d54090c 100644
--- a/tests/test_inference.py
+++ b/tests/test_inference.py
@@ -50,6 +50,11 @@ class TestPrebuilt:
         with pytest.raises(ValueError, match="method must be one of"):
             tsdate.date(ts, population_size=1, mutation_rate=None, method="foo")
 
+    def test_no_mutations_failure(self):
+        ts = utility_functions.single_tree_ts_n2()
+        with pytest.raises(ValueError, match="No mutations present"):
+            tsdate.variational_gamma(ts, mutation_rate=1)
+
     def test_no_population_size(self):
         ts = utility_functions.two_tree_mutation_ts()
         with pytest.raises(ValueError, match="Must specify population size"):