Skip to content
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

MAINT: vectorize _read_annotations_edf #8214

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions doc/changes/latest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Changelog

- Add ``component_order`` parameter to :class:`mne.decoding.CSP` which allows switching between ``mutual_info`` (default) and ``alternate`` (a simpler and frequently used option) by `Martin Billinger`_ and `Clemens Brunner`_

- Speed up reading of annotations in EDF+ files by `Jeroen Van Der Donckt`_

Bug
~~~

Expand Down
2 changes: 2 additions & 0 deletions doc/changes/names.inc
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,5 @@
.. _Lau Møller Andersen: https://github.com/ualsbombe

.. _Martin Schulz: https://github.com/marsipu

.. _Jeroen Van Der Donckt: https://github.com/jvdd
13 changes: 8 additions & 5 deletions mne/io/edf/edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Stefan Appelhoff <stefan.appelhoff@mailbox.org>
# Joan Massich <mailsik@gmail.com>
# Clemens Brunner <clemens.brunner@gmail.com>
# Jeroen Van Der Donckt (IDlab - imec) <jeroen.vanderdonckt@ugent.be>
#
# License: BSD (3-clause)

Expand Down Expand Up @@ -1354,6 +1355,7 @@ def _read_annotations_edf(annotations):
triggers = re.findall(pat, annot_file.read())
else:
tals = bytearray()
annotations = np.atleast_2d(annotations)
for chan in annotations:
this_chan = chan.ravel()
if this_chan.dtype == np.int32: # BDF
Expand All @@ -1362,12 +1364,13 @@ def _read_annotations_edf(annotations):
# Why only keep the first 3 bytes as BDF values
# are stored with 24 bits (not 32)
this_chan = this_chan[:, :3].ravel()
for s in this_chan:
tals.extend(s)
# As ravel() returns a 1D array we can add all values at once
tals.extend(this_chan)
else:
for s in this_chan:
i = int(s)
tals.extend(np.uint8([i % 256, i // 256]))
this_chan = chan.astype(int)
# Exploit np vectorized processing
tals.extend(np.uint8([this_chan % 256, this_chan // 256])
.flatten('F'))

# use of latin-1 because characters are only encoded for the first 256
# code points and utf-8 can triggers an "invalid continuation byte"
Expand Down