{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "import pandas as pd\n",
    "import matplotlib.pylab as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "\n",
    "from src.lesion import (compute_damaged_cluster_stats,\n",
    "                        plot_overall_damaged_clusters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single Lesion Test - One network scatter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../results/lesion_importance_results.pkl', 'rb') as f:\n",
    "    lesion_test_results = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='Sub-module (proportion in layer)', ylabel='Accuracy'>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38224 characters]\n",
      "text/plain": [
       "<Figure size 792x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_overall_damaged_clusters(**lesion_test_results['net_1']['clusters_12'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['true_results', 'all_random_results', 'metadata', 'evaluation'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lesion_test_results['net_0']['clusters_12'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = compute_damaged_cluster_stats(**lesion_test_results['net_1']['clusters_12'])\n",
    "df['mean_diff'] = (df['true'] - df['diff']) - df['mean']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32264 characters]\n",
      "text/plain": [
       "<Figure size 576x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.rcParams.update({'font.size': 12})\n",
    "\n",
    "df2 = df.copy()\n",
    "df2['label_in_layer_proportion'] *= 100\n",
    "df2['diff'] *= 100\n",
    "\n",
    "f, ax = plt.subplots(1, figsize=(8, 5))\n",
    "ax = sns.scatterplot(x='label_in_layer_proportion', y='diff',\n",
    "                hue=df2['taxonomy'].str.capitalize().tolist(),\n",
    "                hue_order=['Important', 'Sig-but-not-diff', 'Prunable', 'Small', 'Other'],\n",
    "                s=100,\n",
    "                palette='cubehelix',\n",
    "                data=df2)\n",
    "ax.set_ylabel('Difference (%)')\n",
    "ax.set_xlabel('Proportion in Layer (%)')\n",
    "ax.legend(title='Taxonomy',\n",
    "          loc='lower right')\n",
    "\n",
    "for index, r in df2.iterrows():\n",
    "    if r['taxonomy'] == 'important':\n",
    "        ax.annotate('{}-{}'.format(*index),\n",
    "                    (r['label_in_layer_proportion']-2,\n",
    "                    r['diff']),\n",
    "                    ha='center',\n",
    "                    va='center',\n",
    "                    fontsize=15)\n",
    "\n",
    "plt.savefig('../results/lesion_FASHION_importance_scatter_net1_k12.png', dpi=300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv('../results/lesion_FASHION_importance_taxonomy_net1_k12.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Clustering Factors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['model', 'network_type', 'Network', 'Dataset', 'ncut', 'ave_in_out',\n",
       "       'n_samples', 'mean', 'stdev', 'z_score', 'percentile', 'train_acc',\n",
       "       'train_loss', 'test_acc', 'test_loss'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('../results/clustering_factors_mlp.csv', index_col=0)\n",
    "\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "ddf = df[df['Dataset'] != 'CIFAR-10']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\nsns.pointplot(x='Network', y='mean', hue='Dataset',\\norder=['Control', \\n 'Modular Task',\\n 'Dropout',\\n 'Pruning',\\n 'Clusterable Init',  'L1 Reg'],\\n linestyles='--',\\ndata=df, ax=ax)\\n\""
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39940 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "f, ax = plt.subplots(1, figsize=(10, 5))\n",
    "\n",
    "sns.pointplot(x='Network', y='ncut', hue='Dataset',\n",
    "order=['Control', \n",
    " 'Modular Task',\n",
    " 'Dropout',\n",
    " 'Pruning',\n",
    " 'Clusterable Init',  'L1 Reg'],\n",
    "data=df, ax=ax)\n",
    "\"\"\"\n",
    "sns.pointplot(x='Network', y='mean', hue='Dataset',\n",
    "order=['Control', \n",
    " 'Modular Task',\n",
    " 'Dropout',\n",
    " 'Pruning',\n",
    " 'Clusterable Init',  'L1 Reg'],\n",
    " linestyles='--',\n",
    "data=df, ax=ax)\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "ddf2 = df[df['Dataset'] == 'MNIST']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x11e55a4c0>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.violinplot(x='Network', y='ncut', data=ddf2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x11bf143a0>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18356 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.violinplot(x='Network', y='ncut', data=ddf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x11e49efd0>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15040 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.violinplot(x='Network', y='test_acc', data=ddf)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.5 64-bit ('nn_clustering': pipenv)",
   "language": "python",
   "name": "python37564bitnnclusteringpipenv0036ea69ae134ffaad31b0bbca1ebb65"
  },
  "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
