Skip to main content

Read and write EEG data in the BrainVision Core data format

Project description

Read and write EEG data in the BrainVision Core data format

This is a Python implementation to read and write EEG data in the BrainVision Core data format as defined by BrainProducts and as used in BIDS.

The BrainVision Core data format consists of three files: the .vhdr file with header information, the .vmrk with markers or events, and the data in a file that usually has the extension .eeg. All files have to be in the same folder.

The information from the header and marker is not parsed but retained as a dictionary with strings. Below some examples are given to get for example the channel names as list

This implementation can read from 16 and 32 bit integer formats and 32 bit float formats. It supports multiplexed and vectorized orientations. The data is returned as a channels-by-samples Numpy array with float32 values.

Installation

This package is maintained on PyPi and can be installed with pip.

pip install brainvision

Example

The following example reads data from disk.

import brainvision

# read the data
(vhdr, vmrk, eeg) = brainvision.read('test/input.vhdr')

# check the size of the data
(nchans, nsamples) = eeg.shape
print(nchans, nsamples)

# parse the header
nchans = int(vhdr['Common Infos']['NumberOfChannels'])
fsample = 1000000.0 / float(vhdr['Common Infos']['SamplingInterval'])
labels = [item.split(',')[0] for item in vhdr['Channel Infos'].values()]
units  = [item.split(',')[3] for item in vhdr['Channel Infos'].values()]
print(nchans, fsample, labels, units)

# parse the markers
type        = [item.split(',')[0] for item in vmrk['Marker Infos'].values()]
description = [item.split(',')[1] for item in vmrk['Marker Infos'].values()]
sample      = [int(item.split(',')[2])-1 for item in vmrk['Marker Infos'].values()]   # in data points, 0-based
duration    = [int(item.split(',')[3])   for item in vmrk['Marker Infos'].values()]   # in data points
channel     = [int(item.split(',')[4])   for item in vmrk['Marker Infos'].values()]   # note that this is 1-based
print(type, description, sample, duration, channel)

The following example constructs data from scratch and writes it to disk. Upon writing the header and markerfile, the vhdr and vmrk dictionaries will be validated to ensure that they contain the required fields.

import numpy as np
import brainvision

vhdr = {'Common Infos': {'Codepage': 'UTF-8', 'DataFile': 'output.eeg', 'MarkerFile': 'output.vmrk', 'DataFormat': 'BINARY', 'DataOrientation': 'MULTIPLEXED', 'NumberOfChannels': '1', 'SamplingInterval': '1000'}, 'Binary Infos': {'BinaryFormat': 'IEEE_FLOAT_32'}, 'Channel Infos': {'Ch1': '1,,0.5,µV'}}

vmrk = {'Common Infos': {'Codepage': 'UTF-8', 'DataFile': 'output.eeg'}, 'Marker Infos': {'Mk1': 'New Segment,,1,1,0'}}

nchans = 1
nsamples = 1000
rng = np.random.default_rng()
eeg = rng.standard_normal((nchans, nsamples))

# write the data
brainvision.write('output.vhdr', vhdr, vmrk, eeg) 

Known limitations

This implementation currently cannot read ASCII data.

This implementation currently cannot deal with little to big endian conversions, hence reading and writing the binary data is only supported on little endian architectures. Apple silicon and Intel-based Mac computers are both little endian. Also Raspberry PI's ARM and Intel NUC's processors are little endian.

Copyright

Copyright (C) 2024, Robert Oostenveld

This code is dual-licensed under the BSD 3-Clause "New" or "Revised" License and under the GPLv3 license, the choice is up to you.

When you make changes/iomprovements, please share them back to us so that others benefit as well.

Acknowledgements

The test data that is included to demonstrate the functionality and to test the reading and writing originates from the pybv package.

See also

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brainvision-0.3.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

brainvision-0.3-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file brainvision-0.3.tar.gz.

File metadata

  • Download URL: brainvision-0.3.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for brainvision-0.3.tar.gz
Algorithm Hash digest
SHA256 3772fd2e345e6026a5a7da1f20d97f464c783301f9de753d959ce6293b2528e9
MD5 7f9d88193ae50169637ce0890bf6e53c
BLAKE2b-256 025f829eb9137397838e145d543a8340933bae4a1fbf6b0fa2c7bc517595b0ee

See more details on using hashes here.

File details

Details for the file brainvision-0.3-py3-none-any.whl.

File metadata

  • Download URL: brainvision-0.3-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for brainvision-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 78b213755b7d9794895935ae7021a7dfe89cb71e6f8d8da3769c4be901f9fb74
MD5 e3e40cfb149d56e97ead7f2f75b314c7
BLAKE2b-256 8411bf3a75fbd4d0c35969100cd5a56910951bf8ba6e742e618ba36455b8fea8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page