Usar un solo selector de curvas para riesgo y supervivencia
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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<string, Figure['data']>();
|
||||
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<string>) => 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: `
|
||||
<div class="choices">
|
||||
@for (c of choices(); track c.id) {
|
||||
<mat-checkbox [checked]="chosen().has(c.id)" (change)="toggle(c.id)">
|
||||
{{ c.name }}
|
||||
</mat-checkbox>
|
||||
}
|
||||
</div>
|
||||
@if (chosen().size) {
|
||||
@for (note of notes()(chosen()); track note) {
|
||||
<p class="muted">{{ note }}</p>
|
||||
}
|
||||
<app-plot [figure]="figure()" [height]="340" [toolbar]="false" />
|
||||
}
|
||||
`,
|
||||
styles: `
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
.choices {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0 16px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
`,
|
||||
})
|
||||
export class CurveChooser {
|
||||
readonly choices = input.required<Choice[]>();
|
||||
readonly draw = input.required<Draw>();
|
||||
/** what the chosen curves assume, said under the choices */
|
||||
readonly notes = input<(ids: Set<string>) => string[]>(() => []);
|
||||
/** what the user toggled away from its default */
|
||||
private readonly toggled = signal(new Set<string>());
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -610,13 +610,13 @@
|
||||
} @else if (combination.error()) {
|
||||
<p class="muted" role="alert">No se pudo leer el informe del modelo.</p>
|
||||
} @else if (combination.value(); as c) {
|
||||
@if (c.figure) {
|
||||
@if (c.curves) {
|
||||
<p class="muted">
|
||||
Ajustes sobre las pacientes con sus mismos valores (Kaplan-Meier con IC 95 %
|
||||
Greenwood; n =
|
||||
{{ c.n }}, fallecidos = {{ c.fallecidos }}).
|
||||
</p>
|
||||
<app-survival-curves [figure]="c.figure" />
|
||||
<app-curve-chooser [choices]="c.curves.choices" [draw]="c.curves.draw" />
|
||||
} @else {
|
||||
<p class="muted">{{ c.missing }}</p>
|
||||
}
|
||||
|
||||
@@ -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<string, unknown> | undef
|
||||
selector: 'app-paciente',
|
||||
imports: [
|
||||
Factores,
|
||||
SurvivalCurves,
|
||||
CurveChooser,
|
||||
Loading,
|
||||
DecimalPipe,
|
||||
MatButtonModule,
|
||||
@@ -237,7 +237,7 @@ export class Paciente {
|
||||
});
|
||||
const found = combinationOf(report as Record<string, unknown>, 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<string, unknown>, found) };
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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: `
|
||||
<p class="muted">Cada serie tiene su propio desenlace y población: no se restan entre sí.</p>
|
||||
<div class="choices">
|
||||
@for (s of available(); track s.id) {
|
||||
<mat-checkbox [checked]="chosen().has(s.id)" (change)="toggle(s.id)">
|
||||
{{ name(s) }}
|
||||
</mat-checkbox>
|
||||
}
|
||||
</div>
|
||||
@if (selected().length) {
|
||||
@for (assumption of assumptions(); track assumption) {
|
||||
<p class="muted">{{ assumption }}</p>
|
||||
}
|
||||
<app-plot [figure]="figure()" [height]="340" [toolbar]="false" />
|
||||
}
|
||||
@if (!available().length) {
|
||||
@if (available().length) {
|
||||
<app-curve-chooser [choices]="choices()" [draw]="draw()" [notes]="assumptions()" />
|
||||
} @else {
|
||||
<p class="muted">No disponible: datos insuficientes.</p>
|
||||
}
|
||||
`,
|
||||
@@ -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<Series[]>();
|
||||
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<string>());
|
||||
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<string>) => 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<string>) => [...new Set(chosen(ids).flatMap((s) => s.assumptions))];
|
||||
});
|
||||
protected readonly draw = computed(() => {
|
||||
const chosen = this.among();
|
||||
return (ids: Set<string>) => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -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: `
|
||||
<div class="choices">
|
||||
@for (g of groups(); track g.name) {
|
||||
<mat-checkbox [checked]="!hidden().has(g.name)" (change)="toggle(g.name)">
|
||||
{{ g.name }}
|
||||
</mat-checkbox>
|
||||
}
|
||||
</div>
|
||||
@if (shown().data.length) {
|
||||
<app-plot [figure]="shown()" [height]="340" [toolbar]="false" />
|
||||
}
|
||||
`,
|
||||
styles: `
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
.choices {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0 16px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
`,
|
||||
})
|
||||
export class SurvivalCurves {
|
||||
readonly figure = input.required<Figure>();
|
||||
protected readonly groups = computed(() => curveGroups(this.figure()));
|
||||
private readonly hidden_ = signal(new Set<string>());
|
||||
protected readonly hidden = this.hidden_.asReadonly();
|
||||
protected readonly shown = computed<Figure>(() => ({
|
||||
...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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user