Skip to content

Commit f7242b6

Browse files
committed
remove assertion for reading EDFs without preload
1 parent 087779c commit f7242b6

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

mne/io/edf/edf.py

+6-3
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 requency
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

+21
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,27 @@ def test_edf_different_sfreqs(stim_channel):
258258
assert_allclose(data1, data2, err_msg="Data mismatch with preload")
259259
assert_allclose(times1, times2)
260260

261+
@testing.requires_testing_data
262+
@pytest.mark.parametrize("stim_channel", (None, False, "auto"))
263+
def test_edf_different_sfreqs_nopreload(stim_channel):
264+
"""Test loading smaller sfreq channels without preloading"""
265+
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(
271+
input_fname=edf_reduced,
272+
verbose='error',
273+
preload=False)
274+
275+
# this should work for channels of all sfreq, even if larger sfreqs
276+
# are present in the file
277+
x1 = raw.get_data(picks=[f'A{i}'], return_times=False)
278+
# load next ch, this is sometimes with a higher sometimes a lower sfreq
279+
x2 = raw.get_data([f'A{i+1}'], return_times=False)
280+
assert x1.shape==x2.shape
281+
261282

262283
def test_edf_data_broken(tmp_path):
263284
"""Test edf files."""

0 commit comments

Comments
 (0)