{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "772bda95",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "553fbcc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "datalist = [\n",
    "    22, 54, 1063, 1067, 12, 18, 23, 59, 188, 307, 1043, 1459, 1475, 1489, 1492, 1497, 1503, 4153, 40499, 44125,\n",
    "    44131, 45062, 44157, 1462, 44160, 29, 37, 53, 49, 1504, 1494, 41143, 44126, 40981, 41168, 44091, 44158, 44123, 44090,\n",
    "    40922, 44161, 45714]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "e34a2f58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8400 8400\n"
     ]
    }
   ],
   "source": [
    "result = pd.DataFrame(columns=(\"shots\", \"numbins\", \"trial\", \"data\", \"model\", \"acc\"))\n",
    "i = 0\n",
    "shot = 1\n",
    "for model in [\"sslbinshuffling\", \"sslbinsampling\"]:\n",
    "    for d in datalist:\n",
    "        for p in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n",
    "            for seed in range(10):\n",
    "                if (p == 4):\n",
    "                    fname = f'/home/SemiTab/results/seed={seed}/shot={shot}/model={model}/data={d}/performance.npy'\n",
    "                elif (p == 1) & (model == \"sslbinshuffling\"):\n",
    "                    fname = f'/home/SemiTab/results/seed={seed}/shot={shot}/model=sslshuffling/data={d}/performance.npy'\n",
    "                elif (p == 1) & (model == \"sslbinsampling\"):\n",
    "                    fname = f'/home/SemiTab/results/seed={seed}/shot={shot}/model=sslscarf/data={d}/performance.npy'\n",
    "                else:\n",
    "                    fname = f'/home/SemiTab/results/numbins/bin={p}/seed={seed}/shot={shot}/model={model}/data={d}/performance.npy'\n",
    "                if os.path.exists(fname):\n",
    "                    dt = np.load(fname, allow_pickle=True).item()\n",
    "                    result.loc[i] = [shot, p, seed, d, model, dt[\"Test\"][\"lr\"][0]]\n",
    "                    i += 1\n",
    "                else:\n",
    "                    print(shot, model, d, p, seed)\n",
    "                    \n",
    "print(len(result), 10*42*10*2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a55c5660",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "595de4a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = result[result[\"model\"] == \"sslbinshuffling\"].groupby([\"shots\", \"numbins\", \"data\"]).mean(\"acc\").reset_index()\n",
    "p1 = p1.groupby([\"shots\", \"numbins\"]).mean(\"acc\").reset_index()\n",
    "\n",
    "p2 = result[result[\"model\"] == \"sslbinsampling\"].groupby([\"shots\", \"numbins\", \"data\"]).mean(\"acc\").reset_index()\n",
    "p2 = p2.groupby([\"shots\", \"numbins\"]).mean(\"acc\").reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "96dd06a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22328 characters]\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from matplotlib.lines import Line2D\n",
    "\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.gca().set_axisbelow(True)\n",
    "plt.grid()\n",
    "plt.plot(p1[\"numbins\"], p1[\"acc\"], marker=\"o\", \n",
    "         label=\"Range-limited\\nshuffling\")\n",
    "plt.plot(p2[\"numbins\"], p2[\"acc\"], marker=\"o\", \n",
    "         label=\"Range-limited\\nsampling\")\n",
    "\n",
    "# Adding the legend\n",
    "plt.legend(fontsize=10.5)\n",
    "\n",
    "plt.xticks(fontsize=11)\n",
    "plt.yticks(fontsize=11)\n",
    "plt.xlabel(\"Number of Ranges\", fontsize=12)\n",
    "plt.ylabel(\"1-shot Average Accuracy\", fontsize=12)\n",
    "# plt.title(model)\n",
    "plt.savefig(f'figs/discussion-numbins-both.png', dpi=300, bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35ab8971",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66c26840",
   "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.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
