diff --git a/src/app/paciente/paciente.html b/src/app/paciente/paciente.html
index 853f328..bb5b82f 100644
--- a/src/app/paciente/paciente.html
+++ b/src/app/paciente/paciente.html
@@ -265,6 +265,9 @@
@if (reference.value(); as ref) {
}
+ @if (riskSeries.value(); as series) {
+
+ }
}
@if (riskModels().length) {
diff --git a/src/app/paciente/paciente.ts b/src/app/paciente/paciente.ts
index 12e981a..98410c5 100644
--- a/src/app/paciente/paciente.ts
+++ b/src/app/paciente/paciente.ts
@@ -27,6 +27,7 @@ import { firstValueFrom } from 'rxjs';
import { ApiConfiguration } from '../api/api-configuration';
import { ANTECEDENTS, Fact, group, split } from './record-groups';
import { referenceView, ReferenceRisk, ReferenceView } from './reference-risk';
+import { parseSeries, RiskSeries, Series } from './risk-series';
import { curveFigure, curveText } from './risk-curve';
import { isRiskModel, riskGroups, riskScales, RiskScales } from './risk-scales';
import { route, Stages } from './stages';
@@ -88,6 +89,7 @@ function methods(figures: { figure: { data: unknown[] } }[]): string[] {
Plot,
RiskScales,
ReferenceRisk,
+ RiskSeries,
Stages,
],
templateUrl: './paciente.html',
@@ -181,6 +183,33 @@ export class Paciente {
protected readonly groups = computed(() =>
this.domain() === 'cacu' ? riskGroups(this.riskModels()) : [],
);
+ /** CaCu, experimental: the series her record can compare; none while the API has it off. */
+ protected readonly riskSeries = resource({
+ params: () =>
+ this.domain() === 'cacu'
+ ? { id: this.id(), cutoff: this.corte(), sample: this.citologia() }
+ : undefined,
+ loader: async ({ params }): Promise => {
+ try {
+ const answer = await firstValueFrom(
+ this.http.get(
+ `${this.root}/cacu/fhir/Patient/${params.id}/$risk-series`,
+ {
+ params: Object.fromEntries(
+ Object.entries({
+ cutoff: params.cutoff,
+ sample: params.sample?.slice(0, 10),
+ }).filter((e): e is [string, string] => !!e[1]),
+ ),
+ },
+ ),
+ );
+ return parseSeries(answer);
+ } catch {
+ return undefined;
+ }
+ },
+ });
/** CaCu, experimental: the NCI reference comparison of the cytology she was opened at. The API
* answers 404 while the feature is off: then nothing is shown. */
protected readonly reference = resource({
diff --git a/src/app/paciente/risk-series.spec.ts b/src/app/paciente/risk-series.spec.ts
new file mode 100644
index 0000000..2191002
--- /dev/null
+++ b/src/app/paciente/risk-series.spec.ts
@@ -0,0 +1,29 @@
+import { parseSeries, seriesName } from './risk-series';
+
+describe('risk series', () => {
+ it('reads each series and keeps an absent interval absent', () => {
+ const [series] = parseSeries({
+ resourceType: 'Parameters',
+ parameter: [
+ {
+ name: 'series',
+ part: [
+ { name: 'id', valueString: 'cacu.cin2_pim' },
+ { name: 'source', valueString: 'Institucional' },
+ { name: 'outcome', valueString: 'CIN2+' },
+ { name: 'status', valueCode: 'resolved' },
+ {
+ name: 'point',
+ part: [
+ { name: 'years', valueInteger: 0 },
+ { name: 'probability', valueDecimal: 0 },
+ ],
+ },
+ ],
+ },
+ ],
+ });
+ expect(series.points).toEqual([{ years: 0, p: 0, low: undefined, high: undefined }]);
+ expect(seriesName(series)).toBe('Institucional · LIEAG+ (CIN2+)');
+ });
+});
diff --git a/src/app/paciente/risk-series.ts b/src/app/paciente/risk-series.ts
new file mode 100644
index 0000000..ad99d16
--- /dev/null
+++ b/src/app/paciente/risk-series.ts
@@ -0,0 +1,206 @@
+import { DecimalPipe } from '@angular/common';
+import { Component, computed, input, signal } from '@angular/core';
+import { MatCheckboxModule } from '@angular/material/checkbox';
+import { MatFormFieldModule } from '@angular/material/form-field';
+import { MatSelectModule } from '@angular/material/select';
+import type { Parameters, ParametersParameter } from 'fhir/r4';
+import { Plot } from '../plot';
+import { curveFigure } from './risk-curve';
+import { REASON_TEXT } from './reference-risk';
+
+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 = {
+ 'CIN2+': 'LIEAG+ (CIN2+)',
+ 'CIN3+': 'CIN3+',
+ cancer: 'Cáncer',
+};
+const REASONS: Record = {
+ ...REASON_TEXT,
+ history_is_registered: 'Tiene antecedentes registrados: no corresponde la tabla de ingreso.',
+};
+
+export const seriesName = (s: Series) => `${s.source} · ${OUTCOME_TEXT[s.outcome] ?? s.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 bars at one horizon and as curves. Series are shown side by side; no
+ * difference between them is computed. */
+@Component({
+ selector: 'app-risk-series',
+ imports: [DecimalPipe, MatCheckboxModule, MatFormFieldModule, MatSelectModule, Plot],
+ template: `
+ Comparar riesgos
+ 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) {
+
+ Horizonte
+
+ @for (h of horizons(); track h) {
+ {{ h === 0 ? 'Al índice' : h + ' años' }}
+ }
+
+
+
+ @for (b of bars(); track b.series.id) {
+ -
+ {{ name(b.series) }}
+ {{ b.series.label }}
+ @if (b.point; as p) {
+
+
+ {{ p.p * 100 | number: '1.1-2' }} %
+ @if (p.low !== undefined && p.high !== undefined) {
+ (IC 95 %: {{ p.low * 100 | number: '1.1-2' }}–{{
+ p.high * 100 | number: '1.1-2'
+ }})
+ } @else {
+ (IC 95 % no publicado)
+ }
+
+ } @else {
+ No disponible en este horizonte.
+ }
+ @for (a of b.series.assumptions; track a) {
+ {{ a }}
+ }
+
+ }
+
+
+ }
+ @for (s of unavailable(); track s.id) {
+ {{ name(s) }}: no disponible. {{ why(s) }}
+ }
+ `,
+ styles: `
+ :host {
+ display: block;
+ }
+ .choices {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 0 16px;
+ margin-bottom: 8px;
+ }
+ .bars {
+ list-style: none;
+ padding: 0;
+ }
+ .bars li {
+ display: flex;
+ flex-direction: column;
+ margin-bottom: 10px;
+ }
+ .track {
+ background: var(--color-action-hover, #f1f6f6);
+ height: 10px;
+ }
+ .fill {
+ background: #4269a0;
+ display: block;
+ height: 100%;
+ }
+ `,
+})
+export class RiskSeries {
+ readonly series = input.required();
+ protected readonly name = seriesName;
+ protected readonly available = computed(() => this.series().filter((s) => s.points.length));
+ protected readonly unavailable = 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),
+ ),
+ );
+ protected readonly selected = computed(() =>
+ this.available().filter((s) => this.chosen().has(s.id)),
+ );
+ protected readonly horizons = computed(() =>
+ [...new Set(this.selected().flatMap((s) => s.points.map((p) => p.years)))].sort(
+ (a, b) => a - b,
+ ),
+ );
+ protected readonly picked = signal(5);
+ protected readonly horizon = computed(() =>
+ this.horizons().includes(this.picked()) ? this.picked() : this.horizons().at(-1)!,
+ );
+ protected readonly bars = computed(() =>
+ this.selected().map((series) => ({
+ series,
+ point: series.points.find((p) => p.years === this.horizon()),
+ })),
+ );
+ 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;
+ });
+
+ protected toggle(id: string) {
+ const next = new Set(this.toggled());
+ if (!next.delete(id)) next.add(id);
+ this.toggled.set(next);
+ }
+
+ protected why(s: Series) {
+ return s.reasons.map((r) => REASONS[r] ?? r).join(' ');
+ }
+}