Skip to content

Commit 982d7a0

Browse files
Read EGI simple-binary event channels in blocks (#14250)
1 parent 01a094d commit 982d7a0

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up :func:`mne.io.read_raw_egi` on simple-binary files by reading the event channels in blocks instead of one sample at a time, by `Bruno Aristimunha`_.

mne/io/egi/egi.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,27 @@ def my_fread(*x, **y):
7979
return info
8080

8181

82+
# read whole frames a few MB at a time rather than the whole recording at once
83+
_EVENT_BLOCK_BYTES = 4 * 1024**2
84+
85+
8286
def _read_events(fid, info):
8387
"""Read events."""
84-
events = np.zeros([info["n_events"], info["n_segments"] * info["n_samples"]])
88+
n_samples = info["n_samples"]
89+
# Each sample is one frame of n_channels data values followed by n_events
90+
# event values, so read whole frames and keep the event rows. Seeking past
91+
# the data channels instead costs a seek and a read per sample, which
92+
# dominates the time to open a long recording.
93+
n_rows = info["n_channels"] + info["n_events"]
94+
events = np.zeros([info["n_events"], info["n_segments"] * n_samples])
8595
fid.seek(36 + info["n_events"] * 4, 0) # skip header
86-
for si in range(info["n_samples"]):
87-
# skip data channels
88-
fid.seek(info["n_channels"] * info["dtype"].itemsize, 1)
89-
# read event channels
90-
events[:, si] = np.fromfile(fid, info["dtype"], info["n_events"])
96+
n_block = max(1, _EVENT_BLOCK_BYTES // (n_rows * info["dtype"].itemsize))
97+
for start in range(0, n_samples, n_block):
98+
n_read = min(n_block, n_samples - start)
99+
frames = np.fromfile(fid, info["dtype"], n_read * n_rows)
100+
events[:, start : start + n_read] = frames.reshape(n_read, n_rows).T[
101+
info["n_channels"] :
102+
]
91103
return events
92104

93105

0 commit comments

Comments
 (0)