Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Add overwrite to export_raw for EDF conversions #930

Merged
merged 5 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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