Skip to content

Commit 5238cf7

Browse files
author
Mathieu Scheltienne
authored
Fix export to EDF format with a set physical range smaller than the data range (#11569)
1 parent 417a9c4 commit 5238cf7

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

doc/changes/latest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Bugs
5656
- Expand tilde (user directory) in config keys (:gh:`11537` by `Clemens Brunner`_)
5757
- Fix bug in :func:`mne.preprocessing.compute_maxwell_basis` where using ``int_order=0`` would raise an error (:gh:`11562` by `Eric Larson`_)
5858
- Fix :func:`mne.io.read_raw` for file names containing multiple dots (:gh:`11521` by `Clemens Brunner`_)
59+
- Fix bug in :func:`mne.export.export_raw` when exporting to EDF with a physical range set smaller than the data range (:gh:`11569` by `Mathieu Scheltienne`_)
5960
6061
API changes
6162
~~~~~~~~~~~

mne/export/_edf.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ def _export_raw(fname, raw, physical_range, add_ch_type):
142142

143143
# check that physical min and max is not exceeded
144144
if data.max() > pmax:
145-
raise RuntimeError(f'The maximum μV of the data {data.max()} is '
146-
f'more than the physical max passed in {pmax}.')
145+
warn(
146+
f"The maximum μV of the data {data.max()} is "
147+
f"more than the physical max passed in {pmax}.",
148+
)
147149
if data.min() < pmin:
148-
raise RuntimeError(f'The minimum μV of the data {data.min()} is '
149-
f'less than the physical min passed in {pmin}.')
150+
warn(
151+
f"The minimum μV of the data {data.min()} is "
152+
f"less than the physical min passed in {pmin}.",
153+
)
150154

151155
# create instance of EDF Writer
152156
with _auto_close(EDFwriter(fname, file_type, n_channels)) as hdl:

mne/export/tests/test_export.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
from contextlib import nullcontext
88
from datetime import datetime, timezone
9-
from mne.io import RawArray
10-
from mne.io.meas_info import create_info
9+
from os import remove
1110
from pathlib import Path
1211

1312
import pytest
@@ -20,8 +19,9 @@
2019
from mne.datasets import testing, misc
2120
from mne.export import export_evokeds, export_evokeds_mff
2221
from mne.fixes import _compare_version
23-
from mne.io import (read_raw_fif, read_raw_eeglab, read_raw_edf,
22+
from mne.io import (RawArray, read_raw_fif, read_raw_eeglab, read_raw_edf,
2423
read_raw_brainvision)
24+
from mne.io.meas_info import create_info
2525
from mne.utils import (_check_eeglabio_installed, requires_version,
2626
object_diff, _check_edflib_installed, _resource_path,
2727
_check_pybv_installed, _record_warnings)
@@ -309,12 +309,23 @@ def test_export_raw_edf(tmp_path, dataset, format):
309309
temp_fname = tmp_path / f"test.{format}"
310310

311311
# test runtime errors
312-
with pytest.raises(RuntimeError, match='The maximum'), \
313-
pytest.warns(RuntimeWarning, match='Data has a non-integer'):
312+
with pytest.warns() as record:
314313
raw.export(temp_fname, physical_range=(-1e6, 0))
315-
with pytest.raises(RuntimeError, match='The minimum'), \
316-
pytest.warns(RuntimeWarning, match='Data has a non-integer'):
314+
if dataset == 'test':
315+
assert any(
316+
"Data has a non-integer" in str(rec.message) for rec in record
317+
)
318+
assert any("The maximum" in str(rec.message) for rec in record)
319+
remove(temp_fname)
320+
321+
with pytest.warns() as record:
317322
raw.export(temp_fname, physical_range=(0, 1e6))
323+
if dataset == 'test':
324+
assert any(
325+
"Data has a non-integer" in str(rec.message) for rec in record
326+
)
327+
assert any("The minimum" in str(rec.message) for rec in record)
328+
remove(temp_fname)
318329

319330
if dataset == 'test':
320331
with pytest.warns(RuntimeWarning, match='Data has a non-integer'):

0 commit comments

Comments
 (0)