CaMa indicators and follow-up list, model governance page

This commit is contained in:
Roberto Treviño
2026-11-25 23:20:52 -06:00
parent 9b02641804
commit 53a6393cb2
19 changed files with 974 additions and 19 deletions
+144
View File
@@ -0,0 +1,144 @@
import type { Figure } from '../plot';
import { PALETTE } from '../plot';
/** A curve with its band, as the reports give them. */
interface Band {
x: number[];
y: number[];
low?: (number | null)[];
high?: (number | null)[];
}
function banded(name: string, b: Band, colour: string, dash?: 'dot'): Partial<Plotly.Data>[] {
const lines: Partial<Plotly.Data>[] = [];
if (b.low && b.high) {
lines.push({
x: [...b.x, ...[...b.x].reverse()],
y: [...b.high, ...[...b.low].reverse()] as number[],
fill: 'toself',
fillcolor: `${colour}33`,
line: { width: 0 },
hoverinfo: 'skip',
showlegend: false,
type: 'scatter',
});
}
lines.push({
x: b.x,
y: b.y,
name,
type: 'scatter',
mode: 'lines',
line: { color: colour, dash },
});
return lines;
}
/** What a model's interpretability report draws, by its kind: survival curves, the mixture's
* observed and predicted risk per subgroup, or the logistic regression's calibration. */
export function interpretabilityFigures(
report: Record<string, any>,
): { title: string; figure: Figure }[] {
switch (report['kind']) {
case 'curves': {
const data = (report['curves'] as any[]).flatMap((c, i) =>
banded(c.name, { x: c.months, y: c.survival, low: c.low, high: c.high }, PALETTE[i]),
);
const p = report['predicted'];
data.push({
x: p.months,
y: p.survival,
error_y: {
type: 'data',
symmetric: false,
array: p.high.map((h: number, i: number) => h - p.survival[i]),
arrayminus: p.survival.map((s: number, i: number) => s - p.low[i]),
},
name: p.name,
type: 'scatter',
mode: 'markers',
marker: { color: PALETTE[4], size: 9 },
});
return [
{
title: 'Supervivencia observada y del modelo',
figure: {
data,
layout: {
xaxis: { title: { text: 'Meses desde el índice' } },
yaxis: { title: { text: 'Supervivencia S(t)' } },
},
},
},
];
}
case 'subgroups':
return (report['groups'] as any[]).slice(0, 6).map((g) => ({
title: `${Object.values(g.combination).join(' · ')} (n = ${g.n})`,
figure: {
data: [
...banded(
'Observado (Turnbull)',
{
x: g.observed.years,
y: g.observed.risk,
low: g.observed.low,
high: g.observed.high,
},
PALETTE[0],
),
...(g.predicted
? banded(
'Modelo',
{
x: g.predicted.years,
y: g.predicted.risk,
low: g.predicted.low,
high: g.predicted.high,
},
PALETTE[4],
'dot',
)
: []),
],
layout: {
xaxis: { title: { text: 'Años desde el índice' } },
yaxis: { title: { text: 'Riesgo acumulado' } },
},
},
}));
case 'coefficients': {
const bins = report['calibration']['bins'] as any[];
return [
{
title: `Calibración por decil de riesgo (Brier ${report['calibration']['brier'].toFixed(3)})`,
figure: {
data: [
{
x: bins.map((b) => b.predicted),
y: bins.map((b) => b.observed),
name: 'Deciles',
type: 'scatter',
mode: 'markers',
},
{
x: [0, 1],
y: [0, 1],
name: 'Calibración perfecta',
type: 'scatter',
mode: 'lines',
line: { dash: 'dot', color: PALETTE[6] },
},
],
layout: {
xaxis: { title: { text: 'Predicho' } },
yaxis: { title: { text: 'Observado' } },
},
},
},
];
}
default:
return [];
}
}