{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7440a5b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py\n",
    "import os\n",
    "import warnings\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2401aaef",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = \"adabase-public-0020-v_0_0_2.h5py\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46280999",
   "metadata": {},
   "outputs": [],
   "source": [
    "SKT_SR = 100\n",
    "ECG_SR = 500\n",
    "RSP_SR = 250\n",
    "EMG_SR = 1000\n",
    "EDA_SR = 500\n",
    "EYE_SR = 250"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e23eb552",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_signals = pd.read_hdf(file_path, \"SIGNALS\", mode=\"r\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7f494d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.max_rows', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd2f4d84",
   "metadata": {},
   "outputs": [],
   "source": [
    "settings = df_signals[['STUDY','PHASE','LEVEL']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1699ddc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "settings.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4731c56",
   "metadata": {},
   "source": [
    "Actions units"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9db0b4b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_signals.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ceccc89",
   "metadata": {},
   "outputs": [],
   "source": [
    "au_data = df_signals.iloc[:,-20:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d4ee088",
   "metadata": {},
   "outputs": [],
   "source": [
    "au_data.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d85a8cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(au_data.shape)\n",
    "print(au_data.isna().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efff356f",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_data = au_data.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42ed1bcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_au_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c7c3f14",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(clean_au_data.columns)):\n",
    "    print(clean_au_data.iloc[:,i].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "332740a8",
   "metadata": {},
   "source": [
    "Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f30b8814",
   "metadata": {},
   "outputs": [],
   "source": [
    "# df_signals_ecg = pd.read_hdf(file_path, \"SIGNALS\", mode=\"r\", columns=[\"STUDY\",\"LEVEL\", \"PHASE\", 'RAW_ECG_I'])\n",
    "df_signals_ecg = df_signals[[\"STUDY\",\"LEVEL\", \"PHASE\", 'RAW_ECG_I']]\n",
    "df_signals_ecg.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee80fd79",
   "metadata": {},
   "outputs": [],
   "source": [
    "study_filter = df_signals[\"STUDY\"] == \"n-back\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ef29446",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(16, 2))\n",
    "# Set the number of seconds to plot\n",
    "seconds = 20\n",
    "# Get the ECG signal data\n",
    "ecg_signal = df_signals.loc[study_filter, \"RAW_ECG_I\"].dropna()\n",
    "# Set the x-axis limits to the number of samples in the specified time range\n",
    "num_samples = ECG_SR * seconds\n",
    "# Plot the ECG signal\n",
    "ax.plot(ecg_signal.index[:num_samples]/1000, ecg_signal[:num_samples]);\n",
    "ax.set_title(\"ECG I\");\n",
    "ax.set_xlabel('Seconds');\n",
    "# Set figure size with a 16:6 aspect ratio\n",
    "fig, ax = plt.subplots(figsize=(16, 2))\n",
    "# Set the number of seconds to plot\n",
    "start_second = 0\n",
    "end_second = 60*30\n",
    "# Get the EYE signal data - we replace inf with nan to get the original signal.␣\n",
    "\n",
    "eye_left_signal = df_signals.loc[study_filter, \"LEFT_PUPIL_DIAMETER\"].dropna()\n",
    "eye_right_signal = df_signals.loc[study_filter, \"RIGHT_PUPIL_DIAMETER\"].dropna()\n",
    "#eye_left_signal = df_signals.loc[:, \"LEFT_PUPIL_DIAMETER\"].replace([np.inf],␣\n",
    "\n",
    "#eye_right_signal = df_signals.loc[:, \"RIGHT_PUPIL_DIAMETER\"].replace([np.inf],␣\n",
    "\n",
    "# Set the x-axis limits to the number of samples in the specified time range\n",
    "num_samples_start = EYE_SR * start_second\n",
    "num_samples_end = EYE_SR * end_second\n",
    "ax.plot(eye_left_signal.index[num_samples_start:num_samples_end]/1000,eye_left_signal[num_samples_start:num_samples_end], label=\"Left\")\n",
    "ax.plot(eye_right_signal.index[num_samples_start:num_samples_end]/1000,eye_right_signal[num_samples_start:num_samples_end], label=\"Right\")\n",
    "ax.set_title(\"Pupil Dilation\")\n",
    "ax.set_xlabel('Seconds')\n",
    "ax.legend()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
