118 lines
4.1 KiB
TypeScript
118 lines
4.1 KiB
TypeScript
import { Component, computed, input } from '@angular/core';
|
|
import type { Parameters, ParametersParameter } from 'fhir/r4';
|
|
import { CurveChooser } from './curve-chooser';
|
|
import { curveFigure } from './risk-curve';
|
|
|
|
export interface SeriesPoint {
|
|
years: number;
|
|
p: number;
|
|
low?: number;
|
|
high?: number;
|
|
}
|
|
|
|
/** One series of `$risk-series`: a source, an outcome and her variables. */
|
|
export interface Series {
|
|
id: string;
|
|
source: string;
|
|
outcome: string;
|
|
label: string;
|
|
resolved: boolean;
|
|
reasons: string[];
|
|
assumptions: string[];
|
|
points: SeriesPoint[];
|
|
}
|
|
|
|
const OUTCOME_TEXT: Record<string, string> = {
|
|
'CIN2+': 'LIEAG+ (CIN2+)',
|
|
'CIN3+': 'CIN3+',
|
|
cancer: 'Cáncer',
|
|
};
|
|
|
|
export const seriesName = (s: Series) => {
|
|
const outcome = OUTCOME_TEXT[s.outcome] ?? s.outcome;
|
|
if (s.source === 'NCI (tabla de ingreso)')
|
|
return `NCI, ingreso a ${outcome.replace(' (CIN2+)', '').replace(/^Cáncer$/, 'cáncer')}`;
|
|
// the platform's own models are the hospital's: its name, and the outcome without its synonym
|
|
return s.source === 'Institucional'
|
|
? `ISSSTELEON, ${outcome.replace(' (CIN2+)', '')}`
|
|
: `${s.source} · ${outcome}`;
|
|
};
|
|
|
|
/** The series of the answer. A value absent from it stays absent. */
|
|
export function parseSeries(answer: Parameters): Series[] {
|
|
const all = (parts: ParametersParameter[] | undefined, name: string) =>
|
|
(parts ?? []).filter((p) => p.name === name);
|
|
const one = (parts: ParametersParameter[] | undefined, name: string) => all(parts, name)[0];
|
|
return all(answer.parameter, 'series').map(({ part }) => ({
|
|
id: one(part, 'id')?.valueString ?? '',
|
|
source: one(part, 'source')?.valueString ?? '',
|
|
outcome: one(part, 'outcome')?.valueString ?? '',
|
|
label: one(part, 'label')?.valueString ?? '',
|
|
resolved: one(part, 'status')?.valueCode === 'resolved',
|
|
reasons: all(part, 'reason').map((r) => r.valueCode ?? ''),
|
|
assumptions: all(part, 'assumption').map((a) => a.valueString ?? ''),
|
|
points: all(part, 'point').map(({ part: at }) => ({
|
|
years: one(at, 'years')?.valueInteger ?? 0,
|
|
p: one(at, 'probability')?.valueDecimal ?? 0,
|
|
low: one(at, 'low')?.valueDecimal,
|
|
high: one(at, 'high')?.valueDecimal,
|
|
})),
|
|
}));
|
|
}
|
|
|
|
/** The chosen series as curves. Series are shown side by side; no difference is computed. */
|
|
@Component({
|
|
selector: 'app-risk-series',
|
|
imports: [CurveChooser],
|
|
template: `
|
|
<p class="muted">Cada serie tiene su propio desenlace y población: no se restan entre sí.</p>
|
|
@if (available().length) {
|
|
<app-curve-chooser [choices]="choices()" [draw]="draw()" [notes]="assumptions()" />
|
|
} @else {
|
|
<p class="muted">No disponible: datos insuficientes.</p>
|
|
}
|
|
`,
|
|
styles: `
|
|
:host {
|
|
display: block;
|
|
}
|
|
`,
|
|
})
|
|
export class RiskSeries {
|
|
readonly series = input.required<Series[]>();
|
|
protected readonly available = computed(() => this.series().filter((s) => s.points.length));
|
|
/** 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',
|
|
})),
|
|
);
|
|
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;
|
|
};
|
|
});
|
|
}
|