{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "9fde7ee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "5de83302",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.python.summary.summary_iterator import summary_iterator"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d26040fa",
   "metadata": {},
   "source": [
    "# 1. Get log files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "id": "cd8ef79b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_tfb_file(directory):\n",
    "    file_list = []\n",
    "    for subdir, dirs, files in os.walk(directory):\n",
    "        for file in files:\n",
    "            filepath = subdir + os.sep + file\n",
    "            file_list.append(filepath)\n",
    "    \n",
    "    return file_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "id": "1ed4e27e",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_pcb_zero_init_file = get_tfb_file(\"logs/pcb/zero_init\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "id": "0b68414a",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_pcb_uniform_init_file = get_tfb_file(\"logs/pcb/uniform_init\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2b9da8a",
   "metadata": {},
   "source": [
    "# 2. Get the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "id": "d04fddd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_tfb_data(files, tag):\n",
    "    data_list = []\n",
    "    for f in files:\n",
    "        data = []\n",
    "        for e in summary_iterator(f):\n",
    "            for v in e.summary.value:\n",
    "                if v.tag == tag:\n",
    "                    data.append(v.simple_value)\n",
    "        data_list.append(data)\n",
    "    \n",
    "    return data_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b087e21f",
   "metadata": {},
   "source": [
    "# 3. Initialization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b71a4b18",
   "metadata": {},
   "source": [
    "## Number of Boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "id": "099d768f",
   "metadata": {},
   "outputs": [],
   "source": [
    "pcb_zero_init_boxes = get_tfb_data(log_pcb_zero_init_file, \"boxes\")\n",
    "pcb_uniform_init_boxes = get_tfb_data(log_pcb_uniform_init_file, \"boxes\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31a51d6f",
   "metadata": {},
   "source": [
    "## Mean Confidence Increace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "ccc04a16",
   "metadata": {},
   "outputs": [],
   "source": [
    "pcb_zero_init_conf = get_tfb_data(log_pcb_zero_init_file, \"mean confidence increase\")\n",
    "pcb_uniform_init_conf = get_tfb_data(log_pcb_uniform_init_file, \"mean confidence increase\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c3cfcaf",
   "metadata": {},
   "source": [
    "## Boxes Variation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "id": "287bb011",
   "metadata": {},
   "outputs": [],
   "source": [
    "pcb_zero_init_boxes_var = get_tfb_data(log_pcb_zero_init_file, \"box variation\")\n",
    "pcb_uniform_init_boxes_var = get_tfb_data(log_pcb_uniform_init_file, \"box variation\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f391d78d",
   "metadata": {},
   "source": [
    "## Plot the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "id": "15f46169",
   "metadata": {},
   "outputs": [],
   "source": [
    "MAX_DATA = 300"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 375,
   "id": "b8601fe2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 162072 characters]\n",
      "text/plain": [
       "<Figure size 2160x576 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "parameters = {'axes.labelsize': 30,\n",
    "              'axes.linewidth': 3,\n",
    "              'figure.figsize': (30, 8),\n",
    "              'font.size': 24,\n",
    "              'font.weight' : 'bold'}\n",
    "\n",
    "plt.rcParams.update(parameters)\n",
    "\n",
    "\n",
    "f, (ax1, ax2, ax3) = plt.subplots(nrows = 1, ncols = 3)\n",
    "\n",
    "f.set_figheight(8)\n",
    "f.set_figwidth(30)\n",
    "f.set_linewidth(3)\n",
    "\n",
    "\n",
    "# plt.subplot(1, 3, 1)\n",
    "\n",
    "ax1.set_xlabel('Iteration')\n",
    "\n",
    "ax1.set_ylabel('Mean Confidence Variation')\n",
    "\n",
    "for idx, d in enumerate(pcb_uniform_init_conf):\n",
    "    if idx == 0:\n",
    "        ax1.plot(d[:MAX_DATA], c = (0.1, 0.2, 0.5, 0.3), linewidth = 3, label = 'uniform init (5 runs)')\n",
    "    else:\n",
    "        ax1.plot(d[:MAX_DATA], c = (0.1, 0.2, 0.5, 0.3), linewidth = 3)\n",
    "\n",
    "ax1.plot(pcb_zero_init_conf[0][:MAX_DATA], 'b-', label='zero init')\n",
    "\n",
    "ax1.legend(loc=\"lower right\")\n",
    "\n",
    "\n",
    "# plt.subplot(1, 3, 2)\n",
    "\n",
    "ax2.set_xlabel('Iteration')\n",
    "\n",
    "ax2.set_ylabel('Number of Boxes')\n",
    "\n",
    "for idx, box in enumerate(pcb_uniform_init_boxes):\n",
    "    if idx == 0:\n",
    "        ax2.plot(box[:MAX_DATA], c = (0.1, 0.2, 0.5, 0.3), label = 'uniform init (5 runs)')\n",
    "    else:\n",
    "        ax2.plot(box[:MAX_DATA], c = (0.1, 0.2, 0.5, 0.3))\n",
    "\n",
    "ax2.plot(pcb_zero_init_boxes[0][:MAX_DATA], 'b-', label='zero init')\n",
    "\n",
    "ax2.legend(loc=\"lower right\")\n",
    "\n",
    "\n",
    "# plt.subplot(1, 3, 3)\n",
    "\n",
    "ax3.set_xlabel('Iteration')\n",
    "\n",
    "ax3.set_ylabel('Relative Box Variation')\n",
    "\n",
    "for idx, d in enumerate(pcb_uniform_init_boxes_var):\n",
    "    if idx == 0:\n",
    "        ax3.plot(d[:MAX_DATA], c = (0.1, 0.2, 0.5, 0.3), label = 'uniform init (5 runs)')\n",
    "    else:\n",
    "        ax3.plot(d[:MAX_DATA], c = (0.1, 0.2, 0.5, 0.3))\n",
    "\n",
    "ax3.plot(pcb_zero_init_boxes_var[0][:MAX_DATA], 'b-', label='zero init')\n",
    "\n",
    "ax3.legend(loc=\"lower right\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 376,
   "id": "bb9e6dbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "f.savefig(\"init\", bbox_inches='tight', pad_inches=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "132eb235",
   "metadata": {},
   "source": [
    "# 4. Learning Rate Decay"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "id": "07ba4570",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_pcb_decay_file = get_tfb_file(\"logs/pcb/decay/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "id": "e0d33d18",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_pcb_decay_file = [\n",
    " 'logs/pcb/decay/1.00/20220812-114158/events.out.tfevents.1660300927.pop-os',\n",
    " 'logs/pcb/decay/0.99/20220812-113414/events.out.tfevents.1660300463.pop-os',\n",
    " 'logs/pcb/decay/0.97/20220812-113917/events.out.tfevents.1660300766.pop-os',\n",
    " 'logs/pcb/decay/0.95/20220812-113040/events.out.tfevents.1660300249.pop-os',\n",
    " 'logs/pcb/decay/0.9/20220812-113648/events.out.tfevents.1660300617.pop-os'\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "id": "d904f442",
   "metadata": {},
   "outputs": [],
   "source": [
    "pcb_decay_boxes = get_tfb_data(log_pcb_decay_file, \"boxes\")\n",
    "pcb_decay_conf = get_tfb_data(log_pcb_decay_file, \"mean confidence increase\")\n",
    "pcb_decay_var = get_tfb_data(log_pcb_decay_file, \"box variation\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 369,
   "id": "bc1b6aab",
   "metadata": {},
   "outputs": [],
   "source": [
    "MAX_DATA = 500\n",
    "legend_labels = [\"k=1.00\", \"k=0.99\", \"k=0.97\", \"k=0.95\", \"k=0.90\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 370,
   "id": "70828082",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cycler import cycler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "id": "984aa5c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table><th>'color'</th><tr><td>'#000000'</td></tr><tr><td>'#ff7f0e'</td></tr><tr><td>'#2ca02c'</td></tr><tr><td>'#d62728'</td></tr><tr><td>'#1f77b4'</td></tr><tr><td>'#8c564b'</td></tr><tr><td>'#e377c2'</td></tr><tr><td>'#7f7f7f'</td></tr><tr><td>'#bcbd22'</td></tr><tr><td>'#17becf'</td></tr></table>"
      ],
      "text/plain": [
       "cycler('color', ['#000000', '#ff7f0e', '#2ca02c', '#d62728', '#1f77b4', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'])"
      ]
     },
     "execution_count": 371,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prop_cycle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 372,
   "id": "7e65ccd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "prop_cycle = plt.rcParams['axes.prop_cycle']\n",
    "my_cycler = cycler('color', ['#000000', '#ff7f0e', '#2ca02c', '#d62728', '#1f77b4', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 373,
   "id": "9e7fc735",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 209948 characters]\n",
      "text/plain": [
       "<Figure size 2160x576 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "parameters = {'axes.labelsize': 30,\n",
    "              'axes.linewidth': 3,\n",
    "              'figure.figsize': (30, 10),\n",
    "              'font.size': 24,\n",
    "              'font.weight' : 'bold',\n",
    "              'axes.prop_cycle': my_cycler\n",
    "             }\n",
    "\n",
    "plt.rcParams.update(parameters)\n",
    "\n",
    "f, (ax1, ax2, ax3) = plt.subplots(nrows = 1, ncols = 3)\n",
    "\n",
    "f.set_figheight(8)\n",
    "f.set_figwidth(30)\n",
    "f.set_linewidth(3)\n",
    "\n",
    "\n",
    "# plt.subplot(1, 3, 1)\n",
    "\n",
    "ax1.set_xlabel('Iteration')\n",
    "\n",
    "ax1.set_ylabel('Mean Confidence Variation')\n",
    "\n",
    "for idx, d in enumerate(pcb_decay_conf):\n",
    "        ax1.plot(d[:MAX_DATA], linewidth = 3, label = legend_labels[idx])\n",
    "\n",
    "ax1.legend(loc=\"lower right\")\n",
    "        \n",
    "# plt.subplot(1, 3, 2)\n",
    "\n",
    "ax2.set_xlabel('Iteration')\n",
    "\n",
    "ax2.set_ylabel('Number of Boxes')\n",
    "\n",
    "for idx, box in enumerate(pcb_decay_boxes):\n",
    "        ax2.plot(box[:MAX_DATA], linewidth = 3, label = legend_labels[idx])\n",
    "\n",
    "ax2.legend(loc=\"lower right\")\n",
    "\n",
    "# plt.subplot(1, 3, 3)\n",
    "\n",
    "ax3.set_xlabel('Iteration')\n",
    "\n",
    "ax3.set_ylabel('Relative Box Variation')\n",
    "\n",
    "for idx, d in enumerate(pcb_decay_var):\n",
    "    ax3.plot(d[:MAX_DATA], linewidth = 3, label=legend_labels[idx])\n",
    "\n",
    "ax3.legend(loc=\"lower right\")    \n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 374,
   "id": "29495ebc",
   "metadata": {},
   "outputs": [],
   "source": [
    "f.savefig(\"lr\", bbox_inches='tight', pad_inches=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e09f3042",
   "metadata": {},
   "source": [
    "# 5. Adversarial Loss Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 380,
   "id": "bd7b5028",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['logs/pc/decay/0.99/20220813-153020/events.out.tfevents.1660401028.pop-os',\n",
       " 'logs/pc/decay/0.97/20220813-153248/events.out.tfevents.1660401177.pop-os',\n",
       " 'logs/pc/decay/0.90/20220813-153713/events.out.tfevents.1660401442.pop-os',\n",
       " 'logs/pc/decay/1.00/20220813-152848/events.out.tfevents.1660400937.pop-os',\n",
       " 'logs/pc/decay/0.95/20220813-153516/events.out.tfevents.1660401325.pop-os']"
      ]
     },
     "execution_count": 380,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_tfb_file(\"logs/pc/decay/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 381,
   "id": "65d77efe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['logs/pcb/decay/0.99/20220812-113414/events.out.tfevents.1660300463.pop-os',\n",
       " 'logs/pcb/decay/0.97/20220812-113917/events.out.tfevents.1660300766.pop-os',\n",
       " 'logs/pcb/decay/0.90/20220812-113648/events.out.tfevents.1660300617.pop-os',\n",
       " 'logs/pcb/decay/1.00/20220812-114158/events.out.tfevents.1660300927.pop-os',\n",
       " 'logs/pcb/decay/0.95/20220812-113040/events.out.tfevents.1660300249.pop-os']"
      ]
     },
     "execution_count": 381,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_tfb_file(\"logs/pcb/decay/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 382,
   "id": "d1d14e87",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['logs/tog/decay/0.99/20220813-151700/events.out.tfevents.1660400223.pop-os',\n",
       " 'logs/tog/decay/0.97/20220813-152025/events.out.tfevents.1660400428.pop-os',\n",
       " 'logs/tog/decay/0.90/20220813-152409/events.out.tfevents.1660400652.pop-os',\n",
       " 'logs/tog/decay/1.00/20220813-151835/events.out.tfevents.1660400318.pop-os',\n",
       " 'logs/tog/decay/0.95/20220813-152155/events.out.tfevents.1660400519.pop-os']"
      ]
     },
     "execution_count": 382,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_tfb_file(\"logs/tog/decay/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 383,
   "id": "fd284a8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_loss_file = ['logs/pc/decay/0.99/20220813-153020/events.out.tfevents.1660401028.pop-os',\n",
    "                 'logs/pc/decay/0.90/20220813-153713/events.out.tfevents.1660401442.pop-os',\n",
    "                 'logs/pcb/decay/0.99/20220812-113414/events.out.tfevents.1660300463.pop-os',\n",
    "                 'logs/pcb/decay/0.90/20220812-113648/events.out.tfevents.1660300617.pop-os',\n",
    "                 'logs/tog/decay/0.99/20220813-151700/events.out.tfevents.1660400223.pop-os',\n",
    "                 'logs/tog/decay/0.90/20220813-152409/events.out.tfevents.1660400652.pop-os',\n",
    "                ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 386,
   "id": "359e8c54",
   "metadata": {},
   "outputs": [],
   "source": [
    "loss_function_boxes = get_tfb_data(log_loss_file, \"boxes\")\n",
    "loss_function_conf  = get_tfb_data(log_loss_file, \"mean confidence increase\")\n",
    "loss_function_var   = get_tfb_data(log_loss_file, \"box variation\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 394,
   "id": "21379553",
   "metadata": {},
   "outputs": [],
   "source": [
    "MAX_DATA = 500\n",
    "legend_labels = [\"PC k=0.99\", \"PC k=0.90\", \"PCB k=0.99\", \"PCB k=0.90\", \"TOG k=0.99\", \"TOG k=0.90\"]\n",
    "linestyle = [\"solid\", \"dashed\", \"solid\", \"dashed\", \"solid\", \"dashed\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 395,
   "id": "730d9935",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "prop_cycle = plt.rcParams['axes.prop_cycle']\n",
    "my_cycler = cycler('color', ['#000000', '#000000', '#ff7f0e', '#ff7f0e', '#1f77b4', '#1f77b4'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 396,
   "id": "a7922ace",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 193664 characters]\n",
      "text/plain": [
       "<Figure size 2160x576 with 3 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "parameters = {'axes.labelsize': 30,\n",
    "              'axes.linewidth': 3,\n",
    "              'figure.figsize': (30, 10),\n",
    "              'font.size': 24,\n",
    "              'font.weight' : 'bold',\n",
    "              'axes.prop_cycle': my_cycler\n",
    "             }\n",
    "\n",
    "plt.rcParams.update(parameters)\n",
    "\n",
    "f, (ax1, ax2, ax3) = plt.subplots(nrows = 1, ncols = 3)\n",
    "\n",
    "f.set_figheight(8)\n",
    "f.set_figwidth(30)\n",
    "f.set_linewidth(3)\n",
    "\n",
    "\n",
    "# plt.subplot(1, 3, 1)\n",
    "\n",
    "ax1.set_xlabel('Iteration')\n",
    "\n",
    "ax1.set_ylabel('Mean Confidence Variation')\n",
    "\n",
    "for idx, d in enumerate(loss_function_conf):\n",
    "        ax1.plot(d[:MAX_DATA], linewidth = 3, label = legend_labels[idx], linestyle=linestyle[idx])\n",
    "\n",
    "ax1.legend(loc=\"lower right\")\n",
    "        \n",
    "# plt.subplot(1, 3, 2)\n",
    "\n",
    "ax2.set_xlabel('Iteration')\n",
    "\n",
    "ax2.set_ylabel('Number of Boxes')\n",
    "\n",
    "for idx, box in enumerate(loss_function_boxes):\n",
    "        ax2.plot(box[:MAX_DATA], linewidth = 3, label = legend_labels[idx], linestyle=linestyle[idx])\n",
    "\n",
    "ax2.legend(loc=\"lower right\")\n",
    "\n",
    "# plt.subplot(1, 3, 3)\n",
    "\n",
    "ax3.set_xlabel('Iteration')\n",
    "\n",
    "ax3.set_ylabel('Relative Box Variation')\n",
    "\n",
    "for idx, d in enumerate(loss_function_var):\n",
    "    ax3.plot(d[:MAX_DATA], linewidth = 3, label=legend_labels[idx], linestyle=linestyle[idx])\n",
    "\n",
    "ax3.legend(loc=\"lower right\")    \n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 397,
   "id": "6b739ef1",
   "metadata": {},
   "outputs": [],
   "source": [
    "f.savefig(\"loss\", bbox_inches='tight', pad_inches=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3424c7f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
