{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "27f7f775-5b18-4bcd-b7c2-666586d93fa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "import numpy as np\n",
    "from openbci_stream.utils.hdf5 import HDF5Reader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "id": "1af3c889-78df-4111-9b5a-ab22837b6f19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================================\n",
      "/home/yeison/.bciframework/records/record-10_28_21-22_10_49.R.h5\n",
      "2021-10-28 22:10:49.579271\n",
      "==================================================\n",
      "MARKERS: ['Right', '1', 'Retention', 'No-changed', 'No-response', 'Left', 'Identical', '2', 'Changed', 'Different', '4']\n",
      "SAMPLE_RATE: 1000\n",
      "STREAMING_SAMPLE_RATE: 100\n",
      "DATETIME: 1635477049.579271\n",
      "MONTAGE: standard_1020\n",
      "CHANNELS: {1: 'Fp1', 2: 'Fp2', 3: 'F7', 4: 'F3', 5: 'Fz', 6: 'F4', 7: 'F8', 8: 'T7', 9: 'T8', 10: 'P7', 11: 'P3', 12: 'Pz', 13: 'P4', 14: 'P8', 15: 'O1', 16: 'O2'}\n",
      "CHANNELS_BY_BOARD: [16]\n",
      "SHAPE: [16, 1527998]\n",
      "==================================================\n"
     ]
    }
   ],
   "source": [
    "with HDF5Reader('/home/yeison/.bciframework/records/record-10_28_21-22_10_49.R.h5') as reader:\n",
    "    print(reader)\n",
    "    \n",
    "    eeg = reader.eeg\n",
    "    aux = reader.aux\n",
    "    timestamp = reader.timestamp\n",
    "    aux_timestamp = reader.aux_timestamp\n",
    "    header = reader.header\n",
    "    markers = reader.markers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f7bdec1-e78a-4fd3-9ded-cdbd449b64d7",
   "metadata": {},
   "source": [
    "# Auxiliar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "id": "fd9a1122-044a-4356-b1ea-0b1f344d5b2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 10000.0)"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26060 characters]\n",
      "text/plain": [
       "<Figure size 1800x450 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(20, 5), dpi=90)\n",
    "\n",
    "threshold = 500\n",
    "\n",
    "sensor = aux[0].copy()\n",
    "\n",
    "sensor[sensor>800] = 1000\n",
    "sensor[sensor<270] = 1000 \n",
    "\n",
    "# sensor[sensor<threshold] = 0\n",
    "# sensor[sensor>=threshold] = 1\n",
    "\n",
    "# plt.plot((np.diff(sensor)), '-')\n",
    "plt.plot(sensor, '-')\n",
    "plt.xlim(0, 10000)\n",
    "# plt.ylim(-20, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "bc5d0698-b29b-473f-8750-969d837fbec0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000.0, 100000.0)"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 43980 characters]\n",
      "text/plain": [
       "<Figure size 1800x450 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(20, 5), dpi=90)\n",
    "\n",
    "\n",
    "# sensor = aux[0].copy()\n",
    "\n",
    "\n",
    "sensor = np.diff(sensor, prepend=0)\n",
    "sensor[sensor<0] = 0\n",
    "\n",
    "plt.plot(aux_timestamp[0], sensor, '--')\n",
    "\n",
    "plt.vlines(np.array(markers['Right']), 0, 1, color='C1')\n",
    "plt.vlines(np.array(markers['Left']), 0, 1, color='C2')\n",
    "# plt.vlines(np.array(markers['Retention']), 0, 1, color='C3')\n",
    "\n",
    "plt.xlim(50000, 100000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2a1db18-f500-4720-91c7-2f0ebe698e3c",
   "metadata": {},
   "source": [
    "# Markers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6bbf7026-a018-46e9-a99f-652bd505d353",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Right', '1', 'Retention', 'No-changed', 'No-response', 'Left', 'Identical', '2', 'Changed', 'Different', '4'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "markers.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "0f890857-94a3-4137-8681-ca1cc809845d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.0, 50000.0)"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 8604 characters]\n",
      "text/plain": [
       "<Figure size 1800x450 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(20, 5), dpi=90)\n",
    "\n",
    "plt.vlines(np.array(markers['Right']), 0, 1, color='C0')\n",
    "plt.vlines(np.array(markers['Left']), 0, 1, color='C1')\n",
    "\n",
    "plt.xlim(0, 50000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "81aedcad-a6e8-4956-bdf8-040ecc8d0749",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Right', '1', 'Retention', 'No-changed', 'No-response', 'Left', 'Identical', '2', 'Changed', 'Different', '4'])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  }
 ],
 "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
