From d1f9dcbea85d1a355485e953681928c09d86faca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Trevi=C3=B1o?= Date: Thu, 26 Nov 2026 14:25:53 -0600 Subject: [PATCH] Governance charts for recurrence scenarios and age curves --- src/app/gobernanza/figures.spec.ts | 35 ++++++++++ src/app/gobernanza/figures.ts | 106 +++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/src/app/gobernanza/figures.spec.ts b/src/app/gobernanza/figures.spec.ts index ded2cf5..0547cd8 100644 --- a/src/app/gobernanza/figures.spec.ts +++ b/src/app/gobernanza/figures.spec.ts @@ -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([]); }); diff --git a/src/app/gobernanza/figures.ts b/src/app/gobernanza/figures.ts index a4e708b..c5474fa 100644 --- a/src/app/gobernanza/figures.ts +++ b/src/app/gobernanza/figures.ts @@ -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 { + 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 }, + }; +}