Gobernanza: Analizar EEG leads to Estudios EEG, which hosts the single-study evaluation; the timeline is only a dotted line; the probability curve and the age-norm bands are drawn from the model and the centiles

This commit is contained in:
2026-12-01 18:25:08 -06:00
parent f7eb170e15
commit 98eda5bc28
14 changed files with 187 additions and 141 deletions
+59 -37
View File
@@ -1,4 +1,4 @@
import { banded } from '../gobernanza/figures';
import { bandFill, type Centile } from '../gobernanza/figures';
import { valueAxis } from '../indicadores/figures';
import { Figure, PALETTE } from '../plot';
import { Metric, Pair, Site, Study, TeaStep, zoneLabel } from './tea-data';
@@ -17,24 +17,12 @@ export function timeline(steps: TeaStep[]): Figure | null {
if (!done.length) return null;
const spans = done.slice(1).map((step, i) => ({ from: done[i], to: step }));
const last = done.at(-1)!.date;
const bars = {
type: 'bar',
orientation: 'h',
base: spans.map(({ from }) => from.date),
x: spans.map(({ from, to }) => Date.parse(to.date) - Date.parse(from.date)),
y: spans.map(({ to }) => to.label),
width: 0.24,
marker: { color: PALETTE[6] },
hoverinfo: 'skip',
showlegend: false,
} satisfies Figure['data'][number] & { base: string[] };
return {
data: [
bars,
{
type: 'scatter',
mode: 'lines+markers',
line: { dash: 'dot', color: PALETTE[0] },
line: { dash: 'dot', color: PALETTE[6], width: 1.5 },
x: done.map((step) => step.date),
y: done.map((step) => step.label),
marker: {
@@ -219,26 +207,32 @@ export function headMap(sites: Site[]): Figure {
};
}
export type Centile = {
age_months: number;
p5: number | null;
median: number | null;
p95: number | null;
};
export function ageNorm(rows: Centile[], metric: Metric, age: number): Figure {
const x = rows.map((row) => row.age_months);
return {
data: [
...banded(
'Mediana',
{
x: rows.map((row) => row.age_months),
y: rows.map((row) => row.median) as number[],
low: rows.map((row) => row.p5),
high: rows.map((row) => row.p95),
},
bandFill(
x,
rows.map((row) => row.p5),
rows.map((row) => row.p95),
PALETTE[4],
'29',
),
bandFill(
x,
rows.map((row) => row.p25 ?? null),
rows.map((row) => row.p75 ?? null),
PALETTE[4],
'52',
),
{
type: 'scatter',
mode: 'lines',
name: 'Mediana',
x,
y: rows.map((row) => row.median),
line: { color: PALETTE[4] },
},
{
type: 'scatter',
mode: 'markers',
@@ -351,20 +345,31 @@ export function zBars(eeg: Study): SingleTrace {
};
}
export function probabilityCurve(
pair: Pair,
logistic?: { intercept: number; slope: number },
): Figure {
const ALTERED_Z = 2;
const expit = (x: number) => 1 / (1 + Math.exp(-x));
export function probabilityCurve(pair: Pair): Figure {
const data: Figure['data'] = [];
if (logistic) {
const extent = Math.max(4, Math.abs(pair.z ?? 0));
const extent = Math.max(4, Math.ceil(Math.abs(pair.z ?? 0)) + 1);
const modelled =
pair.reference != null &&
pair.reference > 0 &&
pair.reference < 1 &&
pair.odds &&
pair.odds > 0;
if (modelled) {
const intercept = Math.log(pair.reference! / (1 - pair.reference!));
const slope = Math.log(pair.odds!);
const x = Array.from({ length: 81 }, (_, i) => (extent * (i - 40)) / 40);
data.push({
type: 'scatter',
mode: 'lines',
name: 'Modelo',
x,
y: x.map((z) => 100 / (1 + Math.exp(-(logistic.intercept + logistic.slope * z)))),
y: x.map((z) => 100 * expit(intercept + slope * z)),
line: { color: PALETTE[4], width: 2.5 },
hovertemplate: 'z = %{x:.1~f}: %{y:.1~f} %<extra></extra>',
});
}
if (pair.z != null && pair.probability != null) {
@@ -387,11 +392,28 @@ export function probabilityCurve(
marker: { size: 10, color: PALETTE[4], symbol: 'diamond-open' },
});
}
const zone = (x0: number, x1: number) => ({
type: 'rect' as const,
xref: 'x' as const,
yref: 'paper' as const,
x0,
x1,
y0: 0,
y1: 1,
fillcolor: 'rgba(194,66,105,0.10)',
line: { width: 0 },
layer: 'below' as const,
});
return {
data,
layout: {
xaxis: { title: { text: 'Puntuación z' }, ...valueAxis([pair.z, 0]) },
xaxis: {
title: { text: 'Puntuación z' },
...valueAxis([-extent, extent]),
range: [-extent, extent],
},
yaxis: { title: { text: 'Probabilidad (%)' }, ...valueAxis([0, 100]), range: [0, 100] },
...(modelled ? { shapes: [zone(-extent, -ALTERED_Z), zone(ALTERED_Z, extent)] } : {}),
},
};
}