271c799d0a
A fixture of the published cumulative risks at 0-5 years of KPNC's tables 1, 17, 18, 24 and 25 (CIN2+ and CIN3+), with the knots, spline coefficients and proportional factors that reproduce them, checked against `cumulative_risk` to 1e-12. Aggregates of public tables only.
50 lines
2.7 KiB
Python
50 lines
2.7 KiB
Python
"""What this module covers: `models.cacu.cin2_pim.likelihood` against KPNC's published risks (Cheung et al. 2020,
|
||
doi:10.1097/LGT.0000000000000528; tables of Egemen et al. 2020, doi:10.1097/LGT.0000000000000529).
|
||
|
||
`data/kpnc_reference.csv` holds the published cumulative risks at 0–5 years of the KPNC curves that one shared
|
||
I-spline baseline (order 4, 3 knots) with a proportional factor per curve reproduces to the rounding of the tables
|
||
(bayes-pim chapters 6 and 25): table 1 (screening, one baseline per current HPV), 17 (post-colposcopy), 18
|
||
(post-colposcopy, 2nd visit, the exact groups only), 24 and 25 (post-treatment). Each row is one curve with its group's
|
||
knots and γ and its own η; its CR(0) is the published one. Tables 4, 19 and 26, which no grouping reproduces, are left
|
||
out. Aggregates of public tables only.
|
||
"""
|
||
|
||
import json
|
||
from pathlib import Path
|
||
|
||
import numpy as np
|
||
import pandas as pd
|
||
import pytest
|
||
from scipy.special import logit
|
||
|
||
from models.cacu.cin2_pim import likelihood as L
|
||
|
||
REFERENCE = pd.read_csv(Path(__file__).parent / "data" / "kpnc_reference.csv", dtype={"table": str})
|
||
YEARS = range(6)
|
||
|
||
|
||
@pytest.mark.parametrize("key, group", list(REFERENCE.groupby(["table", "outcome", "group"])),
|
||
ids=lambda v: " ".join(v) if isinstance(v, tuple) else "")
|
||
def test_the_mixture_gives_kpncs_published_risks(key, group):
|
||
"""Pins down: with KPNC's knots, γ and η, `cumulative_risk` (logistic prevalence, PIMixture's I-splines,
|
||
proportional hazards) gives every published risk at 0–5 years to 1e-12. Fails if the I-spline basis, its
|
||
parameterization (exp γ), the proportional factor or the mixture CR = π + (1 − π)(1 − S) drift from PIMixture's."""
|
||
knots, gamma = json.loads(group["knots"].iloc[0]), np.array(json.loads(group["gamma"].iloc[0]))
|
||
n = len(group)
|
||
with np.errstate(divide="ignore"):
|
||
theta = logit(group["cr0"].to_numpy())
|
||
fitted = {"theta": theta, "beta": group["eta"].to_numpy(), "baseline": L.ISpline(tuple(knots), order=4),
|
||
"baseline_params": gamma}
|
||
|
||
found = L.cumulative_risk(fitted, np.eye(n), np.eye(n), YEARS)
|
||
|
||
published = group[[f"cr{t}" for t in YEARS]].to_numpy()
|
||
np.testing.assert_allclose(found, published, rtol=0, atol=1e-12)
|
||
|
||
|
||
def test_the_reference_covers_the_scenarios_tables():
|
||
"""Pins down: the fixture keeps the screening table and the post-colposcopy and post-treatment ones, both outcomes
|
||
each. Fails if a rebuild of the fixture drops a table the scenarios are checked against."""
|
||
assert {(t, o) for t, o in REFERENCE[["table", "outcome"]].itertuples(index=False)} == {
|
||
(t, o) for t in ("1", "17", "18", "24", "25") for o in ("CIN2+", "CIN3+")}
|