{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(710, 7, 100)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "\n",
    "def visualize(data, subplots, figsize=(20, 10), linewidth=1, markersize=5, marker='s') -> None:\n",
    "    fig, axs = plt.subplots(subplots[0], subplots[1], figsize=figsize)\n",
    "    data_iterator = iter(data)\n",
    "    \n",
    "    for i in range(subplots[0]):\n",
    "        for j in range(subplots[1]):\n",
    "            timeseries = next(data_iterator)\n",
    "            sns.lineplot(x=np.arange(1, len(timeseries)+1), y=timeseries, ax=axs[i, j], linewidth=linewidth, marker=marker, markersize=markersize)\n",
    "            axs[i, j].set_xlabel(\"Time (every 2 mins)\")\n",
    "            axs[i, j].set_ylabel(\"Value\")\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.show() \n",
    "\n",
    "data = np.load('data/hirid-multiple-checkpoint-alive.npy')\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "def visualize(samples: torch.Tensor, figsize=(20, 10), linewidth=1, markersize=5, marker='s', feature_names: list=None) -> None:\n",
    "    samples = samples.numpy()\n",
    "    rows = samples.shape[0] // 3\n",
    "    cols = 3\n",
    "    fig, axs = plt.subplots(rows, cols, figsize=(cols*9, rows*6))\n",
    "    data_iterator = iter(samples)\n",
    "    \n",
    "    for i in range(rows):\n",
    "        for j in range(cols):\n",
    "            timeseries = next(data_iterator)\n",
    "            if len(timeseries.shape) != 1:\n",
    "                for feature_idx in range(timeseries.shape[0]):         \n",
    "                    if feature_names is not None:\n",
    "                        name = feature_names[feature_idx]\n",
    "                    sns.lineplot(x=np.arange(1, timeseries.shape[-1]+1), y=timeseries[feature_idx], ax=axs[i, j], linewidth=1, label=name)\n",
    "            else:\n",
    "                sns.lineplot(x=np.arange(1, len(timeseries)+1), y=timeseries, ax=axs[i, j], linewidth=1)\n",
    "            axs[i, j].set_xlabel(\"Time (every 2 minutes)\")\n",
    "            axs[i, j].set_ylabel(\"Value\")\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.legend()\n",
    "    # self.wandb.log({\"check_point_performance\": self.wandb.Image(plt)})\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([710, 7, 100])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_tensor = torch.tensor(data)\n",
    "data_tensor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([710, 7, 100])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.save(data_tensor, 'data/hirid-multiple-checkpoint-alive.pt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "HEARTRATE_ID = [200]        # heartrate\n",
    "SYSBP_ID = [100]            # Invasive systolic arterial pressure\n",
    "DIABP_ID = [120]            # Invasive diastolic arterial pressure\n",
    "MAP_ID = [110]                # Invasive mean arterial pressure\n",
    "SATURATION_ID = [4000, 8280]        # Peripheral oxygen saturation\n",
    "ST_ID = [210, 211, 212]        # ST elevation\n",
    "CVP_ID = [700, 15001441, 960]    # CVP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9431528 characters]",
      "text/plain": [
       "<Figure size 2700x18000 with 90 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "visualize(data_tensor[:90], feature_names=['HR', 'SBP', 'DBP', 'MAP', 'SPO2', 'ST', 'CVP'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gen",
   "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.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
