{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c6a31e9d",
   "metadata": {},
   "source": [
    "# EEG records exploring\n",
    "\n",
    "BCI-Framework store the record by default using the data format defined by [OpenBCI-Stream](https://openbci-stream.readthedocs.io/en/latest/notebooks/07-data_storage_handler.html), this makes it a lot easy to extract and crop data based on markers. We are working with a run generated by the [4-Class motor imagery (Arrows)](bci_framework/default_extensions/Class_motor_imagery/paradigm.ipynb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de481544",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================================\n",
      "record-04_10_21-18_07_25.h5\n",
      "2021-04-10 18:07:25.843257\n",
      "==================================================\n",
      "MARKERS: ['Up', 'Bottom', 'Left', 'Right']\n",
      "SAMPLE_RATE: 1000\n",
      "STREAMING_SAMPLE_RATE: 100\n",
      "DATETIME: 1618096045.843257\n",
      "MONTAGE: standard_1020\n",
      "CHANNELS: {1: 'Fp1', 2: 'Fp2', 3: 'T3', 4: 'C3', 5: 'C4', 6: 'T4', 7: 'O1', 8: 'O2'}\n",
      "START-OFFSET: 11.801958084106445\n",
      "SHAPE: [8, 259700]\n",
      "END-OFFSET: 12.935876846313477\n",
      "==================================================\n"
     ]
    }
   ],
   "source": [
    "from openbci_stream.utils.hdf5 import HDF5Reader\n",
    "\n",
    "filename = \"record-04_10_21-18_07_25.h5\"\n",
    "file = HDF5Reader(filename)\n",
    "print(file)\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ab6411e",
   "metadata": {},
   "source": [
    "Notice the 4 markers, and the channels."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c7a00ef",
   "metadata": {},
   "source": [
    "## MNE epochs\n",
    "\n",
    "`get_epochs()` return an `mne.EpochsArray` object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d10c87df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Info | 8 non-empty values\n",
       " bads: []\n",
       " ch_names: C3, Fp1, Fp2, C4, T3, T4, O2, O1\n",
       " chs: 8 EEG\n",
       " custom_ref_applied: False\n",
       " dig: 11 items (3 Cardinal, 8 EEG)\n",
       " highpass: 0.0 Hz\n",
       " lowpass: 500.0 Hz\n",
       " meas_date: unspecified\n",
       " nchan: 8\n",
       " projs: []\n",
       " sfreq: 1000.0 Hz\n",
       ">"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with HDF5Reader(filename) as file:\n",
    "    epochs = file.get_epochs(tmin=-2, duration=6)\n",
    "\n",
    "epochs.info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddf7be90",
   "metadata": {},
   "source": [
    "## Export to EDF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b47d7eff",
   "metadata": {},
   "outputs": [],
   "source": [
    "with HDF5Reader(filename) as file:\n",
    "    file.to_edf(filename.replace('.h5', '.edf'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99f1e977",
   "metadata": {},
   "source": [
    "## RAW `ndarray` objects\n",
    "\n",
    "`get_data()` return a standard array `(epochs, channels, time)` and their respective (`classes`):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7507c4cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((40, 8, 6000), (40,))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with HDF5Reader(filename) as file:\n",
    "    data, classes = file.get_data(tmin=-2, duration=6)\n",
    "\n",
    "data.shape, classes.shape"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
