Patient record: eligibility and currency of her risk, risk scales with intervals and factors, curves with the NCI series, survival by her values, antecedents and findings, route sources, CaMa PDF; TEA sections in their own component
This commit is contained in:
+122
-51
@@ -1,7 +1,7 @@
|
||||
import { DecimalPipe } from '@angular/common';
|
||||
import { Component, input } from '@angular/core';
|
||||
|
||||
interface Prediction {
|
||||
export interface Prediction {
|
||||
years: number;
|
||||
p: number;
|
||||
low?: number;
|
||||
@@ -11,65 +11,136 @@ 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[];
|
||||
}
|
||||
|
||||
/** 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[] {
|
||||
/** "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((years) => ({
|
||||
label: `${m.text}, riesgo ${years ? 'a 5 años' : 'inmediato'}`,
|
||||
risk: m.predictions.find((p) => p.years === years),
|
||||
[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],
|
||||
template: `
|
||||
@for (s of scales(); track s.label) {
|
||||
<div class="scale">
|
||||
<span>{{ s.label }}</span>
|
||||
<div class="line">
|
||||
@if (s.risk; as r) {
|
||||
<span class="mark" [style.left.%]="r.p * 100"></span>
|
||||
}
|
||||
</div>
|
||||
@if (s.risk; as r) {
|
||||
<strong>{{ r.p * 100 | number: '1.1-2' }} %</strong>
|
||||
@if (r.low !== undefined) {
|
||||
<span>
|
||||
IC 95 %: {{ r.low * 100 | number: '1.1-2' }}–{{ r.high! * 100 | number: '1.1-2' }} %
|
||||
</span>
|
||||
}
|
||||
} @else {
|
||||
<strong>No disponible</strong>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
`,
|
||||
styles: `
|
||||
.scale {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
.line {
|
||||
background: linear-gradient(to right, #a8c243, #c24269);
|
||||
flex: 1 1 200px;
|
||||
height: 8px;
|
||||
position: relative;
|
||||
}
|
||||
.mark {
|
||||
background: #333;
|
||||
height: 16px;
|
||||
position: absolute;
|
||||
top: -4px;
|
||||
width: 3px;
|
||||
}
|
||||
`,
|
||||
templateUrl: './risk-scales.html',
|
||||
styleUrl: './risk-scales.scss',
|
||||
})
|
||||
export class RiskScales {
|
||||
readonly scales = input.required<RiskScale[]>();
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user