Jump to content

User Reference:Python FileLoader

From BCI2000 Wiki

Introduction

The BCI2kReader package allows to read BCI2000 .dat files in Python. The reader is written completely in python and works with python 2 and python 3.

Installing the package

The package can either be installed with the python package manager (pip) or you can download the latest version from GitHub.

To install the package via pip:

pip install BCI2kReader

To get the latest (development) version from GitHub

git clone https://github.com/markusadamek/BCI2kReader

To Install the development version move to the cloned directory and run:

pip install .

Usage

To use the reader you have to create a BCI2kReader object, which inherits from io.IOBase. The reader has two basic modes, caching and non-caching. In caching mode, the BCI2kReader object will retain the data in memory as soon as a full readout of the file has taken place. If you need to save memory, the BCI2kReader object also supports that all read operations are done directly from the file.

Reading a File

The simplest way to open a file is with pythons with command (see https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects for more information about file IO and the with statement).


from BCI2kReader import BCI2kReader as b2k


with b2k.BCI2kReader('yourbci2000testfile.dat') as file:

if you want to open the file without caching enabled:

with b2k.BCI2kReader('yourbci2000testfile.dat', False) as file:

It is also possible to clear the cache to free memory:

      file.purge()

Getting Data from the object

The easiest way to get the data is to use the properties for signals and states. The signals are provided as a numpy ndarray with the dimensions (channels, samples). The states are provided as a StateDictionary which inherits from dict with the state name as key.

   eeg_data = file.signals
   states = file.states

Working with random access