Skip to content

Instantly share code, notes, and snippets.

@maedoc
Created March 13, 2014 10:10
Show Gist options
  • Save maedoc/9525595 to your computer and use it in GitHub Desktop.
Save maedoc/9525595 to your computer and use it in GitHub Desktop.
lightweight Brain Vision data reader
import StringIO
import ConfigParser
import numpy as np
class VHDR(object):
"""
Brain Vision file.
"""
def __init__(self, filename):
self.filename = filename
# read file
with open(self.filename, 'r') as fd:
self.srclines = fd.readlines()
# config parser expects each section to have header
# but vhdr has some decorative information at the beginning
while not self.srclines[0].startswith('['):
self.srclines.pop(0)
self.sio = StringIO.StringIO()
self.sio.write('\n'.join(self.srclines))
self.sio.seek(0)
self.cp = ConfigParser.ConfigParser()
self.cp.readfp(self.sio)
for opt in self.cp.options('Common Infos'):
setattr(self, opt, self.cp.get('Common Infos', opt))
self.binaryformat = self.cp.get('Binary Infos', 'BinaryFormat')
self.labels = [self.cp.get('Channel Infos', o).split(',')[0]
for o in self.cp.options('Channel Infos')]
self.nchan = int(self.numberofchannels)
def read_data(self, mmap=True, dt='float32', mode='r'):
"""
VHDR stores data in channel contiguous way such that reading disparate pieces
in time is fast, when using memmap.
"""
if mmap:
ary = np.memmap(self.datafile, dt, mode)
else:
ary = np.fromfile(self.datafile, dt)
return ary.reshape((-1, self.nchan)).T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment