{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Approach 4: Autoencoder - Boxplot\n",
    "This notebook reads eval results of sets of identical trainings and plots them into a boxplot. The names of the training results can be configured in the cell below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n",
      "Found 3 sessions\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import numpy as np\n",
    "from sklearn.neighbors import KernelDensity\n",
    "from glob import glob\n",
    "from sklearn.metrics import roc_curve, auc\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm\n",
    "\n",
    "from py.Dataset import Dataset\n",
    "from py.FileUtils import load\n",
    "from py.PlotUtils import get_percentiles\n",
    "\n",
    "DIR = '/home/kleinsteuber/vscode/ResizedSessions256_NoBackup' # dataset directory\n",
    "CONFIGS_BY_LATENT_FEATURES = [\n",
    "    {\n",
    "        \"label\": \"16\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_16f_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"32\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_32f_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"64\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_64f_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"128\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_128f_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"256\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_256f_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"512\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"1024\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "]\n",
    "\n",
    "CONFIGS_DENOISING = [\n",
    "    {\n",
    "        \"label\": \"0\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"0.100\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"0.150\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.0225_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"0.200\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.04_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"0.300\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.09_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"0.400\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.16_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "]\n",
    "\n",
    "CONFIGS_SPARSE = [\n",
    "    {\n",
    "        \"label\": \"0\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"1e-5\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_sparse1e-5_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"1e-4\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_sparse_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"1e-3\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_sparse1e-3_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"1e-2\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_sparse1e-2_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "]\n",
    "\n",
    "CONFIGS_DENOISING_AND_SPARSE = [\n",
    "    {\n",
    "        \"label\": \"Base\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\sigma = 0.1$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\sigma = 0.15$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.0225_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\sigma = 0.2$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.04_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\sigma = 0.3$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.09_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\sigma = 0.4$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.16_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\lambda = 10^{-5}$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_sparse1e-5_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\lambda = 10^{-4}$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_200e_sparse_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\lambda = 10^{-3}$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_sparse1e-3_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"label\": \"$\\lambda = 10^{-2}$\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_sparse1e-2_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    #{\n",
    "    #    \"label\": \"$\\sigma = 0.1, \\lambda = 1e-4$\",\n",
    "    #    \"train_names\": [f\"ae2_beaver_01_noise0.01_sparse1e-4_200e_{i}\" for i in range(10)]\n",
    "    #},\n",
    "]\n",
    "\n",
    "CONFIGS_COMPARE_SESSIONS = [\n",
    "    {\n",
    "        \"session\": \"Beaver_01\",\n",
    "        \"label\": \"Beaver_01\",\n",
    "        \"train_names\": [f\"ae2_beaver_01_noise0.01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"session\": \"Marten_01\",\n",
    "        \"label\": \"Marten_01\",\n",
    "        \"train_names\": [f\"ae2_marten_01_noise0.01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "    {\n",
    "        \"session\": \"GFox_03\",\n",
    "        \"label\": \"GFox_03\",\n",
    "        \"train_names\": [f\"ae2_gfox_03_noise0.01_200e_{i}\" for i in range(10)]\n",
    "    },\n",
    "]\n",
    "\n",
    "ds = Dataset(DIR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculating the scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_scores(train_names, session = \"Beaver_01\", **kwargs):\n",
    "    kde_scores = {\n",
    "        \"auc\": [],\n",
    "        \"tnr90\": [],\n",
    "        \"tnr95\": [],\n",
    "        \"tnr99\": []\n",
    "    }\n",
    "    loss_scores = {\n",
    "        \"auc\": [],\n",
    "        \"tnr90\": [],\n",
    "        \"tnr95\": [],\n",
    "        \"tnr99\": []\n",
    "    }\n",
    "    # correct name\n",
    "    session = ds.create_session(session).name\n",
    "    for train_name in tqdm(train_names):\n",
    "        try:\n",
    "            lapse_losses, lapse_encodings, lapse_labels, lapse_images = load(f\"./ae_train_NoBackup/{train_name}/eval/{session}_lapse.pickle\")\n",
    "            motion_losses, motion_encodings, motion_labels, motion_images = load(f\"./ae_train_NoBackup/{train_name}/eval/{session}_motion.pickle\")\n",
    "        except:\n",
    "            print(f\"{train_name} is missing, skipping...\")\n",
    "            continue\n",
    "\n",
    "        # # # # # # # # # # # # # # # # # #\n",
    "        # 1. KDE AUC & elimination rates\n",
    "        # # # # # # # # # # # # # # # # # #\n",
    "\n",
    "        kde = KernelDensity(kernel=\"gaussian\", bandwidth=0.01).fit(lapse_encodings)\n",
    "        preds = kde.score_samples(motion_encodings)\n",
    "        y_anom = preds[motion_labels == 1]\n",
    "        y_norm = preds[motion_labels == 0]\n",
    "        # likelihoods need to be inverted (anomalies have lower likelihood)\n",
    "        X = -np.concatenate([y_norm, y_anom]).reshape((-1, 1))\n",
    "        y = np.concatenate([-np.ones_like(y_norm), np.ones_like(y_anom)])\n",
    "\n",
    "        fpr, tpr, thresholds = roc_curve(y, X[:,0])\n",
    "        kde_scores[\"auc\"].append(auc(fpr, tpr))\n",
    "        kde_scores[\"tnr90\"].append(get_percentiles(fpr, tpr, thresholds, percentiles=[0.9], verbose = False)[0])\n",
    "        kde_scores[\"tnr95\"].append(get_percentiles(fpr, tpr, thresholds, percentiles=[0.95], verbose = False)[0])\n",
    "        kde_scores[\"tnr99\"].append(get_percentiles(fpr, tpr, thresholds, percentiles=[0.99], verbose = False)[0])\n",
    "\n",
    "        # # # # # # # # # # # # # # # # # # # # # # # # #\n",
    "        # 2. Reconstruction Loss AUC & elimination rates\n",
    "        # # # # # # # # # # # # # # # # # # # # # # # # #\n",
    "\n",
    "        y_anom = motion_losses[motion_labels == 1]\n",
    "        y_norm = motion_losses[motion_labels == 0]\n",
    "        # likelihoods do not need to be inverted (anomalies have higher reconstruction loss)\n",
    "        X = np.concatenate([y_norm, y_anom]).reshape((-1, 1))\n",
    "        y = np.concatenate([-np.ones_like(y_norm), np.ones_like(y_anom)])\n",
    "\n",
    "        fpr, tpr, thresholds = roc_curve(y, X[:,0])\n",
    "        loss_scores[\"auc\"].append(auc(fpr, tpr))\n",
    "        loss_scores[\"tnr90\"].append(get_percentiles(fpr, tpr, thresholds, percentiles=[0.9], verbose = False)[0])\n",
    "        loss_scores[\"tnr95\"].append(get_percentiles(fpr, tpr, thresholds, percentiles=[0.95], verbose = False)[0])\n",
    "        loss_scores[\"tnr99\"].append(get_percentiles(fpr, tpr, thresholds, percentiles=[0.99], verbose = False)[0])\n",
    "    return kde_scores, loss_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_boxes(values, color=\"darkgoldenrod\", label_below=[], colors=None, label_dist_overwrites={}):\n",
    "    bp_dict = plt.boxplot(values, medianprops={\"linewidth\": 1.5, \"color\": color}, widths=0.72)\n",
    "\n",
    "    # Add labels to medians\n",
    "    for i, line in enumerate(bp_dict['medians']):\n",
    "        # get position data for median line\n",
    "        x, y = line.get_xydata()[1] # top of median line\n",
    "        # custom color\n",
    "        if colors is not None:\n",
    "            color = colors[i]\n",
    "        line.set_color(color)\n",
    "        # overlay median value\n",
    "        if i in label_below:\n",
    "            label_dist = label_dist_overwrites[i] if i in label_dist_overwrites else 0.001\n",
    "            plt.text(x - 0.355, y - label_dist, f\"{y:.3f}\", verticalalignment=\"top\", horizontalalignment=\"center\", color=color) # draw below, centered\n",
    "        else:\n",
    "            label_dist = label_dist_overwrites[i] if i in label_dist_overwrites else 0.0005\n",
    "            plt.text(x - 0.355, y + label_dist, f\"{y:.3f}\", verticalalignment=\"bottom\", horizontalalignment=\"center\", color=color) # draw above, centered\n",
    "\n",
    "def plot_scores(configs, scores, metric=\"auc\", savefile=None, figsize=(15, 10), xlabel=None, **kwargs):\n",
    "    plt.rcParams['text.usetex'] = True\n",
    "    plt.rcParams.update({\"font.size\": 18})\n",
    "    plt.figure(figsize=figsize)\n",
    "\n",
    "    plot_boxes([kde_scores[metric] for kde_scores, loss_scores in scores], **kwargs)\n",
    "    # plot_boxes([loss_scores[metric] for kde_scores, loss_scores in scores], color=\"blue\")\n",
    "\n",
    "    # Add labels\n",
    "    plt.xticks(np.arange(1, len(configs) + 1), [config[\"label\"] for config in configs])\n",
    "    if xlabel is not None:\n",
    "        plt.xlabel(xlabel)\n",
    "    plt.grid(True, ls=\"dotted\", lw=0.5)\n",
    "    # plt.ylim((0.7, 0.9))\n",
    "    # plt.legend()\n",
    "    if savefile is not None:\n",
    "        plt.savefig(savefile, bbox_inches=\"tight\")\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot result\n",
    "## By latent features\n",
    "Choose a metric (\"auc\", \"tnr90\", \"tnr95\", \"tnr99\") and optionally an output file for plotting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:00<00:00, 20.47it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:00<00:00, 19.51it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:00<00:00, 15.15it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:01<00:00,  9.58it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:01<00:00,  5.59it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:02<00:00,  3.64it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:02<00:00,  3.62it/s]\n"
     ]
    }
   ],
   "source": [
    "scores = [get_scores(**config) for config in CONFIGS_BY_LATENT_FEATURES]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27848 characters]",
      "text/plain": [
       "<Figure size 504x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_scores(CONFIGS_BY_LATENT_FEATURES, scores, metric=\"auc\", savefile=\"plots/approach4/boxplot_kde_latentfeatures.pdf\", figsize=(7, 8), label_below=[0,3,4], xlabel=\"Latent features\", label_dist_overwrites={0: 0.005})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24032 characters]",
      "text/plain": [
       "<Figure size 504x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_scores(CONFIGS_BY_LATENT_FEATURES, scores, metric=\"tnr95\", savefile=\"plots/approach4/boxplot_kde_latentfeatures_tnr95.pdf\", figsize=(7, 8), label_below=[0, 1], xlabel=\"Latent features\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Denoising autoencoders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:02<00:00,  3.57it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:02<00:00,  3.43it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  2.79it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  2.98it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  2.81it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  2.72it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  2.52it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:03<00:00,  2.63it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:04<00:00,  2.16it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:06<00:00,  1.65it/s]\n"
     ]
    }
   ],
   "source": [
    "denoising_and_sparse_scores = [get_scores(**config) for config in CONFIGS_DENOISING_AND_SPARSE]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35952 characters]",
      "text/plain": [
       "<Figure size 1008x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_scores(CONFIGS_DENOISING_AND_SPARSE, denoising_and_sparse_scores, metric=\"auc\", savefile=\"plots/approach4/boxplot_kde_denoising_and_sparse.pdf\", figsize=(14, 6), label_below=[3,5,6], colors = [\"black\"] + [\"darkgoldenrod\"] * 5 + [\"blue\"] * 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 31068 characters]",
      "text/plain": [
       "<Figure size 648x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_scores(CONFIGS_DENOISING_AND_SPARSE, denoising_and_sparse_scores, metric=\"tnr95\", savefile=\"plots/approach4/boxplot_kde_denoising_and_sparse_tnr95.pdf\", figsize=(9, 8), label_below=[], colors = [\"black\"] + [\"darkgoldenrod\"] * 5 + [\"blue\"] * 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare sessions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Beaver_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Beaver_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:02<00:00,  3.67it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'Marten_01' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session01-VIELAAS_Marten_01\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:13<00:00,  1.35s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session 'GFox_03' at folder: /home/kleinsteuber/vscode/ResizedSessions256_NoBackup/VIELAAS_Spring_Session03-VIELAAS_GFox_03\n",
      "Loaded scans.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 10/10 [00:08<00:00,  1.18it/s]\n"
     ]
    }
   ],
   "source": [
    "compare_sessions_scores = [get_scores(**config) for config in CONFIGS_COMPARE_SESSIONS]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17904 characters]",
      "text/plain": [
       "<Figure size 360x504 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_scores(CONFIGS_COMPARE_SESSIONS, compare_sessions_scores, metric=\"auc\", savefile=\"plots/approach4/boxplot_kde_sessions.pdf\", figsize=(5, 7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14868 characters]",
      "text/plain": [
       "<Figure size 360x504 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_scores(CONFIGS_COMPARE_SESSIONS, compare_sessions_scores, metric=\"tnr95\", savefile=\"plots/approach4/boxplot_kde_sessions_tnr95.pdf\", figsize=(5, 7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('pytorch-gpu')",
   "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.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "17cd5c528a3345b75540c61f907eece919c031d57a2ca1e5653325af249173c9"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
