{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "95ebf436",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2116a606",
   "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": 22,
   "id": "299bdab6",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"/home/SemiTab/motivation/clustering.csv\", index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f0fae98a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['masking', 'shuffling', 'noisemasking', 'rq', 'binshuffling-2',\n",
       "       'binsampling-2', 'binshuffling-3', 'binsampling-3',\n",
       "       'binshuffling-4', 'binsampling-4', 'binshuffling-5',\n",
       "       'binsampling-5', 'binshuffling-6', 'binsampling-6',\n",
       "       'binshuffling-7', 'binsampling-7', 'binshuffling-8',\n",
       "       'binsampling-8', 'binshuffling-9', 'binsampling-9',\n",
       "       'binshuffling-10', 'binsampling-10', 'scarf', 'cutmix'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"transform\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "099a87b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = data.groupby(\"transform\").mean(\"correlation\").reset_index()\n",
    "p = p[p[\"transform\"].isin([\n",
    "    \"masking\", \"scarf\", \"shuffling\", \"noisemasking\", \"rq\", \"cutmix\",\n",
    "    \"binshuffling-4\", \"binsampling-4\"])].reset_index(drop=True)\n",
    "p = p.set_index(\"transform\").loc[[\n",
    "    \"masking\", \"scarf\", \"shuffling\", \"noisemasking\", \"rq\", \"cutmix\", \"binshuffling-4\", \"binsampling-4\"]].reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "0441a498",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>transform</th>\n",
       "      <th>data_id</th>\n",
       "      <th>clustering</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>masking</td>\n",
       "      <td>16182.326531</td>\n",
       "      <td>0.388184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>scarf</td>\n",
       "      <td>18056.261905</td>\n",
       "      <td>0.399868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>shuffling</td>\n",
       "      <td>16182.326531</td>\n",
       "      <td>0.471507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>noisemasking</td>\n",
       "      <td>16182.326531</td>\n",
       "      <td>0.467287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>rq</td>\n",
       "      <td>16182.326531</td>\n",
       "      <td>-0.000249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>cutmix</td>\n",
       "      <td>17636.860465</td>\n",
       "      <td>0.681398</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>binshuffling-4</td>\n",
       "      <td>16182.326531</td>\n",
       "      <td>0.842730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>binsampling-4</td>\n",
       "      <td>16182.326531</td>\n",
       "      <td>0.826398</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        transform       data_id  clustering\n",
       "0         masking  16182.326531    0.388184\n",
       "1           scarf  18056.261905    0.399868\n",
       "2       shuffling  16182.326531    0.471507\n",
       "3    noisemasking  16182.326531    0.467287\n",
       "4              rq  16182.326531   -0.000249\n",
       "5          cutmix  17636.860465    0.681398\n",
       "6  binshuffling-4  16182.326531    0.842730\n",
       "7   binsampling-4  16182.326531    0.826398"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "61c4f7ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13204 characters]\n",
      "text/plain": [
       "<Figure size 360x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(5, 3))\n",
    "plt.gca().set_axisbelow(True)\n",
    "plt.bar(p[\"transform\"], p[\"clustering\"])\n",
    "plt.xticks(ticks=[0, 1, 2, 3, 4, 5, 6, 7], \n",
    "           labels=[\n",
    "               'Masking', 'Sampling', 'Shuffling',\n",
    "               'Noise', 'RQ', 'CutMix', 'Ours(Shuffling)', 'Ours(Sampling)'], \n",
    "           rotation=90, fontsize=12)\n",
    "plt.yscale('log')\n",
    "plt.grid(axis='y', which='both')\n",
    "plt.minorticks_on() \n",
    "plt.ylabel(\"ARI Score\", fontsize=11.5)\n",
    "plt.savefig(\"figs/clustering.png\", dpi=300, bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3a99679f",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = data.groupby(\"transform\").mean(\"correlation\").reset_index()\n",
    "# p = p[p[\"transform\"].str.startswith(\"bin\")].reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6998b0fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1, p2 = [], []\n",
    "for nb in range(2, 11):\n",
    "    p1.append(p.loc[p[\"transform\"] == f'binshuffling-{nb}', \"clustering\"].values[0])\n",
    "    p2.append(p.loc[p[\"transform\"] == f'binsampling-{nb}', \"clustering\"].values[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fadd4e43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19828 characters]\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(4, 3))\n",
    "plt.gca().set_axisbelow(True)\n",
    "plt.grid()\n",
    "plt.plot(p1, marker=\"o\", label=\"Range-limited shuffling\")\n",
    "plt.plot(p2, marker=\"o\", label=\"Range-limited sampling\")\n",
    "plt.xlabel(\"Number of ranges\", fontsize=11)\n",
    "plt.ylabel(\"ARI Score\", fontsize=11)\n",
    "plt.legend()\n",
    "plt.savefig(\"figs/clustering-numbins.png\", dpi=300, bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd3b07dc",
   "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
}
