{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import torch\n",
    "plt.rcParams['figure.dpi'] = 200\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 = 5\n",
    "    fig, axs = plt.subplots(rows, cols, figsize=(cols*11, rows*8))\n",
    "    data_iterator = iter(samples)\n",
    "    x = 5*np.arange(1, samples.shape[-1]+1) / 60\n",
    "    plot_dict = {0: 'Heart Rate (Beats/Min)', 2: 'Respiration (Breaths/Min)', 4: 'SPO2 (%)', 6: 'Mean Arterial Pressure (mmHg)', 8: 'Mortality (1/0)'}\n",
    "    \n",
    "    for i in range(rows):\n",
    "        timeseries = next(data_iterator)\n",
    "        color_choice = iter(sns.color_palette())\n",
    "        for j in range(cols):\n",
    "            idx = 2*j\n",
    "            if idx == 8:\n",
    "                sns.lineplot(x=x, y=timeseries[idx], ax=axs[i, j], linewidth=5, label=plot_dict[idx], color=next(color_choice))\n",
    "                axs[i, j].set_ylim(-0.3, 1.3)\n",
    "            else:\n",
    "                data = np.where(timeseries[idx+1] == 1, np.nan, timeseries[idx])\n",
    "                if np.all(np.isnan(data)):\n",
    "                    next(color_choice)\n",
    "                    continue\n",
    "                else:\n",
    "                    sns.lineplot(x=x, y=data, ax=axs[i, j], linewidth=2, label=plot_dict[idx], color=next(color_choice))\n",
    "            axs[i, j].set_xlabel(\"Time (Hours)\", fontsize=30)\n",
    "            axs[i, j].set_ylabel(\"Measurement Value\", fontsize=30)\n",
    "            axs[i, j].legend(fontsize=30)\n",
    "            axs[i, j].tick_params(axis='both', which='major', labelsize=30)\n",
    "\n",
    "    \n",
    "    plt.tight_layout()\n",
    "    plt.legend()\n",
    "    # self.wandb.log({\"check_point_performance\": self.wandb.Image(plt)})\n",
    "    # plt.show()\n",
    "    # plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## All Patients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-eicu_multiple_60_2880_564.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_all_48hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-eicu_multiple_60_1440_276.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_all_24hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sepsis/Septicemia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-SEPSIS-eicu_multiple_60_2880_564.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_sepsis_48hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-SEPSIS-eicu_multiple_60_1440_276.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_sepsis_24hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Acute Myocardial Infarction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-AMI-eicu_multiple_60_2880_564.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_ami_48hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-AMI-eicu_multiple_60_1440_276.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_ami_24hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Acute Kidney Failure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-AKF-eicu_multiple_60_2880_564.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_akf_48hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-AKF-eicu_multiple_60_1440_276.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_akf_24hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Heart Failure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-HF-eicu_multiple_60_2880_564.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_hf_48hrs.png')\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seed set to 2023\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from helpers import seed_everything\n",
    "seed_everything(2023)\n",
    "data = torch.load('data/eicu-extract/TRAIN-HF-eicu_multiple_60_1440_276.pt')\n",
    "perm = torch.randperm(data.shape[0])\n",
    "data = data[perm, :, :]\n",
    "visualize(data[:50, :, :])\n",
    "plt.savefig('img/eicu_hf_24hrs.png')\n",
    "plt.close()"
   ]
  }
 ],
 "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
}
