Skip to content

Refactor _handle_events_reading to allow extracting annotation information stand-alone #1389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c2d078a
refactoring to get events_file info standalone
matthiasdold Apr 3, 2025
61fd6bb
shortened docstring
matthiasdold Apr 3, 2025
c3f3317
added authors and whats_new
matthiasdold Apr 3, 2025
8428c75
remove _ for private naming convention of events_file_to_annotation_k…
matthiasdold Apr 3, 2025
f17ef79
corrected _ in events_file_to_annotation_kwargs
matthiasdold Apr 3, 2025
44a885e
added html-noplot directive to check local doc builds without plots
matthiasdold Apr 4, 2025
49fa312
back to private naming convention
matthiasdold Apr 4, 2025
2942010
removing func handles as modified are private functions
matthiasdold Apr 4, 2025
f98ac2b
added unit tests for events_file_to_annotation_kwargs
matthiasdold Apr 7, 2025
2dd91cc
added example to docstring
matthiasdold Apr 7, 2025
8868355
docstring for pytest function
matthiasdold Apr 7, 2025
4fdf132
updated whats_new and keeping the Makefile changes local
matthiasdold Apr 8, 2025
d7f92da
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 8, 2025
10f634c
exposing events_file_to_annotation_kwargs at mne_bids level
matthiasdold Apr 8, 2025
4e6b554
trying double backticks
matthiasdold Apr 9, 2025
02d26b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 9, 2025
cce5558
Update doc/whats_new.rst
sappelhoff Apr 10, 2025
9c75050
added events_file_to_anntation_kwargs to doc/api.rst
matthiasdold Apr 10, 2025
b62ae70
fixing docstring
matthiasdold Apr 10, 2025
a4ce5e5
pytest adjustments
matthiasdold Apr 16, 2025
6559e55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ authors:
family-names: Gerçek
affiliation: 'University of Geneva, Department of Fundamental Neuroscience'
orcid: 'https://orcid.org/0000-0003-1063-6769'
- given-names: Matthias
family-names: Dold
affiliation: 'Donders Institute for Brain, Cognition and Behaviour, Radboud University, Nijmegen, Netherlands'
orcid: 'https://orcid.org/0009-0003-1477-4912'
- given-names: Alexandre
family-names: Gramfort
affiliation: 'Université Paris-Saclay, Inria, CEA, Palaiseau, France'
Expand Down
1 change: 1 addition & 0 deletions doc/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
.. _Mara Wolter: https://github.com/marakw
.. _Marijn van Vliet: https://github.com/wmvanvliet
.. _Mathieu Scheltienne: https://github.com/mscheltienne
.. _Matthias Dold: https://github.com/matthiasdold
.. _Matt Sanderson: https://github.com/monkeyman192
.. _Maximilien Chaumon: https://github.com/dnacombo
.. _Moritz Gerster: http://moritz-gerster.com
Expand Down
2 changes: 2 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following authors contributed for the first time. Thank you so much! 🤩
* `Christian O'Reilly`_
* `Berk Gerçek`_
* `Arne Gottwald`_
* `Matthias Dold`_

The following authors had contributed before. Thank you for sticking around! 🤘

Expand All @@ -39,6 +40,7 @@ Detailed list of changes
- Empty-room matching now preferentially finds recordings in the subject directory tagged as `task-noise` before looking in the `sub-emptyroom` directories. This adds support for a part of the BIDS specification for ER recordings, by `Berk Gerçek`_ (:gh:`1364`)
- Path matching is now implemenented in a more efficient manner within :meth:`mne_bids.BIDSPath.match()` and :func:`mne_bids.find_matching_paths()`, by `Arne Gottwald` (:gh:`1355`)
- :func:`mne_bids.get_entity_vals()` has a new parameter ``include_match`` to prefilter item matching and ignore non-matched items from begin of directory scan, by `Arne Gottwald` (:gh:`1355`)
- `_handle_events_reading()` was refactored to now exposed the filtering of the `events.tsv` data as a separate function :func:`mne_bids.read.events_file_to_annotation_kwargs`, by `Matthias Dold` (:gh:`1389`)


🧐 API and behavior changes
Expand Down
18 changes: 15 additions & 3 deletions mne_bids/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ def _handle_info_reading(sidecar_fname, raw):
return raw


def _handle_events_reading(events_fname, raw):
"""Read associated events.tsv and convert valid events to annotations on Raw."""
def events_file_to_annotation_kwargs(events_fname: str) -> dict:
"""Read the `events.tsv` file and extract onset, duration, and description."""
logger.info(f"Reading events from {events_fname}.")
events_dict = _from_tsv(events_fname)

Expand Down Expand Up @@ -601,9 +601,21 @@ def _handle_events_reading(events_fname, raw):
[0 if du == "n/a" else du for du in events_dict["duration"]], dtype=float
)

return {"onset": ons, "duration": durs, "description": descrs, "event_id": event_id}


def _handle_events_reading(events_fname, raw):
"""Read associated events.tsv and convert valid events to annotations on Raw."""
annotations_info = events_file_to_annotation_kwargs(events_fname)
event_id = annotations_info["event_id"]

# Add events as Annotations, but keep essential Annotations present in raw file
annot_from_raw = raw.annotations.copy()
annot_from_events = mne.Annotations(onset=ons, duration=durs, description=descrs)
annot_from_events = mne.Annotations(
onset=annotations_info["onset"],
duration=annotations_info["duration"],
description=annotations_info["description"],
)
raw.set_annotations(annot_from_events)

annot_idx_to_keep = [
Expand Down
Loading