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

[MRG] allow copyfile_brainvision to work with .dat extension #1008

Merged
merged 3 commits into from
May 16, 2022
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
3 changes: 3 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The following authors had contributed before. Thank you for sticking around!
* `Richard Höchenberger`_
* `Robert Luke`_
* `Stefan Appelhoff`_
* `Dominik Welke`_

Detailed list of changes
~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -63,6 +64,8 @@ Detailed list of changes

- The MNE-BIDS Inspector didn't work if ``mne-qt-browser`` was installed and used as the default plotting backend, as the Inspector currently only supports the Matplotlib backend, by `Richard Höchenberger`_ (:gh:`1007`)

- :func:`mne.copyfiles.copyfile_brainvision` can now deal with ``.dat`` file extension, by `Dominik Welke`_ (:gh:`1008`)

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

.. include:: authors.rst
30 changes: 17 additions & 13 deletions mne_bids/copyfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _get_brainvision_encoding(vhdr_file):


def _get_brainvision_paths(vhdr_path):
"""Get the .eeg and .vmrk file paths from a BrainVision header file.
"""Get the .eeg/.dat and .vmrk file paths from a BrainVision header file.

Parameters
----------
Expand All @@ -83,7 +83,7 @@ def _get_brainvision_paths(vhdr_path):
Returns
-------
paths : tuple
Paths to the .eeg file at index 0 and the .vmrk file at index 1 of
Paths to the .eeg/.dat file at index 0 and the .vmrk file at index 1 of
the returned tuple.

"""
Expand All @@ -100,10 +100,11 @@ def _get_brainvision_paths(vhdr_path):
with open(vhdr_path, 'r', encoding=enc) as f:
lines = f.readlines()

# Try to find data file .eeg
eeg_file_match = re.search(r'DataFile=(.*\.eeg)', ' '.join(lines))
# Try to find data file .eeg/.dat
eeg_file_match = re.search(r'DataFile=(.*\.(eeg|dat))', ' '.join(lines))

if not eeg_file_match:
raise ValueError('Could not find a .eeg file link in'
raise ValueError('Could not find a .eeg or .dat file link in'
f' {vhdr_path}')
else:
eeg_file = eeg_file_match.groups()[0]
Expand Down Expand Up @@ -154,7 +155,7 @@ def copyfile_ctf(src, dest):
"""
_copytree(src, dest)
# list of file types to rename
file_types = ('.acq', '.eeg', '.hc', '.hist', '.infods', '.bak',
file_types = ('.acq', '.eeg', '.dat', '.hc', '.hist', '.infods', '.bak',
'.meg4', '.newds', '.res4')
# Rename files in dest with the name of the dest directory
fnames = [f for f in os.listdir(dest) if f.endswith(file_types)]
Expand Down Expand Up @@ -266,10 +267,11 @@ def _anonymize_brainvision(vhdr_file, date):
def copyfile_brainvision(vhdr_src, vhdr_dest, anonymize=None, verbose=None):
"""Copy a BrainVision file triplet to a new location and repair links.

The BrainVision file format consists of three files: .vhdr, .eeg, and .vmrk
The .eeg and .vmrk files associated with the .vhdr file will be given names
as in `vhdr_dest` with adjusted extensions. Internal file pointers will be
fixed.
The BrainVision file format consists of three files:
.vhdr, .eeg/.dat, and .vmrk
The .eeg/.dat and .vmrk files associated with the .vhdr file will be
given names as in `vhdr_dest` with adjusted extensions. Internal file
pointers will be fixed.

Parameters
----------
Expand Down Expand Up @@ -319,17 +321,19 @@ def copyfile_brainvision(vhdr_src, vhdr_dest, anonymize=None, verbose=None):
# extract encoding from brainvision header file, or default to utf-8
enc = _get_brainvision_encoding(vhdr_src)

# Copy data .eeg ... no links to repair
# Copy data .eeg/.dat ... no links to repair
sh.copyfile(eeg_file_path, fname_dest + '.eeg')

# Write new header and marker files, fixing the file pointer links
# For that, we need to replace an old "basename" with a new one
# assuming that all .eeg, .vhdr, .vmrk share one basename
# assuming that all .eeg/.dat, .vhdr, .vmrk share one basename
__, basename_src = op.split(fname_src)
assert basename_src + '.eeg' == op.split(eeg_file_path)[-1]
assert op.split(eeg_file_path)[-1] in [
basename_src + '.eeg', basename_src + '.dat']
assert basename_src + '.vmrk' == op.split(vmrk_file_path)[-1]
__, basename_dest = op.split(fname_dest)
search_lines = ['DataFile=' + basename_src + '.eeg',
'DataFile=' + basename_src + '.dat',
'MarkerFile=' + basename_src + '.vmrk']

with open(vhdr_src, 'r', encoding=enc) as fin:
Expand Down