CaMa recurrence: the patient's own scenarios, without a cohort menu

This commit is contained in:
Roberto Treviño Cervantes
2026-11-22 18:04:57 -06:00
parent 5bf6af8ecf
commit 29c9e6853b
4 changed files with 37 additions and 39 deletions
@@ -4,16 +4,11 @@
@if (r === undefined) { <p role="status">Buscando el análisis de recurrencia…</p> }
@if (r === null) { <p role="status">Todavía no hay un modelo de recurrencia en producción.</p> }
@if (r) {
@if (r.escenarios.length === 0) { <p role="status">Todavía no hay escenarios clínicos de recurrencia para mostrar.</p> }
@if (escenarios().length === 0) { <p role="status">La paciente no cumple las condiciones de ningún escenario clínico de recurrencia.</p> }
@else {
<label class="selector">Cohorte
<select [value]="cohorte()" (change)="elegirCohorte($any($event.target).value)">
@for (c of cohortes; track c.id) { <option [value]="c.id" [selected]="c.id === cohorte()">{{ c.nombre }}</option> }
</select>
</label>
<label class="selector">Escenario clínico
<select [value]="escenario()?.escenario" (change)="elegirEscenario($any($event.target).value)">
@for (f of escenarios(); track f.escenario) { <option [value]="f.escenario" [selected]="f.escenario === escenario()?.escenario">{{ f.escenario }}</option> }
<select [value]="escenario() ? clave(escenario()!) : ''" (change)="elegirEscenario($any($event.target).value)">
@for (f of escenarios(); track clave(f)) { <option [value]="clave(f)" [selected]="f === escenario()">{{ f.escenario }} ({{ f.cohorte_nombre }})</option> }
</select>
</label>
@if (figura(); as figure) { <app-plotly-chart [figure]="figure" [showNotes]="false" [label]="'Recurrencia: ' + escenario()?.escenario + ', ' + escenario()?.cohorte_nombre" /> }
@@ -22,7 +17,7 @@
</section>
<section aria-labelledby="factores-recurrencia-titulo">
<h5 id="factores-recurrencia-titulo">Factores de riesgo</h5>
@if (r && r.escenarios.length) {
@if (escenario()) {
<label class="selector horizons">Horizonte
<select [value]="horizonte()" (change)="elegirHorizonte(+$any($event.target).value)">
@for (h of horizontes(); track h) { <option [value]="h" [selected]="h === horizonte()">{{ h }} meses</option> }
@@ -4,15 +4,14 @@ import { switchMap } from 'rxjs';
import { PlotlyChart } from '../../../../shared/plotly/plotly-chart';
import { CamaFactoresRiesgo } from './cama-factores-riesgo.component';
import { figuraRecurrencia } from './cama-recurrencia-figura';
import { CamaRecurrenciaService } from './cama-recurrencia.service';
import { CamaRecurrenciaService, FiguraRecurrencia } from './cama-recurrencia.service';
export const COHORTES: { id: string; nombre: string }[] = [{ id: 'metabric', nombre: 'METABRIC' }, { id: 'scanb', nombre: 'SCAN-B' }];
export const HORIZONTE_PREDETERMINADO = 60;
/**
* «Análisis de recurrencia» and its «Factores de riesgo»: two chained menus, the cohort (METABRIC or SCAN-B) and then its clinical
* scenarios; the scenario chosen decides the curve and the label at once, one at a time, and the cohorts are never mixed. The label has
* a horizon (12, 36 or 60 months, 60 by default). The menu opens on the scenario the patient's covariates place her in, when one does.
* «Análisis de recurrencia» and its «Factores de riesgo» for one patient: the clinical scenarios her own age, tumor size, grade and
* receptors place her in, from either external cohort (METABRIC or SCAN-B, named with each scenario and never mixed in one figure).
* The scenario chosen decides the curve and the label at once; the label has a horizon (12, 36 or 60 months, 60 by default).
*/
@Component({
selector: 'app-cama-analisis-recurrencia',
@@ -23,29 +22,28 @@ export const HORIZONTE_PREDETERMINADO = 60;
export class CamaAnalisisRecurrencia {
readonly pacId = input.required<string>();
private readonly service = inject(CamaRecurrenciaService);
readonly cohortes = COHORTES;
/** The analysis; `undefined` while it loads, `null` when there is no recurrence model in production. */
readonly respuesta = toSignal(toObservable(this.pacId).pipe(switchMap(id => this.service.recurrencia(id))));
private readonly cohorteElegida = signal<string | null>(null);
private readonly escenarioElegido = signal<string | null>(null);
private readonly elegido = signal<string | null>(null);
readonly horizonte = signal<number>(HORIZONTE_PREDETERMINADO);
readonly cohorte = computed(() => this.cohorteElegida() ?? this.respuesta()?.predeterminado?.cohorte
?? COHORTES.find(c => this.respuesta()?.escenarios.some(f => f.cohorte === c.id))?.id ?? null);
/** The scenarios of the cohort chosen, from the list of that cohort only. */
readonly escenarios = computed(() => this.respuesta()?.escenarios.filter(f => f.cohorte === this.cohorte()) ?? []);
readonly escenario = computed(() => {
const lista = this.escenarios();
const pedido = this.escenarioElegido() ?? (this.respuesta()?.predeterminado?.cohorte === this.cohorte() ? this.respuesta()?.predeterminado?.escenario : null);
return lista.find(f => f.escenario === pedido) ?? lista[0] ?? null;
/** The figures of her scenarios, in the order the server ranked them. */
readonly escenarios = computed(() => {
const r = this.respuesta();
if (!r) return [];
return r.deLaPaciente.map(s => r.escenarios.find(f => f.cohorte === s.cohorte && f.escenario === s.escenario))
.filter((f): f is FiguraRecurrencia => !!f);
});
readonly escenario = computed(() => this.escenarios().find(f => clave(f) === this.elegido()) ?? this.escenarios()[0] ?? null);
readonly figura = computed(() => { const f = this.escenario(); return f ? figuraRecurrencia(f) : null; });
readonly horizontes = computed(() => this.respuesta()?.horizontes ?? []);
readonly factores = computed(() => {
const f = this.escenario();
return f ? this.respuesta()?.resumen.find(r => r.cohorte === f.cohorte && r.escenario === f.escenario && r.horizonte_meses === this.horizonte()) ?? null : null;
});
readonly clave = clave;
elegirCohorte(id: string): void { this.cohorteElegida.set(id); this.escenarioElegido.set(null); }
elegirEscenario(nombre: string): void { this.escenarioElegido.set(nombre); }
elegirEscenario(valor: string): void { this.elegido.set(valor); }
elegirHorizonte(meses: number): void { this.horizonte.set(meses); }
}
function clave(f: FiguraRecurrencia): string { return `${f.cohorte}|${f.escenario}`; }
@@ -130,7 +130,8 @@ describe('Análisis de recurrencia', () => {
const respuesta = () => respuestaRecurrencia({ modelo: { nombre: 'cama-recurrence', version: '1' }, horizontes: [12, 36, 60],
escenarios: [figura('metabric', '< 45 años'), figura('metabric', 'Tumor >50 mm'), figura('scanb', '< 45 años'), figura('scanb', 'Grado 3')],
resumen: ['metabric', 'scanb'].flatMap(c => [12, 36, 60].flatMap(h => [resumen(c, '< 45 años', h), resumen(c, 'Tumor >50 mm', h), resumen(c, 'Grado 3', h)])),
predeterminado: { cohorte: 'scanb', escenario: 'Grado 3' } });
predeterminado: { cohorte: 'scanb', escenario: 'Grado 3' },
de_la_paciente: [{ cohorte: 'scanb', escenario: 'Grado 3' }, { cohorte: 'metabric', escenario: '< 45 años' }] });
beforeEach(() => TestBed.configureTestingModule({ providers: [plotly(), { provide: CamaRecurrenciaService, useValue: { recurrencia: () => of(respuesta()) } }] }));
afterEach(() => vi.unstubAllGlobals());
const montar = () => {
@@ -143,24 +144,23 @@ describe('Análisis de recurrencia', () => {
const opciones = (f: { nativeElement: unknown }, etiqueta: string) => [...(f.nativeElement as HTMLElement).querySelectorAll('label.selector')].find(l => l.textContent!.includes(etiqueta))!
.querySelectorAll('option');
// Fails if the menus do not open on the patient's cohort and scenario, or the scenario list is not that of the cohort chosen.
it('opens on the patient\'s cohort and scenario, with only that cohort\'s scenarios', () => {
// Fails if the menu offers a cohort selector or scenarios the patient's covariates do not place her in, or does not open on the first.
it('offers only the patient\'s scenarios, each with its cohort, and opens on the first', () => {
const f = montar();
expect([...opciones(f, 'Cohorte')].map(o => o.textContent!.trim())).toEqual(['METABRIC', 'SCAN-B']);
expect([...opciones(f, 'Escenario clínico')].map(o => o.textContent!.trim())).toEqual(['< 45 años', 'Grado 3']);
expect(texto(f)).toContain('Marca=scanb-60'); // her scenario, the label at 60 months
expect([...(f.nativeElement as HTMLElement).querySelectorAll('label.selector')].some(l => l.textContent!.includes('Cohorte'))).toBe(false);
expect([...opciones(f, 'Escenario clínico')].map(o => o.textContent!.trim())).toEqual(['Grado 3 (SCAN-B)', '< 45 años (METABRIC)']);
expect(texto(f)).toContain('Marca=scanb-60'); // her first scenario, the label at 60 months
});
// Fails if a change of cohort leaves the other cohort's scenarios, curve or label on screen: the cohorts are never mixed.
it('reloads the scenarios, the curve and the label when the cohort changes, and keeps one curve', () => {
// Fails if choosing another of her scenarios leaves the previous curve or label on screen: the cohorts are never mixed.
it('changes the curve and the label together when she has another scenario chosen, and keeps one curve', () => {
const f = montar();
elegir(f, 'Cohorte', 'metabric');
elegir(f, 'Escenario clínico', 'metabric|< 45 años');
expect([...opciones(f, 'Escenario clínico')].map(o => o.textContent!.trim())).toEqual(['< 45 años', 'Tumor >50 mm']);
expect(texto(f)).toContain('Marca=metabric-60');
expect(texto(f)).not.toContain('scanb');
expect(texto(f)).not.toContain('Marca=scanb');
expect(f.nativeElement.querySelectorAll('app-plotly-chart')).toHaveLength(1);
});
@@ -20,6 +20,8 @@ export interface RespuestaRecurrencia {
resumen: FilaFactores[];
/** The cohort and scenario the patient's own covariates place her in first, if any. */
predeterminado: { cohorte: string; escenario: string } | null;
/** Every scenario her covariates place her in, the larger first within each cohort; empty when none or no patient was asked. */
deLaPaciente: { cohorte: string; escenario: string }[];
}
const record = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
@@ -48,9 +50,12 @@ export function respuestaRecurrencia(v: unknown): RespuestaRecurrencia {
if (typeof m['nombre'] !== 'string' || typeof m['version'] !== 'string') invalid();
if (!Array.isArray(v['resumen'])) invalid();
const d = v['predeterminado'];
const suyos = Array.isArray(v['de_la_paciente']) ? (v['de_la_paciente'] as unknown[]) : [];
return { modelo: { nombre: m['nombre'] as string, version: m['version'] as string }, horizontes: v['horizontes'] as number[],
escenarios: (v['escenarios'] as unknown[]).map(e => figura(e, h)), resumen: (v['resumen'] as unknown[]).map(filaFactores),
predeterminado: record(d) && typeof d['cohorte'] === 'string' && typeof d['escenario'] === 'string' ? { cohorte: d['cohorte'], escenario: d['escenario'] } : null };
predeterminado: record(d) && typeof d['cohorte'] === 'string' && typeof d['escenario'] === 'string' ? { cohorte: d['cohorte'], escenario: d['escenario'] } : null,
deLaPaciente: suyos.filter((e): e is Record<string, string> => record(e) && typeof e['cohorte'] === 'string' && typeof e['escenario'] === 'string')
.map(e => ({ cohorte: e['cohorte'], escenario: e['escenario'] })) };
}
@Injectable({ providedIn: 'root' })