import { DecimalPipe } from '@angular/common'; import { Component, input } from '@angular/core'; interface Prediction { years: number; p: number; low?: number; high?: number; } export interface RiskScale { label: string; risk?: Prediction; } /** Whether a model's estimate is a risk of the outcome the scales and curves draw. Survival (S(t)) and * recurrence-free survival are estimates of another outcome, shown in their own analysis: they are * never drawn on these scales, and no conversion (1 − S) is made. */ export const isRiskModel = (model: string) => !/.(survival|recurrence)$/.test(model); /** Each model's risk at its index visit and at five years; none where it gives no such horizon. */ export function riskScales(models: { text: string; predictions: Prediction[] }[]): RiskScale[] { return models.flatMap((m) => [0, 5].map((years) => ({ label: `${m.text.replace(/riesgo/gi, (word) => (word[0] === 'R' ? 'Probabilidad' : 'probabilidad'))}, probabilidad ${years ? 'a 5 años' : 'inmediata'}`, risk: m.predictions.find((p) => p.years === years), })), ); } const unit = (n?: number): n is number => typeof n === 'number' && n >= 0 && n <= 1; /** A model's published prediction at one horizon, when it is a probability: zero is a real value, * a missing horizon is none (never taken from another horizon), a value outside 0 to 1 is none. */ export function horizon(predictions: Prediction[], years: number): Prediction | undefined { const found = predictions.find((p) => p.years === years); if (!found || !unit(found.p)) return undefined; const interval = unit(found.low) && unit(found.high) && found.low <= found.high; return { ...found, low: interval ? found.low : undefined, high: interval ? found.high : undefined, }; } /** One model's two horizons, each with its prediction or none. */ export interface RiskGroup { /** the model's own title and the day it was scored (none: no model has scored her) */ title?: string; scenario?: string; index?: string; survival?: boolean; inputs?: Record; /** the model and version that scored her, and the day it did */ model?: string; evaluated?: string; scales: { label: string; risk?: Prediction }[]; } /** Stored survival-model probabilities are mortality: complement them to obtain S(t). */ export function survivalGroups( models: { index: string; predictions: Prediction[]; scenarios?: string[]; inputs?: Record; text?: string; evaluated?: string; }[], ): RiskGroup[] { 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.flatMap((p) => { const v = horizon(m.predictions, p.years); return v ? [ { label: `Supervivencia a ${p.years} ${p.years === 1 ? 'año' : 'años'}`, risk: { years: p.years, p: 1 - v.p, low: v.high === undefined ? undefined : 1 - v.high, high: v.low === undefined ? undefined : 1 - v.low, }, }, ] : []; }), })) .filter((g) => g.scales.length); } const HORIZONS: [number, string][] = [ [0, 'Probabilidad inmediata (al índice)'], [5, 'Probabilidad a 5 años'], ]; /** A model's published title, "Riesgo de X en 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: title.replace(/riesgo/gi, (word) => (word[0] === 'R' ? 'Probabilidad' : 'probabilidad')), scenario, }; } /** The scales the page draws: both horizons of every model, or, with no model, both horizons * unavailable. */ export function riskGroups( models: { text: string; index: string; predictions: Prediction[]; inputs?: Record; evaluated?: string; }[], ): RiskGroup[] { const scales = (predictions: Prediction[]) => HORIZONS.map(([years, label]) => ({ label, risk: horizon(predictions, years) })); return models.length ? models.map((m) => ({ ...titleParts(m.text), index: m.index, inputs: m.inputs, model: m.text, evaluated: m.evaluated, scales: scales(m.predictions), })) : [{ scales: scales([]) }]; } /** Segmented scales of 0 to 100 %. The four equal parts are numeric and descriptive: they do not * stand for clinical thresholds. Without a prediction the bar is neutral grey, says it is not * available and has no marker. */ @Component({ selector: 'app-risk-scales', imports: [DecimalPipe], template: ` @for (g of groups(); track $index) { @if (g.title) {

{{ g.title }}

} @for (s of g.scales; track s.label) {

{{ s.label }} @if (s.risk; as r) { @if (r.low !== undefined && r.high !== undefined) { · IC 95 %: {{ r.low * 100 | number: '1.1-2' }}–{{ r.high * 100 | number: '1.1-2' }} % } } @else { · No disponible }

@if (s.risk; as r) { @if (r.low !== undefined && r.high !== undefined) { } {{ r.p * 100 | number: '1.1-2' }} % }
} @if (g.index) {

{{ g.scenario ? 'Escenario: ' + g.scenario + ' · ' : '' }}Fecha índice {{ g.index }}

} @if (g.title && !g.survival) {

Factores de riesgo considerados

@if (g.inputs) {
@for (item of entries(g.inputs); track item[0]) {
{{ inputName(item[0]) }}
{{ inputValue(item[0], item[1]) }}
}
} @else {

Los valores utilizados no están disponibles para esta estimación.

}
} @if (g.model) {
Métodos
Modelo y versión
{{ g.model }}
Fecha índice
{{ g.index }}
Fecha de evaluación
{{ g.evaluated ?? 'No disponible' }}
Covariables
@for (item of entries(g.inputs ?? {}); track item[0]) {
{{ inputName(item[0]) }}: {{ inputValue(item[0], item[1]) }}
} @empty { No disponibles para esta estimación. }
} } `, styles: ` .risk-scale { margin: 8px 0 14px; max-width: 740px; } .risk-scale p { margin: 0; } .risk-plot { padding-top: 30px; position: relative; } .risk-line { background: linear-gradient( to right, #42a77e 0 25%, #a8c243 25% 50%, #df8640 50% 75%, #c24269 75% 100% ); height: 16px; } .unavailable .risk-line { background: repeating-linear-gradient( to right, #d9dfe2 0, #d9dfe2 calc(25% - 1px), #fff calc(25% - 1px), #fff 25% ); } .survival .risk-line { background: linear-gradient( to right, #c24269 0 25%, #df8640 25% 50%, #a8c243 50% 75%, #42a77e 75% 100% ); } .risk-interval { border-left: 2px solid #17324d; border-right: 2px solid #17324d; height: 8px; min-width: 2px; position: absolute; top: 20px; } .risk-interval::before { background: #17324d; content: ''; height: 2px; left: 0; position: absolute; right: 0; top: 3px; } .foot { font-size: 12px; } .risk-marker { background: #17324d; bottom: -2px; height: 20px; position: absolute; transform: translateX(-50%); width: 3px; } .risk-value { color: #17324d; font-size: 13px; position: absolute; top: 1px; transform: translateX(-50%); white-space: nowrap; } .risk-labels { display: flex; font-size: 12px; justify-content: space-between; margin-top: 3px; } `, }) export class RiskScales { readonly groups = input.required(); protected readonly entries = Object.entries; protected readonly inputName = (name: string) => ( ({ age_group: 'Edad al diagnóstico', cytology: 'Citología', hpv: 'VPH', hpv_positive: 'VPH', hpv_16_18: 'VPH 16/18', referral_grade: 'Citología de referencia', bethesda: 'Citología', hpv_16: 'VPH 16', hpv_18: 'VPH 18', }) as Record )[name] ?? name; protected readonly inputValue = (name: string, value: unknown) => { 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') && (value === 0 || value === 1 || typeof value === 'boolean')) return value ? 'Positivo' : 'Negativo'; if (name.startsWith('hpv') && value === 'positive') return 'Positivo'; if (name.startsWith('hpv') && value === 'negative') return 'Negativo'; return String(value); }; }