export: optional KTLX -> EDF+ exporter vendored from eeg@92bc2a0
Step 2 of the plan. src/eeg/utils copied into tea_modulo.export with only the imports changed; outside the EDF pipeline (extra `export`, script ktlx2edf). Synthetic tests (ktlx_synth, edf export, patient) carried over: 43 pass.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
"""Unit tests for the sex / birthdate extraction (tea_modulo.export.patient)."""
|
||||
|
||||
import struct
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
from tea_modulo.export import patient
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw, expected", [
|
||||
("F", "F"), ("Female", "F"), ("Mujer", "F"), ("madre", "F"), ("2", "F"),
|
||||
("M", "M"), ("Male", "M"), ("Masculino", "M"), ("Hombre", "M"), ("1", "M"),
|
||||
("", None), ("X", None), ("unknown", None),
|
||||
])
|
||||
def test_normalizar_sexo(raw, expected):
|
||||
assert patient.normalizar_sexo(raw) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ident, orden, expected", [
|
||||
("11012022", "dmy", date(2022, 1, 11)),
|
||||
("11012022", "mdy", date(2022, 11, 1)),
|
||||
("25122019", "mdy", date(2019, 12, 25)), # month 25 impossible: swapped
|
||||
(" 01022020 ", "dmy", date(2020, 2, 1)),
|
||||
("ID-0001", "dmy", None),
|
||||
("99992020", "dmy", None),
|
||||
])
|
||||
def test_fecha_del_identificador(ident, orden, expected):
|
||||
d, motivo = patient.fecha_del_identificador(ident, orden)
|
||||
assert (d.date() if d else None) == expected
|
||||
assert motivo
|
||||
|
||||
|
||||
def _ole(d: datetime) -> bytes:
|
||||
return struct.pack("<d", (d - datetime(1899, 12, 30)) / timedelta(days=1))
|
||||
|
||||
|
||||
def test_fields_skip_label_and_read_value():
|
||||
blob = (b"GenderLabel\x00Sexo\x00" # display label: skipped
|
||||
b"Gender\x00\x00Female\x00"
|
||||
b"BirthDateLabel\x00Fecha\x00"
|
||||
b"BirthDate\x00\x00" + _ole(datetime(2021, 3, 4)))
|
||||
bloques = [blob]
|
||||
assert patient.sexo_del_eeg(bloques)[0] == "F"
|
||||
d, _ = patient.fecha_tras_campo(bloques, referencia=datetime(2025, 1, 1))
|
||||
assert d.date() == date(2021, 3, 4)
|
||||
|
||||
|
||||
def test_fields_inside_hex_block():
|
||||
inner = b"Gender\x00M\x00"
|
||||
blob = b'(."Bin", 0x' + inner.hex().encode() + b"00" * 12 + b")"
|
||||
assert patient.sexo_del_eeg(patient.desde_hex(blob))[0] == "M"
|
||||
|
||||
|
||||
def test_birthdate_rejects_implausible_age():
|
||||
blob = b"BirthDate\x00" + _ole(datetime(1850, 1, 1))
|
||||
d, _ = patient.fecha_tras_campo([blob], referencia=datetime(2025, 1, 1))
|
||||
assert d is None
|
||||
|
||||
|
||||
def test_patient_info_sources(tmp_path):
|
||||
eeg = tmp_path / "x.eeg"
|
||||
eeg.write_bytes(b"Gender\x00F\x00BirthDate\x00"
|
||||
+ _ole(datetime(2018, 6, 7)))
|
||||
rec = datetime(2025, 1, 1)
|
||||
# identifier is a date: it wins over the OLE double
|
||||
assert patient.patient_info(eeg, "11012022", rec) == ("F", date(2022, 1, 11))
|
||||
# identifier is not a date: OLE double after BirthDate
|
||||
assert patient.patient_info(eeg, "ID-0001", rec) == ("F", date(2018, 6, 7))
|
||||
# no .eeg at all
|
||||
assert patient.patient_info(None, "ID-0001", rec) == (None, None)
|
||||
Reference in New Issue
Block a user