diff --git a/src/app/paciente/curve-chooser.spec.ts b/src/app/paciente/curve-chooser.spec.ts new file mode 100644 index 0000000..7f5eba5 --- /dev/null +++ b/src/app/paciente/curve-chooser.spec.ts @@ -0,0 +1,10 @@ +import { figureChoices } from './curve-chooser'; + +describe('figureChoices', () => { + it('keeps each band with its line, one choice per curve', () => { + const data = [{ showlegend: false }, { name: 'Kaplan-Meier' }, { name: 'Weibull' }]; + const { choices, draw } = figureChoices({ data: data as never }); + expect(choices.map((c) => c.name)).toEqual(['Kaplan-Meier', 'Weibull']); + expect(draw(new Set(['Kaplan-Meier'])).data.length).toBe(2); + }); +}); diff --git a/src/app/paciente/curve-chooser.ts b/src/app/paciente/curve-chooser.ts new file mode 100644 index 0000000..7dd49b4 --- /dev/null +++ b/src/app/paciente/curve-chooser.ts @@ -0,0 +1,86 @@ +import { Component, computed, input, signal } from '@angular/core'; +import { MatCheckboxModule } from '@angular/material/checkbox'; +import { Figure, Plot } from '../plot'; + +export interface Choice { + id: string; + name: string; + /** drawn before the user touches it */ + on: boolean; +} + +/** A figure's curves as choices, all on: a band has no name of its own and goes with the line + * after it. */ +export function figureChoices(figure: Figure): { choices: Choice[]; draw: Draw } { + const groups = new Map(); + let pending: Figure['data'] = []; + for (const trace of figure.data) { + pending.push(trace); + const { name, showlegend } = trace as { name?: string; showlegend?: boolean }; + if (name && showlegend !== false) { + groups.set(name, pending); + pending = []; + } + } + return { + choices: [...groups.keys()].map((name) => ({ id: name, name, on: true })), + draw: (ids) => ({ ...figure, data: [...ids].flatMap((id) => groups.get(id) ?? []) }), + }; +} + +export type Draw = (ids: Set) => Figure; + +/** Curves chosen or not, to compare them on one plot: the record's risk and survival curves. */ +@Component({ + selector: 'app-curve-chooser', + imports: [MatCheckboxModule, Plot], + template: ` +
+ @for (c of choices(); track c.id) { + + {{ c.name }} + + } +
+ @if (chosen().size) { + @for (note of notes()(chosen()); track note) { +

{{ note }}

+ } + + } + `, + styles: ` + :host { + display: block; + } + .choices { + display: flex; + flex-wrap: wrap; + gap: 0 16px; + margin-bottom: 8px; + } + `, +}) +export class CurveChooser { + readonly choices = input.required(); + readonly draw = input.required(); + /** what the chosen curves assume, said under the choices */ + readonly notes = input<(ids: Set) => string[]>(() => []); + /** what the user toggled away from its default */ + private readonly toggled = signal(new Set()); + protected readonly chosen = computed( + () => + new Set( + this.choices() + .filter((c) => c.on !== this.toggled().has(c.id)) + .map((c) => c.id), + ), + ); + protected readonly figure = computed(() => this.draw()(this.chosen())); + + protected toggle(id: string) { + const next = new Set(this.toggled()); + if (!next.delete(id)) next.add(id); + this.toggled.set(next); + } +} diff --git a/src/app/paciente/paciente.html b/src/app/paciente/paciente.html index 5f7abed..9a4aca5 100644 --- a/src/app/paciente/paciente.html +++ b/src/app/paciente/paciente.html @@ -610,13 +610,13 @@ } @else if (combination.error()) { } @else if (combination.value(); as c) { - @if (c.figure) { + @if (c.curves) {

Ajustes sobre las pacientes con sus mismos valores (Kaplan-Meier con IC 95 % Greenwood; n = {{ c.n }}, fallecidos = {{ c.fallecidos }}).

- + } @else {

{{ c.missing }}

} diff --git a/src/app/paciente/paciente.ts b/src/app/paciente/paciente.ts index d9cb261..60d30cf 100644 --- a/src/app/paciente/paciente.ts +++ b/src/app/paciente/paciente.ts @@ -50,7 +50,7 @@ import type { Relation } from '../api/models/relation'; import { Figure, PALETTE, Plot } from '../plot'; import { Loading } from '../loading'; import { Factores, ScenarioFactors } from './factores'; -import { SurvivalCurves } from './survival-curves'; +import { CurveChooser, figureChoices } from './curve-chooser'; /** An Observation's value as the screens say it, whatever its type. */ function shown(o: Observation): string { @@ -104,7 +104,7 @@ function modelInputs(value: string | undefined): Record | undef selector: 'app-paciente', imports: [ Factores, - SurvivalCurves, + CurveChooser, Loading, DecimalPipe, MatButtonModule, @@ -237,7 +237,7 @@ export class Paciente { }); const found = combinationOf(report as Record, inputs); return found?.km - ? { n: found.n, fallecidos: found.fallecidos, figure: survivalFigure(found) } + ? { n: found.n, fallecidos: found.fallecidos, curves: figureChoices(survivalFigure(found)) } : { missing: noCurveText(report as Record, found) }; }, }); diff --git a/src/app/paciente/risk-series.ts b/src/app/paciente/risk-series.ts index 6abc285..78d41c6 100644 --- a/src/app/paciente/risk-series.ts +++ b/src/app/paciente/risk-series.ts @@ -1,7 +1,6 @@ -import { Component, computed, input, signal } from '@angular/core'; -import { MatCheckboxModule } from '@angular/material/checkbox'; +import { Component, computed, input } from '@angular/core'; import type { Parameters, ParametersParameter } from 'fhir/r4'; -import { Plot } from '../plot'; +import { CurveChooser } from './curve-chooser'; import { curveFigure } from './risk-curve'; export interface SeriesPoint { @@ -64,23 +63,12 @@ export function parseSeries(answer: Parameters): Series[] { /** The chosen series as curves. Series are shown side by side; no difference is computed. */ @Component({ selector: 'app-risk-series', - imports: [MatCheckboxModule, Plot], + imports: [CurveChooser], template: `

Cada serie tiene su propio desenlace y población: no se restan entre sí.

-
- @for (s of available(); track s.id) { - - {{ name(s) }} - - } -
- @if (selected().length) { - @for (assumption of assumptions(); track assumption) { -

{{ assumption }}

- } - - } - @if (!available().length) { + @if (available().length) { + + } @else {

No disponible: datos insuficientes.

} `, @@ -88,52 +76,42 @@ export function parseSeries(answer: Parameters): Series[] { :host { display: block; } - .choices { - display: flex; - flex-wrap: wrap; - gap: 0 16px; - margin-bottom: 8px; - } `, }) export class RiskSeries { readonly series = input.required(); - protected readonly name = seriesName; protected readonly available = computed(() => this.series().filter((s) => s.points.length)); - /** what the user toggled away from the default (the institutional series) */ - private readonly toggled = signal(new Set()); - protected readonly chosen = computed( - () => - new Set( - this.available() - .filter((s) => (s.source === 'Institucional') !== this.toggled().has(s.id)) - .map((s) => s.id), - ), + /** the platform's own series are drawn until the user says otherwise */ + protected readonly choices = computed(() => + this.available().map((s) => ({ + id: s.id, + name: seriesName(s), + on: s.source === 'Institucional', + })), ); - protected readonly selected = computed(() => - this.available().filter((s) => this.chosen().has(s.id)), - ); - /** what the chosen series assume, each said once */ - protected readonly assumptions = computed(() => [ - ...new Set(this.selected().flatMap((s) => s.assumptions)), - ]); - protected readonly figure = computed(() => { - const shown = this.selected().map((s) => ({ - model: s.id, - text: seriesName(s), - index: '', - predictions: s.points, - })); - const top = Math.max(...shown.flatMap((m) => m.predictions.map((p) => p.high ?? p.p))); - const figure = curveFigure(shown, top); - // sources do not share an index date: the axis says only the horizon - figure.layout!.xaxis!.title = { text: 'Horizonte (años)' }; - return figure; + private readonly among = computed(() => { + const all = this.available(); + return (ids: Set) => all.filter((s) => ids.has(s.id)); + }); + /** what the chosen series assume, each said once */ + protected readonly assumptions = computed(() => { + const chosen = this.among(); + return (ids: Set) => [...new Set(chosen(ids).flatMap((s) => s.assumptions))]; + }); + protected readonly draw = computed(() => { + const chosen = this.among(); + return (ids: Set) => { + const shown = chosen(ids).map((s) => ({ + model: s.id, + text: seriesName(s), + index: '', + predictions: s.points, + })); + const top = Math.max(...shown.flatMap((m) => m.predictions.map((p) => p.high ?? p.p))); + const figure = curveFigure(shown, top); + // sources do not share an index date: the axis says only the horizon + figure.layout!.xaxis!.title = { text: 'Horizonte (años)' }; + return figure; + }; }); - - protected toggle(id: string) { - const next = new Set(this.toggled()); - if (!next.delete(id)) next.add(id); - this.toggled.set(next); - } } diff --git a/src/app/paciente/survival-curves.spec.ts b/src/app/paciente/survival-curves.spec.ts deleted file mode 100644 index eb33f0f..0000000 --- a/src/app/paciente/survival-curves.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { curveGroups } from './survival-curves'; - -describe('curveGroups', () => { - it('keeps each band with its line, one choice per curve', () => { - const data = [{ showlegend: false }, { name: 'Kaplan-Meier' }, { name: 'Weibull' }]; - const groups = curveGroups({ data: data as never }); - expect(groups.map((g) => [g.name, g.traces.length])).toEqual([ - ['Kaplan-Meier', 2], - ['Weibull', 1], - ]); - }); -}); diff --git a/src/app/paciente/survival-curves.ts b/src/app/paciente/survival-curves.ts deleted file mode 100644 index 5577e44..0000000 --- a/src/app/paciente/survival-curves.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Component, computed, input, signal } from '@angular/core'; -import { MatCheckboxModule } from '@angular/material/checkbox'; -import { Figure, Plot } from '../plot'; - -type Trace = Figure['data'][number]; - -/** The figure's curves by name; a band has no name of its own and goes with the line after it. */ -export function curveGroups(figure: Figure): { name: string; traces: Trace[] }[] { - const groups: { name: string; traces: Trace[] }[] = []; - let pending: Trace[] = []; - for (const trace of figure.data) { - pending.push(trace); - const { name, showlegend } = trace as { name?: string; showlegend?: boolean }; - if (name && showlegend !== false) { - groups.push({ name, traces: pending }); - pending = []; - } - } - return groups; -} - -/** The survival fits, each one chosen or not, to compare them on one plot. */ -@Component({ - selector: 'app-survival-curves', - imports: [MatCheckboxModule, Plot], - template: ` -
- @for (g of groups(); track g.name) { - - {{ g.name }} - - } -
- @if (shown().data.length) { - - } - `, - styles: ` - :host { - display: block; - } - .choices { - display: flex; - flex-wrap: wrap; - gap: 0 16px; - margin-bottom: 8px; - } - `, -}) -export class SurvivalCurves { - readonly figure = input.required
(); - protected readonly groups = computed(() => curveGroups(this.figure())); - private readonly hidden_ = signal(new Set()); - protected readonly hidden = this.hidden_.asReadonly(); - protected readonly shown = computed
(() => ({ - ...this.figure(), - data: this.groups() - .filter((g) => !this.hidden().has(g.name)) - .flatMap((g) => g.traces), - })); - - protected toggle(name: string) { - const next = new Set(this.hidden()); - if (!next.delete(name)) next.add(name); - this.hidden_.set(next); - } -}