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

Use built-in MNE export for BrainVision #417

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 2 additions & 28 deletions src/mnelab/io/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from pathlib import Path

import mne
import numpy as np
from numpy.core.records import fromarrays
from scipy.io import savemat

Expand Down Expand Up @@ -59,33 +57,9 @@ def write_edf(fname, raw):
raw.export(fname)


def write_bv(fname, raw, events=None):
def write_bv(fname, raw):
"""Export data to BrainVision EEG/VHDR/VMRK file (requires pybv)."""
import pybv

name, _ = Path(fname).stem, "".join(Path(fname).suffixes)
parent = Path(fname).parent
data = raw.get_data()
fs = raw.info["sfreq"]
ch_names = raw.info["ch_names"]
if events is None:
if raw.annotations:
events = mne.events_from_annotations(raw, regexp=None)[0]
dur = raw.annotations.duration * fs
events = np.column_stack([events[:, [0, 2]], dur.astype(int)])
else:
events = events[:, [0, 2]]
units = [ch["unit"] for ch in raw.info["chs"]]
units = ["µV" if unit == 107 else "AU" for unit in units]
pybv.write_brainvision(
data=data,
sfreq=fs,
ch_names=ch_names,
fname_base=name,
folder_out=parent,
events=events,
unit=units,
)
raw.export(fname=Path(fname).with_suffix(".vhdr"))


# this dict contains each supported file extension as a key; the corresponding value is a
Expand Down