{
 "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": 2,
   "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": 3,
   "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": 4,
   "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": [
    "### Unsupervised Learning of Blinking State"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "candidates = ebpm.unsupervised.extract_candidates(np.concatenate([ear_l, ear_r]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "prototype = candidates[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 204508 characters]",
      "text/plain": [
       "<Figure size 1000x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ebpm.plot.candidates_overview(candidates=candidates)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57476 characters]",
      "text/plain": [
       "<Figure size 1000x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ebpm.plot.candidates_overview(candidates=prototype)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Detection of the Prototype in the Time Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "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": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Contains 11 matches\n",
      "Matches: [[591, 691, 4.54211799664586], [1709, 1809, 2.1264802266890244], [4155, 4255, 2.7390968929682145], [6145, 6245, 3.1460980163222034], [6689, 6789, 7.64305367267573], [9487, 9587, 4.739295662054528], [11118, 11218, 5.550462616380028], [11973, 12073, 7.409097990229975], [12485, 12585, 3.2583760763760066], [14852, 14952, 2.9444718535223777], [17270, 17370, 3.4382259360712535]]\n"
     ]
    }
   ],
   "source": [
    "ebpm.match.describe(matches_l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 146228 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": "markdown",
   "metadata": {},
   "source": [
    "#### Sync the matches between the eyes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 145464 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": [
    "### Compare different windwo sizes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49300 characters]",
      "text/plain": [
       "<Figure size 1000x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 143076 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "candidates = ebpm.unsupervised.extract_candidates(np.concatenate([ear_l, ear_r]), window_length=50)\n",
    "prototype = candidates[0]\n",
    "ebpm.plot.candidates_overview(candidates=prototype)\n",
    "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": [
    "#### 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57476 characters]",
      "text/plain": [
       "<Figure size 1000x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 145464 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "candidates = ebpm.unsupervised.extract_candidates(np.concatenate([ear_l, ear_r]), window_length=100)\n",
    "prototype = candidates[0]\n",
    "ebpm.plot.candidates_overview(candidates=prototype)\n",
    "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": [
    "#### 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 64084 characters]",
      "text/plain": [
       "<Figure size 1000x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 147364 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "candidates = ebpm.unsupervised.extract_candidates(np.concatenate([ear_l, ear_r]), window_length=200)\n",
    "prototype = candidates[0]\n",
    "ebpm.plot.candidates_overview(candidates=prototype)\n",
    "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()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ebpm",
   "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
}
