TEA on its new staging: silver load, ten measures answered on request with the status at the period's end, several groups and column strata, and FHIR R4 endpoints for measures, follow-up and patient summary

This commit is contained in:
2026-11-12 15:21:25 -06:00
parent c430a5bd11
commit a0407e41d6
55 changed files with 2609 additions and 790 deletions
+7 -48
View File
@@ -1,4 +1,3 @@
from datetime import date, timedelta
from pathlib import PurePath
from typing import Any
@@ -11,15 +10,12 @@ from webapi.imports.connectors.errors import ConnectorError
from webapi.imports.connectors.registry import register_connector
from webapi.imports.connectors.tabular import FileConnector
MAX_LISTED_EVENTS = 20
@register_connector
class EdfConnector(FileConnector):
"""EEG recordings (`.edf`), one row per file, shaped for `tea.EegEstudio`:
patient and dates from the header, an estimated background rhythm from
the posterior channels, a text summary of the recording, and the file
itself (column `estudio`, stored in `eeg_estudio.eeg_estudio`)."""
"""EEG recordings (`.edf`), one row per file, shaped for `tea.Eeg`: the laboratory's study id, the patient
and the study date from the header, the channels and duration of the recording, and the file itself (column
`estudio`, stored in `eeg.eeg_estudio`). The quality criteria, sleep and metrics are the EEG analysis'."""
kind = "edf"
label = "EEG (EDF)"
@@ -57,49 +53,12 @@ class EdfConnector(FileConnector):
eeg = EegRecording.read(raw)
except ExtractionError as exc:
raise ConnectorError(f"{name}: {exc}") from exc
patient = eeg.patient
study_date = eeg.start.date() if eeg.start else None
rhythm = eeg.background_rhythm()
return {
"id_registro_laboratorio": eeg.recording_fields["admin_code"] or PurePath(name).stem,
"paciente": patient.code,
"sexo": patient.sex,
"fecha_nacimiento_declarada": patient.birthdate,
"edad_meses": age_in_months(patient.birthdate, study_date),
"fecha_estudio": study_date,
"duracion_min": round(eeg.duration_seconds / 60) if eeg.duration_seconds else None,
"ritmo_base_frecuencia": rhythm["frequency_hz"],
"ritmo_base_amplitud": rhythm["amplitude_uv"],
"descripcion_senal": describe(eeg),
"paciente": eeg.patient.code,
"fecha_estudio": eeg.start.date() if eeg.start else None,
"canales": len(eeg.channels),
"duracion_min": round(eeg.duration_seconds / 60, 1) if eeg.duration_seconds else None,
"archivo": name,
"estudio": raw,
}
def describe(eeg: EegRecording) -> str:
channels = eeg.channels
parts = [
f"{eeg.variant}, {len(channels)} canales a {channels[0].sample_rate:g} Hz" if channels else eeg.variant,
f"duración {timedelta(seconds=round(eeg.duration_seconds))}",
"montaje: " + ", ".join(c.label for c in channels),
]
fields = eeg.recording_fields
if fields["equipment"]:
parts.append(f"equipo: {fields['equipment']}")
if fields["technician"]:
parts.append(f"técnico: {fields['technician']}")
events = eeg.annotations()
if events:
listed = "; ".join(f"{timedelta(seconds=round(e.onset_seconds))} {e.text}" for e in events[:MAX_LISTED_EVENTS])
more = f" (+{len(events) - MAX_LISTED_EVENTS})" if len(events) > MAX_LISTED_EVENTS else ""
parts.append(f"eventos: {listed}{more}")
return ". ".join(parts) + "."
def age_in_months(birthdate: date | None, on: date | None) -> float | None:
if birthdate is None or on is None or on < birthdate:
return None
months = (on.year - birthdate.year) * 12 + (on.month - birthdate.month)
if on.day < birthdate.day:
months -= 1
return float(months)