71 lines
4.9 KiB
TypeScript
71 lines
4.9 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
import { catchError, map, Observable, of } from 'rxjs';
|
|
import { FilaFactores, filaFactores } from './cama-curvas.service';
|
|
|
|
// CaMa's recurrence figures of a patient, `GET /cama/modelo/recurrencia?paciente=`: the clinical scenarios her covariates meet,
|
|
// each as the recurrence analysis of an external cohort (METABRIC or SCAN-B) drew it. They describe the scenario's patients in
|
|
// that cohort, not her; the answer names the version that published them. A 404 or a failure is "none to show".
|
|
export interface FiguraRecurrencia {
|
|
cohorte: string; cohorte_nombre: string; escenario: string; n: number; recaidas: number;
|
|
horizons_months: number[]; observado_hasta: number; months_max: number;
|
|
km: { months: number[]; S: number[]; low: number[]; high: number[] };
|
|
weibull: { t: number[]; S: number[] };
|
|
weibull_fractional: { t: number[]; S: number[] };
|
|
points: { COMB: { S: number[]; low: number[]; high: number[] } };
|
|
}
|
|
export interface RespuestaRecurrencia {
|
|
modelo: { nombre: string; version: string }; horizontes: number[]; escenarios: FiguraRecurrencia[];
|
|
/** The summary's rows, one per cohort, scenario and horizon: the risk-factor label. */
|
|
resumen: FilaFactores[];
|
|
/** The cohort and scenario the patient's own covariates place her in first, if any. */
|
|
predeterminado: { cohorte: string; escenario: string } | null;
|
|
/** Every scenario her covariates place her in, the larger first within each cohort; empty when none or no patient was asked. */
|
|
deLaPaciente: { cohorte: string; escenario: string }[];
|
|
}
|
|
|
|
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
const numbers = (v: unknown, length?: number): v is number[] => Array.isArray(v) && v.every(x => typeof x === 'number' && Number.isFinite(x)) && (length === undefined || v.length === length);
|
|
const probabilities = (v: unknown, length?: number): v is number[] => numbers(v, length) && v.every(p => p >= 0 && p <= 1);
|
|
|
|
function invalid(): never { throw new Error('Respuesta de recurrencia inválida'); }
|
|
|
|
function figura(v: unknown, horizons: number): FiguraRecurrencia {
|
|
if (!record(v) || typeof v['escenario'] !== 'string' || typeof v['cohorte_nombre'] !== 'string' || typeof v['cohorte'] !== 'string') invalid();
|
|
const f = v as Record<string, any>;
|
|
if (!numbers(f['horizons_months'], horizons) || !Number.isFinite(f['n']) || !Number.isFinite(f['recaidas']) || !Number.isFinite(f['observado_hasta']) || !Number.isFinite(f['months_max'])) invalid();
|
|
const km = f['km'], w = f['weibull'], k = f['weibull_fractional'], p = f['points']?.['COMB'];
|
|
const n = Array.isArray(km?.months) ? km.months.length : -1;
|
|
if (!numbers(km?.months) || !probabilities(km?.S, n) || !probabilities(km?.low, n) || !probabilities(km?.high, n)) invalid();
|
|
for (const c of [w, k]) if (!numbers(c?.t) || !probabilities(c?.S, c.t.length)) invalid();
|
|
if (!probabilities(p?.S, horizons) || !probabilities(p?.low, horizons) || !probabilities(p?.high, horizons)) invalid();
|
|
if ((p.low as number[]).some((l, i) => l > p.high[i])) invalid();
|
|
return f as FiguraRecurrencia;
|
|
}
|
|
|
|
/** Validate at the boundary: the shapes and ranges, so the chart never draws a partial or impossible curve. */
|
|
export function respuestaRecurrencia(v: unknown): RespuestaRecurrencia {
|
|
if (!record(v) || !record(v['modelo']) || !Array.isArray(v['escenarios']) || !numbers(v['horizontes'])) invalid();
|
|
const m = v['modelo'] as Record<string, unknown>, h = (v['horizontes'] as number[]).length;
|
|
if (typeof m['nombre'] !== 'string' || typeof m['version'] !== 'string') invalid();
|
|
if (!Array.isArray(v['resumen'])) invalid();
|
|
const d = v['predeterminado'];
|
|
const suyos = Array.isArray(v['de_la_paciente']) ? (v['de_la_paciente'] as unknown[]) : [];
|
|
return { modelo: { nombre: m['nombre'] as string, version: m['version'] as string }, horizontes: v['horizontes'] as number[],
|
|
escenarios: (v['escenarios'] as unknown[]).map(e => figura(e, h)), resumen: (v['resumen'] as unknown[]).map(filaFactores),
|
|
predeterminado: record(d) && typeof d['cohorte'] === 'string' && typeof d['escenario'] === 'string' ? { cohorte: d['cohorte'], escenario: d['escenario'] } : null,
|
|
deLaPaciente: suyos.filter((e): e is Record<string, string> => record(e) && typeof e['cohorte'] === 'string' && typeof e['escenario'] === 'string')
|
|
.map(e => ({ cohorte: e['cohorte'], escenario: e['escenario'] })) };
|
|
}
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class CamaRecurrenciaService {
|
|
private readonly http = inject(HttpClient);
|
|
|
|
/** The recurrence analysis of both cohorts and the patient's default, or `null` when there is no recurrence model in production or it fails. */
|
|
recurrencia(pacId: string): Observable<RespuestaRecurrencia | null> {
|
|
return this.http.get<unknown>('/cama/modelo/recurrencia', { params: { paciente: pacId } }).pipe(
|
|
map(respuestaRecurrencia), catchError(() => of(null)));
|
|
}
|
|
}
|