Skip to content

C port followup: degenerate inputs in rescaling and changepoints #538

Backlog

Description

@nspope

These behaviours of the mutational-clock rescaling (lib/rescaling.c, tsdate/rescaling.py) were kept as-is when the numerical code moved from numba to C (#534), and are worth a look.

The reproducers that call internal functions of _tsdate (e.g. _tsdate.moments) run at commit d8a95ac; later commits in #534 removed those exports and test the functions in lib/tests/tests.c instead.

rescale_tree_sequence fails: it passes the constraints as nodes_fixed

tsdate.rescaling.rescale_tree_sequence builds a 2-D constraints array and passes it to mutational_timescale in the position of nodes_fixed, which is a 1-D boolean array, so every call fails:

import msprime
from tsdate import rescaling
ts = msprime.sim_ancestry(4, sequence_length=1e4, population_size=1e4, random_seed=1)
ts = msprime.sim_mutations(ts, rate=1e-8, random_seed=1)
rescaling.rescale_tree_sequence(ts, 1e-8)
# ValueError: object too deep for desired array

The function was implemented as a standalone API but not updated alongside the main date code path.

mutational_timescale ignores nodes_fixed

_tsdate.mutational_timescale (exposed as tsdate.rescaling.mutational_timescale) checks that nodes_fixed has one entry per node and then does not use it: tsd_mutational_timescale has no such parameter. Which nodes are flagged as fixed makes no difference to the result.

fixed_changepoints with all-zero or empty counts

tsd_fixed_changepoints normalises the cumulative counts by their total. If all counts are zero (or there are none), the normalised values are 0 / 0 = NaN, the search places every quantile after them, and the breakpoints are [-1, ..., -1, num_counts]:

import numpy as np, _tsdate
_tsdate.fixed_changepoints(np.zeros(3), 3)   # [-1, -1, -1, 3]
_tsdate.fixed_changepoints(np.zeros(0), 3)   # [-1, -1, -1, 0]

tsd_mutational_timescale wraps the negative indices as in numpy, so the first interval becomes the last epoch, and the call then fails with the "Zero edge span" error below rather than a message about the counts.

mutational_timescale fails with "Zero edge span" when spans are zero or there are no edges

When every edge overlapping an interval has zero span (e.g. a zero mutation rate), or there are no edges, tsd_mutational_timescale fails with TSD_ERR_ZERO_EDGE_SPAN ("Zero edge span in interval"):

f = np.zeros(2, bool)
_tsdate.mutational_timescale(np.array([0.0, 1.0]), np.zeros((0, 2)), f,
                             np.zeros(0, np.int32), np.zeros(0, np.int32), 1)
# LibraryError: Zero edge span in interval

mutational_area always starts the first epoch at zero

tsd_mutational_area starts the first epoch at time zero, even if the youngest node is older; the youngest node's own time is not a breakpoint. With node times [1, 2] and one edge, the single epoch is [0, 2] (duration 2) rather than [1, 2]:

_tsdate.mutational_area(np.array([1.0, 2.0]), np.array([[2.0, 10.0]]),
                        np.array([1], np.int32), np.array([0], np.int32))
# counts [2.], offset [10.], duration [2.], nodes_index [0, 1]

piecewise_scale_point_estimate wraps times below the first breakpoint

In tsd_piecewise_scale_point_estimate, a time below the first breakpoint gets search index -1, which wraps (as in numpy) to the last breakpoint; -inf gives NaN:

from tsdate import rescaling
o, r = np.array([0.0, 1.0, 2.0]), np.array([0.0, 2.0, 4.0])
rescaling.piecewise_scale_point_estimate(np.array([-1.0, -1e-9, 0.5, -np.inf]),
                                         np.zeros(4, bool), o, r)
# [4., 4., 1., nan]

tsd_piecewise_scale_posterior, by contrast, fails with TSD_ERR_TIME_BELOW_FIRST_BREAKPOINT in the same situation.

piecewise_scale_posterior edge cases

In tsdate.rescaling.piecewise_scale_posterior (tsd_piecewise_scale_posterior):

  • max_shape=0 is accepted and gives shape -1 (improper) and rate 0:
    P = np.array([[1.0, 1.0], [2.0, 3.0]])
    rescaling.piecewise_scale_posterior(P, np.zeros(2, bool), np.array([0., 1., 10.]),
                                        np.array([0., 2., 20.]), 0.5, 0.0)
    # [[-1., 0.], [-1., 0.]]
    (approximate_gamma_iqr does not validate max_shape; see the gamma-approximation issue.)
  • No posteriors is an error, with numpy's message carried over:
    rescaling.piecewise_scale_posterior(np.zeros((0, 2)), np.zeros(0, bool),
                                        np.array([0., 1.]), np.array([0., 2.]), 0.5, 1000.)
    # LibraryError: zero-size array to reduction operation minimum which has no identity
  • An infinite shape passes the validity check (shape > -1 and rate > 0) and then fails inside the gamma fit with an unrelated message:
    rescaling.piecewise_scale_posterior(np.array([[np.inf, 1.0]]), np.zeros(1, bool),
                                        np.array([0., 1.]), np.array([0., 2.]), 0.5, 1000.)
    # LibraryError: Quantiles must be sorted
  • For a shape very close to -1 the lower quantile underflows to zero and the call fails with "Floating-point division by zero", rather than as a failed gamma fit:
    rescaling.piecewise_scale_posterior(np.array([[-1 + 1e-3, 1.0]]), np.zeros(1, bool),
                                        np.array([0., 1.]), np.array([0., 2.]), 0.5, 1000.)

Activity

  1. self-assigned this
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions