diff --git a/openapi.json b/openapi.json index 0577d59..1c9d22c 100644 --- a/openapi.json +++ b/openapi.json @@ -1065,6 +1065,14 @@ "title": "Code", "type": "string" }, + "electrodes": { + "default": [], + "items": { + "$ref": "#/components/schemas/Site" + }, + "title": "Electrodes", + "type": "array" + }, "median": { "anyOf": [ { @@ -1134,6 +1142,14 @@ } ], "title": "Z" + }, + "zones": { + "default": [], + "items": { + "$ref": "#/components/schemas/Site" + }, + "title": "Zones", + "type": "array" } }, "required": [ @@ -1512,6 +1528,36 @@ "title": "SignIn", "type": "object" }, + "Site": { + "properties": { + "site": { + "title": "Site", + "type": "string" + }, + "value": { + "title": "Value", + "type": "number" + }, + "z": { + "anyOf": [ + { + "type": "number" + }, + { + "type": "null" + } + ], + "title": "Z" + } + }, + "required": [ + "site", + "value", + "z" + ], + "title": "Site", + "type": "object" + }, "Source": { "additionalProperties": false, "description": "One table, file set or API resource, copied as it is (dlt). Merged on the source's\nown primary key or `primary_key`; replaced whole when it has none.", diff --git a/src/app/api/models.ts b/src/app/api/models.ts index 0521c53..0e434a7 100644 --- a/src/app/api/models.ts +++ b/src/app/api/models.ts @@ -35,6 +35,7 @@ export type { Route } from './models/route'; export type { RunRequest } from './models/run-request'; export type { SavedPipeline } from './models/saved-pipeline'; export type { SignIn } from './models/sign-in'; +export type { Site } from './models/site'; export type { Source } from './models/source'; export type { SourceRef } from './models/source-ref'; export type { SourceType } from './models/source-type'; diff --git a/src/app/api/models/metric.ts b/src/app/api/models/metric.ts index fcbbafd..d210c52 100644 --- a/src/app/api/models/metric.ts +++ b/src/app/api/models/metric.ts @@ -1,8 +1,10 @@ /* eslint-disable */ /* Code generated by ng-openapi-gen DO NOT EDIT. */ +import { Site } from '../models/site'; export interface Metric { code: string; + electrodes?: Array; median?: (number | null); model: (string | null); outside: (number | null); @@ -10,4 +12,5 @@ export interface Metric { p95?: (number | null); value: number; z: (number | null); + zones?: Array; } diff --git a/src/app/api/models/site.ts b/src/app/api/models/site.ts new file mode 100644 index 0000000..2cfc960 --- /dev/null +++ b/src/app/api/models/site.ts @@ -0,0 +1,8 @@ +/* eslint-disable */ +/* Code generated by ng-openapi-gen DO NOT EDIT. */ + +export interface Site { + site: string; + value: number; + z: (number | null); +} diff --git a/src/app/eeg-labels.ts b/src/app/eeg-labels.ts new file mode 100644 index 0000000..5431bbd --- /dev/null +++ b/src/app/eeg-labels.ts @@ -0,0 +1,99 @@ +import type { Figure } from './plot'; + +/** The EEG metrics (models.tea.eeg.biomarkers.CODES), as the pages name them. */ +export const METRIC_NAMES: Record = { + posterior_rhythm: 'Ritmo posterior (Hz)', + spindles_n2: 'Husos de sueño N2 (por min)', + spindles_n3: 'Husos de sueño N3 (por min)', + paroxysmal_spikes: 'Picos paroxísticos (por min)', + anteroposterior_gradient: 'Gradiente anteroposterior', +}; + +/** The zones the metrics are summed over (models.tea.eeg.spindles.ZONES). */ +export const ZONE_NAMES: Record = { + frontal_left: 'Frontal izquierda', + frontal_right: 'Frontal derecha', + frontal_midline: 'Frontal media', + central: 'Central', + parietal: 'Parietal', + temporal_left: 'Temporal izquierda', + temporal_right: 'Temporal derecha', + occipital: 'Occipital', +}; + +/** Where each electrode of the 10-20 system sits on a head seen from above (nose up). */ +const AT: Record = { + Fp1: [-0.31, 0.95], Fp2: [0.31, 0.95], + F7: [-0.81, 0.59], F3: [-0.4, 0.53], Fz: [0, 0.5], F4: [0.4, 0.53], F8: [0.81, 0.59], + T3: [-1, 0], C3: [-0.5, 0], Cz: [0, 0], C4: [0.5, 0], T4: [1, 0], + T5: [-0.81, -0.59], P3: [-0.4, -0.53], Pz: [0, -0.5], P4: [0.4, -0.53], T6: [0.81, -0.59], + O1: [-0.31, -0.95], O2: [0.31, -0.95], +}; // prettier-ignore + +export interface Site { + site: string; + value: number; + z?: number | null; +} + +/** Her electrodes on the head, coloured by how far each is from its age curve (z, blue + * below and red above), or by her own value where no curve scores electrodes. */ +export function headMap(electrodes: Site[]): Figure { + const placed = electrodes.filter((e) => AT[e.site]); + const scored = placed.some((e) => e.z !== null && e.z !== undefined); + const colour = placed.map((e) => (scored ? (e.z ?? 0) : e.value)); + const ring = Array.from({ length: 73 }, (_, i) => (i * 2 * Math.PI) / 72); + return { + data: [ + { + x: ring.map((t) => 1.12 * Math.sin(t)), + y: ring.map((t) => 1.12 * Math.cos(t)), + mode: 'lines', + line: { color: '#9aa0a6', width: 1 }, + hoverinfo: 'skip', + showlegend: false, + }, + { + x: [-0.12, 0, 0.12], + y: [1.1, 1.24, 1.1], + mode: 'lines', + line: { color: '#9aa0a6', width: 1 }, + hoverinfo: 'skip', + showlegend: false, + }, + { + x: placed.map((e) => AT[e.site][0]), + y: placed.map((e) => AT[e.site][1]), + text: placed.map((e) => e.site), + customdata: colour, + mode: 'markers+text', + textposition: 'top center', + marker: { + size: 22, + color: colour, + colorscale: 'RdBu', + reversescale: true, + ...(scored ? { cmin: -3, cmax: 3, cmid: 0 } : {}), + colorbar: { title: { text: scored ? 'z' : 'valor' }, thickness: 10 }, + line: { color: '#ffffff', width: 1 }, + }, + hovertemplate: scored ? '%{text}: z = %{customdata:.2f}' + : '%{text}: %{customdata:.3f}', // prettier-ignore + showlegend: false, + }, + ], + layout: { + xaxis: { visible: false, range: [-1.35, 1.35], scaleanchor: 'y' }, + yaxis: { visible: false, range: [-1.25, 1.35] }, + margin: { l: 10, r: 10, t: 10, b: 10 }, + }, + } as Figure; +} + +/** The zones, most affected first: by how far from the age curve, else by value. */ +export function byDeviation(zones: Site[]): Site[] { + const scored = zones.some((z) => z.z !== null && z.z !== undefined); + return [...zones].sort((a, b) => + scored ? Math.abs(b.z ?? 0) - Math.abs(a.z ?? 0) : b.value - a.value, + ); +} diff --git a/src/app/gobernanza/figures.ts b/src/app/gobernanza/figures.ts index 504f7af..105cbc1 100644 --- a/src/app/gobernanza/figures.ts +++ b/src/app/gobernanza/figures.ts @@ -70,15 +70,7 @@ export function interpretabilityFigures( }, }, }, - // Its clinical scenarios, those with enough deaths to draw. - ...((report['escenarios'] ?? []) as any[]).map((e) => - scenarioFigure( - `${e.escenario} (n = ${e.n.toLocaleString('es-MX')}, fallecidos = ${e.fallecidos.toLocaleString('es-MX')})`, - e, - e.horizons_months, - 'Supervivencia S(t)', - ), - ), + // Its clinical scenarios are drawn in the patient record (Análisis de supervivencia). ]; } case 'subgroups': diff --git a/src/app/gobernanza/gobernanza.ts b/src/app/gobernanza/gobernanza.ts index d6e21a3..12d6bc1 100644 --- a/src/app/gobernanza/gobernanza.ts +++ b/src/app/gobernanza/gobernanza.ts @@ -1,3 +1,4 @@ +import { METRIC_NAMES } from '../eeg-labels'; import { HttpErrorResponse } from '@angular/common/http'; import { DatePipe, DecimalPipe } from '@angular/common'; import { Component, computed, inject, input, resource, signal } from '@angular/core'; @@ -93,13 +94,7 @@ export class Gobernanza { params: () => this.eeg(), loader: ({ params }) => this.api.invoke(study, { eeg_id: params }), }); - protected readonly metricNames: Record = { - posterior_rhythm: 'Ritmo posterior (Hz)', - spindles_n2: 'Husos de sueño N2 (por min)', - spindles_n3: 'Husos de sueño N3 (por min)', - paroxysmal_spikes: 'Picos paroxísticos (por min)', - anteroposterior_gradient: 'Gradiente anteroposterior', - }; + protected readonly metricNames = METRIC_NAMES; protected readonly asOf = signal(''); protected readonly run = signal<{ kind: string; status: string } | undefined>(undefined); protected readonly message = signal(''); diff --git a/src/app/paciente/paciente.html b/src/app/paciente/paciente.html index 8461815..c3e81bb 100644 --- a/src/app/paciente/paciente.html +++ b/src/app/paciente/paciente.html @@ -202,8 +202,18 @@ @for (m of alterations(); track m.code) { - - {{ m.code }} + + + + {{ m.value | number: '1.0-3' }} {{ m.p5 | number: '1.0-3' }} – {{ m.p95 | number: '1.0-3' }} {{ m.median | number: '1.0-3' }} @@ -213,6 +223,39 @@ } +

Elija una alteración para ver sus zonas más afectadas.

+ @if (zoned(); as z) { +
+

Zonas más afectadas

+

+ {{ metricNames[z.code] ?? z.code }} · + {{ + zonesScored() + ? 'desviación de cada zona frente a su norma por edad' + : 'valor de cada zona (sin norma por edad por zona)' + }} +

+ @if (z.electrodes.length) { + + } + @if (z.zones.length) { +
    + @for (r of zoneOrder(); track r.site) { +
  1. + {{ zoneNames[r.site] ?? r.site }} + {{ + r.z !== null && r.z !== undefined + ? 'z = ' + (r.z | number: '1.1-2') + : (r.value | number: '1.0-3') + }} +
  2. + } +
+ } @else if (!z.electrodes.length) { +

Esta métrica no se mide por zona.

+ } +
+ } } @else {

Sin alteraciones: todas sus métricas están dentro de la norma por edad.

} diff --git a/src/app/paciente/paciente.scss b/src/app/paciente/paciente.scss index a95f781..6d406c9 100644 --- a/src/app/paciente/paciente.scss +++ b/src/app/paciente/paciente.scss @@ -56,3 +56,26 @@ mat-card-content { flex-wrap: wrap; gap: 16px; } + +.zones { + margin-top: 12px; +} +.regions { + list-style: none; + margin: 8px 0 0; + padding: 0; + max-width: 420px; + li { + display: flex; + justify-content: space-between; + padding: 4px 8px; + border-bottom: 1px solid var(--mat-sys-outline-variant); + } + li.marked { + font-weight: 600; + color: var(--mat-sys-error); + } +} +tr.chosen td { + background: var(--mat-sys-surface-container); +} diff --git a/src/app/paciente/paciente.ts b/src/app/paciente/paciente.ts index 87e20da..1459266 100644 --- a/src/app/paciente/paciente.ts +++ b/src/app/paciente/paciente.ts @@ -21,6 +21,7 @@ import { code, EXT, ext, Fhir, label, resources } from '../fhir'; import { scenarioFigure } from '../gobernanza/figures'; import { CURP } from '../indicadores/follow-up'; import { name } from '../indicadores/measures'; +import { byDeviation, headMap, METRIC_NAMES, ZONE_NAMES } from '../eeg-labels'; import { Figure, PALETTE, Plot } from '../plot'; import { Loading } from '../loading'; @@ -233,6 +234,21 @@ export class Paciente { (this.eeg.value()?.analysis?.metrics ?? []).some((m) => m.z !== null && m.z !== undefined), ); protected readonly shown = signal(undefined); + protected readonly metricNames = METRIC_NAMES; + protected readonly zoneNames = ZONE_NAMES; + /** "Zonas más afectadas": the alteration chosen, its zones and electrodes. */ + protected readonly zone = signal(undefined); + protected readonly zoned = computed(() => { + const m = this.alterations().find((a) => a.code === this.zone()); + return m ? { code: m.code, zones: m.zones ?? [], electrodes: m.electrodes ?? [] } : undefined; + }); + protected readonly zonesScored = computed(() => + [...(this.zoned()?.zones ?? []), ...(this.zoned()?.electrodes ?? [])].some( + (s) => s.z !== null && s.z !== undefined, + ), + ); + protected readonly zoneOrder = computed(() => byDeviation(this.zoned()?.zones ?? [])); + protected readonly head = computed(() => headMap(this.zoned()?.electrodes ?? [])); protected readonly curve = computed
(() => { const models = this.models().filter((m) => m.predictions.length > 1); return {