From 4abd83c58439232e87fc6c0f0a5791b48a9f6843 Mon Sep 17 00:00:00 2001 From: Jacob Woessner Date: Mon, 16 Dec 2024 11:15:38 -0600 Subject: [PATCH 1/5] Init commit --- mne/annotations.py | 15 ++++++++++++++- mne/utils/__init__.pyi | 2 ++ mne/utils/check.py | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/mne/annotations.py b/mne/annotations.py index 629ee7b20cb..e200d1c24d4 100644 --- a/mne/annotations.py +++ b/mne/annotations.py @@ -36,6 +36,7 @@ _check_option, _check_pandas_installed, _check_time_format, + _check_wfdb_installed, _convert_times, _DefaultEventParser, _dt_to_stamp, @@ -1140,7 +1141,7 @@ def _write_annotations_txt(fname, annot): @fill_doc def read_annotations( - fname, sfreq="auto", uint16_codec=None, encoding="utf8", ignore_marker_types=False + fname, sfreq="auto", uint16_codec=None, encoding="utf8", ignore_marker_types=False, fmt="auto", suffix=None ) -> Annotations: r"""Read annotations from a file. @@ -1222,6 +1223,7 @@ def read_annotations( ".bdf": {"encoding": encoding}, ".gdf": {"encoding": encoding}, } + print(fname.suffix) if fname.suffix in readers: annotations = readers[fname.suffix](fname, **kwargs.get(fname.suffix, {})) elif fname.name.endswith(("fif", "fif.gz")): @@ -1231,6 +1233,8 @@ def read_annotations( annotations = _read_annotations_fif(fid, tree) elif fname.name.startswith("events_") and fname.suffix == ".mat": annotations = _read_brainstorm_annotations(fname) + elif fmt == "wfdb": + annotations = _read_wfdb_annotations(fname, suffix=suffix) else: raise OSError(f'Unknown annotation file format "{fname}"') @@ -1512,6 +1516,15 @@ def _check_event_description(event_desc, events): return event_desc +def _read_wfdb_annotations(fname, suffix=None): + """Read annotations from wfdb format.""" + wfdb = _check_wfdb_installed(strict=True) + anno = wfdb.io.rdann(fname.stem, extension=suffix) + print(anno) + print(anno.__dict__) + + + @verbose def events_from_annotations( diff --git a/mne/utils/__init__.pyi b/mne/utils/__init__.pyi index 46d272e972d..c6663a68a4d 100644 --- a/mne/utils/__init__.pyi +++ b/mne/utils/__init__.pyi @@ -48,6 +48,7 @@ __all__ = [ "_check_preload", "_check_pybv_installed", "_check_pymatreader_installed", + "_check_wfdb_installed", "_check_qt_version", "_check_range", "_check_rank", @@ -246,6 +247,7 @@ from .check import ( _check_preload, _check_pybv_installed, _check_pymatreader_installed, + _check_wfdb_installed, _check_qt_version, _check_range, _check_rank, diff --git a/mne/utils/check.py b/mne/utils/check.py index 973fa33fe79..8fbaab694b1 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -452,6 +452,10 @@ def _check_edfio_installed(strict=True): """Aux function.""" return _soft_import("edfio", "exporting to EDF", strict=strict) +def _check_wfdb_installed(strict=True): + """Aux function.""" + return _soft_import("wfdb", "MIT WFDB IO", strict=strict) + def _check_pybv_installed(strict=True): """Aux function.""" From 644d31d63013e6b10e29b417d266814806bbaede Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:24:42 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/annotations.py | 11 ++++++++--- mne/utils/__init__.pyi | 2 +- mne/utils/check.py | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mne/annotations.py b/mne/annotations.py index e200d1c24d4..30f651203b1 100644 --- a/mne/annotations.py +++ b/mne/annotations.py @@ -1141,7 +1141,13 @@ def _write_annotations_txt(fname, annot): @fill_doc def read_annotations( - fname, sfreq="auto", uint16_codec=None, encoding="utf8", ignore_marker_types=False, fmt="auto", suffix=None + fname, + sfreq="auto", + uint16_codec=None, + encoding="utf8", + ignore_marker_types=False, + fmt="auto", + suffix=None, ) -> Annotations: r"""Read annotations from a file. @@ -1516,6 +1522,7 @@ def _check_event_description(event_desc, events): return event_desc + def _read_wfdb_annotations(fname, suffix=None): """Read annotations from wfdb format.""" wfdb = _check_wfdb_installed(strict=True) @@ -1524,8 +1531,6 @@ def _read_wfdb_annotations(fname, suffix=None): print(anno.__dict__) - - @verbose def events_from_annotations( raw, diff --git a/mne/utils/__init__.pyi b/mne/utils/__init__.pyi index c6663a68a4d..770afd0bde6 100644 --- a/mne/utils/__init__.pyi +++ b/mne/utils/__init__.pyi @@ -247,7 +247,6 @@ from .check import ( _check_preload, _check_pybv_installed, _check_pymatreader_installed, - _check_wfdb_installed, _check_qt_version, _check_range, _check_rank, @@ -256,6 +255,7 @@ from .check import ( _check_stc_units, _check_subject, _check_time_format, + _check_wfdb_installed, _ensure_events, _ensure_int, _import_h5io_funcs, diff --git a/mne/utils/check.py b/mne/utils/check.py index 8fbaab694b1..723b88b05bc 100644 --- a/mne/utils/check.py +++ b/mne/utils/check.py @@ -452,6 +452,7 @@ def _check_edfio_installed(strict=True): """Aux function.""" return _soft_import("edfio", "exporting to EDF", strict=strict) + def _check_wfdb_installed(strict=True): """Aux function.""" return _soft_import("wfdb", "MIT WFDB IO", strict=strict) From 70b78a760d41d38bfd01ca9d82e7ac5aca8a75ed Mon Sep 17 00:00:00 2001 From: Jacob Woessner Date: Mon, 16 Dec 2024 11:48:14 -0600 Subject: [PATCH 3/5] Update mne/annotations.py Co-authored-by: Eric Larson --- mne/annotations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mne/annotations.py b/mne/annotations.py index 30f651203b1..629b5aa108b 100644 --- a/mne/annotations.py +++ b/mne/annotations.py @@ -1143,6 +1143,7 @@ def _write_annotations_txt(fname, annot): def read_annotations( fname, sfreq="auto", + *, uint16_codec=None, encoding="utf8", ignore_marker_types=False, From 892a59e4e3e54d1612cb22230ad12bd77799967a Mon Sep 17 00:00:00 2001 From: Jacob Woessner Date: Mon, 16 Dec 2024 12:15:19 -0600 Subject: [PATCH 4/5] Update annotations --- mne/annotations.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mne/annotations.py b/mne/annotations.py index 629b5aa108b..24eb14e6720 100644 --- a/mne/annotations.py +++ b/mne/annotations.py @@ -1524,12 +1524,17 @@ def _check_event_description(event_desc, events): return event_desc -def _read_wfdb_annotations(fname, suffix=None): +def _read_wfdb_annotations(fname, suffix=None, sfreq="auto"): """Read annotations from wfdb format.""" wfdb = _check_wfdb_installed(strict=True) anno = wfdb.io.rdann(fname.stem, extension=suffix) - print(anno) - print(anno.__dict__) + anno_dict = anno.__dict__ + sfreq = anno_dict["fs"] if sfreq == "auto" else sfreq + onset = anno_dict["sample"] / sfreq + duration = np.zeros_like(onset) + description = anno_dict["symbol"] + return Annotations(onset, duration, description) + @verbose From 1016d8edfa91fa884a256f7c8b435ed9617a077f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Dec 2024 18:15:36 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mne/annotations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mne/annotations.py b/mne/annotations.py index 24eb14e6720..b60ea1502e0 100644 --- a/mne/annotations.py +++ b/mne/annotations.py @@ -1536,7 +1536,6 @@ def _read_wfdb_annotations(fname, suffix=None, sfreq="auto"): return Annotations(onset, duration, description) - @verbose def events_from_annotations( raw,