147 lines
5.0 KiB
TypeScript
147 lines
5.0 KiB
TypeScript
import { DecimalPipe } from '@angular/common';
|
||
import { Component, input } from '@angular/core';
|
||
|
||
export interface Prediction {
|
||
years: number;
|
||
p: number;
|
||
low?: number;
|
||
high?: number;
|
||
}
|
||
export interface RiskScale {
|
||
label: string;
|
||
risk?: Prediction;
|
||
}
|
||
/** One model's scales: its title, scenario and index, what it was fed and who scored her. */
|
||
export interface RiskGroup {
|
||
title?: string;
|
||
scenario?: string;
|
||
index?: string;
|
||
/** S(t): its scale reads the other way (high is good) */
|
||
survival?: boolean;
|
||
inputs?: Record<string, unknown>;
|
||
model?: string;
|
||
evaluated?: string;
|
||
scales: RiskScale[];
|
||
}
|
||
interface Scored {
|
||
text: string;
|
||
index: string;
|
||
predictions: Prediction[];
|
||
inputs?: Record<string, unknown>;
|
||
evaluated?: string;
|
||
scenarios?: string[];
|
||
}
|
||
|
||
/** "Riesgo" as "probabilidad", its capital kept. */
|
||
const probability = (text: string) =>
|
||
text.replace(/riesgo/gi, (w) => (w[0] === 'R' ? 'Probabilidad' : 'probabilidad'));
|
||
const unit = (n?: number): n is number => typeof n === 'number' && n >= 0 && n <= 1;
|
||
const years = (n: number) => `${n} ${n === 1 ? 'año' : 'años'}`;
|
||
|
||
/** A model's prediction at one horizon when it is a probability: zero is a value, a missing
|
||
* horizon or a value outside 0–1 is none, and an interval stands only when it is whole. */
|
||
export function horizon(predictions: Prediction[], at: number): Prediction | undefined {
|
||
const found = predictions.find((p) => p.years === at);
|
||
if (!found || !unit(found.p)) return undefined;
|
||
const whole = unit(found.low) && unit(found.high) && found.low <= found.high;
|
||
return whole ? found : { years: at, p: found.p };
|
||
}
|
||
|
||
/** Each model's probability at its index visit and at five years (the record's PDF). */
|
||
export function riskScales(models: Scored[]): RiskScale[] {
|
||
return models.flatMap((m) =>
|
||
[0, 5].map((at) => ({
|
||
label: `${probability(m.text)}, probabilidad ${at ? 'a 5 años' : 'inmediata'}`,
|
||
risk: horizon(m.predictions, at),
|
||
})),
|
||
);
|
||
}
|
||
|
||
/** "Riesgo de X en <escenario> v1" as its outcome and its scenario. */
|
||
export function titleParts(text: string): { title: string; scenario?: string } {
|
||
const [, title, scenario] = /^(.*?)(?: en (.+?))?(?: v\d+)?$/.exec(text)!;
|
||
return { title: probability(title), scenario };
|
||
}
|
||
|
||
const HORIZONS: [number, string][] = [
|
||
[0, 'Probabilidad inmediata (al índice)'],
|
||
[5, 'Probabilidad a 5 años'],
|
||
];
|
||
|
||
/** Both horizons of every risk model; with no model, both horizons unavailable. */
|
||
export function riskGroups(models: Scored[]): RiskGroup[] {
|
||
const scales = (p: Prediction[]) =>
|
||
HORIZONS.map(([at, label]) => ({ label, risk: horizon(p, at) }));
|
||
if (!models.length) return [{ scales: scales([]) }];
|
||
return models.map((m) => ({
|
||
...titleParts(m.text),
|
||
index: m.index,
|
||
inputs: m.inputs,
|
||
model: m.text,
|
||
evaluated: m.evaluated,
|
||
scales: scales(m.predictions),
|
||
}));
|
||
}
|
||
|
||
/** Survival models publish mortality: their scales are its complement, S(t), interval swapped. */
|
||
export function survivalGroups(models: Scored[]): RiskGroup[] {
|
||
const complement = (n?: number) => (n === undefined ? undefined : 1 - n);
|
||
return models
|
||
.map((m) => ({
|
||
title: 'Supervivencia',
|
||
index: m.index,
|
||
survival: true,
|
||
inputs: m.inputs,
|
||
model: m.text,
|
||
evaluated: m.evaluated,
|
||
scenario: m.scenarios?.join(' · '),
|
||
scales: m.predictions
|
||
.map((p) => horizon(m.predictions, p.years))
|
||
.filter((v) => !!v)
|
||
.map((v) => ({
|
||
label: `Supervivencia a ${years(v.years)}`,
|
||
risk: { years: v.years, p: 1 - v.p, low: complement(v.high), high: complement(v.low) },
|
||
})),
|
||
}))
|
||
.filter((g) => g.scales.length);
|
||
}
|
||
|
||
const INPUTS: Record<string, string> = {
|
||
age_group: 'Edad al diagnóstico',
|
||
cytology: 'Citología',
|
||
bethesda: 'Citología',
|
||
hpv: 'VPH',
|
||
hpv_positive: 'VPH',
|
||
hpv_16_18: 'VPH 16/18',
|
||
hpv_16: 'VPH 16',
|
||
hpv_18: 'VPH 18',
|
||
referral_grade: 'Citología de referencia',
|
||
};
|
||
|
||
/** A model input's value as the screens say it. */
|
||
export function inputValue(name: string, value: unknown): string {
|
||
if (value === null || value === undefined) return 'No documentado';
|
||
if (name === 'referral_grade' && (value === 'high' || value === 'low'))
|
||
return value === 'high' ? 'Alto grado' : 'Bajo grado';
|
||
if (name.startsWith('hpv')) {
|
||
if (value === 1 || value === true || value === 'positive') return 'Positivo';
|
||
if (value === 0 || value === false || value === 'negative') return 'Negativo';
|
||
}
|
||
return String(value);
|
||
}
|
||
|
||
/** Scales of 0 to 100 % in four equal parts (descriptive, not clinical thresholds), the
|
||
* probability marked with its interval; one with no prediction is grey and says so. */
|
||
@Component({
|
||
selector: 'app-risk-scales',
|
||
imports: [DecimalPipe],
|
||
templateUrl: './risk-scales.html',
|
||
styleUrl: './risk-scales.scss',
|
||
})
|
||
export class RiskScales {
|
||
readonly groups = input.required<RiskGroup[]>();
|
||
protected readonly entries = (o?: Record<string, unknown>) => Object.entries(o ?? {});
|
||
protected readonly inputName = (name: string) => INPUTS[name] ?? name;
|
||
protected readonly inputValue = inputValue;
|
||
}
|