Governance charts for recurrence scenarios and age curves

This commit is contained in:
Roberto Treviño
2026-11-26 14:25:53 -06:00
parent 471ed3a18c
commit d1f9dcbea8
2 changed files with 141 additions and 0 deletions
+35
View File
@@ -13,6 +13,41 @@ describe('governance figures', () => {
expect((found.figure.data[2] as any).error_y.array[0]).toBeCloseTo(0.05);
});
it('draws a recurrence scenario: KM band, both Weibulls and the model points', () => {
const [found] = interpretabilityFigures({
kind: 'scenarios',
recurrencia: {
horizons_months: [12, 36],
escenarios: [
{
cohorte_nombre: 'METABRIC',
escenario: 'RE+',
n: 10,
recaidas: 3,
km: { months: [0, 12], S: [1, 0.9], low: [1, 0.8], high: [1, 1] },
weibull: { t: [0, 12], S: [1, 0.9] },
weibull_fractional: { t: [0, 12], S: [1, 0.91] },
points: { COMB: { S: [0.95, 0.8], low: [0.9, 0.7], high: [0.99, 0.9] } },
},
],
},
});
expect(found.figure.data.length).toBe(5); // band, KM, Weibull, fractional, points
});
it('draws an age curve with its centile band', () => {
const [found] = interpretabilityFigures({
kind: 'age_curves',
curves: {
value: {
kind: 'curves',
curve: { age_months: [16, 36], p5: [1, 2], median: [2, 3], p95: [3, 4] },
},
},
});
expect(found.figure.data.length).toBe(2);
});
it('draws nothing for a report it does not know', () => {
expect(interpretabilityFigures({ kind: 'other' })).toEqual([]);
});
+106
View File
@@ -138,7 +138,113 @@ export function interpretabilityFigures(
},
];
}
case 'scenarios':
return (report['recurrencia']['escenarios'] as any[]).slice(0, 6).map((e) => ({
title: `${e.cohorte_nombre} · ${e.escenario} (n = ${e.n}, ${e.recaidas} recaídas)`,
figure: {
data: [
...banded(
'Kaplan-Meier (IC 95 %)',
{ x: e.km.months, y: e.km.S, low: e.km.low, high: e.km.high },
PALETTE[0],
),
{
x: e.weibull.t,
y: e.weibull.S,
name: 'Weibull',
type: 'scatter',
mode: 'lines',
line: { color: PALETTE[1], dash: 'dot' },
},
{
x: e.weibull_fractional.t,
y: e.weibull_fractional.S,
name: 'Weibull fraccional',
type: 'scatter',
mode: 'lines',
line: { color: PALETTE[2] },
},
points(
'Modelo (IC 95 % bootstrap)',
report['recurrencia']['horizons_months'],
e.points.COMB,
),
],
layout: {
xaxis: { title: { text: 'Meses desde el diagnóstico' } },
yaxis: { title: { text: 'Supervivencia libre de recaída' } },
},
},
}));
case 'age_curves': {
// The metric's own curve; a topography has only electrodes: its first one.
const value = report['curves']['value'] ?? Object.values(report['curves'])[0];
if (!value) return [];
const label = report['curves']['value'] ? '' : ` · ${Object.keys(report['curves'])[0]}`;
if (value.kind === 'count_rate') {
return [
{
title: `Tasa de referencia: ${value.rate.toFixed(3)} por minuto (${value.reference} estudios)`,
figure: {
data: [
{
x: value.ages,
y: [value.rate, value.rate],
name: 'Tasa',
type: 'scatter',
mode: 'lines',
},
],
layout: {
xaxis: { title: { text: 'Edad (meses)' } },
yaxis: { title: { text: 'Eventos por minuto' } },
},
},
},
];
}
const c = value.curve;
return [
{
title: `Curva de referencia por edad (p5, mediana, p95)${label}`,
figure: {
data: [
...banded(
'p5–p95',
{ x: c.age_months, y: c.median, low: c.p5, high: c.p95 },
PALETTE[4],
),
].map((d, i) => (i === 1 ? { ...d, name: 'Mediana' } : d)),
layout: {
xaxis: { title: { text: 'Edad (meses)' }, type: 'log' },
yaxis: { title: { text: 'Valor' } },
},
},
},
];
}
default:
return [];
}
}
function points(
name: string,
x: number[],
p: { S: number[]; low: number[]; high: number[] },
): Partial<Plotly.Data> {
return {
x,
y: p.S,
error_y: {
type: 'data',
symmetric: false,
array: p.high.map((h, i) => h - p.S[i]),
arrayminus: p.S.map((v, i) => v - p.low[i]),
},
name,
type: 'scatter',
mode: 'markers',
marker: { color: PALETTE[4], size: 9 },
};
}