|
| 1 | +""" |
| 2 | +====================== |
| 3 | +Reading BCI2000 files |
| 4 | +====================== |
| 5 | +
|
| 6 | +In this example, we use MNE-Python to read a BCI2000 ``.dat`` file. |
| 7 | +BCI2000 is a general-purpose brain-computer interface (BCI) system widely |
| 8 | +used in EEG research. The file is downloaded from the MNE testing data |
| 9 | +repository using ``pooch``. |
| 10 | +""" # noqa: D205 D400 |
| 11 | + |
| 12 | +# Authors: The MNE-Python contributors. |
| 13 | +# License: BSD-3-Clause |
| 14 | +# Copyright the MNE-Python contributors. |
| 15 | + |
| 16 | +import pooch |
| 17 | + |
| 18 | +import mne |
| 19 | + |
| 20 | +# %% |
| 21 | +# First, we download the sample BCI2000 ``.dat`` file using ``pooch``. |
| 22 | + |
| 23 | +data_dir = mne.datasets.default_path() / "bci2k_data" |
| 24 | +data_dir.mkdir(exist_ok=True) |
| 25 | + |
| 26 | +fname = pooch.retrieve( |
| 27 | + url="https://raw.githubusercontent.com/mne-tools/mne-testing-data/master/BCI2k/bci2k_test.dat", |
| 28 | + known_hash="sha256:8efc7b5f700660a044086cb1449806ca408c2e6d32d9338c32e1bf31ce3ca9cb", |
| 29 | + path=data_dir, |
| 30 | +) |
| 31 | + |
| 32 | +# %% |
| 33 | +# Now we can read the file using :func:`mne.io.read_raw_bci2k`. |
| 34 | +# Note that ``preload=True`` is required for BCI2000 files. |
| 35 | + |
| 36 | +raw = mne.io.read_raw_bci2k(fname, preload=True) |
| 37 | +print(raw.info) |
| 38 | + |
| 39 | +# %% |
| 40 | +# We can inspect the object representation, channel names, types, sampling |
| 41 | +# frequency, and recording duration. |
| 42 | + |
| 43 | +print(raw) |
| 44 | +print(f"Channel names : {raw.ch_names}") |
| 45 | +print(f"Channel types : {raw.get_channel_types()}") |
| 46 | +print(f"Sampling freq : {raw.info['sfreq']} Hz") |
| 47 | +print(f"Duration : {raw.times[-1]:.2f} s") |
| 48 | +print(f"n_channels : {raw.info['nchan']}") |
| 49 | +print(f"Data shape : {raw.get_data().shape} (n_channels, n_samples)") |
| 50 | + |
| 51 | +# %% |
| 52 | +# If the BCI2000 file contains a ``StimulusCode`` state, it is automatically |
| 53 | +# mapped to a ``STI 014`` stim channel. We can extract events from it using |
| 54 | +# :func:`mne.find_events`. |
| 55 | + |
| 56 | +if "STI 014" in raw.ch_names: |
| 57 | + events = mne.find_events(raw, shortest_event=1) |
| 58 | + print(f"Found {len(events)} events") |
| 59 | + print(mne.count_events(events)) |
| 60 | +else: |
| 61 | + print("No stim channel found in this file.") |
| 62 | + |
| 63 | +# %% |
| 64 | +# Finally, we can visualize the raw data. |
| 65 | + |
| 66 | +raw.plot(duration=5, n_channels=len(raw.ch_names), scalings="auto") |
0 commit comments