diff --git a/e2e/pages.spec.ts b/e2e/pages.spec.ts index f9fa564..1540539 100644 --- a/e2e/pages.spec.ts +++ b/e2e/pages.spec.ts @@ -195,8 +195,14 @@ test('tea complete child: every screen has data', async ({ page }) => { section('Razones de momios por par').locator('app-plot .point').first(), ).toBeAttached(); await expect( - section('Probabilidad frente a la referencia').locator('tbody tr').first(), + section('Probabilidad de un puntaje ADOS-2 severo').locator('tbody tr').first(), ).toBeVisible(); + await expect( + section('Probabilidad de un puntaje ADOS-2 severo').getByRole('columnheader'), + ).toHaveText(['Par', 'Prob. ADOS-2 severo', 'Referencia']); + await expect(section('Probabilidad de un puntaje ADOS-2 severo').locator('p')).toHaveText( + 'No es la probabilidad de TEA.', + ); await expect(section('Probabilidad según z').locator('app-plot .point').first()).toBeAttached(); await expect(section('Limitaciones').locator('p').first()).toHaveText(/cohorte/); diff --git a/src/app/gobernanza/figures.ts b/src/app/gobernanza/figures.ts index 8c3fdec..6a23cbb 100644 --- a/src/app/gobernanza/figures.ts +++ b/src/app/gobernanza/figures.ts @@ -2,6 +2,7 @@ import type { Analysis } from '../api/models/analysis'; import type { Figure } from '../plot'; import { PALETTE } from '../plot'; import { BIOMARKER_NAMES, OUTCOME_NAMES, ZONE_NAMES } from '../eeg-labels'; +import { logAxis } from '../indicadores/figures'; interface Band { x: number[]; @@ -245,7 +246,16 @@ export function interpretabilityFigures( }; }), layout: { - xaxis: { type: 'log', title: { text: 'OR (IC 95 %)' } }, + xaxis: { + ...logAxis( + (report['rows'] as AssociationRow[]).flatMap((row) => [ + row.odds_ratio, + row.ci_low, + row.ci_high, + ]), + ), + title: { text: 'OR (IC 95 %)' }, + }, yaxis: { type: 'category', automargin: true }, }, }, @@ -358,7 +368,10 @@ export function interpretabilityFigures( }, ], layout: { - xaxis: { title: { text: 'Razón de momios (escala log)' }, type: 'log' }, + xaxis: { + ...logAxis([1, ...rows.flatMap((r) => [r.odds_ratio, r.ci_low, r.ci_high])]), + title: { text: 'Razón de momios (escala log)' }, + }, yaxis: { automargin: true, autorange: 'reversed' }, shapes: [ { diff --git a/src/app/indicadores/figures.spec.ts b/src/app/indicadores/figures.spec.ts new file mode 100644 index 0000000..2096b50 --- /dev/null +++ b/src/app/indicadores/figures.spec.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from 'vitest'; +import { logAxis } from './figures'; + +describe('logAxis', () => { + it('uses the 1-2-3-5-7 sequence across decades with decimal labels', () => { + expect(logAxis([0.1, 10])).toMatchObject({ + type: 'log', + tickmode: 'array', + tickvals: [0.1, 0.2, 0.3, 0.5, 0.7, 1, 2, 3, 5, 7, 10], + ticktext: ['0.1', '0.2', '0.3', '0.5', '0.7', '1', '2', '3', '5', '7', '10'], + }); + }); + + it('covers the data range with whole decades', () => { + expect(logAxis(new Set([4.5, 1.2])).tickvals).toEqual([1, 2, 3, 5, 7, 10]); + expect(logAxis([1]).tickvals).toEqual([1]); + }); + + it('keeps small decimal ticks distinct and large ticks out of scientific notation', () => { + expect(logAxis([0.001, 0.01]).ticktext).toEqual([ + '0.001', + '0.002', + '0.003', + '0.005', + '0.007', + '0.01', + ]); + expect(logAxis([1e21, 1e22]).ticktext).toEqual([ + '1000000000000000000000', + '2000000000000000000000', + '3000000000000000000000', + '5000000000000000000000', + '7000000000000000000000', + '10000000000000000000000', + ]); + }); + + it('ignores missing, nonfinite and nonpositive values', () => { + const invalid = [null, undefined, NaN, Infinity, -Infinity, -1, 0]; + expect(logAxis([...invalid, 0.1, 10])).toEqual(logAxis([0.1, 10])); + expect(logAxis(invalid)).toMatchObject({ tickvals: [], ticktext: [] }); + expect(logAxis([])).toMatchObject({ tickvals: [], ticktext: [] }); + }); +}); diff --git a/src/app/indicadores/figures.ts b/src/app/indicadores/figures.ts index 4c846c4..4f577ef 100644 --- a/src/app/indicadores/figures.ts +++ b/src/app/indicadores/figures.ts @@ -251,7 +251,8 @@ export function rateDots( }; } -const decimal = (n: number) => n.toLocaleString('es-MX', { maximumFractionDigits: 2 }); +const decimal = (n: number, options: Intl.NumberFormatOptions = { maximumFractionDigits: 2 }) => + n.toLocaleString('es-MX', options); export function fill(template: string, r: Reading): string { const p = r.populations; @@ -450,6 +451,29 @@ export function valueAxis(values: Iterable) { }; } +export function logAxis(values: Iterable) { + const found = [...values].filter((v): v is number => v != null && Number.isFinite(v) && v > 0); + const tickvals: number[] = []; + if (found.length) { + const first = Math.floor(Math.log10(Math.min(...found))); + const last = Math.ceil(Math.log10(Math.max(...found))); + for (let decade = first; decade <= last; decade++) { + for (const multiplier of [1, 2, 3, 5, 7]) { + if (decade === last && multiplier > 1) continue; + const value = Number(`${multiplier}e${decade}`); + if (value > 0 && Number.isFinite(value)) tickvals.push(value); + } + } + } + return { + type: 'log' as const, + tickmode: 'array' as const, + tickvals, + ticktext: tickvals.map((v) => decimal(v, { maximumSignificantDigits: 1, useGrouping: false })), + hoverformat: SHORT, + }; +} + export function historyFigure( panel: HistoryPanel, series: { label: string; r: Reading | undefined }[], diff --git a/src/app/paciente/tea-figures.ts b/src/app/paciente/tea-figures.ts index 08296c8..c8ca8d3 100644 --- a/src/app/paciente/tea-figures.ts +++ b/src/app/paciente/tea-figures.ts @@ -1,5 +1,5 @@ import { bandFill, type Centile } from '../gobernanza/figures'; -import { valueAxis } from '../indicadores/figures'; +import { logAxis, valueAxis } from '../indicadores/figures'; import { Figure, PALETTE } from '../plot'; import { Metric, Pair, Site, Study, TeaStep, zoneLabel } from './tea-data'; @@ -281,7 +281,10 @@ export function forest(pairs: Pair[], alteredOnly: boolean): SingleTrace { }, ], layout: { - xaxis: { type: 'log', title: { text: 'Razón de momios (OR) · IC 95 %' } }, + xaxis: { + ...logAxis([1, ...visible.flatMap((p) => [p.odds, p.low, p.high])]), + title: { text: 'Razón de momios (OR) · IC 95 %' }, + }, yaxis: { autorange: 'reversed', automargin: true }, shapes: [ { diff --git a/src/app/paciente/tea-patient.spec.ts b/src/app/paciente/tea-patient.spec.ts index 2a933f3..f169a10 100644 --- a/src/app/paciente/tea-patient.spec.ts +++ b/src/app/paciente/tea-patient.spec.ts @@ -157,13 +157,28 @@ describe('Figuras del paciente TEA', () => { const pairs = [pair, { ...pair, title: 'Zona normal', altered: false, odds: 2 }]; expect(forest(pairs, false).data[0].x).toEqual([3, 2]); const filtered = forest(pairs, true); - expect(filtered.layout?.xaxis?.type).toBe('log'); + expect(filtered.layout?.xaxis).toMatchObject({ + type: 'log', + tickvals: [1, 2, 3, 5, 7, 10], + ticktext: ['1', '2', '3', '5', '7', '10'], + }); expect(filtered.data[0]).toMatchObject({ x: [3], error_x: { array: [1.5], arrayminus: [1.8] }, }); }); + it('incluye los intervalos y la referencia en los ticks decimales del bosque', () => { + const figure = forest([{ ...pair, odds: 0.3, low: 0.12, high: 7 }], false); + expect(figure.layout?.xaxis).toMatchObject({ + tickvals: [0.1, 0.2, 0.3, 0.5, 0.7, 1, 2, 3, 5, 7, 10], + ticktext: ['0.1', '0.2', '0.3', '0.5', '0.7', '1', '2', '3', '5', '7', '10'], + }); + expect(forest([{ ...pair, low: 2, high: 30 }], false).layout?.xaxis?.tickvals).toEqual([ + 1, 2, 3, 5, 7, 10, 20, 30, 50, 70, 100, + ]); + }); + it('marca la banda de z entre −2 y 2', () => { const figure = zBars(eeg); expect(figure.data[0]).toMatchObject({ @@ -273,6 +288,19 @@ describe('Expediente TEA', () => { ]); }); + it('identifica la probabilidad del ADOS-2 severo en la tabla del expediente', () => { + const section = recordSections(record, new Map()).find( + (section) => section.title === 'Relación con el ADOS-2', + ); + expect(section?.blocks[0].table?.headers).toEqual([ + 'Par', + 'OR', + 'IC 95 %', + 'Prob. ADOS-2 severo', + 'Referencia', + ]); + }); + it('dibuja la línea del tiempo como un Gantt con rombos, no como tabla', () => { const [section] = recordSections(record, new Map()); expect(section.title).toBe('Línea del tiempo'); @@ -362,7 +390,7 @@ describe('Análisis de riesgo TEA', () => { 'Qué calcula', 'Puntuaciones z por métrica y zona', forestTitle, - 'Probabilidad frente a la referencia', + 'Probabilidad de un puntaje ADOS-2 severo', probabilityTitle, 'Limitaciones', ]); @@ -374,6 +402,24 @@ describe('Análisis de riesgo TEA', () => { expect(effects?.blocks[0].chart?.figure.data[0]).toMatchObject({ x: [3] }); const chosen = sections.find((section) => section.title === probabilityTitle); expect(chosen?.blocks[0].chart?.title).toBe(pair.title); + expect(sections[0].blocks[0].text).toBe( + 'Para cada alteración del EEG y región cerebral, el modelo da la probabilidad de que un ítem, un área o el puntaje total (CSS) del ADOS-2 salga severo, comparado con un niño en la referencia (z = 0).', + ); + const probability = sections.find( + (section) => section.title === 'Probabilidad de un puntaje ADOS-2 severo', + ); + expect(probability?.blocks).toEqual([ + { + table: { + headers: ['Par', 'Prob. ADOS-2 severo', 'Referencia'], + rows: [['Husos N2 · Central · CSS (principal)', '70%', '20%']], + }, + }, + { text: 'No es la probabilidad de TEA.' }, + ]); + expect(teaPdfContent('Análisis de riesgo TEA', sections, [])).toContain( + 'No es la probabilidad de TEA.', + ); }); }); diff --git a/src/app/paciente/tea-sections.ts b/src/app/paciente/tea-sections.ts index aeea330..43e73a5 100644 --- a/src/app/paciente/tea-sections.ts +++ b/src/app/paciente/tea-sections.ts @@ -19,7 +19,7 @@ export const forestTitle = 'Razones de momios por par'; export const probabilityTitle = 'Probabilidad según z'; const calculation = - 'El modelo estima, para cada par de métrica o zona del EEG y resultado del ADOS-2, la probabilidad de un puntaje elevado de severidad calibrada (CSS) o de un ítem alterado. La razón de momios corresponde al cambio asociado con una unidad de z. La probabilidad de referencia corresponde a z = 0, la edad media y el módulo más frecuente de la cohorte.'; + 'Para cada alteración del EEG y región cerebral, el modelo da la probabilidad de que un ítem, un área o el puntaje total (CSS) del ADOS-2 salga severo, comparado con un niño en la referencia (z = 0).'; const limitations = 'Las estimaciones dependen de la cohorte de referencia, de la calidad del EEG y de la disponibilidad de mediciones y pares. Las asociaciones no establecen causalidad. Las probabilidades no sustituyen la evaluación clínica ni establecen un diagnóstico.'; @@ -87,7 +87,7 @@ export function recordSections(patient: TeaPatient, norms: ReadonlyMap [ p.headline ? `${p.title} (principal)` : p.title, percent(p.probability),