{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sample Experiment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from scipy import signal\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.gridspec as gridspec\n",
    "import matplotlib as mpl\n",
    "import stumpy\n",
    "\n",
    "import eye_state_prototype\n",
    "from eye_state_prototype import motif_extraction, learn_prototype, learn_prototypes, combined_gaussian\n",
    "from eye_state_prototype import fpm, find_peaks_in_ear_ts, cal_bpm_results\n",
    "from eye_state_prototype import plot_ear, plot_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "mpl.rcParams['text.usetex'] = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 100620 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load data\n",
    "filepath = \"data/Proband 06_2023-08-02_15-15-05.csv\"\n",
    "proband_df = pd.read_csv(filepath)\n",
    "\n",
    "ear_r = proband_df[\"EAR2D6_r\"].values\n",
    "ear_l = proband_df[\"EAR2D6_l\"].values\n",
    "n = len(ear_r)\n",
    "\n",
    "eye_state_prototype.plot_ear(ear_r, ear_l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Learned Prototype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prototype Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get motif distances and indices\n",
    "motif_distances, motif_indices = motif_extraction(ear_r[0:20000], m=100, max_matches=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fa3d1782530>]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 27788 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# get motif No.1\n",
    "motifs = learn_prototypes(ear_r[0:20000], m=100, max_matches=10)\n",
    "plt.plot(motifs[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Blink Pattern Matching"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # load example data\n",
    "# learned_prototype = np.loadtxt(\"./data/learned_prototype_example_th3.5_top9_no1.txt\")\n",
    "# plt.plot(learned_prototype)\n",
    "# plt.xlabel(\"Frame\")\n",
    "# plt.ylabel(\"EAR\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_learned = cal_bpm_results(ear_r, ear_l, prototype=learned_prototype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'EAR (left)')"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40684 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axs = plt.subplots(2, 1, figsize=(10, 3), sharex=True, sharey=True)\n",
    "plot_results(ax=axs[0], ears=(ear_r, ear_l), results=results_learned, side=\"right\", xmin=0, xmax=20000)\n",
    "plot_results(ax=axs[1], ears=(ear_r, ear_l), results=results_learned, side=\"left\", xmin=0, xmax=20000)\n",
    "axs[0].set_xlabel(\"Frame\")\n",
    "axs[0].set_ylabel(\"EAR (right)\")\n",
    "axs[1].set_ylabel(\"EAR (left)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Statistics\n",
    "todo?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manual Prototype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Manual Prototype Definition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "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>sigma1</th>\n",
       "      <th>sigma2</th>\n",
       "      <th>avg_open</th>\n",
       "      <th>prominence</th>\n",
       "      <th>noise_std</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6.333333</td>\n",
       "      <td>13.666667</td>\n",
       "      <td>0.302698</td>\n",
       "      <td>0.254082</td>\n",
       "      <td>0.006772</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     sigma1     sigma2  avg_open  prominence  noise_std\n",
       "0  6.333333  13.666667  0.302698    0.254082   0.006772"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# load parameters\n",
    "gaussian_params_df = pd.read_csv(\"./data/gaussian_params_example.csv\")\n",
    "display(gaussian_params_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set values\n",
    "sig1 = gaussian_params_df[\"sigma1\"].values\n",
    "sig2 = gaussian_params_df[\"sigma2\"].values\n",
    "avg = gaussian_params_df[\"avg_open\"].values\n",
    "prom = gaussian_params_df[\"prominence\"].values\n",
    "std = gaussian_params_df[\"noise_std\"].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7fc65e2f9d20>]"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21940 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "manual_prototype = combined_gaussian(sig1, sig2, avg, prom, m=100, noise=None)\n",
    "plt.plot(manual_prototype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Blink Pattern Matching"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # load example data\n",
    "# manual_prototype = np.loadtxt(\"./data/manual_prototype_example_fs_m100.txt\")\n",
    "# plt.plot(learned_prototype)\n",
    "# plt.xlabel(\"Frame\")\n",
    "# plt.ylabel(\"EAR\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_learned = cal_bpm_results(ear_r, ear_l, prototype=learned_prototype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'EAR (left)')"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40684 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axs = plt.subplots(2, 1, figsize=(10, 3), sharex=True, sharey=True)\n",
    "plot_results(ax=axs[0], ears=(ear_r, ear_l), results=results_learned, side=\"right\", xmin=0, xmax=20000)\n",
    "plot_results(ax=axs[1], ears=(ear_r, ear_l), results=results_learned, side=\"left\", xmin=0, xmax=20000)\n",
    "axs[0].set_xlabel(\"Frame\")\n",
    "axs[0].set_ylabel(\"EAR (right)\")\n",
    "axs[1].set_ylabel(\"EAR (left)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Statistics\n",
    "todo?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myml",
   "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
