Skip to content

Commit bd8c318

Browse files
skjernspre-commit-ci[bot]larsoner
authored
[FIX] Reading an EDF with preload=False and mixed frequency (mne-tools#13069)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Eric Larson <[email protected]>
1 parent 087779c commit bd8c318

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

doc/changes/devel/13069.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug cause by unnecessary assertion when loading mixed frequency EDFs without preloading :func:`mne.io.read_raw_edf` by `Simon Kern`_.

mne/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ def pytest_configure(config: pytest.Config):
186186
ignore:.*builtin type swigvarlink has no.*:DeprecationWarning
187187
# eeglabio
188188
ignore:numpy\.core\.records is deprecated.*:DeprecationWarning
189+
ignore:Starting field name with a underscore.*:
189190
# joblib
190191
ignore:process .* is multi-threaded, use of fork/exec.*:DeprecationWarning
191192
""" # noqa: E501

mne/io/edf/edf.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,21 +436,24 @@ def _read_segment_file(data, idx, fi, start, stop, raw_extras, filenames, cals,
436436
ones[orig_idx, smp_read : smp_read + len(one_i)] = one_i
437437
n_smp_read[orig_idx] += len(one_i)
438438

439+
# resample channels with lower sample frequency
439440
# skip if no data was requested, ie. only annotations were read
440-
if sum(n_smp_read) > 0:
441+
if any(n_smp_read) > 0:
441442
# expected number of samples, equals maximum sfreq
442443
smp_exp = data.shape[-1]
443-
assert max(n_smp_read) == smp_exp
444444

445445
# resample data after loading all chunks to prevent edge artifacts
446446
resampled = False
447+
447448
for i, smp_read in enumerate(n_smp_read):
448449
# nothing read, nothing to resample
449450
if smp_read == 0:
450451
continue
451452
# upsample if n_samples is lower than from highest sfreq
452453
if smp_read != smp_exp:
453-
assert (ones[i, smp_read:] == 0).all() # sanity check
454+
# sanity check that we read exactly how much we expected
455+
assert (ones[i, smp_read:] == 0).all()
456+
454457
ones[i, :] = resample(
455458
ones[i, :smp_read].astype(np.float64),
456459
smp_exp,

mne/io/edf/tests/test_edf.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,24 @@ def test_edf_different_sfreqs(stim_channel):
259259
assert_allclose(times1, times2)
260260

261261

262+
@testing.requires_testing_data
263+
@pytest.mark.parametrize("stim_channel", (None, False, "auto"))
264+
def test_edf_different_sfreqs_nopreload(stim_channel):
265+
"""Test loading smaller sfreq channels without preloading."""
266+
# load without preloading, then load a channel that has smaller sfreq
267+
# as other channels, produced an error, see mne-python/issues/12897
268+
269+
for i in range(1, 13):
270+
raw = read_raw_edf(input_fname=edf_reduced, verbose="error", preload=False)
271+
272+
# this should work for channels of all sfreq, even if larger sfreqs
273+
# are present in the file
274+
x1 = raw.get_data(picks=[f"A{i}"], return_times=False)
275+
# load next ch, this is sometimes with a higher sometimes a lower sfreq
276+
x2 = raw.get_data([f"A{i + 1}"], return_times=False)
277+
assert x1.shape == x2.shape
278+
279+
262280
def test_edf_data_broken(tmp_path):
263281
"""Test edf files."""
264282
raw = _test_raw_reader(

0 commit comments

Comments
 (0)