{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Preprocess data for HiRiD dataset to feed into model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "path = \"data/hirid/raw_stage/observation_tables\"\n",
    "index_df = pd.read_csv(f\"{path}/observation_tables_index.csv\")\n",
    "general_df = pd.read_csv(\"data/hirid/reference_data/general_table.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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>patientid</th>\n",
       "      <th>admissiontime</th>\n",
       "      <th>sex</th>\n",
       "      <th>age</th>\n",
       "      <th>discharge_status</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2197-04-03 19:15:00</td>\n",
       "      <td>M</td>\n",
       "      <td>75</td>\n",
       "      <td>alive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2117-10-18 22:35:00</td>\n",
       "      <td>M</td>\n",
       "      <td>80</td>\n",
       "      <td>alive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2163-10-17 19:15:00</td>\n",
       "      <td>M</td>\n",
       "      <td>55</td>\n",
       "      <td>alive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2149-01-08 15:30:00</td>\n",
       "      <td>M</td>\n",
       "      <td>75</td>\n",
       "      <td>alive</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2144-06-06 16:15:00</td>\n",
       "      <td>M</td>\n",
       "      <td>20</td>\n",
       "      <td>alive</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   patientid        admissiontime sex  age discharge_status\n",
       "0          1  2197-04-03 19:15:00   M   75            alive\n",
       "1          2  2117-10-18 22:35:00   M   80            alive\n",
       "2          3  2163-10-17 19:15:00   M   55            alive\n",
       "3          4  2149-01-08 15:30:00   M   75            alive\n",
       "4          5  2144-06-06 16:15:00   M   20            alive"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "general_df.head()"
   ]
  },
  {
   "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>patientid</th>\n",
       "      <th>part</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>216</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   patientid  part\n",
       "0          1    43\n",
       "1          2    74\n",
       "2          3    51\n",
       "3          4   102\n",
       "4          5   216"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(33904, 2)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(33905, 5)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "general_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 31604 alive patients\n",
      "There are 2062 dead patients\n",
      "Mortality Rate: 6.12%\n"
     ]
    }
   ],
   "source": [
    "alive_id = general_df[general_df[\"discharge_status\"] == 'alive']['patientid']\n",
    "dead_id = general_df[general_df[\"discharge_status\"] == 'dead']['patientid']\n",
    "print(f\"There are {len(alive_id)} alive patients\\nThere are {len(dead_id)} dead patients\\nMortality Rate: {100*len(dead_id) / (len(alive_id) + len(dead_id)):.2f}%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dask.dataframe as dd\n",
    "\n",
    "class Extractor:\n",
    "    def __init__(self, index_df, path=\"data/hirid/raw_stage/observation_tables/csv\"):\n",
    "        self.index_df = index_df\n",
    "        self.path = path\n",
    "    \n",
    "    def get_patient(self, patient_id, mode='dask'):\n",
    "        assert patient_id in self.index_df['patientid'].values, \"patient_id not found\"\n",
    "        row = self.index_df[self.index_df['patientid'] == patient_id]\n",
    "        part = int(row['part'])\n",
    "        if mode == 'pandas':\n",
    "            df = pd.read_csv(f\"{self.path}/part-{part}.csv\")\n",
    "        elif mode == 'dask':\n",
    "            df = dd.read_csv(f\"{self.path}/part-{part}.csv\", dtype={'stringvalue': 'object', 'type': 'object'})\n",
    "        df = df[df['patientid'] == patient_id]\n",
    "        return df\n",
    "    \n",
    "    def get_feature(self, patient_id, variable_id_arr, mode='dask'):\n",
    "        patient_df = self.get_patient(patient_id, mode=mode)\n",
    "        feature_df = patient_df[patient_df['variableid'].isin(variable_id_arr)]\n",
    "        \n",
    "        if isinstance(feature_df, pd.DataFrame):\n",
    "            return feature_df\n",
    "        elif isinstance(feature_df, dd.DataFrame):\n",
    "            return feature_df.compute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "extractor = Extractor(index_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize Alive Patients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "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": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [200] ========\n",
      "Patient 0 done\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 80 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25812 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [100] ========\n",
      "Patient 0 done\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 80 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24108 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [120] ========\n",
      "Patient 0 done\n",
      "Difference is above threshold, 90th percentile is 2.08604\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 80 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 23204 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [110] ========\n",
      "Patient 0 done\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 80 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25436 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [4000, 8280] ========\n",
      "Patient 0 done\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 80 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 27420 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [210, 211, 212] ========\n",
      "Patient 0 done\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 26760 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== [700, 15001441, 960] ========\n",
      "Patient 0 done\n",
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Difference is above threshold, 90th percentile is 856.6212\n",
      "Patient 60 done\n",
      "Patient 90 done\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 28444 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import warnings\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "for feature_arr in [HEARTRATE_ID, SYSBP_ID, DIABP_ID, MAP_ID, SATURATION_ID, ST_ID, CVP_ID]:\n",
    "    print(f\"======== {feature_arr} ========\")\n",
    "    num_measurements = []\n",
    "    for i, patient in enumerate(random.sample(list(dead_id), 100)):\n",
    "        df = extractor.get_feature(patient, feature_arr)\n",
    "        df['entertime'] = pd.to_datetime(df['entertime'])\n",
    "        df['time_difference'] = df['entertime'].diff()\n",
    "        df['time_difference'] = df['time_difference'].dt.total_seconds() / 60\n",
    "        if len(df) == 0:\n",
    "            continue\n",
    "        if df['time_difference'].quantile(0.8) <= 2.01:\n",
    "            pass\n",
    "        else:\n",
    "            print(f\"Difference is above threshold, 90th percentile is {df['time_difference'].quantile(0.9)}\")\n",
    "        num_measurements.append(len(df))\n",
    "\n",
    "        if i % 10 == 0:\n",
    "            print(f\"Patient {i} done\")\n",
    "\n",
    "    sns.histplot(num_measurements)\n",
    "    plt.xlabel(\"Number of measurements\")\n",
    "    plt.title(f\"Number of measurements for {feature_arr}\")\n",
    "    plt.ylabel(\"Frequency\")\n",
    "    plt.xlim(0, 2000)\n",
    "    plt.show()\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import numpy as np\n",
    "import joblib\n",
    "\n",
    "def extract_data(extractor, timesteps, num_patients, variable_id_arr, patient_id) -> np.ndarray:\n",
    "    samples = []\n",
    "    # pbar = tqdm(total=num_patients, desc=f'Extracting patients with {timesteps} timesteps...')\n",
    "    for i, patient in enumerate(list(patient_id)):          # for every patient\n",
    "        feature_samples = []\n",
    "        \n",
    "        for variable_id in variable_id_arr:             # extract every needed feature and append\n",
    "            df = extractor.get_feature(patient, variable_id)\n",
    "\n",
    "            if len(df) >= timesteps:\n",
    "                feature_samples.append(list(df['value'].astype(float))[:timesteps])\n",
    "                # pbar.update(1)\n",
    "        \n",
    "        if len(feature_samples) != len(variable_id_arr):        # if this patient does not have all the features, skip\n",
    "            continue\n",
    "        \n",
    "        samples.append(feature_samples)\n",
    "        if len(samples) % 10 == 0:\n",
    "            print(\"Patient {} done\".format(len(samples)))\n",
    "            np.save(\"data/hirid-multiple-checkpoint.npy\", samples)          # check point\n",
    "        \n",
    "        if len(samples) == num_patients:\n",
    "            break\n",
    "        \n",
    "    return samples\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()        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Patient 10 done\n",
      "Patient 20 done\n",
      "Patient 30 done\n",
      "Patient 40 done\n",
      "Patient 50 done\n",
      "Patient 60 done\n",
      "Patient 70 done\n",
      "Patient 80 done\n",
      "Patient 90 done\n",
      "Patient 100 done\n",
      "Patient 110 done\n",
      "Patient 120 done\n",
      "Patient 130 done\n",
      "Patient 140 done\n",
      "Patient 150 done\n",
      "Patient 160 done\n",
      "Patient 170 done\n",
      "Patient 180 done\n",
      "Patient 190 done\n",
      "Patient 200 done\n",
      "Patient 210 done\n",
      "Patient 220 done\n",
      "Patient 230 done\n",
      "Patient 240 done\n",
      "Patient 250 done\n",
      "Patient 260 done\n",
      "Patient 270 done\n",
      "Patient 280 done\n",
      "Patient 290 done\n",
      "Patient 300 done\n",
      "Patient 310 done\n",
      "Patient 320 done\n",
      "Patient 330 done\n",
      "Patient 340 done\n",
      "Patient 350 done\n",
      "Patient 360 done\n",
      "Patient 370 done\n",
      "Patient 380 done\n",
      "Patient 390 done\n",
      "Patient 400 done\n",
      "Patient 410 done\n",
      "Patient 420 done\n",
      "Patient 430 done\n",
      "Patient 440 done\n",
      "Patient 450 done\n",
      "Patient 460 done\n",
      "Patient 470 done\n",
      "Patient 480 done\n",
      "Patient 490 done\n",
      "Patient 500 done\n",
      "Patient 510 done\n",
      "Patient 520 done\n",
      "Patient 530 done\n",
      "Patient 540 done\n",
      "Patient 550 done\n",
      "Patient 560 done\n",
      "Patient 570 done\n",
      "Patient 580 done\n",
      "Patient 590 done\n",
      "Patient 600 done\n",
      "Patient 610 done\n",
      "Patient 620 done\n",
      "Patient 630 done\n",
      "Patient 640 done\n",
      "Patient 650 done\n",
      "Patient 660 done\n",
      "Patient 670 done\n",
      "Patient 680 done\n",
      "Patient 690 done\n",
      "Patient 700 done\n",
      "Patient 710 done\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "alive_id = list(pd.read_csv('data/hirid-alive-id.csv')['patientid'])            # load randomly shuffle patient_id\n",
    "dead_id = list(pd.read_csv('data/hirid-dead-id.csv')['patientid'])          # load randomly shuffle patient_id\n",
    "alive_samples = extract_data(extractor, timesteps=100, num_patients=10000, variable_id_arr=[HEARTRATE_ID, SYSBP_ID, DIABP_ID, MAP_ID, SATURATION_ID, ST_ID, CVP_ID], patient_id=alive_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 3967012 characters]",
      "text/plain": [
       "<Figure size 2700x12000 with 60 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "rows = 20\n",
    "cols = 3\n",
    "visualize(samples, (rows, cols), figsize=(cols*9, rows*6), markersize=5, marker=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m np\u001b[39m.\u001b[39msave(\u001b[39m'\u001b[39m\u001b[39mdata/hirid_heartrate_2000_450.npy\u001b[39m\u001b[39m'\u001b[39m, samples)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "np.save('data/hirid_heartrate_2000_450.npy', samples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[97., 97., 97.,  ..., 92., 91., 91.],\n",
       "        [55., 59., 60.,  ..., 54., 65., 61.],\n",
       "        [96., 96., 96.,  ..., 73., 74., 74.],\n",
       "        ...,\n",
       "        [80., 80., 80.,  ..., 80., 81., 80.],\n",
       "        [70., 70., 70.,  ..., 78., 78., 78.],\n",
       "        [64., 59., 56.,  ..., 71., 67., 66.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "\n",
    "samples = np.load('data/hirid_heartrate_2000_450.npy')\n",
    "samples = torch.from_numpy(samples).float()\n",
    "samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "samples = samples.unsqueeze(1)\n",
    "samples.shape\n",
    "samples = samples[:512, :, :400]\n",
    "samples.shape\n",
    "torch.save(samples, \"data/hirid_heartrate_512_400.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([512, 1, 400])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples = torch.load(\"data/hirid_heartrate_512_400.pt\")\n",
    "samples.shape[0]\n",
    "samples.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 4549204 characters]",
      "text/plain": [
       "<Figure size 2700x12000 with 60 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "import torch\n",
    "samples = torch.load(\"data/hirid_heartrate_256_400.pt\").squeeze().numpy()\n",
    "rows = 20\n",
    "cols = 3\n",
    "visualize(samples, (rows, cols), figsize=(cols*9, rows*6), markersize=5, marker=None)"
   ]
  }
 ],
 "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
}
