{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotting Figures for 16-bit FL and HFL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MNIST MLP IID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== MNIST MLP IID ===== \n",
    "datamodelset = \"MNIST_MLP_IID\"\n",
    "filename1 = \"FL_mnist_mlp_468_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "filename2 = \"HFL2_mnist_mlp_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "filename3 = \"HFL4_mnist_mlp_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "filename4 = \"HFL4_mnist_mlp_30_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "\n",
    "with open(r\"../save/objects_fp16/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + 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": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function matplotlib.pyplot.show(*args, **kw)>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18296 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 21392 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot Average Accuracy vs Communication rounds\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(tracc1)), tracc1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_acc_FP16.png')\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_loss_FP16.png')\n",
    "\n",
    "plt.show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== MNIST MLP NON-IID ===== \n",
    "datamodelset = \"MNIST_MLP_NONIID\"\n",
    "filename1 = \"FL_mnist_mlp_1196_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "filename2 = \"HFL2_mnist_mlp_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "filename3 = \"HFL4_mnist_mlp_150_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "filename4 = \"HFL8_mnist_mlp_30_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "\n",
    "with open(r\"../save/objects_fp16/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + 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": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function matplotlib.pyplot.show(*args, **kw)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26216 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33880 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot Average Accuracy vs Communication rounds\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(tracc1)), tracc1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_acc_FP16.png')\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_loss_FP16.png')\n",
    "\n",
    "plt.show\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== MNIST CNN IID ===== \n",
    "datamodelset = \"MNIST_CNN_IID\"\n",
    "filename1 = \"FL_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "filename2 = \"HFL2_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "filename3 = \"HFL4_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "filename4 = \"HFL8_mnist_cnn_30_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "\n",
    "with open(r\"../save/objects_fp16/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + 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": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function matplotlib.pyplot.show(*args, **kw)>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18812 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 22352 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot Average Accuracy vs Communication rounds\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(tracc1)), tracc1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_acc_FP16.png')\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_loss_FP16.png')\n",
    "\n",
    "plt.show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ===== MNIST CNN NON-IID ===== \n",
    "datamodelset = \"MNIST_CNN_NONIID\"\n",
    "filename1 = \"FL_mnist_cnn_261_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "filename2 = \"HFL2_mnist_cnn_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "filename3 = \"HFL4_mnist_cnn_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "filename4 = \"HFL8_mnist_cnn_30_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "\n",
    "with open(r\"../save/objects_fp16/\" + filename1 + \".pkl\", \"rb\") as input_file: data1 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename2 + \".pkl\", \"rb\") as input_file: data2 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + filename3 + \".pkl\", \"rb\") as input_file: data3 = pickle.load(input_file)\n",
    "with open(r\"../save/objects_fp16/\" + 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": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function matplotlib.pyplot.show(*args, **kw)>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 25248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 26384 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plot Average Accuracy vs Communication rounds\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(tracc1)), tracc1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc2)), tracc2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc3)), tracc3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(tracc4)), tracc4, label=\"HFL8\")\n",
    "plt.legend(loc=\"lower right\")\n",
    "\n",
    "plt.ylabel('Average Accuracy')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_acc_FP16.png')\n",
    "\n",
    "# Plot Loss curve\n",
    "plt.figure()\n",
    "plt.title(datamodelset)\n",
    "\n",
    "plt.plot(range(len(trloss1)), trloss1, label=\"FL\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss2)), trloss2, label=\"HFL2\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss3)), trloss3, label=\"HFL4\", linewidth=0.9)\n",
    "plt.plot(range(len(trloss4)), trloss4, label=\"HFL8\")\n",
    "plt.legend(loc=\"upper right\")\n",
    "\n",
    "plt.ylabel('Training loss')\n",
    "plt.xlabel('Communication Rounds')\n",
    "plt.savefig('../save/' + datamodelset + '_loss_FP16.png')\n",
    "\n",
    "plt.show"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Function to find out the number of communication rounds needed to exceed a certain prediction accuracy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The number of global training round just greater than 97.0% : 74\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "##### CIFAR\n",
    "#filename1 = \"FL_cifar_cnn_500_lr[0.01]_C[0.1]_iid[1]_E[5]_B[50]_FP16\"\n",
    "#filename1 = \"HFL2_cifar_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[5]_B[50]_FP16\"\n",
    "#filename1 = \"HFL4_cifar_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[5]_B[50]_FP16\"\n",
    "#filename1 = \"HFL8_cifar_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[5]_B[50]_FP16\"\n",
    "\n",
    "##### MNIST_MLP_IID\n",
    "#filename1 = \"FL_mnist_mlp_650_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL2_mnist_mlp_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL4_mnist_mlp_150_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL8_mnist_mlp_30_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "\n",
    "##### MNIST_MLP_NON-IID\n",
    "#filename1 = \"FL_mnist_mlp_1196_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL2_mnist_mlp_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL4_mnist_mlp_150_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL8_mnist_mlp_30_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "\n",
    "##### MNUST_CNN_IID\n",
    "filename1 = \"FL_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL2_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL4_mnist_cnn_100_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL8_mnist_cnn_30_lr[0.01]_C[0.1]_iid[1]_E[1]_B[10]_FP16\"\n",
    "\n",
    "##### MNIST_CNN_NON-IID\n",
    "#filename1 = \"FL_mnist_cnn_261_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL2_mnist_cnn_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL4_mnist_cnn_100_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "#filename1 = \"HFL8_mnist_cnn_30_lr[0.01]_C[0.1]_iid[0]_E[1]_B[10]_FP16\"\n",
    "\n",
    "with open(r\"../save/objects_fp16/\" + filename1 + \".pkl\", \"rb\") as input_file: data = pickle.load(input_file)\n",
    "\n",
    "trloss = data[0]\n",
    "tracc = data[1]\n",
    "\n",
    "# using enumerate() + next() to find index of first element just greater than a certain percentage\n",
    "testacc = 0.97\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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
