diff --git a/src/app/eeg-labels.ts b/src/app/eeg-labels.ts index 762d7a5..2e1a33f 100644 --- a/src/app/eeg-labels.ts +++ b/src/app/eeg-labels.ts @@ -43,3 +43,28 @@ export const OUTCOME_NAMES: Record = { ados2_item_22: 'Intereses repetitivos o conductas estereotipadas', CSS: 'Puntuación de severidad (CSS)', }; + +export const REASON_NAMES: Record = { + 'no-n2': 'sin N2', + 'no-n3': 'sin N3', + 'few-wake-windows': 'pocas ventanas en vigilia', + 'no-dominant-peak': 'sin ritmo dominante', + 'no-sleep': 'sin sueño', + 'few-electrodes': 'pocos electrodos', + 'no-age': 'sin edad', + 'age-out-of-range': 'edad fuera de la curva', + 'no-exposure': 'sin minutos de exposición', + 'invalid-eeg': 'EEG no válido', + 'few-clean-minutes': 'pocos minutos limpios', + 'triage-not-analyzable': 'triage no analizable', + 'invalid-file': 'archivo no válido', + gap: 'hueco de grabación', + state: 'estado excluido', + 'mixed-states': 'ventana entre dos estados', + muscle: 'músculo', + frozen: 'señal congelada', +}; + +export function reasonLabel(reason: string): string { + return REASON_NAMES[reason] ?? reason; +} diff --git a/src/app/paciente/tea-data.ts b/src/app/paciente/tea-data.ts index b532715..3e36e13 100644 --- a/src/app/paciente/tea-data.ts +++ b/src/app/paciente/tea-data.ts @@ -7,8 +7,8 @@ import type { RiskAssessment, } from 'fhir/r4'; import { ados2Component, ados2Groups } from '../ados2'; -import { ZONE_NAMES } from '../eeg-labels'; -import { code, CURP, ext, Fhir, label, resources } from '../fhir'; +import { reasonLabel, ZONE_NAMES } from '../eeg-labels'; +import { code, CURP, ext, extensions, Fhir, label, resources } from '../fhir'; export const TEA_STEPS = [ { code: 'mchat_only', label: 'M-CHAT' }, @@ -216,6 +216,7 @@ function metric(o: Observation) { unit: o.valueQuantity?.unit, z: ext(o, 'eeg-z')?.valueDecimal, outside: ext(o, 'eeg-outside')?.valueInteger, + reason: ext(o, 'eeg-reason')?.valueCode, model: ext(o, 'eeg-model')?.valueString, sites: (o.component ?? []).map((site): Site => ({ name: code(site.code) ?? '', @@ -232,6 +233,7 @@ function study(report: DiagnosticReport, observations: Map observations.get(r.reference?.split('/').pop())) .filter((o): o is Observation => !!o); + const reasons = extensions(report, 'eeg-reason').flatMap((e) => e.valueCode ?? []); return { id: report.id, date: shown(report.effectiveDateTime), @@ -241,6 +243,7 @@ function study(report: DiagnosticReport, observations: Map o.code.coding?.[0]?.system?.endsWith('tea-eeg-check')) @@ -266,8 +269,9 @@ export type Pair = { export type Study = ReturnType; -export function alteration(outside: number | undefined): string { - return outside == null ? '—' : outside < 0 ? 'Bajo' : outside > 0 ? 'Alto' : 'En rango'; +export function alteration(outside: number | undefined, reason?: string): string { + if (outside == null) return reason ? `No evaluable · ${reasonLabel(reason)}` : '—'; + return outside < 0 ? 'Bajo' : outside > 0 ? 'Alto' : 'En rango'; } function associations( diff --git a/src/app/paciente/tea-patient.spec.ts b/src/app/paciente/tea-patient.spec.ts index 424129f..bd10807 100644 --- a/src/app/paciente/tea-patient.spec.ts +++ b/src/app/paciente/tea-patient.spec.ts @@ -1,7 +1,16 @@ import type { Observation, Patient } from 'fhir/r4'; import { describe, expect, it } from 'vitest'; -import { EXT } from '../fhir'; -import { identityRows, Metric, Pair, pairTitle, TeaStep, zoneLabel } from './tea-data'; +import { EXT, Fhir } from '../fhir'; +import { + alteration, + identityRows, + loadTeaPatient, + Metric, + Pair, + pairTitle, + TeaStep, + zoneLabel, +} from './tea-data'; import { ageNorm, forest, @@ -28,6 +37,7 @@ const metric: Metric = { model: 'tea.eeg-spindles-n2/1', z: 0.3, outside: 0, + reason: undefined, sites: [ { kind: 'zone', name: 'central', z: 2.4, outside: 1 }, { kind: 'electrode', name: 'O1', z: -1.2, outside: 0 }, @@ -398,6 +408,74 @@ describe('Expediente TEA', () => { ]); }); + it('muestra "No evaluable" con el motivo donde antes había un guion', () => { + expect(alteration(undefined, 'no-n3')).toBe('No evaluable · sin N3'); + expect(alteration(undefined, 'age-out-of-range')).toBe('No evaluable · edad fuera de la curva'); + expect(alteration(undefined, 'unknown-code')).toBe('No evaluable · unknown-code'); + expect(alteration(undefined)).toBe('—'); + expect(alteration(1, 'no-n3')).toBe('Alto'); + const unmeasured = { ...metric, z: undefined, outside: undefined, reason: 'invalid-eeg' }; + const section = recordSections( + { ...record, studies: [{ ...eeg, metrics: [unmeasured] }] }, + new Map(), + ).find((s) => s.title === 'Alteraciones'); + expect(section?.blocks[0].fields).toEqual([ + ['z', '—'], + ['Resultado', 'No evaluable · EEG no válido'], + ]); + }); + + it('lee los motivos de la métrica y del estudio desde FHIR', async () => { + const reason = (valueCode: string) => ({ url: EXT + 'eeg-reason', valueCode }); + const bundle = { + resourceType: 'Bundle', + type: 'searchset', + entry: [ + { + search: { mode: 'match' }, + resource: { resourceType: 'Patient', id: 'child' }, + }, + { + search: { mode: 'include' }, + resource: { + resourceType: 'DiagnosticReport', + id: 'eeg-1', + status: 'final', + code: { coding: [{ code: 'eeg' }] }, + effectiveDateTime: '2025-11-30', + conclusionCode: [{ coding: [{ code: 'bad' }] }, { coding: [{ code: 'not_valid' }] }], + extension: [reason('few-clean-minutes'), reason('triage-not-analyzable')], + result: [{ reference: 'Observation/n3' }], + }, + }, + { + search: { mode: 'include' }, + resource: { + resourceType: 'Observation', + id: 'n3', + status: 'final', + code: { + coding: [ + { + system: 'https://isssteleon.org/fhir/CodeSystem/tea-eeg-metric', + code: 'spindles_n3', + }, + ], + }, + extension: [reason('no-n3')], + }, + }, + ], + }; + const fhir = { search: async () => bundle } as unknown as Fhir; + const { studies } = await loadTeaPatient(fhir, 'child'); + expect(studies[0].metrics[0]).toMatchObject({ key: 'spindles_n3', reason: 'no-n3' }); + expect(studies[0].summary).toContainEqual([ + 'Motivos', + 'pocos minutos limpios · triage no analizable', + ]); + }); + it('exporta las secciones, tablas y figuras con un nombre sin identificador', () => { const sections = [ ...recordSections(record, new Map()), diff --git a/src/app/paciente/tea-sections.ts b/src/app/paciente/tea-sections.ts index 9e1ae29..ba6b323 100644 --- a/src/app/paciente/tea-sections.ts +++ b/src/app/paciente/tea-sections.ts @@ -71,7 +71,7 @@ export function recordSections(patient: TeaPatient, norms: ReadonlyMap