{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Figures for 32-bit FL and HFL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/wesleyjtann/miniconda3/envs/fl_pytorch/lib/python3.7/site-packages/ipykernel_launcher.py:5: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.\n",
      "  \"\"\"\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "# PLOTTING \n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "matplotlib.use('Agg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MNIST MLP IID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "datamodelset = \"MNIST_MLP_IID\"\n",
    "filename1 = \"[1]FL_mnist_mlp_468_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "filename2 = \"[3]HFL2_mnist_mlp_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "filename3 = \"[5]HFL4_mnist_mlp_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "filename4 = \"[7]HFL4_mnist_mlp_30_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "\n",
    "with open(r\"../save/objects/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename4 + \".pkl\", \"rb\") as input_file: data4 = pickle.load(input_file)    \n",
    "\n",
    "trloss1 = data1[0]    \n",
    "trloss2 = data2[0]    \n",
    "trloss3 = data3[0]    \n",
    "trloss4 = data4[0]    \n",
    "    \n",
    "tracc1 = data1[1]\n",
    "tracc2 = data2[1]\n",
    "tracc3 = data3[1]\n",
    "tracc4 = data4[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, 'Communication Rounds')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 52836 characters]\n",
      "text/plain": [
       "<Figure size 1368x504 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ===== Plot Average Accuracy vs Communication rounds ===== \n",
    "plt.figure(figsize=(19, 7))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.title(datamodelset)\n",
    "plt.plot(range(len(tracc1)), tracc1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_acc.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_acc.png')\n",
    "\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_loss.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_loss.png')\n",
    "# plt.show"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MNIST MLP NON-IID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "datamodelset = \"MNIST_MLP_NON-IID\"\n",
    "filename1 = \"[2]FL_mnist_mlp_1196_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "filename2 = \"[4]HFL2_mnist_mlp_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "filename3 = \"[6]HFL4_mnist_mlp_150_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "# filename4 = \"\"\n",
    "\n",
    "with open(r\"../save/objects/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "# with open(r\"../save/objects/\" + filename4 + \".pkl\", \"rb\") as input_file: data4 = pickle.load(input_file)    \n",
    "\n",
    "trloss1 = data1[0]    \n",
    "trloss2 = data2[0]    \n",
    "trloss3 = data3[0]    \n",
    "# trloss4 = data4[0]    \n",
    "    \n",
    "tracc1 = data1[1]\n",
    "tracc2 = data2[1]\n",
    "tracc3 = data3[1]\n",
    "# tracc4 = data4[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, 'Communication Rounds')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 69104 characters]\n",
      "text/plain": [
       "<Figure size 1368x504 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ===== Plot Average Accuracy vs Communication rounds ===== \n",
    "plt.figure(figsize=(19, 7))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.title(datamodelset)\n",
    "plt.plot(range(len(tracc1)), tracc1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_acc.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_acc.png')\n",
    "\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_loss.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_loss.png')\n",
    "# plt.show"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MNIST CNN IID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "datamodelset = \"MNIST_CNN_IID\"\n",
    "filename1 = \"[9]FL_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "filename2 = \"[11]HFL2_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "filename3 = \"[13]HFL4_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "filename4 = \"[15]HFL8_mnist_cnn_30_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]\"\n",
    "\n",
    "\n",
    "with open(r\"../save/objects/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename4 + \".pkl\", \"rb\") as input_file: data4 = pickle.load(input_file)    \n",
    "\n",
    "trloss1 = data1[0]    \n",
    "trloss2 = data2[0]    \n",
    "trloss3 = data3[0]    \n",
    "trloss4 = data4[0]    \n",
    "    \n",
    "tracc1 = data1[1]\n",
    "tracc2 = data2[1]\n",
    "tracc3 = data3[1]\n",
    "tracc4 = data4[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, 'Communication Rounds')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 53360 characters]\n",
      "text/plain": [
       "<Figure size 1368x504 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ===== Plot Average Accuracy vs Communication rounds ===== \n",
    "plt.figure(figsize=(19, 7))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.title(datamodelset)\n",
    "plt.plot(range(len(tracc1)), tracc1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_acc.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_acc.png')\n",
    "\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_loss.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_loss.png')\n",
    "# plt.show"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MNIST CNN NON-IID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "datamodelset = \"MNIST_CNN_NONIID\"\n",
    "filename1 = \"[10]FL_mnist_cnn_261_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "filename2 = \"[12]HFL2_mnist_cnn_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "filename3 = \"[14]HFL4_mnist_cnn_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "filename4 = \"[16]HFL8_mnist_cnn_30_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]\"\n",
    "\n",
    "with open(r\"../save/objects/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects/\" + filename4 + \".pkl\", \"rb\") as input_file: data4 = pickle.load(input_file)    \n",
    "\n",
    "trloss1 = data1[0]    \n",
    "trloss2 = data2[0]    \n",
    "trloss3 = data3[0]    \n",
    "trloss4 = data4[0]    \n",
    "    \n",
    "tracc1 = data1[1]\n",
    "tracc2 = data2[1]\n",
    "tracc3 = data3[1]\n",
    "tracc4 = data4[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, 'Communication Rounds')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 60764 characters]\n",
      "text/plain": [
       "<Figure size 1368x504 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ===== Plot Average Accuracy vs Communication rounds ===== \n",
    "plt.figure(figsize=(19, 7))\n",
    "\n",
    "plt.subplot(1, 2, 1)\n",
    "plt.title(datamodelset)\n",
    "plt.plot(range(len(tracc1)), tracc1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_acc.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_acc.png')\n",
    "\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.subplot(1, 2, 2)\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, color='r', label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, '-.', color='b', label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, '--', color='k', label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, color='k', label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "# plt.savefig('../save/fed_{}_{}_{}_C[{}]_iid[{}]_E[{}]_B[{}]_loss.png'.\n",
    "#             format(args.dataset, args.model, args.epochs, args.frac,\n",
    "#                    args.iid, args.local_ep, args.local_bs))\n",
    "\n",
    "# plt.savefig('../save/' + datamodelset + '_loss.png')\n",
    "# plt.show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "import pickle\n",
    "\n",
    "filename1 = \"FL_cifar_cnn_300_lr[0.01]_C[0.1]_iid[1]_E[5]_B[50]\"\n",
    "with open(r\"../save/objects/\" + filename1 + \".pkl\", \"rb\") as input_file: data = pickle.load(input_file)\n",
    "        \n",
    "# print(data)\n",
    "trloss = data[0]\n",
    "tracc = data[1]\n",
    "# testloss = data[2]\n",
    "# print(len(trloss))\n",
    "# (len(tracc))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "# using enumerate() + next() to find index of first element just greater than 80%\n",
    "testacc = 0.63\n",
    "res = next(x for x, val in enumerate(tracc) if val >= testacc) \n",
    "\n",
    "# printing result \n",
    "print (\"The number of global training round just greater than \" + str(testacc*100) + \"% : \" + str(res+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
