EDF annotation texts not in UTF-8 (ISSSTELEÓN): legacy reads them as edf_convert does, modern retries latin1
This commit is contained in:
@@ -69,7 +69,12 @@ class Recording:
|
|||||||
import mne
|
import mne
|
||||||
|
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
|
try:
|
||||||
raw = mne.io.read_raw_edf(path, preload=True, verbose="error")
|
raw = mne.io.read_raw_edf(path, preload=True, verbose="error")
|
||||||
|
except Exception as e: # annotation texts in a Windows code page
|
||||||
|
if "latin1" not in str(e):
|
||||||
|
raise
|
||||||
|
raw = mne.io.read_raw_edf(path, preload=True, verbose="error", encoding="latin1")
|
||||||
return cls(raw=raw, age_months=age_months, source_sha256=sha256_of(path),
|
return cls(raw=raw, age_months=age_months, source_sha256=sha256_of(path),
|
||||||
source_name=path.stem)
|
source_name=path.stem)
|
||||||
|
|
||||||
|
|||||||
@@ -183,8 +183,24 @@ def _annotations(edf_path):
|
|||||||
"""Every EDF+ annotation with its exact onset and duration, as edf_convert reads them
|
"""Every EDF+ annotation with its exact onset and duration, as edf_convert reads them
|
||||||
(the director keeps BAD_ACQ_SKIP among his events too)."""
|
(the director keeps BAD_ACQ_SKIP among his events too)."""
|
||||||
import mne
|
import mne
|
||||||
a = mne.read_annotations(edf_path)
|
from ._director import edf_convert
|
||||||
return mne.Annotations(list(a.onset), list(a.duration), list(a.description))
|
out = []
|
||||||
|
with open(edf_path, "rb") as f: # edf_convert.leer_edf, annotation channels only
|
||||||
|
h = edf_convert.leer_cabecera(f)
|
||||||
|
idx = [i for i, e in enumerate(h["etiquetas"]) if e.strip().upper() == "EDF ANNOTATIONS"]
|
||||||
|
if idx:
|
||||||
|
n_reg, por_reg = h["n_registros"], sum(h["n_muestras"])
|
||||||
|
if n_reg < 0:
|
||||||
|
f.seek(0, 2)
|
||||||
|
n_reg = (f.tell() - h["bytes_cabecera"]) // (por_reg * 2)
|
||||||
|
f.seek(h["bytes_cabecera"])
|
||||||
|
crudo = np.fromfile(f, dtype="<i2", count=n_reg * por_reg)
|
||||||
|
n_reg = len(crudo) // por_reg
|
||||||
|
crudo = crudo[:n_reg * por_reg].reshape(n_reg, por_reg)
|
||||||
|
cortes = np.cumsum([0] + h["n_muestras"])
|
||||||
|
for i in idx: # the director's TAL decoder (texts utf-8, replace)
|
||||||
|
out += edf_convert._anotaciones_tal(crudo[:, cortes[i]:cortes[i + 1]].tobytes())
|
||||||
|
return mne.Annotations([o for o, _, _ in out], [d for _, d, _ in out], [t for _, _, t in out])
|
||||||
|
|
||||||
|
|
||||||
MODULE = LegacyPreprocessing()
|
MODULE = LegacyPreprocessing()
|
||||||
|
|||||||
@@ -55,7 +55,12 @@ class Preprocessing:
|
|||||||
import mne
|
import mne
|
||||||
|
|
||||||
p = PreprocessPlan.from_dict(plan)
|
p = PreprocessPlan.from_dict(plan)
|
||||||
|
try:
|
||||||
raw = mne.io.read_raw_edf(edf_path, preload=True, verbose="error")
|
raw = mne.io.read_raw_edf(edf_path, preload=True, verbose="error")
|
||||||
|
except Exception as e: # annotation texts in a Windows code page
|
||||||
|
if "latin1" not in str(e):
|
||||||
|
raise
|
||||||
|
raw = mne.io.read_raw_edf(edf_path, preload=True, verbose="error", encoding="latin1")
|
||||||
ren, seen, drop = {}, set(), []
|
ren, seen, drop = {}, set(), []
|
||||||
for c in raw.ch_names: # edf_convert: unknown dropped, first duplicate kept
|
for c in raw.ch_names: # edf_convert: unknown dropped, first duplicate kept
|
||||||
n = _channels.normalize(c)
|
n = _channels.normalize(c)
|
||||||
|
|||||||
@@ -49,8 +49,13 @@ class Validation:
|
|||||||
import mne
|
import mne
|
||||||
|
|
||||||
info = check_file(edf_path)
|
info = check_file(edf_path)
|
||||||
raw = mne.io.read_raw_edf(edf_path, include=[o for o, _ in info["keep"]], preload=True,
|
kw = dict(include=[o for o, _ in info["keep"]], preload=True, verbose="error")
|
||||||
verbose="error")
|
try:
|
||||||
|
raw = mne.io.read_raw_edf(edf_path, **kw)
|
||||||
|
except Exception as e: # annotation texts in a Windows code page
|
||||||
|
if "latin1" not in str(e):
|
||||||
|
raise
|
||||||
|
raw = mne.io.read_raw_edf(edf_path, encoding="latin1", **kw)
|
||||||
raw.rename_channels({o: n for o, n in info["keep"] if o in raw.ch_names and o != n})
|
raw.rename_channels({o: n for o, n in info["keep"] if o in raw.ch_names and o != n})
|
||||||
classes = _channels.classify(raw.ch_names)
|
classes = _channels.classify(raw.ch_names)
|
||||||
names = [n for n in raw.ch_names if classes[n] in INCLUDE]
|
names = [n for n in raw.ch_names if classes[n] in INCLUDE]
|
||||||
|
|||||||
@@ -93,6 +93,23 @@ def test_no_posterior_channels_is_not_an_error(tmp_path):
|
|||||||
assert not np.isfinite(t.posterior_rhythm_hz.iloc[0]), impl
|
assert not np.isfinite(t.posterior_rhythm_hz.iloc[0]), impl
|
||||||
|
|
||||||
|
|
||||||
|
def test_annotation_text_not_utf8_is_not_an_error(tmp_path):
|
||||||
|
"""ISSSTELEÓN: annotation texts written in a Windows code page (bytes invalid in UTF-8).
|
||||||
|
Legacy reads them as the director's edf_convert does (UTF-8 with replacement)."""
|
||||||
|
p = make_edf(tmp_path / "cp1252.edf", minutes=6, annotations=[(30.0, None, "ojos cerrXdos")])
|
||||||
|
b = Path(p).read_bytes()
|
||||||
|
assert b.count(b"cerrXdos") == 1
|
||||||
|
Path(p).write_bytes(b.replace(b"cerrXdos", b"cerr\x8cdos"))
|
||||||
|
for impl in ("legacy", "modern"):
|
||||||
|
t = Pipeline.default(impl=impl).map([p], plan=DIRECTOR_CLUSTER, ages_months=[24],
|
||||||
|
biomarkers=("posterior_rhythm",),
|
||||||
|
**({"cluster": "sync"} if impl == "modern" else {}))
|
||||||
|
assert t.error.iloc[0] == "", (impl, t.error.iloc[0])
|
||||||
|
from tea_modulo.legacy.preprocessing.api import _annotations
|
||||||
|
a = _annotations(p)
|
||||||
|
assert any(d.startswith("ojos cerr") for d in a.description)
|
||||||
|
|
||||||
|
|
||||||
def _inverse(x):
|
def _inverse(x):
|
||||||
return 1 / x
|
return 1 / x
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user