Skip to content

Commit

Permalink
[BUG] Add overwrite to export_raw for EDF conversions (#930)
Browse files Browse the repository at this point in the history
* Fix bug

* Add changelog

* Fix unit test and add doc string

* Fix

* Small change to test

Co-authored-by: Richard Höchenberger <richard.hoechenberger@gmail.com>
  • Loading branch information
adam2392 and hoechenberger authored Dec 18, 2021
1 parent 4c0755c commit 8a29504
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Requirements
Bug fixes
^^^^^^^^^

- ...
- Forcing EDF conversion in :func:`mne_bids.write_raw_bids` properly uses the ``overwrite`` parameter now, by `Adam Li`_ (:gh:`930`)

:doc:`Find out what was new in previous releases <whats_new_previous_releases>`

Expand Down
33 changes: 33 additions & 0 deletions mne_bids/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
from pathlib import Path
import codecs
import warnings

from pkg_resources import parse_version

Expand Down Expand Up @@ -2851,6 +2852,38 @@ def test_convert_eeg_formats(dir_name, format, fname, reader, tmp_path):
raw.get_data(), raw2.get_data()[:, :orig_len], decimal=6)


@requires_version('mne', '0.24')
@requires_version('pybv', '0.6')
@pytest.mark.parametrize(
'dir_name, format, fname, reader', test_converteeg_data)
@pytest.mark.filterwarnings(
warning_str['channel_unit_changed'], warning_str['edfblocks'])
def test_format_conversion_overwrite(dir_name, format, fname, reader,
tmp_path):
"""Test that overwrite works when format is passed to write_raw_bids."""
bids_root = tmp_path / format
data_path = op.join(testing.data_path(), dir_name)
raw_fname = op.join(data_path, fname)

# the BIDS path for test datasets to get written to
bids_path = _bids_path.copy().update(root=bids_root, datatype='eeg')

raw = reader(raw_fname)
# drop 'misc' type channels when exporting
raw = raw.pick_types(eeg=True)
kwargs = dict(raw=raw, format=format, bids_path=bids_path, verbose=False)

with warnings.catch_warnings():
# ignore all warnings for this case to remove verbosity
# this unit test is not meant to test for warnings
warnings.filterwarnings('ignore')

# writing with the 'format' parameter should always work
# if overwrite is True
write_raw_bids(**kwargs)
write_raw_bids(**kwargs, overwrite=True)


@requires_version('mne', '0.22')
@pytest.mark.parametrize(
'dir_name, format, fname, reader', test_converteeg_data)
Expand Down
8 changes: 5 additions & 3 deletions mne_bids/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ def _write_raw_brainvision(raw, bids_fname, events, overwrite):
meas_date=meas_date)


def _write_raw_edf(raw, bids_fname):
def _write_raw_edf(raw, bids_fname, overwrite):
"""Store data as EDF.
Parameters
Expand All @@ -969,9 +969,11 @@ def _write_raw_edf(raw, bids_fname):
Raw data to save.
bids_fname : str
The output filename.
overwrite : bool
Whether to overwrite an existing file or not.
"""
assert str(bids_fname).endswith('.edf')
raw.export(bids_fname)
raw.export(bids_fname, overwrite=overwrite)


@verbose
Expand Down Expand Up @@ -1644,7 +1646,7 @@ def write_raw_bids(raw, bids_path, events_data=None, event_id=None,
if ext == '.pdf' else bids_path.fpath))
elif bids_path.datatype in ['eeg', 'ieeg'] and format == 'EDF':
warn('Converting data files to EDF format')
_write_raw_edf(raw, bids_path.fpath)
_write_raw_edf(raw, bids_path.fpath, overwrite=overwrite)
else:
warn('Converting data files to BrainVision format')
bids_path.update(suffix=bids_path.datatype, extension='.vhdr')
Expand Down

0 comments on commit 8a29504

Please sign in to comment.