{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unsupervised Learning and Extraction of Blinking Patterns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import ebpm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data Loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Frame</th>\n",
       "      <th>EAR_r</th>\n",
       "      <th>EAR_l</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.286274</td>\n",
       "      <td>0.269362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.291212</td>\n",
       "      <td>0.268412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0.291212</td>\n",
       "      <td>0.269362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>0.297798</td>\n",
       "      <td>0.270195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.291479</td>\n",
       "      <td>0.269233</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Frame     EAR_r     EAR_l\n",
       "0      0  0.286274  0.269362\n",
       "1      1  0.291212  0.268412\n",
       "2      2  0.291212  0.269362\n",
       "3      3  0.297798  0.270195\n",
       "4      4  0.291479  0.269233"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ear_data = pd.read_csv('data/sample_ear_short.csv')\n",
    "display(ear_data.head())\n",
    "\n",
    "ear_l = ear_data['EAR_l'].values\n",
    "ear_r = ear_data['EAR_r'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 134584 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ebpm.plot.ear_time_series(ear_l, ear_r)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Manual Definition of Blinking State"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40 <class 'int'>\n",
      "(200,) (200,)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 56392 characters]",
      "text/plain": [
       "<Figure size 1000x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "prototype, params = ebpm.manual.define_prototype(return_params=True)\n",
    "ebpm.plot.manual_prototype(prototype=prototype, params=params)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "matches_l = ebpm.match.find_prototype(ear_l, prototype, max_prototype_distance=3.0)\n",
    "matches_r = ebpm.match.find_prototype(ear_r, prototype, max_prototype_distance=3.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Contains 11 matches\n",
      "Matches: [[589, 689, 3.5358267697667203], [1708, 1808, 3.0173705742151613], [2947, 3047, 7.50379639420479], [4154, 4254, 2.549847694541923], [6146, 6246, 3.8319857889474234], [6688, 6788, 7.9008293941488486], [9486, 9586, 5.908257854329013], [11121, 11221, 5.947827087428848], [12484, 12584, 4.0799082441139225], [14851, 14951, 3.5085707593298734], [17270, 17370, 3.6808498247376025]]\n"
     ]
    }
   ],
   "source": [
    "ebpm.match.describe(matches_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 146132 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ebpm.plot.matches(ear_l, ear_r, matches_l, matches_r)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 145400 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "matches_l = ebpm.match.find_prototype(ear_l, prototype)\n",
    "matches_r = ebpm.match.find_prototype(ear_r, prototype)\n",
    "matches_l, matches_r = ebpm.match.index_matching(matches_l, matches_r)\n",
    "ebpm.plot.matches(ear_l, ear_r, matches_l, matches_r)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Interactive Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e6b425316e3541ea8aa8a4a0f6ce22e2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=10.0, description='sig1', max=20.0, min=1.0, step=1.0), FloatSlider(va…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# interactive selection of prototype parameters\n",
    "@interact(\n",
    "    sig1=(1.0, 20.0, 1.0),\n",
    "    sig2=(1.0, 20.0, 1.0),\n",
    "    prominance=(0.0, 0.3, 0.01),\n",
    "    apex_location=(0.0, 1.0, 0.1),\n",
    "    window_size=(50, 200, 25),\n",
    "    noise=[False, True],\n",
    ")\n",
    "def g(sig1, sig2, prominance, apex_location, window_size, noise):\n",
    "    prototype, params = ebpm.manual.define_prototype(\n",
    "        sig1=sig1,\n",
    "        sig2=sig2,\n",
    "        baseline=0.3,\n",
    "        prominance=prominance,\n",
    "        apex_location=apex_location,\n",
    "        window_size=window_size,\n",
    "        noise=noise,\n",
    "        return_params=True\n",
    "    )\n",
    "    ebpm.plot.manual_prototype(prototype=prototype, params=params)"
   ]
  }
 ],
 "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.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
