Indicadores TEA: Resumen, Trazabilidad, Seguimiento, Análisis de riesgo, periodicities
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { Bundle, DiagnosticReport, Observation, Patient, RiskAssessment } from 'fhir/r4';
|
||||
import { code, ext, resources } from '../fhir';
|
||||
import { code, ext, label, resources } from '../fhir';
|
||||
|
||||
/** One row of the follow-up list: what the list shows of a patient at the cut-off. */
|
||||
export interface Row {
|
||||
@@ -204,3 +204,164 @@ export function camaRows(page: Bundle, cutoff: string): CamaRow[] {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export const TEA_STEPS = [
|
||||
{ code: 'mchat_only', label: 'M-CHAT' },
|
||||
{ code: 'psychology', label: 'Psicología' },
|
||||
{ code: 'neurology', label: 'Neuropediatría' },
|
||||
{ code: 'eeg', label: 'EEG' },
|
||||
] as const;
|
||||
|
||||
const ADOS2_ITEMS = [
|
||||
'vocalization_directed',
|
||||
'pointing',
|
||||
'gestures',
|
||||
'unusual_eye_contact',
|
||||
'facial_expressions',
|
||||
'gaze_integration',
|
||||
'shared_enjoyment',
|
||||
'response_to_name',
|
||||
'ignoring',
|
||||
'requesting',
|
||||
'showing',
|
||||
'joint_attention_initiation',
|
||||
'joint_attention_response',
|
||||
'social_overtures_quality',
|
||||
'social_overtures_caregiver',
|
||||
'reciprocal_communication',
|
||||
'rapport_quality',
|
||||
'intonation',
|
||||
'stereotyped_language',
|
||||
'unusual_sensory_interest',
|
||||
'hand_finger_mannerisms',
|
||||
'repetitive_interests',
|
||||
];
|
||||
|
||||
type Prediction = NonNullable<RiskAssessment['prediction']>[number];
|
||||
|
||||
function associationPair(p: Prediction) {
|
||||
const stat = (name: string) => ext(p, `association-${name}`)?.valueDecimal;
|
||||
return {
|
||||
pair: code(p.outcome),
|
||||
or: stat('odds-ratio'),
|
||||
low: stat('ci-low'),
|
||||
high: stat('ci-high'),
|
||||
q: stat('q'),
|
||||
probability: p.probabilityDecimal,
|
||||
reference: stat('reference-probability'),
|
||||
sameDirection: ext(p, 'association-same-direction')?.valueBoolean,
|
||||
headline: ext(p, 'association-headline')?.valueBoolean === true,
|
||||
};
|
||||
}
|
||||
|
||||
function concordance(headline?: ReturnType<typeof associationPair>, ados?: Observation): string {
|
||||
if (!headline || !ados || headline.probability == null || headline.reference == null) return '—';
|
||||
const outcome = headline.pair?.split('|')[2];
|
||||
const item = ADOS2_ITEMS[Number(outcome?.match(/^ados2_item_(\d{2})$/)?.[1]) - 1];
|
||||
const score =
|
||||
outcome === 'CSS'
|
||||
? ados.valueInteger
|
||||
: ados.component?.find((c) => item && code(c.code) === `ados2.${item}`)?.valueQuantity?.value;
|
||||
if (score == null) return '—';
|
||||
const event = score >= (outcome === 'CSS' ? 8 : 1);
|
||||
const predicted = headline.probability >= headline.reference;
|
||||
return event === predicted ? 'Concordante' : 'No concordante';
|
||||
}
|
||||
|
||||
function byPatient<T extends DiagnosticReport | Observation | RiskAssessment>(
|
||||
records: T[],
|
||||
): Map<string | undefined, T[]> {
|
||||
const grouped = new Map<string | undefined, T[]>();
|
||||
for (const record of records) {
|
||||
const subject = record.subject?.reference;
|
||||
const group = grouped.get(subject);
|
||||
if (group) group.push(record);
|
||||
else grouped.set(subject, [record]);
|
||||
}
|
||||
return grouped;
|
||||
}
|
||||
|
||||
export function teaRows(page: Bundle) {
|
||||
const reports = byPatient(resources<DiagnosticReport>(page, 'DiagnosticReport'));
|
||||
const observations = byPatient(resources<Observation>(page, 'Observation'));
|
||||
const risks = byPatient(resources<RiskAssessment>(page, 'RiskAssessment'));
|
||||
const dateOrder = (a: Observation | DiagnosticReport, b: Observation | DiagnosticReport) =>
|
||||
(a.effectiveDateTime ?? '').localeCompare(b.effectiveDateTime ?? '');
|
||||
return resources<Patient>(page, 'Patient', 'match').map((patient) => {
|
||||
const subject = `Patient/${patient.id}`;
|
||||
const obs = observations.get(subject) ?? [];
|
||||
const eegs = (reports.get(subject) ?? []).filter((r) => code(r.code) === 'eeg').sort(dateOrder);
|
||||
const ados = obs.filter((o) => code(o.code) === 'ados2').sort(dateOrder);
|
||||
const mchat = obs.filter((o) => code(o.code) === 'mchat.score').sort(dateOrder)[0];
|
||||
const neurology = obs
|
||||
.filter(
|
||||
(o) =>
|
||||
code(o.code) === 'clinical_note.specialty' &&
|
||||
code(o.valueCodeableConcept) === 'pediatric_neurology',
|
||||
)
|
||||
.sort(dateOrder)[0];
|
||||
const risk = risks.get(subject)?.[0];
|
||||
const pairs = (risk?.prediction ?? []).map(associationPair);
|
||||
const headline = pairs.find((p) => p.headline);
|
||||
const basisAdos = risk?.basis?.find((ref) =>
|
||||
ref.reference?.startsWith('Observation/'),
|
||||
)?.reference;
|
||||
const evaluation = basisAdos
|
||||
? ados.find((o) => `Observation/${o.id}` === basisAdos)
|
||||
: ados.at(-1);
|
||||
const status = ext(patient, 'follow-up-status')?.valueCoding?.code;
|
||||
const dates = [
|
||||
mchat?.effectiveDateTime,
|
||||
ados[0]?.effectiveDateTime,
|
||||
neurology?.effectiveDateTime,
|
||||
[
|
||||
...eegs.map((e) => e.effectiveDateTime),
|
||||
...obs.filter((o) => code(o.code) === 'eeg.lab_record_id').map((o) => o.effectiveDateTime),
|
||||
]
|
||||
.filter((day) => !!day)
|
||||
.sort()[0],
|
||||
];
|
||||
const explicitStep = TEA_STEPS.findIndex((s) => s.code === ext(patient, 'tea-step')?.valueCode);
|
||||
const lastReached =
|
||||
explicitStep >= 0
|
||||
? explicitStep
|
||||
: status === 'finished'
|
||||
? 3
|
||||
: dates.slice(0, 3).reduce((last, date, index) => (date ? index : last), -1);
|
||||
return {
|
||||
patient,
|
||||
name: patient.name?.[0]?.text,
|
||||
recordNumbers: (patient.identifier ?? [])
|
||||
.filter((i) => i.system?.startsWith('urn:isssteleon:') && !!i.value)
|
||||
.map((i) => i.value!),
|
||||
status,
|
||||
steps: TEA_STEPS.map((step, i) => ({
|
||||
...step,
|
||||
date: dates[i],
|
||||
reached: i <= lastReached || (i === 0 && status === 'active'),
|
||||
})),
|
||||
mchatScore: mchat?.valueQuantity?.value,
|
||||
ados: evaluation && {
|
||||
date: evaluation.effectiveDateTime,
|
||||
css: evaluation.valueInteger,
|
||||
module: label(
|
||||
evaluation.component?.find((c) => code(c.code) === 'ados2.module')?.valueCodeableConcept,
|
||||
),
|
||||
classification: label(evaluation.interpretation?.[0]),
|
||||
},
|
||||
eegs: eegs.map((e) => ({
|
||||
id: e.id,
|
||||
date: e.effectiveDateTime,
|
||||
quality: label(e.conclusionCode?.[0]),
|
||||
validity: label(e.conclusionCode?.[1]),
|
||||
duration: ext(e, 'eeg-duration-min')?.valueDecimal,
|
||||
channels: ext(e, 'eeg-channels')?.valueInteger,
|
||||
})),
|
||||
headline,
|
||||
concordance: concordance(headline, evaluation),
|
||||
pairs,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export type TeaRow = ReturnType<typeof teaRows>[number];
|
||||
|
||||
Reference in New Issue
Block a user