Patient risk: a column per outcome, the index visit's sentence, and the model's details folded
The record reads every RiskAssessment of the patient. CaCu's table always has the LIEAG+ and cancer columns, each risk with its interval in parentheses; a column with no current model says "No disponible", and one whose model didn't estimate her says so, with the reason under the table. Another outcome (a survival model's death) shows only when she has its risk, with its own index date in its header. The models' details fold under the table, without the interval's method; the collapsible curves draw one line per outcome with the index date on the x axis, under a sentence that words the index cytology's results and each outcome's risk at the longest common horizon.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { isoDate } from '../cacu-seguimiento.models';
|
||||
import { EstimandoRiesgo, HorizonteRiesgoCacu, LoteRiesgoCacu, RiesgoPacienteCacu } from './paciente-riesgo.models';
|
||||
import { EstimandoRiesgo, HorizonteRiesgoCacu, LoteRiesgoCacu, ResultadosIndice, RiesgoPacienteCacu } from './paciente-riesgo.models';
|
||||
|
||||
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
|
||||
const finite = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
|
||||
@@ -10,7 +10,10 @@ const DAYS_PER_YEAR = 365.25;
|
||||
const INTERVAL = 'https://isssteleon.org/fhir/StructureDefinition/intervalo-confianza';
|
||||
const SIMULATED = 'simulado';
|
||||
const ESTIMAND = 'https://isssteleon.org/fhir/StructureDefinition/estimando';
|
||||
const ESTIMAND_KEYS: readonly (keyof EstimandoRiesgo)[] = ['quantity', 'description', 'outcome', 'index_date', 'horizons_unit', 'interval'];
|
||||
const NOT_SCORED = 'https://isssteleon.org/fhir/StructureDefinition/sin-estimacion';
|
||||
const INDEX_RESULTS = 'https://isssteleon.org/fhir/StructureDefinition/resultados-indice';
|
||||
const INDEX_RESULT_KEYS: readonly (keyof ResultadosIndice)[] = ['escamosas', 'glandulares', 'vph'];
|
||||
const ESTIMAND_KEYS: readonly (keyof EstimandoRiesgo)[] = ['quantity', 'description', 'outcome', 'index_date', 'horizons_unit'];
|
||||
|
||||
function utcTimestamp(v: unknown): v is string {
|
||||
return typeof v === 'string'
|
||||
@@ -41,23 +44,40 @@ function interval(prediction: Record<string, unknown>): { ic_bajo: number | null
|
||||
return { ic_bajo: low, ic_alto: high };
|
||||
}
|
||||
|
||||
/** The resource's `estimando` extension: its known keys with a text value; `null` without one. */
|
||||
function estimand(resource: Record<string, unknown>): EstimandoRiesgo | null {
|
||||
/** The resource's extension `url` as its known `keys` with a text value; `null` without that extension. */
|
||||
function stringExtension<K extends string>(resource: Record<string, unknown>, url: string,
|
||||
keys: readonly K[]): Partial<Record<K, string>> | null {
|
||||
const extensions = resource['extension'];
|
||||
if (extensions === undefined) return null;
|
||||
if (!Array.isArray(extensions)) invalid();
|
||||
const found = extensions.find(e => record(e) && e['url'] === ESTIMAND);
|
||||
const found = extensions.find(e => record(e) && e['url'] === url);
|
||||
if (found === undefined) return null;
|
||||
if (!Array.isArray(found['extension'])) invalid();
|
||||
const result: EstimandoRiesgo = {};
|
||||
const result: Partial<Record<K, string>> = {};
|
||||
for (const item of found['extension']) {
|
||||
if (!record(item) || !text(item['url'])) invalid();
|
||||
const key = item['url'] as keyof EstimandoRiesgo;
|
||||
if (ESTIMAND_KEYS.includes(key) && text(item['valueString'])) result[key] = item['valueString'];
|
||||
const key = item['url'] as K;
|
||||
if (keys.includes(key) && text(item['valueString'])) result[key] = item['valueString'];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const estimand = (resource: Record<string, unknown>): EstimandoRiesgo | null =>
|
||||
stringExtension(resource, ESTIMAND, ESTIMAND_KEYS);
|
||||
|
||||
/** The `sin-estimacion` extension: the outcome of a current model that didn't score her, and why; `null` without it. */
|
||||
function notScoredOutcome(resource: Record<string, unknown>): { desenlace: string; motivo: string } | null {
|
||||
const extensions = resource['extension'];
|
||||
if (!Array.isArray(extensions)) return null;
|
||||
const found = extensions.find(e => record(e) && e['url'] === NOT_SCORED);
|
||||
if (found === undefined) return null;
|
||||
const items = Array.isArray(found['extension']) ? found['extension'].filter(record) : invalid();
|
||||
const desenlace = items.find(i => i['url'] === 'desenlace')?.['valueCode'];
|
||||
const motivo = items.find(i => i['url'] === 'motivo')?.['valueString'];
|
||||
if (!text(desenlace)) invalid();
|
||||
return { desenlace, motivo: text(motivo) ? motivo : 'el modelo no estima a esta paciente' };
|
||||
}
|
||||
|
||||
function horizon(prediction: unknown, start: string | undefined): { horizonte: HorizonteRiesgoCacu; start: string } {
|
||||
if (!record(prediction) || !probability(prediction['probabilityDecimal'])) invalid();
|
||||
const period = prediction['whenPeriod'];
|
||||
@@ -81,6 +101,15 @@ function riskAssessment(v: unknown, pacId: string | null): LoteRiesgoCacu {
|
||||
const method = v['method'];
|
||||
const model = record(method) && Array.isArray(method['coding']) ? method['coding'][0] : undefined;
|
||||
if (!record(model) || !text(model['code']) || !text(model['version'])) invalid();
|
||||
const notScored = notScoredOutcome(v);
|
||||
if (notScored !== null && v['prediction'] === undefined) {
|
||||
return {
|
||||
id: v['id'] as string, modelo_nombre: model['code'] as string, modelo_version: model['version'] as string,
|
||||
fecha_indice: '', evaluado_en: v['occurrenceDateTime'] as string, estado_lote: v['status'] as 'preliminary' | 'final',
|
||||
desenlace: notScored.desenlace, desenlace_texto: null, horizontes: [], estimando: estimand(v),
|
||||
resultados_indice: null, sin_estimacion: notScored.motivo,
|
||||
};
|
||||
}
|
||||
const predictions = v['prediction'];
|
||||
if (!Array.isArray(predictions) || predictions.length === 0) invalid();
|
||||
let start: string | undefined;
|
||||
@@ -99,7 +128,8 @@ function riskAssessment(v: unknown, pacId: string | null): LoteRiesgoCacu {
|
||||
id: v['id'] as string, modelo_nombre: model['code'] as string, modelo_version: model['version'] as string,
|
||||
fecha_indice: start!, evaluado_en: v['occurrenceDateTime'] as string, estado_lote: v['status'] as 'preliminary' | 'final',
|
||||
desenlace: outcome['code'] as string, desenlace_texto: text(outcome['display']) ? outcome['display'] : null,
|
||||
horizontes, estimando: estimand(v),
|
||||
horizontes, estimando: estimand(v), resultados_indice: stringExtension(v, INDEX_RESULTS, INDEX_RESULT_KEYS),
|
||||
sin_estimacion: null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -108,16 +138,27 @@ function simulated(resource: Record<string, unknown>): boolean {
|
||||
return record(meta) && Array.isArray(meta['tag']) && meta['tag'].some(t => record(t) && t['code'] === SIMULATED);
|
||||
}
|
||||
|
||||
/** Validate at the boundary: a `searchset` Bundle of the patient's `RiskAssessment`, tagged `simulado` (DEV only).
|
||||
* `null` when the Bundle is empty (no confirmed risk). Rejects real data, coercions and malformed fields. */
|
||||
/** Where an outcome goes among the patient's risks: CIN2+ first, then cancer, then any other. */
|
||||
export function ordenDesenlace(desenlace: string): number {
|
||||
return desenlace.startsWith('cin2') ? 0 : desenlace === 'cancer' ? 1 : 2;
|
||||
}
|
||||
|
||||
/** Validate at the boundary: a `searchset` Bundle of the patient's `RiskAssessment`s, one per outcome, each tagged
|
||||
* `simulado` (DEV only), and one with no prediction for a current model that didn't score her. `riesgo` is the first
|
||||
* that has a risk. `null` when the Bundle is empty (no confirmed risk). Rejects real data, coercions and
|
||||
* malformed fields. */
|
||||
export function riesgoPacienteResponse(v: unknown, pacId: string | null = null): RiesgoPacienteCacu | null {
|
||||
if (!record(v) || v['resourceType'] !== 'Bundle' || v['type'] !== 'searchset' || !finite(v['total'])) invalid();
|
||||
const entries = v['entry'] ?? [];
|
||||
if (!Array.isArray(entries)) invalid();
|
||||
if (entries.length === 0) return null;
|
||||
const resource = record(entries[0]) ? entries[0]['resource'] : undefined;
|
||||
if (!record(resource) || !simulated(resource)) invalid();
|
||||
return { simulado: true, riesgo: riskAssessment(structuredClone(resource), pacId) };
|
||||
const riesgos = entries.map(entry => {
|
||||
const resource = record(entry) ? entry['resource'] : undefined;
|
||||
if (!record(resource) || !simulated(resource)) invalid();
|
||||
return riskAssessment(structuredClone(resource), pacId);
|
||||
});
|
||||
riesgos.sort((a, b) => ordenDesenlace(a.desenlace) - ordenDesenlace(b.desenlace));
|
||||
return { simulado: true, riesgo: riesgos.find(r => r.horizontes.length) ?? riesgos[0], riesgos };
|
||||
}
|
||||
|
||||
/** Presentation helper: an incomplete interval must never become a fabricated range. */
|
||||
|
||||
Reference in New Issue
Block a user