CaMa: present diagnostic pathway and biopsy results

This commit is contained in:
Angel Capetillo Zavala
2026-12-01 12:27:16 -06:00
parent fe4dd55452
commit 749afe765d
12 changed files with 762 additions and 72 deletions
+95 -10
View File
@@ -5,15 +5,34 @@ import { name, Reading } from './measures';
export type Category = { code: string; label: string; color: string };
/** One way of reading a card, as bars from the reports, or a figure. */
export type Mode = { label: string; caption?: string } & (
| { kind: 'counts'; measure: string; catalog: Category[] }
| { kind: 'shares'; measure: string; stratum: string; order: string[] }
| { kind: 'positives'; measure: string; stratum: string; order: string[] }
| { kind: 'signs'; measure: string; strata: string[] }
| { kind: 'days'; steps: { label: string; measure: string }[] }
| { kind: 'days-by'; measure: string; stratum: string }
| { kind: 'figure'; measure: string; figure: 'donut' | 'scenarios'; catalog?: Category[] }
);
export type Mode = {
label: string;
caption?: string;
/** said above the table: what its rows count (patients, reported biopsies…) */
heading?: string;
} &
/** `percent`: every bar on a fixed scale of 0 to 100 %, so its length is the share shown */
(
| { kind: 'counts'; measure: string; catalog: Category[]; percent?: boolean }
| { kind: 'shares'; measure: string; stratum: string; order: string[] }
| { kind: 'positives'; measure: string; stratum: string; order: string[] }
| { kind: 'signs'; measure: string; strata: string[] }
| { kind: 'days'; steps: { label: string; measure: string }[] }
| { kind: 'days-by'; measure: string; stratum: string }
/** A proportion as its two parts (what counted and what did not), in units, with the shares */
| { kind: 'split'; measure: string; yes: string; no: string; colors?: [string, string] }
/** Each row's units by result (a bar in segments), and the share of the row's units with a
* result that are `malignant`; a row with units and no result is not a row with none */
| {
kind: 'outcomes';
measure: string;
stratum: string;
rows: Category[];
results: Category[];
malignant: string[];
}
| { kind: 'figure'; measure: string; figure: 'donut' | 'scenarios'; catalog?: Category[] }
);
/** One bar: its length and quartile marks in % of the scale, and its cells of text. */
export interface Bar {
@@ -22,10 +41,16 @@ export interface Bar {
width: number;
marks: number[];
cells: string[];
/** the bar in parts (in % of the bar), each with what it says when hovered */
segments?: { width: number; color: string; title: string }[];
/** said in place of the bar (a row with nothing to draw) */
none?: string;
}
export interface Bars {
head: string[];
rows: Bar[];
/** the colours of the segments */
legend?: { label: string; color: string }[];
}
const pct = (n: number, of: number) => (of ? `${((100 * n) / of).toFixed(1)} %` : '—');
@@ -46,7 +71,7 @@ export function bars(mode: Mode, of: (measure: string) => Reading | undefined):
...mode.catalog,
...rest.map((code) => ({ code, label: name(code), color: '#9AA3AB' })),
];
const most = Math.max(...Object.values(cells).map(size));
const most = mode.percent ? total : Math.max(...Object.values(cells).map(size));
return {
head: ['n', '%'],
rows: all.map(({ code, label, color }) => {
@@ -107,6 +132,66 @@ export function bars(mode: Mode, of: (measure: string) => Reading | undefined):
.sort((a, b) => b.n - a.n),
);
}
case 'split': {
const { numerator = 0, denominator = 0 } = of(mode.measure)?.populations ?? {};
if (!denominator) return undefined;
const parts: [string, number][] = [
[mode.yes, numerator],
[mode.no, denominator - numerator],
];
return {
head: ['n', '%'],
rows: parts.map(([label, n], i) => ({
label,
color: mode.colors?.[i],
width: (100 * n) / denominator,
marks: [],
cells: [`${n}`, pct(n, denominator)],
})),
};
}
case 'outcomes': {
const r = of(mode.measure);
const units = r?.strata[mode.stratum] ?? {};
const cells = r?.strata[`${mode.stratum}-category`] ?? {};
if (!Object.keys(units).length) return undefined;
const graded = (i: number) => !['no_biopsy', 'no_data'].includes(mode.results[i].code);
return {
head: ['Pacientes', ...mode.results.map((x) => x.label), '% maligno'],
legend: mode.results.map(({ label, color }) => ({ label, color })),
rows: mode.rows.map(({ code, label }) => {
const n = size(units[code]);
const by = mode.results.map((x) => size(cells[`${code}|${x.code}`]));
const den = by.reduce((t, k, i) => t + (graded(i) ? k : 0), 0);
const bad = by.reduce(
(t, k, i) => t + (mode.malignant.includes(mode.results[i].code) ? k : 0),
0,
);
return {
label,
width: 100,
marks: [],
none: n ? undefined : 'Sin pacientes en el periodo',
segments: mode.results.map((x, i) => ({
width: n ? (100 * by[i]) / n : 0,
color: x.color,
title: `${x.label}: ${by[i]} de ${n} (${pct(by[i], n)})`,
})),
cells: [
`${n}`,
...by.map((k) => (n ? `${k}` : '—')),
n
? den
? `${bad}/${den} (${pct(bad, den)})`
: by[mode.results.findIndex((x) => x.code === 'no_data')]
? 'Sin biopsias clasificables'
: 'Sin biopsias'
: '—',
],
};
}),
};
}
default:
return undefined;
}