Skip to content

v3.0.0

Choose a tag to compare

@DiogoRibeiro7 DiogoRibeiro7 released this 25 Aug 15:58
· 97 commits to develop since this release
710e3e9

A general multistate engine, with the two illness-death models rebuilt on top of it.

Breaking changes

gen_cmm and gen_thmm no longer reproduce their 2.1.0 output for a given seed. Both are configurations of gen_multistate now, which draws one candidate per outgoing edge per visit where the old implementations drew all three latent times up front.

Columns, dtypes and id bases are unchanged — thmm still numbers subjects from 1 — and every distribution test passes untouched. Analysis code keeps working; only the numbers move. Pin the version alongside the seed if you need earlier results.

NaN and infinity are rejected wherever a number is expected. Calls that previously returned a frame quietly full of NaN, raised an unrelated OverflowError from NumPy, or — for gen_recurrent_events(followup_time=nan) — never returned at all, now raise a ValidationError naming the argument. Thirty-nine arguments across the twelve generators were affected.

The engine

from gen_surv import ExponentialBaseline, Transition, WeibullBaseline, gen_multistate

transitions = [
    Transition(1, 2, WeibullBaseline(shape=1.2, scale=3.0), [0.4]),   # onset
    Transition(1, 3, ExponentialBaseline(rate=0.2), [0.1]),           # death, healthy
    Transition(2, 1, ExponentialBaseline(rate=0.6), [0.0]),           # recovery
    Transition(2, 3, WeibullBaseline(shape=0.8, scale=1.5), [0.6]),   # death, ill
]

df = gen_multistate(n=500, transitions=transitions, clock="reset", seed=1)

Each edge carries its own baseline hazard and coefficients. clock="forward" measures the hazard from entry to the study, giving a Markov process; clock="reset" restarts it at each state, giving a semi-Markov one. Either canonical layout can be returned. A state with no outgoing edge is absorbing, and cycles are allowed — recovery is a transition like any other.

It is not reachable through generate(): a graph is a list of objects rather than a set of scalars, so there is no string form and no command-line equivalent.

Typing

py.typed ships. Every public function was annotated and mypy checked them on every commit, but PEP 561 tells a type checker to ignore an installed package's inline types without the marker — so a downstream mypy reported Cannot find implementation or library stub for module named "gen_surv" and accepted gen_cphm(n="not an integer", ...) without comment. It now reports the argument type.

Performance

The engine advances the whole cohort a wave at a time: subjects sharing a state are drawn for together, so the sampling and the hazard inversions are array operations. At ten thousand subjects thmm is 8.8× faster than in 2.1.0 — it had always looped per subject — while cmm, which had been vectorised, is 1.7× slower. That is the price of generality.

Fixes

  • cmm and thmm validated only the length of rate, never its contents, so a negative entry surfaced as ValueError: scale < 0 from inside NumPy.
  • cphm's beta is a log hazard ratio, so no positivity check reached it and nothing had been validating it at all.

Install: pip install gen-surv==3.0.0