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

Fixes for EEGLAB: do not 'simplify cells' when loading. #1039

Merged
merged 4 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Detailed list of changes

- :func:`~mne_bids.read_raw_bids` doesn't populate ``raw.info['subject_info']`` with invalid values anymore, preventing users from writing the data to disk again, by `Richard Höchenberger`_ (:gh:`1031`)

- Writing EEGLAB files was sometimes broken when ``.set`` and ``.fdt`` pairs were supplied. This is now fixed in :func:`~mne_bids.copyfiles.copyfile_eeglab`, by `Stefan Appelhoff`_ (:gh:`1039`)

- Writing and copying CTF files now works on Windows when files already exist (``overwrite=True``), by `Stefan Appelhoff`_ (:gh:`1035`)

- Instead of deleting files and raising cryptic errors, an intentional error message is now sent when calling :func:`~mne_bids.write_raw_bids` with the source file identical to the destination file, unless ``format`` is specified, by `Adam Li`_ and `Stefan Appelhoff`_ (:gh:`889`)
Expand Down
40 changes: 27 additions & 13 deletions mne_bids/copyfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from mne_bids.path import BIDSPath, _parse_ext, _mkdir_p
from mne_bids.utils import _get_mrk_meas_date, _check_anonymize
import numpy as np


def _copytree(src, dst, **kwargs):
Expand Down Expand Up @@ -495,12 +496,12 @@ def copyfile_edf(src, dest, anonymize=None):


def copyfile_eeglab(src, dest):
"""Copy a EEGLAB files to a new location and adjust pointer to '.fdt' file.
"""Copy an EEGLAB file to a new location and adjust .fdt pointer in file.
sappelhoff marked this conversation as resolved.
Show resolved Hide resolved

Some EEGLAB .set files come with a .fdt binary file that contains the data.
When moving a .set file, we need to check for an associated .fdt file and
move it to an appropriate location as well as update an internal pointer
within the .set file.
Sometimes an EEGLAB .set file comes with a .fdt binary file that contains
the data. When moving a .set file, we need to check for an associated .fdt
file and move it to an appropriate location as well as update an internal
pointer within the .set file.
sappelhoff marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
Expand Down Expand Up @@ -529,27 +530,40 @@ def copyfile_eeglab(src, dest):
f' but got {ext_src}, {ext_dest}')

# Load the EEG struct
# NOTE: do *not* simplify cells, because this changes the underlying
# structure and potentially breaks re-reading of the file
uint16_codec = None
eeg = loadmat(file_name=src, simplify_cells=True,
eeg = loadmat(file_name=src, simplify_cells=False,
appendmat=False, uint16_codec=uint16_codec)
oldstyle = False
if 'EEG' in eeg:
eeg = eeg['EEG']
oldstyle = True

if isinstance(eeg['data'], str):
try:
# If the data field is a string, it points to a .fdt file in src dir
fdt_fname = eeg['data']
assert fdt_fname.endswith('.fdt')
head, tail = op.split(src)
if isinstance(eeg['data'][0, 0][0], str):
has_fdt_link = True
except IndexError:
has_fdt_link = False

if has_fdt_link:
fdt_fname = eeg['data'][0, 0][0]

assert fdt_fname.endswith('.fdt'), f'Unexpected fdt name: {fdt_fname}'
head, _ = op.split(src)
fdt_path = op.join(head, fdt_fname)

# Copy the .fdt file and give it a new name
sh.copyfile(fdt_path, fname_dest + '.fdt')
fdt_name_new = fname_dest + '.fdt'
sh.copyfile(fdt_path, fdt_name_new)

# Now adjust the pointer in the .set file
head, tail = op.split(fname_dest + '.fdt')
eeg['data'] = tail
# NOTE: Clunky numpy code is to match MATLAB structure for "savemat"
_, tail = op.split(fdt_name_new)
new_value = np.empty((1, 1), dtype=object)
new_value[0, 0] = np.atleast_1d(np.array(tail))
eeg['data'] = new_value

# Save the EEG dictionary as a Matlab struct again
mdict = dict(EEG=eeg) if oldstyle else eeg
Expand Down
9 changes: 1 addition & 8 deletions mne_bids/tests/test_copyfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pytest

import mne
from mne.fixes import _compare_version
from mne.datasets import testing
from mne_bids import BIDSPath
from mne_bids.path import _parse_ext
Expand Down Expand Up @@ -198,16 +197,10 @@ def test_copyfile_edfbdf_uppercase(tmp_path):
'test_raw_2021.set'))
def test_copyfile_eeglab(tmp_path, fname):
"""Test the copying of EEGlab set and fdt files."""
if (
fname == 'test_raw_chanloc.set' and
_compare_version(testing.get_version(), '<', '0.112')
):
return

bids_root = str(tmp_path)
data_path = op.join(testing.data_path(), 'EEGLAB')
raw_fname = op.join(data_path, fname)
new_name = op.join(bids_root, 'tested_conversion.set')
new_name = op.join(bids_root, f'CONVERTED_{fname}.set')

# IO error testing
with pytest.raises(ValueError, match="Need to move data with same ext"):
Expand Down