Merge parity/F1g

This commit is contained in:
2026-12-01 13:45:25 -06:00
8 changed files with 203 additions and 137 deletions
+42 -13
View File
@@ -410,6 +410,28 @@ export function ageShare(r: Reading, axis: string): Figure {
};
}
export function wholeNumberAxis(values: Iterable<number | null | undefined>) {
let min = Infinity;
let max = -Infinity;
for (const value of values) {
if (value == null || !Number.isFinite(value)) continue;
min = Math.min(min, value);
max = Math.max(max, value);
}
const target = Math.max(1, (max - min) / 4);
const magnitude = 10 ** Math.floor(Math.log10(target));
const step = [1, 2, 5, 10].find((step) => step * magnitude >= target) ?? 1;
return {
tickmode: 'linear' as const,
tick0: 0,
dtick: step * magnitude,
tickformat: '.0f',
...(Math.ceil(min) > Math.floor(max) && min <= max
? { range: [Math.floor(min), Math.ceil(max)] }
: {}),
};
}
export function historyFigure(
panel: HistoryPanel,
series: { label: string; r: Reading | undefined }[],
@@ -417,9 +439,9 @@ export function historyFigure(
xTitle: string,
): Figure {
const x = series.map((s) => s.label);
const layout = (y: string) => ({
const layout = (y: string, values: (number | null)[]) => ({
xaxis: { title: { text: xTitle }, type: 'category' as const },
yaxis: { title: { text: y } },
yaxis: { title: { text: y }, ...wholeNumberAxis(values) },
});
if (panel.kind === 'stack') {
const found = [...new Set(series.flatMap((s) => Object.keys(s.r?.strata['category'] ?? {})))];
@@ -430,24 +452,31 @@ export function historyFigure(
.filter((c) => !declared.some((d) => d.code === c))
.map((c, i) => ({ code: c, label: name(c), color: PALETTE[i % PALETTE.length] })),
];
const data = named.map((c) => ({
type: 'bar' as const,
name: c.label,
x,
y: series.map((s) => s.r?.strata['category']?.[c.code]?.['initial-population'] ?? 0),
marker: { color: c.color },
}));
const totals = x.map((_, i) => data.reduce((sum, trace) => sum + trace.y[i], 0));
return {
data: named.map((c) => ({
type: 'bar',
name: c.label,
x,
y: series.map((s) => s.r?.strata['category']?.[c.code]?.['initial-population'] ?? 0),
marker: { color: c.color },
})),
layout: { ...layout(panel.axis ?? 'Pacientes'), barmode: 'stack' },
data,
layout: { ...layout(panel.axis ?? 'Pacientes', [0, ...totals]), barmode: 'stack' },
};
}
const k = panel.kind === 'percent' ? 100 : 1;
const at = (v?: number) => (v === undefined ? null : v * k);
const band = [
...series.map((s) => at(s.r?.high)),
...[...series].reverse().map((s) => at(s.r?.low)),
];
const scores = series.map((s) => at(s.r?.score));
return {
data: [
{
x: [...x, ...[...x].reverse()],
y: [...series.map((s) => at(s.r?.high)), ...[...series].reverse().map((s) => at(s.r?.low))],
y: band,
mode: 'lines',
fill: 'toself',
fillcolor: line + '33',
@@ -457,14 +486,14 @@ export function historyFigure(
},
{
x,
y: series.map((s) => at(s.r?.score)),
y: scores,
mode: 'lines+markers',
name: panel.kind === 'percent' ? 'Porcentaje' : 'Mediana',
line: { color: line },
marker: { color: line },
},
],
layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días'),
layout: layout(panel.kind === 'percent' ? 'Porcentaje' : 'Días', [...band, ...scores]),
};
}
+1 -48
View File
@@ -161,22 +161,9 @@
<tr>
<td>{{ row.recordNumbers.join(', ') || '—' }}</td>
<td>
<a [routerLink]="['/indicadores', 'tea', 'pacientes', id]">
<a [routerLink]="['/indicadores', 'tea', 'pacientes', id, 'riesgo']">
{{ row.name || '—' }}
</a>
<div class="row-actions">
@if (row.pairs.length) {
<button
mat-button
type="button"
(click)="toggle(id)"
[attr.aria-expanded]="expanded()[id] ?? false"
>
{{ expanded()[id] ? 'Ocultar' : 'Ver detalles' }}
</button>
}
<a [routerLink]="['/indicadores', 'tea', 'pacientes', id, 'riesgo']">Riesgo</a>
</div>
</td>
<td>{{ row.patient.gender ? name(row.patient.gender) : '—' }}</td>
<td>{{ ageMonths(row.patient.birthDate, cutoff()) ?? '—' }}</td>
@@ -187,40 +174,6 @@
<td>{{ formatProbability(row.headline?.probability) }}</td>
<td>{{ formatProbability(row.headline?.reference) }}</td>
</tr>
@if (expanded()[id] && row.pairs.length) {
<tr>
<td colspan="10">
<table class="plain pairs">
<thead>
<tr>
<th>Par</th>
<th>Razón de momios</th>
<th>IC 95 %</th>
<th>q</th>
<th>Probabilidad</th>
<th>Probabilidad de referencia</th>
<th>Mismo sentido</th>
</tr>
</thead>
<tbody>
@for (pair of row.pairs; track pair.pair) {
<tr>
<td>{{ pair.title }}</td>
<td>{{ formatOr(pair.or) }}</td>
<td>{{ formatIc(pair.low, pair.high) }}</td>
<td>{{ formatQ(pair.q) }}</td>
<td>{{ formatProbability(pair.probability) }}</td>
<td>{{ formatProbability(pair.reference) }}</td>
<td>
{{ pair.sameDirection == null ? '—' : pair.sameDirection ? 'Sí' : 'No' }}
</td>
</tr>
}
</tbody>
</table>
</td>
</tr>
}
} @empty {
<tr>
<td colspan="10" class="muted">No hay registros para los filtros aplicados.</td>
-7
View File
@@ -18,10 +18,3 @@
margin-bottom: 0;
list-style: none;
}
.row-actions {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--space-3);
}
-5
View File
@@ -71,7 +71,6 @@ export class TabRiesgosTea {
protected readonly associationOptions = ['all', 'with', 'without'] as const;
protected readonly size = signal(20);
protected readonly index = signal(0);
protected readonly expanded = signal<Record<string, boolean>>({});
protected readonly exporting = signal(false);
protected readonly actionError = signal('');
@@ -136,10 +135,6 @@ export class TabRiesgosTea {
this.filters.update((filters) => ({ ...filters, [key]: value }));
}
protected toggle(id: string): void {
this.expanded.update((open) => ({ ...open, [id]: !open[id] }));
}
protected async export(): Promise<void> {
this.exporting.set(true);
this.actionError.set('');
@@ -4,6 +4,7 @@ import { teaSteps } from '../paciente/tea-data';
import {
ageShare,
ageStack,
historyFigure,
nestedDonut,
sexCountBars,
shareDonut,
@@ -130,6 +131,38 @@ function fixture(): Bundle {
}
describe('TEA indicators', () => {
it('uses integer ticks and spaces them using the stacked totals', () => {
const panel = { title: 'Pacientes', measure: 'M23', kind: 'stack' } as const;
const figure = (count: number) =>
historyFigure(
panel,
[
{
label: 'Enero',
r: {
populations: {},
strata: {
category: {
confirmed: { 'initial-population': count },
in_process: { 'initial-population': count },
},
},
},
},
],
'#123456',
'Mes',
);
expect(figure(1).layout?.yaxis).toMatchObject({
tickmode: 'linear',
tick0: 0,
dtick: 1,
tickformat: '.0f',
});
expect(figure(100).layout?.yaxis?.dtick).toBe(50);
});
it('builds the category and group rings from the supplied counts', () => {
const catalog: Catalog = {
groups: [{ id: 'female', label: 'Femenino', color: '#123456' }],