{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# MLP Double lesion Test"
      ],
      "metadata": {
        "papermill": {
          "duration": 0.038348,
          "end_time": "2019-11-22T16:04:58.576972",
          "exception": false,
          "start_time": "2019-11-22T16:04:58.538624",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "MODEL_TAG = 'FASHION+DROPOUT'\n",
        "MODE = 'STORE'\n",
        "WITH_ONE_WAY_RANDOM_DEMO = False"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "tags": [
          "parameters"
        ]
      }
    },
    {
      "cell_type": "code",
      "source": [
        "assert MODE in ('STORE', 'LOAD')"
      ],
      "outputs": [],
      "execution_count": 2,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "%load_ext autoreload\n",
        "%autoreload 2"
      ],
      "outputs": [],
      "execution_count": 3,
      "metadata": {
        "papermill": {
          "duration": 0.053341,
          "end_time": "2019-11-22T16:04:58.646996",
          "exception": false,
          "start_time": "2019-11-22T16:04:58.593655",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "# os.environ['CUDA_VISIBLE_DEVICES'] = '1'\n",
        "\n",
        "import sys\n",
        "sys.path.append('..')\n",
        "\n",
        "import pickle\n",
        "\n",
        "from IPython import display\n",
        "from tqdm import tqdm\n",
        "import numpy as np\n",
        "import matplotlib.pylab as plt\n",
        "import seaborn as sns\n",
        "import pandas as pd\n",
        "\n",
        "from src.pointers import DATA_PATHS\n",
        "from src.experiment_tagging import get_model_path\n",
        "from src.lesion import (perform_lesion_experiment,\n",
        "                              report_lesion_test)\n",
        "from src.visualization import (run_double_spectral_cluster, draw_mlp_clustering_report,\n",
        "                               build_weighted_dist_mat, plot_weighted_dist_mat,\n",
        "                               draw_ow_weight_dependency_graph)\n",
        "\nsns.set(style='whitegrid')"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "Using TensorFlow backend.\n"
          ]
        }
      ],
      "execution_count": 4,
      "metadata": {
        "papermill": {
          "duration": 2.751473,
          "end_time": "2019-11-22T16:05:01.499408",
          "exception": false,
          "start_time": "2019-11-22T16:04:58.747935",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "DATASET_NAME, *_ = MODEL_TAG.split('+')\n",
        "DATASET_NAME = DATASET_NAME.lower()\n",
        "\nPICKLE_PATH = f'../results/double-lesion-{MODEL_TAG}-iclr.pickle'"
      ],
      "outputs": [],
      "execution_count": 5,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "if MODE == 'LOAD':\n",
        "    with open(PICKLE_PATH, 'rb') as f:\n",
        "        (N_CLUSTERS, single_df,\n",
        "         TWO_WAY_N_SHUFFLES, TWO_WAY_PVALUE_THRESHOLD,\n",
        "         double_joint_df, joint_metadata, \n",
        "         double_conditional_df, conditional_metadata) = pickle.load(f)\n",
        "        \n",
        "if MODE == 'STORE':\n",
        "    N_CLUSTERS = 8\n",
        "    ONE_WAY_N_SHUFFLES = 20\n",
        "    TWO_WAY_N_SHUFFLES = 20\n",
        "    TWO_WAY_PVALUE_THRESHOLD = 1 / TWO_WAY_N_SHUFFLES"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "papermill": {
          "duration": 0.080934,
          "end_time": "2019-11-22T16:05:01.720440",
          "exception": false,
          "start_time": "2019-11-22T16:05:01.639506",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "model_path = get_model_path(MODEL_TAG)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "../tmp/201030/fashion_mlp_20epochs_dropout/091251\n"
          ]
        }
      ],
      "execution_count": 7,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Clustering"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "%%time\n",
        "\n",
        "model_path = get_model_path(MODEL_TAG)\n",
        "one_way_clustering_result = run_double_spectral_cluster(model_path, n_clusters=N_CLUSTERS, with_shuffle=False)\n",
        "\n",
        "draw_mlp_clustering_report(model_path,\n",
        "                           one_way_clustering_result,\n",
        "                           n_cluster=N_CLUSTERS, title=f'{MODEL_TAG}')"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "CPU times: user 13min 6s, sys: 27min 2s, total: 40min 8s\n",
            "Wall time: 1min 35s\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 661572 characters]\n"
            ],
            "text/plain": [
              "<Figure size 1440x2160 with 4 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 8,
      "metadata": {
        "papermill": {
          "duration": 23.575014,
          "end_time": "2019-11-22T16:05:25.323304",
          "exception": false,
          "start_time": "2019-11-22T16:05:01.748290",
          "status": "completed"
        },
        "scrolled": false,
        "tags": []
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Single: Taxonomy of layer-clusters"
      ],
      "metadata": {
        "papermill": {
          "duration": 0.031096,
          "end_time": "2019-11-22T16:05:26.469742",
          "exception": false,
          "start_time": "2019-11-22T16:05:26.438646",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%%time\n",
        "\n",
        "if MODE == 'STORE':\n",
        "    single_df = report_lesion_test(MODEL_TAG,\n",
        "                                     '.' + DATA_PATHS[DATASET_NAME],\n",
        "                                     model_path,\n",
        "                                     n_clusters=N_CLUSTERS, n_shuffles=ONE_WAY_N_SHUFFLES, n_way=1,\n",
        "                                     verbose=True)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Single worker!\n",
            "Loading data...\n",
            "Running spectral clustering...\n",
            "Loading model and extracting weights...\n",
            "Evaluate original model...\n",
            "Extract metadata...\n",
            "Apply lesion trial on the true clustering...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "  0%|          | 0/20 [00:00<?, ?it/s]"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Apply lesion trial on the random clusterings...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "100%|██████████| 20/20 [03:46<00:00, 11.32s/it]\n",
            "/usr/local/lib/python3.7/dist-packages/pandas/plotting/_core.py:396: UserWarning: FixedFormatter should only be used together with FixedLocator\n",
            "  ax.set_xticklabels(xticklabels)\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "CPU times: user 16min 14s, sys: 18min 31s, total: 34min 46s\n",
            "Wall time: 4min 49s\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 41756 characters]\n"
            ],
            "text/plain": [
              "<Figure size 792x432 with 1 Axes>"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 109816 characters]\n"
            ],
            "text/plain": [
              "<Figure size 504x360 with 1 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 9,
      "metadata": {
        "papermill": {
          "duration": 1354.988489,
          "end_time": "2019-11-22T16:28:01.486921",
          "exception": false,
          "start_time": "2019-11-22T16:05:26.498432",
          "status": "completed"
        },
        "scrolled": false,
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "single_df"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 40,
          "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></th>\n",
              "      <th>diff</th>\n",
              "      <th>corrected_pvalue</th>\n",
              "      <th>label_in_layer_proportion</th>\n",
              "      <th>true</th>\n",
              "      <th>taxonomy</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "      <th>z_score</th>\n",
              "      <th>n_layer_label</th>\n",
              "      <th>mean_diff</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>layer</th>\n",
              "      <th>label</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"7\" valign=\"top\">1</th>\n",
              "      <th>0</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.004808</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>small</td>\n",
              "      <td>0.869010</td>\n",
              "      <td>0.000210</td>\n",
              "      <td>0.428546</td>\n",
              "      <td>1</td>\n",
              "      <td>0.000090</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>-0.2484</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.403846</td>\n",
              "      <td>0.6207</td>\n",
              "      <td>important</td>\n",
              "      <td>0.861585</td>\n",
              "      <td>0.004804</td>\n",
              "      <td>-50.138268</td>\n",
              "      <td>84</td>\n",
              "      <td>0.007515</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>-0.0685</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.153846</td>\n",
              "      <td>0.8006</td>\n",
              "      <td>important</td>\n",
              "      <td>0.867305</td>\n",
              "      <td>0.002218</td>\n",
              "      <td>-30.071409</td>\n",
              "      <td>32</td>\n",
              "      <td>0.001795</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>-0.0016</td>\n",
              "      <td>0.428571</td>\n",
              "      <td>0.110577</td>\n",
              "      <td>0.8675</td>\n",
              "      <td>other</td>\n",
              "      <td>0.867855</td>\n",
              "      <td>0.001045</td>\n",
              "      <td>-0.339705</td>\n",
              "      <td>23</td>\n",
              "      <td>0.001245</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.428571</td>\n",
              "      <td>0.004808</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>small</td>\n",
              "      <td>0.868990</td>\n",
              "      <td>0.000346</td>\n",
              "      <td>0.317682</td>\n",
              "      <td>1</td>\n",
              "      <td>0.000110</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>-0.1761</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.312500</td>\n",
              "      <td>0.6930</td>\n",
              "      <td>important</td>\n",
              "      <td>0.864570</td>\n",
              "      <td>0.003146</td>\n",
              "      <td>-54.531135</td>\n",
              "      <td>65</td>\n",
              "      <td>0.004530</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.009615</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>small</td>\n",
              "      <td>0.869045</td>\n",
              "      <td>0.000393</td>\n",
              "      <td>0.140069</td>\n",
              "      <td>2</td>\n",
              "      <td>0.000055</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"6\" valign=\"top\">2</th>\n",
              "      <th>0</th>\n",
              "      <td>-0.0134</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.472656</td>\n",
              "      <td>0.8557</td>\n",
              "      <td>important</td>\n",
              "      <td>0.865485</td>\n",
              "      <td>0.002956</td>\n",
              "      <td>-3.310768</td>\n",
              "      <td>121</td>\n",
              "      <td>0.003615</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.007812</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>small</td>\n",
              "      <td>0.869115</td>\n",
              "      <td>0.000252</td>\n",
              "      <td>-0.059550</td>\n",
              "      <td>2</td>\n",
              "      <td>-0.000015</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>-0.0023</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.027344</td>\n",
              "      <td>0.8668</td>\n",
              "      <td>small</td>\n",
              "      <td>0.869105</td>\n",
              "      <td>0.000378</td>\n",
              "      <td>-6.103840</td>\n",
              "      <td>7</td>\n",
              "      <td>-0.000005</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>-0.0060</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.113281</td>\n",
              "      <td>0.8631</td>\n",
              "      <td>sig-but-not-diff</td>\n",
              "      <td>0.868650</td>\n",
              "      <td>0.001139</td>\n",
              "      <td>-4.872606</td>\n",
              "      <td>29</td>\n",
              "      <td>0.000450</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>-0.0014</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.187500</td>\n",
              "      <td>0.8677</td>\n",
              "      <td>other</td>\n",
              "      <td>0.867835</td>\n",
              "      <td>0.001469</td>\n",
              "      <td>-0.091872</td>\n",
              "      <td>48</td>\n",
              "      <td>0.001265</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>0.0014</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.191406</td>\n",
              "      <td>0.8705</td>\n",
              "      <td>prunable</td>\n",
              "      <td>0.867670</td>\n",
              "      <td>0.001306</td>\n",
              "      <td>2.167092</td>\n",
              "      <td>49</td>\n",
              "      <td>0.001430</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"3\" valign=\"top\">3</th>\n",
              "      <th>0</th>\n",
              "      <td>-0.0343</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.617188</td>\n",
              "      <td>0.8348</td>\n",
              "      <td>important</td>\n",
              "      <td>0.869600</td>\n",
              "      <td>0.003309</td>\n",
              "      <td>-10.517282</td>\n",
              "      <td>158</td>\n",
              "      <td>-0.000500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>0.0005</td>\n",
              "      <td>0.904762</td>\n",
              "      <td>0.023438</td>\n",
              "      <td>0.8696</td>\n",
              "      <td>small</td>\n",
              "      <td>0.869085</td>\n",
              "      <td>0.000300</td>\n",
              "      <td>1.718930</td>\n",
              "      <td>6</td>\n",
              "      <td>0.000015</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>-0.0129</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.359375</td>\n",
              "      <td>0.8562</td>\n",
              "      <td>important</td>\n",
              "      <td>0.871120</td>\n",
              "      <td>0.001861</td>\n",
              "      <td>-8.015435</td>\n",
              "      <td>92</td>\n",
              "      <td>-0.002020</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"4\" valign=\"top\">4</th>\n",
              "      <th>0</th>\n",
              "      <td>-0.0379</td>\n",
              "      <td>0.142857</td>\n",
              "      <td>0.562500</td>\n",
              "      <td>0.8312</td>\n",
              "      <td>diff-but-not-sig</td>\n",
              "      <td>0.849015</td>\n",
              "      <td>0.020016</td>\n",
              "      <td>-0.890040</td>\n",
              "      <td>144</td>\n",
              "      <td>0.020085</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.0002</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.003906</td>\n",
              "      <td>0.8693</td>\n",
              "      <td>small</td>\n",
              "      <td>0.869055</td>\n",
              "      <td>0.000170</td>\n",
              "      <td>1.440652</td>\n",
              "      <td>1</td>\n",
              "      <td>0.000045</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>-0.0017</td>\n",
              "      <td>0.095238</td>\n",
              "      <td>0.113281</td>\n",
              "      <td>0.8674</td>\n",
              "      <td>other</td>\n",
              "      <td>0.868210</td>\n",
              "      <td>0.000860</td>\n",
              "      <td>-0.941673</td>\n",
              "      <td>29</td>\n",
              "      <td>0.000890</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>-0.2048</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.320312</td>\n",
              "      <td>0.6643</td>\n",
              "      <td>important</td>\n",
              "      <td>0.864655</td>\n",
              "      <td>0.001381</td>\n",
              "      <td>-145.092784</td>\n",
              "      <td>82</td>\n",
              "      <td>0.004445</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "               diff  corrected_pvalue  label_in_layer_proportion    true  \\\n",
              "layer label                                                                \n",
              "1     0      0.0000          0.476190                   0.004808  0.8691   \n",
              "      1     -0.2484          0.047619                   0.403846  0.6207   \n",
              "      2     -0.0685          0.047619                   0.153846  0.8006   \n",
              "      3     -0.0016          0.428571                   0.110577  0.8675   \n",
              "      5      0.0000          0.428571                   0.004808  0.8691   \n",
              "      6     -0.1761          0.047619                   0.312500  0.6930   \n",
              "      7      0.0000          0.476190                   0.009615  0.8691   \n",
              "2     0     -0.0134          0.047619                   0.472656  0.8557   \n",
              "      1      0.0000          0.476190                   0.007812  0.8691   \n",
              "      2     -0.0023          0.047619                   0.027344  0.8668   \n",
              "      3     -0.0060          0.047619                   0.113281  0.8631   \n",
              "      6     -0.0014          0.476190                   0.187500  0.8677   \n",
              "      7      0.0014          1.000000                   0.191406  0.8705   \n",
              "3     0     -0.0343          0.047619                   0.617188  0.8348   \n",
              "      4      0.0005          0.904762                   0.023438  0.8696   \n",
              "      7     -0.0129          0.047619                   0.359375  0.8562   \n",
              "4     0     -0.0379          0.142857                   0.562500  0.8312   \n",
              "      3      0.0002          1.000000                   0.003906  0.8693   \n",
              "      4     -0.0017          0.095238                   0.113281  0.8674   \n",
              "      7     -0.2048          0.047619                   0.320312  0.6643   \n",
              "\n",
              "                     taxonomy      mean       std     z_score  n_layer_label  \\\n",
              "layer label                                                                    \n",
              "1     0                 small  0.869010  0.000210    0.428546              1   \n",
              "      1             important  0.861585  0.004804  -50.138268             84   \n",
              "      2             important  0.867305  0.002218  -30.071409             32   \n",
              "      3                 other  0.867855  0.001045   -0.339705             23   \n",
              "      5                 small  0.868990  0.000346    0.317682              1   \n",
              "      6             important  0.864570  0.003146  -54.531135             65   \n",
              "      7                 small  0.869045  0.000393    0.140069              2   \n",
              "2     0             important  0.865485  0.002956   -3.310768            121   \n",
              "      1                 small  0.869115  0.000252   -0.059550              2   \n",
              "      2                 small  0.869105  0.000378   -6.103840              7   \n",
              "      3      sig-but-not-diff  0.868650  0.001139   -4.872606             29   \n",
              "      6                 other  0.867835  0.001469   -0.091872             48   \n",
              "      7              prunable  0.867670  0.001306    2.167092             49   \n",
              "3     0             important  0.869600  0.003309  -10.517282            158   \n",
              "      4                 small  0.869085  0.000300    1.718930              6   \n",
              "      7             important  0.871120  0.001861   -8.015435             92   \n",
              "4     0      diff-but-not-sig  0.849015  0.020016   -0.890040            144   \n",
              "      3                 small  0.869055  0.000170    1.440652              1   \n",
              "      4                 other  0.868210  0.000860   -0.941673             29   \n",
              "      7             important  0.864655  0.001381 -145.092784             82   \n",
              "\n",
              "             mean_diff  \n",
              "layer label             \n",
              "1     0       0.000090  \n",
              "      1       0.007515  \n",
              "      2       0.001795  \n",
              "      3       0.001245  \n",
              "      5       0.000110  \n",
              "      6       0.004530  \n",
              "      7       0.000055  \n",
              "2     0       0.003615  \n",
              "      1      -0.000015  \n",
              "      2      -0.000005  \n",
              "      3       0.000450  \n",
              "      6       0.001265  \n",
              "      7       0.001430  \n",
              "3     0      -0.000500  \n",
              "      4       0.000015  \n",
              "      7      -0.002020  \n",
              "4     0       0.020085  \n",
              "      3       0.000045  \n",
              "      4       0.000890  \n",
              "      7       0.004445  "
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 40,
      "metadata": {
        "papermill": {
          "duration": 0.206508,
          "end_time": "2019-11-22T16:28:01.819154",
          "exception": false,
          "start_time": "2019-11-22T16:28:01.612646",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "single_df.to_csv('../results/mlp_lesion_single_FASHION_importance_net1_k8.csv')"
      ],
      "outputs": [],
      "execution_count": 88,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "plt.rcParams.update({'font.size': 12})\n",
        "\n",
        "present_single_df = single_df.copy()\n",
        "present_single_df['label_in_layer_proportion'] *= 100\n",
        "present_single_df['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=present_single_df['taxonomy'].str.capitalize().tolist(),\n",
        "                hue_order=['Important', 'Sig-but-not-diff', 'Diff-but-not-sig', 'Prunable', 'Small', 'Other'],\n",
        "                s=100,\n",
        "                palette='cubehelix',\n",
        "                data=present_single_df)\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 present_single_df.iterrows():\n",
        "    if r['taxonomy'] == 'important':\n",
        "        ax.annotate('{}-{}'.format(*index),\n",
        "                    (r['label_in_layer_proportion']-2.5,\n",
        "                    r['diff']),\n",
        "                    ha='center',\n",
        "                    va='center',\n",
        "                    fontsize=15)\n",
        "\nplt.savefig('../results/lesion_FASHION_importance_scatter_net1_k8.png', dpi=300)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 29888 characters]\n"
            ],
            "text/plain": [
              "<Figure size 576x360 with 1 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 53,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "from src.lesion.output import plot_cluster_scatter\n",
        "\n# plot_cluster_scatter(single_df);"
      ],
      "outputs": [],
      "execution_count": 11,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "# plot_cluster_scatter(single_df, y='z_score');"
      ],
      "outputs": [],
      "execution_count": 12,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "# plot_cluster_scatter(single_df, x='diff', y='z_score');"
      ],
      "outputs": [],
      "execution_count": 13,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Let's look on a random clustering"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "if WITH_ONE_WAY_RANDOM_DEMO:\n",
        "    ax = plot_cluster_scatter(report_lesion_test(MODEL_TAG,\n",
        "                                             '.' + DATA_PATHS[DATASET_NAME],\n",
        "                                             model_path,\n",
        "                                             n_clusters=N_CLUSTERS, n_shuffles=ONE_WAY_N_SHUFFLES, n_way=1,\n",
        "                                             with_overall_plot=False, with_accuracy_profile=False,\n",
        "                                             true_as_random=True,\n",
        "                                             verbose=False),\n",
        "                        vmin=-13)\n",
        "\n    ax.set_ylim(-13, 1);"
      ],
      "outputs": [],
      "execution_count": 14,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Double: Exploring Dependencies between layer-clusters"
      ],
      "metadata": {
        "papermill": {
          "duration": 0.119255,
          "end_time": "2019-11-22T16:28:02.070919",
          "exception": false,
          "start_time": "2019-11-22T16:28:01.951664",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from src.lesion.output import (build_double_mat,\n",
        "                                     build_double_joint_interaction_mat,\n",
        "                                     build_conditional_double_df,\n",
        "                                     plot_double_heatmap,\n",
        "                                     compute_damaged_cluster_stats,\n",
        "                                     enrich_score_double_conditional_df)"
      ],
      "outputs": [],
      "execution_count": 15,
      "metadata": {
        "papermill": {
          "duration": 0.192162,
          "end_time": "2019-11-22T16:28:02.617775",
          "exception": false,
          "start_time": "2019-11-22T16:28:02.425613",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "if MODE == 'STORE':\n",
        "\n",
        "    (joint_true_results,\n",
        "     joint_all_random_results,\n",
        "     joint_metadata,\n",
        "     joint_evaluation) = perform_lesion_experiment('.' + DATA_PATHS[DATASET_NAME],\n",
        "                                                   model_path,\n",
        "                                                   n_clusters=N_CLUSTERS,\n",
        "                                                   n_shuffles=TWO_WAY_N_SHUFFLES,\n",
        "                                                   n_way=2,\n",
        "                                                   n_way_type='joint',\n",
        "                                                   with_random=True,\n",
        "                                                   verbose=True)\n",
        "\n"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Single worker!\n",
            "Loading data...\n",
            "Running spectral clustering...\n",
            "Loading model and extracting weights...\n",
            "Evaluate original model...\n",
            "Extract metadata...\n",
            "Apply lesion trial on the true clustering...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "  0%|          | 0/20 [00:00<?, ?it/s]"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Apply lesion trial on the random clusterings...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "100%|██████████| 20/20 [44:00<00:00, 132.04s/it]\n"
          ]
        }
      ],
      "execution_count": 16,
      "metadata": {
        "papermill": {
          "duration": 4227.496925,
          "end_time": "2019-11-22T17:38:30.261484",
          "exception": false,
          "start_time": "2019-11-22T16:28:02.764559",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from src.lesion.output import compute_damaged_cluster_stats"
      ],
      "outputs": [],
      "execution_count": 17,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "if MODE == 'STORE':\n",
        "    double_joint_df = compute_damaged_cluster_stats(joint_true_results, joint_all_random_results,\n",
        "                                                     joint_metadata, joint_evaluation)\n",
        "\ndouble_joint_df.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 18,
          "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>value</th>\n",
              "      <th>diff</th>\n",
              "      <th>corrected_pvalue</th>\n",
              "      <th>true</th>\n",
              "      <th>taxonomy</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "      <th>z_score</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>labels_in_layers</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 0))</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>other</td>\n",
              "      <td>0.869010</td>\n",
              "      <td>0.000210</td>\n",
              "      <td>0.428546</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 1))</th>\n",
              "      <td>-0.2484</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.6207</td>\n",
              "      <td>important</td>\n",
              "      <td>0.861440</td>\n",
              "      <td>0.004739</td>\n",
              "      <td>-50.800717</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 2))</th>\n",
              "      <td>-0.0685</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.8006</td>\n",
              "      <td>important</td>\n",
              "      <td>0.867275</td>\n",
              "      <td>0.002304</td>\n",
              "      <td>-28.942017</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 3))</th>\n",
              "      <td>-0.0016</td>\n",
              "      <td>0.380952</td>\n",
              "      <td>0.8675</td>\n",
              "      <td>other</td>\n",
              "      <td>0.867815</td>\n",
              "      <td>0.001093</td>\n",
              "      <td>-0.288152</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 5))</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.666667</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>other</td>\n",
              "      <td>0.868900</td>\n",
              "      <td>0.000407</td>\n",
              "      <td>0.491974</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "value               diff  corrected_pvalue    true   taxonomy      mean  \\\n",
              "labels_in_layers                                                          \n",
              "((1, 0), (1, 0))  0.0000          0.476190  0.8691      other  0.869010   \n",
              "((1, 0), (1, 1)) -0.2484          0.047619  0.6207  important  0.861440   \n",
              "((1, 0), (1, 2)) -0.0685          0.047619  0.8006  important  0.867275   \n",
              "((1, 0), (1, 3)) -0.0016          0.380952  0.8675      other  0.867815   \n",
              "((1, 0), (1, 5))  0.0000          0.666667  0.8691      other  0.868900   \n",
              "\n",
              "value                  std    z_score  \n",
              "labels_in_layers                       \n",
              "((1, 0), (1, 0))  0.000210   0.428546  \n",
              "((1, 0), (1, 1))  0.004739 -50.800717  \n",
              "((1, 0), (1, 2))  0.002304 -28.942017  \n",
              "((1, 0), (1, 3))  0.001093  -0.288152  \n",
              "((1, 0), (1, 5))  0.000407   0.491974  "
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 18,
      "metadata": {
        "papermill": {
          "duration": 0.409635,
          "end_time": "2019-11-22T17:38:30.833071",
          "exception": false,
          "start_time": "2019-11-22T17:38:30.423436",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### $Diff_{ij}$"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "double_joint_diff_mat = -build_double_mat(double_joint_df, single_df, col='diff')\n",
        "\n",
        "plot_double_heatmap(double_joint_diff_mat, double_joint_df,\n",
        "                     metadata=joint_metadata, pvalue_threshod=TWO_WAY_PVALUE_THRESHOLD,\n",
        "                     vmax=50);"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 174468 characters]\n"
            ],
            "text/plain": [
              "<Figure size 1440x1080 with 2 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 19,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Joint\n",
        "\n",
        "#### What can we learn from the joint TWBD?\n",
        "\n",
        "Using the single TWBD prior, with created a taxonomy of clusters, we get the primary division is to important (diff > 1 and significant) and not-important. Let's use this dichotomy to analyze the joint TWBD.\n",
        "\n",
        "Let's look at layer-cluster X and Y. If we assume that they are both important (as single), we won't be surprised that the joint damaged XY is also important(diff > 1 and significant). Therefore, we should focus on XY if it is not important.\n",
        "\n",
        "If we assume that X and Y are both *not* important, it would be interesting if the joint damage XY is important. It means that only their combined formation together is impactful.\n",
        "\n",
        "Finally, considering the case for X is important, and Y is not (without loss of generality), our prior would be that the joint damaged XY will also be important, because of the contribution of X. So not important XY matters in that situation.\n",
        "\n",
        "To summary up, these are the cases we care about, because they convey information that changes our single TWBD prior:\n",
        "\n",
        "| X | Y | XY |\n",
        "|---|---|----|\n",
        "| ✔️ | ✔️ | ❌  |\n",
        "| ✔️ | ❌ | ❌  |\n",
        "| ❌ | ❌ | ✔️  |"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "from src.lesion.output import build_double_joint_imp_grouped_df\n",
        "\n",
        "double_joint_imp_df, double_joint_imp_grouped_df = build_double_joint_imp_grouped_df(double_joint_df, single_df)\n",
        "\ndouble_joint_imp_grouped_df"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 20,
          "data": {
            "text/plain": [
              "first_is_important  second_is_important  is_important\n",
              "❌                   ❌                    ❌               88\n",
              "✔️                  ❌                    ✔️              48\n",
              "❌                   ✔️                   ✔️              41\n",
              "✔️                  ✔️                   ✔️              28\n",
              "❌                   ❌                    ✔️               3\n",
              "✔️                  ❌                    ❌                2\n",
              "dtype: int64"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 20,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "##### Let's iterate on the interesting cases:\n",
        "\nWe should be REALLY CAREFUL when we look at double damage when both of the clusters come from the same layer. The space of possible shuffles behaves different, so I won't try to come up with observation in these cases."
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "from collections import Counter\n",
        "\n",
        "double_joint_interesting_cases = [('✔️', '✔️', '❌'),\n",
        "                              ('✔️', '❌', '❌'),\n",
        "                              ('❌', '✔️', '❌'),\n",
        "                              ('❌', '❌', '✔️')]\n",
        "\nis_important_fields = ['first_is_important', 'second_is_important', 'is_important'] "
      ],
      "outputs": [],
      "execution_count": 21,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "for interesting_case in double_joint_interesting_cases:\n",
        "    \n",
        "    intresting_double_joint_imp_df = (double_joint_imp_df[(double_joint_imp_df[is_important_fields]\n",
        "                                                   == interesting_case)\n",
        "                                              .all(axis=1)])\n",
        "\n",
        "    # same cluster-layer is an anomaly that we should remove (we do so with the heatmap plotting)\n",
        "    intresting_double_joint_imp_df = intresting_double_joint_imp_df[intresting_double_joint_imp_df['first']\n",
        "                                                            != intresting_double_joint_imp_df['second']]\n",
        "\n",
        "    if not intresting_double_joint_imp_df.empty:\n",
        "        \n",
        "        print(interesting_case)\n",
        "\n",
        "        display.display(intresting_double_joint_imp_df.sort_values('diff'))\n",
        "        \n",
        "        display.display(Counter(tuple(sorted(v))\n",
        "                        for v in (intresting_double_joint_imp_df[['first_taxonomy', 'second_taxonomy']]).values))"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "('✔️', '❌', '❌')\n"
          ]
        },
        {
          "output_type": "display_data",
          "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>value</th>\n",
              "      <th>labels_in_layers</th>\n",
              "      <th>diff</th>\n",
              "      <th>corrected_pvalue</th>\n",
              "      <th>true</th>\n",
              "      <th>taxonomy</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "      <th>z_score</th>\n",
              "      <th>first</th>\n",
              "      <th>second</th>\n",
              "      <th>first_taxonomy</th>\n",
              "      <th>second_taxonomy</th>\n",
              "      <th>is_important</th>\n",
              "      <th>first_is_important</th>\n",
              "      <th>second_is_important</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>185</th>\n",
              "      <td>((3, 0), (4, 0))</td>\n",
              "      <td>-0.0843</td>\n",
              "      <td>0.190476</td>\n",
              "      <td>0.7848</td>\n",
              "      <td>diff-but-not-sig</td>\n",
              "      <td>0.82612</td>\n",
              "      <td>0.039793</td>\n",
              "      <td>-1.038368</td>\n",
              "      <td>(3, 0)</td>\n",
              "      <td>(4, 0)</td>\n",
              "      <td>important</td>\n",
              "      <td>diff-but-not-sig</td>\n",
              "      <td>❌</td>\n",
              "      <td>✔️</td>\n",
              "      <td>❌</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>128</th>\n",
              "      <td>((2, 0), (4, 0))</td>\n",
              "      <td>-0.0512</td>\n",
              "      <td>0.142857</td>\n",
              "      <td>0.8179</td>\n",
              "      <td>diff-but-not-sig</td>\n",
              "      <td>0.83437</td>\n",
              "      <td>0.026380</td>\n",
              "      <td>-0.624345</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>(4, 0)</td>\n",
              "      <td>important</td>\n",
              "      <td>diff-but-not-sig</td>\n",
              "      <td>❌</td>\n",
              "      <td>✔️</td>\n",
              "      <td>❌</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "value  labels_in_layers    diff  corrected_pvalue    true          taxonomy  \\\n",
              "185    ((3, 0), (4, 0)) -0.0843          0.190476  0.7848  diff-but-not-sig   \n",
              "128    ((2, 0), (4, 0)) -0.0512          0.142857  0.8179  diff-but-not-sig   \n",
              "\n",
              "value     mean       std   z_score   first  second first_taxonomy  \\\n",
              "185    0.82612  0.039793 -1.038368  (3, 0)  (4, 0)      important   \n",
              "128    0.83437  0.026380 -0.624345  (2, 0)  (4, 0)      important   \n",
              "\n",
              "value   second_taxonomy is_important first_is_important second_is_important  \n",
              "185    diff-but-not-sig            ❌                 ✔️                   ❌  \n",
              "128    diff-but-not-sig            ❌                 ✔️                   ❌  "
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "Counter({('diff-but-not-sig', 'important'): 2})"
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "('❌', '❌', '✔️')\n"
          ]
        },
        {
          "output_type": "display_data",
          "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>value</th>\n",
              "      <th>labels_in_layers</th>\n",
              "      <th>diff</th>\n",
              "      <th>corrected_pvalue</th>\n",
              "      <th>true</th>\n",
              "      <th>taxonomy</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "      <th>z_score</th>\n",
              "      <th>first</th>\n",
              "      <th>second</th>\n",
              "      <th>first_taxonomy</th>\n",
              "      <th>second_taxonomy</th>\n",
              "      <th>is_important</th>\n",
              "      <th>first_is_important</th>\n",
              "      <th>second_is_important</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>202</th>\n",
              "      <td>((4, 0), (4, 4))</td>\n",
              "      <td>-0.2325</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.6366</td>\n",
              "      <td>important</td>\n",
              "      <td>0.84541</td>\n",
              "      <td>0.020806</td>\n",
              "      <td>-10.036148</td>\n",
              "      <td>(4, 0)</td>\n",
              "      <td>(4, 4)</td>\n",
              "      <td>diff-but-not-sig</td>\n",
              "      <td>other</td>\n",
              "      <td>✔️</td>\n",
              "      <td>❌</td>\n",
              "      <td>❌</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>172</th>\n",
              "      <td>((2, 6), (4, 4))</td>\n",
              "      <td>-0.0165</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.8526</td>\n",
              "      <td>important</td>\n",
              "      <td>0.86627</td>\n",
              "      <td>0.002154</td>\n",
              "      <td>-6.346066</td>\n",
              "      <td>(2, 6)</td>\n",
              "      <td>(4, 4)</td>\n",
              "      <td>other</td>\n",
              "      <td>other</td>\n",
              "      <td>✔️</td>\n",
              "      <td>❌</td>\n",
              "      <td>❌</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>64</th>\n",
              "      <td>((1, 3), (2, 3))</td>\n",
              "      <td>-0.0105</td>\n",
              "      <td>0.047619</td>\n",
              "      <td>0.8586</td>\n",
              "      <td>important</td>\n",
              "      <td>0.86722</td>\n",
              "      <td>0.001889</td>\n",
              "      <td>-4.562121</td>\n",
              "      <td>(1, 3)</td>\n",
              "      <td>(2, 3)</td>\n",
              "      <td>other</td>\n",
              "      <td>sig-but-not-diff</td>\n",
              "      <td>✔️</td>\n",
              "      <td>❌</td>\n",
              "      <td>❌</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "value  labels_in_layers    diff  corrected_pvalue    true   taxonomy     mean  \\\n",
              "202    ((4, 0), (4, 4)) -0.2325          0.047619  0.6366  important  0.84541   \n",
              "172    ((2, 6), (4, 4)) -0.0165          0.047619  0.8526  important  0.86627   \n",
              "64     ((1, 3), (2, 3)) -0.0105          0.047619  0.8586  important  0.86722   \n",
              "\n",
              "value       std    z_score   first  second    first_taxonomy  \\\n",
              "202    0.020806 -10.036148  (4, 0)  (4, 4)  diff-but-not-sig   \n",
              "172    0.002154  -6.346066  (2, 6)  (4, 4)             other   \n",
              "64     0.001889  -4.562121  (1, 3)  (2, 3)             other   \n",
              "\n",
              "value   second_taxonomy is_important first_is_important second_is_important  \n",
              "202               other           ✔️                  ❌                   ❌  \n",
              "172               other           ✔️                  ❌                   ❌  \n",
              "64     sig-but-not-diff           ✔️                  ❌                   ❌  "
            ]
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "Counter({('other', 'sig-but-not-diff'): 1,\n",
              "         ('other', 'other'): 1,\n",
              "         ('diff-but-not-sig', 'other'): 1})"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 22,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "It is hard to come up with a firm conclusions, but we can clearly see that in that pattern, \"almost\" important layer-cluster (diff-but-sig or sig-but-diff) is hitting the important criterion with the help of another layer-cluster.\n",
        "\n",
        "One tempting interpretation, if we continue our previous line of reasoning until now, is that we found a higher-level clustering. These two layer-clusters should be grouped together. However, while the single BDT and its taxonomy are based on spectral clustering, and it is justified by the static structure (stronger weights inside a cluster compared to outside), this is not the case here.\n",
        "\n",
        "An alternative explanation could be that we grouped with joint TWBDT two unrelated layer-clusters, so we did damage in two different \"functions\"; therefore, it a sign for a separate grouping. \n",
        "\n\nBy the joint TWBDt experiment, we cannot distinguish the two possible explanations; therefore, we'll turn to another method to perform double BDT, which helps us to discover dependencies (in the sense of information flow) between layer-clusters)."
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Conditional - $first|second$\n",
        "\n",
        "* Difference - As in joint $Diff_{first,second}$\n",
        "* P-value - Fix the *second* layer-cluster and shuffle the *first*"
      ],
      "metadata": {
        "papermill": {
          "duration": 0.141558,
          "end_time": "2019-11-22T17:38:38.073578",
          "exception": false,
          "start_time": "2019-11-22T17:38:37.932020",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "if MODE == 'STORE':\n",
        "    (conditional_true_results,\n",
        "     conditional_all_random_results,\n",
        "     conditional_metadata,\n",
        "     conditional_evaluation) = perform_lesion_experiment('.' + DATA_PATHS[DATASET_NAME],\n",
        "                                                   model_path,\n",
        "                                                   n_clusters=N_CLUSTERS,\n",
        "                                                   n_shuffles=TWO_WAY_N_SHUFFLES,\n",
        "                                                   n_way=2,\n",
        "                                                   n_way_type='conditional',\n",
        "                                                   with_random=True,\n",
        "                                                   verbose=True)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Single worker!\n",
            "Loading data...\n",
            "Running spectral clustering...\n",
            "Loading model and extracting weights...\n",
            "Evaluate original model...\n",
            "Extract metadata...\n",
            "Apply lesion trial on the true clustering...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "  0%|          | 0/20 [00:00<?, ?it/s]"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Apply lesion trial on the random clusterings...\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "100%|██████████| 20/20 [1:11:18<00:00, 213.95s/it]\n"
          ]
        }
      ],
      "execution_count": 23,
      "metadata": {
        "papermill": {
          "duration": 7830.365643,
          "end_time": "2019-11-22T19:49:08.576735",
          "exception": false,
          "start_time": "2019-11-22T17:38:38.211092",
          "status": "completed"
        },
        "scrolled": false,
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "if MODE == 'STORE':\n",
        "    double_conditional_df = compute_damaged_cluster_stats(conditional_true_results,\n",
        "                                                           conditional_all_random_results,\n",
        "                                                           conditional_metadata,\n",
        "                                                           conditional_evaluation,\n",
        "                                                           double_joint_df=double_joint_df,\n",
        "                                                           single_df=single_df,\n",
        "                                                           diff_field='s_i|j')\n",
        "\n",
        "    double_conditional_df = enrich_score_double_conditional_df(double_conditional_df, single_df)\n",
        "\n\ndouble_conditional_df.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "../src/lesion/output.py:172: UserWarning: Make sure that `n_shuffled` for conditional double results should be the same as the one for generating the joint double df!\n",
            "  warnings.warn('Make sure that `n_shuffled` for conditional double results should be the same'\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 24,
          "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>value</th>\n",
              "      <th>diff</th>\n",
              "      <th>corrected_pvalue</th>\n",
              "      <th>true</th>\n",
              "      <th>taxonomy</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "      <th>z_score</th>\n",
              "      <th>s_i|j</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>labels_in_layers</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 0))</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>other</td>\n",
              "      <td>0.869010</td>\n",
              "      <td>0.000210</td>\n",
              "      <td>0.428546</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 1))</th>\n",
              "      <td>-0.2484</td>\n",
              "      <td>0.619048</td>\n",
              "      <td>0.6207</td>\n",
              "      <td>other</td>\n",
              "      <td>0.620155</td>\n",
              "      <td>0.001089</td>\n",
              "      <td>0.500492</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 2))</th>\n",
              "      <td>-0.0685</td>\n",
              "      <td>0.333333</td>\n",
              "      <td>0.8006</td>\n",
              "      <td>other</td>\n",
              "      <td>0.800430</td>\n",
              "      <td>0.000849</td>\n",
              "      <td>0.200186</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 3))</th>\n",
              "      <td>-0.0016</td>\n",
              "      <td>0.476190</td>\n",
              "      <td>0.8675</td>\n",
              "      <td>other</td>\n",
              "      <td>0.867505</td>\n",
              "      <td>0.000224</td>\n",
              "      <td>-0.022367</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>((1, 0), (1, 5))</th>\n",
              "      <td>0.0000</td>\n",
              "      <td>0.285714</td>\n",
              "      <td>0.8691</td>\n",
              "      <td>other</td>\n",
              "      <td>0.869125</td>\n",
              "      <td>0.000308</td>\n",
              "      <td>-0.081280</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "value               diff  corrected_pvalue    true taxonomy      mean  \\\n",
              "labels_in_layers                                                        \n",
              "((1, 0), (1, 0))  0.0000          0.476190  0.8691    other  0.869010   \n",
              "((1, 0), (1, 1)) -0.2484          0.619048  0.6207    other  0.620155   \n",
              "((1, 0), (1, 2)) -0.0685          0.333333  0.8006    other  0.800430   \n",
              "((1, 0), (1, 3)) -0.0016          0.476190  0.8675    other  0.867505   \n",
              "((1, 0), (1, 5))  0.0000          0.285714  0.8691    other  0.869125   \n",
              "\n",
              "value                  std   z_score  s_i|j  \n",
              "labels_in_layers                             \n",
              "((1, 0), (1, 0))  0.000210  0.428546    0.0  \n",
              "((1, 0), (1, 1))  0.001089  0.500492    0.0  \n",
              "((1, 0), (1, 2))  0.000849  0.200186    0.0  \n",
              "((1, 0), (1, 3))  0.000224 -0.022367    0.0  \n",
              "((1, 0), (1, 5))  0.000308 -0.081280    0.0  "
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 24,
      "metadata": {
        "papermill": {
          "duration": 0.534141,
          "end_time": "2019-11-22T19:49:09.306824",
          "exception": false,
          "start_time": "2019-11-22T19:49:08.772683",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Let's plot on heatmap the results:\n",
        "* Rows: First (\"shuffled\") layer cluster\n",
        "* Columns: Second (\"fixed\"/\"conditioned\") layer-cluster\n",
        "* Cells: Difference of true layter-cluster (number) as well as significance (★)\n",
        "\n",
        "Of course, the matrix will be symmetric concerning the difference; however, the significance won't be necessary symmetric.\n",
        "\nThe diagonal represents the single BDT. "
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "double_conditional_true_mat = -build_double_mat(double_conditional_df, single_df, col='diff')\n",
        "\n",
        "plot_double_heatmap(double_conditional_true_mat, double_conditional_df, is_trig=False,\n",
        "                     metadata=conditional_metadata, pvalue_threshod=TWO_WAY_PVALUE_THRESHOLD,\n",
        "                     vmax=50);"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 240360 characters]\n"
            ],
            "text/plain": [
              "<Figure size 1440x1080 with 2 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 25,
      "metadata": {
        "papermill": {
          "duration": 2.489359,
          "end_time": "2019-11-22T19:49:11.952506",
          "exception": false,
          "start_time": "2019-11-22T19:49:09.463147",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### We can look also into the $s_{i|j}$ - which was used to define the taxonomy (see remark later):\n",
        "\n## $s_{i|j} = Diff_{ij} - Diff_{j} = (Acc - Acc_{ij}) - (Acc - Acc_{j}) = Acc_{j} - Acc_{ij}$"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "tw_cond_sij_mat = -build_double_mat(double_conditional_df, single_df, col='s_i|j')\n",
        "\n",
        "plot_double_heatmap(tw_cond_sij_mat, double_conditional_df, is_trig=False,\n",
        "                     metadata=conditional_metadata, pvalue_threshod=TWO_WAY_PVALUE_THRESHOLD,\n",
        "                     vmax=50);\n",
        "\nplt.savefig('../results/double_lesion_FASHION_importance_heatmap_net1_k8.png', dpi=300)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 198308 characters]\n"
            ],
            "text/plain": [
              "<Figure size 1440x1080 with 2 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 55,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Remark & TODO: Integrate into notebook, not as contra to `diff`\n",
        "\n",
        "Let's look at the case of `X -> Y`, where both X and Y are *important* single layer-cluster.\n",
        "If we damage Y, then the output of X won't have any impact.\n",
        "So `d_Y ≈ d_XY` (or `s_X|Y ≈ 0`).\n",
        "The opposite argument gives us `d_X ≈ d_XY` (or `s_Y|X ≈ 0`).\n",
        "\n",
        "I defined an *important conditional* damage X|Y by these two necessary conditions:\n",
        "1. `d_XY > 1%`\n",
        "2. The p-value (damaging X while fixing Y) is minimal, i.e., `1 / (#shuffles + 1)`. In other words, `d_XY` is bigger than `d_ZY` for all (sampled) random shuffles of X (here Z).\n",
        "\n",
        "Now, I'd argue that condition (1) indeed doesn't make sense, but (2) is totally fine.\n",
        "\n",
        "First, the \"importance\" of a conditional damage `X|Y` should be defined in terms of `s_X|Y` and not `d_XY`. The latter is dependent on the impact of Y, which we would like to remove, while `s_X|Y` takes it into account. So a better first condition would be `s_X|Y > 1%`, i.e., the additional damage of X on top of Y is at least 1% of accuracy.\n",
        "\n",
        "Second, recall that we are focusing on the case of `X -> Y`. Assume we damage Y. Damaging X won't increase the damage of Y ( `s_X|Y ≈ 0`), while any other random shuffle Z might contain other neurons that influence other neurons in the layer of Y.\n",
        "**So, they will have a bigger difference, and we would have p-value equals to 1!**\n",
        "However, all the four types of influence (such as `X -> Y`) are *idealized*, and X probably influences on other neurons in the layer of Y, so we introduce some noise to the calculated accuracy. Therefore, we won't get precisely one for p-value.\n",
        "A prediction we can make here is that the standard deviation would be relatively small, and **we should check it**.\n",
        "\nThis line of argumentation works as well for the `Y|X` damage, and other types of `(X|Y, Y|X)` combinations."
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### We can also explore the relationship between `s_i|j` and `diff`. Keep in mind the taxonomy is based on `s_i|j`."
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "# sns.scatterplot(x='diff', y='s_i|j', hue='taxonomy', data=double_conditional_df);\n",
        "# plt.plot([-1, 0], [-1, 0], ls='-', c='.3');\n",
        "# plt.plot([-0.01, -0.01], [-1, 0.1], ls='--', c='.3');\n",
        "# plt.plot([-1, 0.1], [-0.01, -0.01], ls='--', c='.3');"
      ],
      "outputs": [],
      "execution_count": 52,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "As we could expect, there is a linear relationship between the two metrics. The different \"parallel\" lines are probably coming from different layer-cluster we conditioned on them because it set the offset (because $diff_j$ is fixed.\n",
        "\n#### Let's see if we get $s_{i|j}$ > 1% with p-value = 1 (called `least_important` in the taxonomy):"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "double_conditional_df['taxonomy'].value_counts()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 28,
          "data": {
            "text/plain": [
              "other               171\n",
              "important           150\n",
              "diff-but-not-sig     31\n",
              "sig-but-not-diff     29\n",
              "prunable             19\n",
              "Name: taxonomy, dtype: int64"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 28,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "double_conditional_df[double_conditional_df['taxonomy'] == 'least_important']"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 29,
          "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>value</th>\n",
              "      <th>diff</th>\n",
              "      <th>corrected_pvalue</th>\n",
              "      <th>true</th>\n",
              "      <th>taxonomy</th>\n",
              "      <th>mean</th>\n",
              "      <th>std</th>\n",
              "      <th>z_score</th>\n",
              "      <th>s_i|j</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>labels_in_layers</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "Empty DataFrame\n",
              "Columns: [diff, corrected_pvalue, true, taxonomy, mean, std, z_score, s_i|j]\n",
              "Index: []"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 29,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### How can we use conditional TWBDT? Discovering Dependencies!\n",
        "\n",
        "Suppose we have two layer-cluster $X$ and $Y$, where $X$ is in an earlier layer then $Y$. We will assume that both of them are *important* by the single BDT (diff. and sig.).\n",
        "\n",
        "Let's focus only on *important* layer-clusters, because we know by the single BDT that they are important to the functioning of the network.\n",
        "\n",
        "We will say that a layter-cluster $Y$ is *depended* on layer-cluster $X$ if there is a *flow of information* from $X$ to $Y$, i.e. the values of $X$ are required for computing $Y$. We will show it as:\n",
        "\n",
        "$$X \\rightarrow Y$$\n",
        "\n",
        "(⚠️ We need a better formalization!)\n",
        "\n",
        "#### There are four kind of possible (idealistic) dependencies, whether there are additional involved layer-clusters ($Z$ and $Z'$):\n",
        "\n",
        "![](imgs/ctwbd-dependencies.jpg)\n",
        "\n",
        "#### How will conditional TWBDT behave?\n",
        "(keep in mind, both $X$ and $Y$ are important)"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "| Type | X\\|Y | Y\\|X | Why? |\n",
        "|------|-----|-----|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n",
        "| I | ❌ | ❌ | All the (information) output of X goes to Y, and all of the (information) input of Y comes from X. So they are tied together, and damage in one for them will be equivalent to damage the other one. |\n",
        "| II | ✔️ | ❌ | Damaging X will block already all the input of Y, so no additional impact for damaging Y afterwords. However, Z gets input from X independently, so only conditioning on Y won't block all the information from X. |\n",
        "| III | ❌ | ✔️ | The opposite argument from the previous row holds. |\n",
        "| IV | ✔️ | ✔️ | Both of the two previous arguments hold. |\n",
        "\n**We cannot learn anything from the last type IV**, because it will have the same behavior as no flow of information between $X$ and $Y$. Therefore, we care only about the first three types. It does make sense because, by our prior (single BDT), we should be \"surprised\" if a layer-cluster stops to be important."
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "from src.lesion.output import build_tw_cond_imp_merged_df\n",
        "\n",
        "tw_cond_imp_df = build_tw_cond_imp_merged_df(double_conditional_df, single_df)\n",
        "\n",
        "tw_cond_imp_df['first'], tw_cond_imp_df['second'] = list(zip(*tw_cond_imp_df['sorted'].tolist()))\n",
        "\n",
        "tw_cond_imp_df['X'] = tw_cond_imp_df['first'].apply(lambda x: '-'.join(map(str, x)))\n",
        "tw_cond_imp_df['Y'] = tw_cond_imp_df['second'].apply(lambda x: '-'.join(map(str, x)))\n",
        "\ntw_cond_imp_df"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 85,
          "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>value</th>\n",
              "      <th>sorted</th>\n",
              "      <th>X|Y</th>\n",
              "      <th>Y|X</th>\n",
              "      <th>s_X|Y</th>\n",
              "      <th>s_Y|X</th>\n",
              "      <th>first</th>\n",
              "      <th>second</th>\n",
              "      <th>X</th>\n",
              "      <th>Y</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>((1, 1), (2, 0))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.2611</td>\n",
              "      <td>-0.0261</td>\n",
              "      <td>(1, 1)</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>1-1</td>\n",
              "      <td>2-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>((1, 1), (3, 0))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.2367</td>\n",
              "      <td>-0.0226</td>\n",
              "      <td>(1, 1)</td>\n",
              "      <td>(3, 0)</td>\n",
              "      <td>1-1</td>\n",
              "      <td>3-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>((1, 1), (3, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>False</td>\n",
              "      <td>-0.2495</td>\n",
              "      <td>-0.0140</td>\n",
              "      <td>(1, 1)</td>\n",
              "      <td>(3, 7)</td>\n",
              "      <td>1-1</td>\n",
              "      <td>3-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>((1, 1), (4, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.2441</td>\n",
              "      <td>-0.2005</td>\n",
              "      <td>(1, 1)</td>\n",
              "      <td>(4, 7)</td>\n",
              "      <td>1-1</td>\n",
              "      <td>4-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>((1, 2), (2, 0))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.0949</td>\n",
              "      <td>-0.0398</td>\n",
              "      <td>(1, 2)</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>1-2</td>\n",
              "      <td>2-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>((1, 2), (3, 0))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.0920</td>\n",
              "      <td>-0.0578</td>\n",
              "      <td>(1, 2)</td>\n",
              "      <td>(3, 0)</td>\n",
              "      <td>1-2</td>\n",
              "      <td>3-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>((1, 2), (3, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>False</td>\n",
              "      <td>-0.0542</td>\n",
              "      <td>0.0014</td>\n",
              "      <td>(1, 2)</td>\n",
              "      <td>(3, 7)</td>\n",
              "      <td>1-2</td>\n",
              "      <td>3-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>((1, 2), (4, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.0881</td>\n",
              "      <td>-0.2244</td>\n",
              "      <td>(1, 2)</td>\n",
              "      <td>(4, 7)</td>\n",
              "      <td>1-2</td>\n",
              "      <td>4-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>((1, 6), (2, 0))</td>\n",
              "      <td>True</td>\n",
              "      <td>False</td>\n",
              "      <td>-0.1567</td>\n",
              "      <td>0.0060</td>\n",
              "      <td>(1, 6)</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>1-6</td>\n",
              "      <td>2-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>((1, 6), (3, 0))</td>\n",
              "      <td>True</td>\n",
              "      <td>False</td>\n",
              "      <td>-0.1936</td>\n",
              "      <td>-0.0518</td>\n",
              "      <td>(1, 6)</td>\n",
              "      <td>(3, 0)</td>\n",
              "      <td>1-6</td>\n",
              "      <td>3-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>10</th>\n",
              "      <td>((1, 6), (3, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.1802</td>\n",
              "      <td>-0.0170</td>\n",
              "      <td>(1, 6)</td>\n",
              "      <td>(3, 7)</td>\n",
              "      <td>1-6</td>\n",
              "      <td>3-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>11</th>\n",
              "      <td>((1, 6), (4, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.1528</td>\n",
              "      <td>-0.1815</td>\n",
              "      <td>(1, 6)</td>\n",
              "      <td>(4, 7)</td>\n",
              "      <td>1-6</td>\n",
              "      <td>4-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>12</th>\n",
              "      <td>((2, 0), (3, 0))</td>\n",
              "      <td>False</td>\n",
              "      <td>False</td>\n",
              "      <td>-0.1252</td>\n",
              "      <td>-0.1461</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>(3, 0)</td>\n",
              "      <td>2-0</td>\n",
              "      <td>3-0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>13</th>\n",
              "      <td>((2, 0), (3, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.1249</td>\n",
              "      <td>-0.1244</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>(3, 7)</td>\n",
              "      <td>2-0</td>\n",
              "      <td>3-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>14</th>\n",
              "      <td>((2, 0), (4, 7))</td>\n",
              "      <td>False</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.1005</td>\n",
              "      <td>-0.2919</td>\n",
              "      <td>(2, 0)</td>\n",
              "      <td>(4, 7)</td>\n",
              "      <td>2-0</td>\n",
              "      <td>4-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>15</th>\n",
              "      <td>((3, 0), (4, 7))</td>\n",
              "      <td>True</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.5023</td>\n",
              "      <td>-0.6728</td>\n",
              "      <td>(3, 0)</td>\n",
              "      <td>(4, 7)</td>\n",
              "      <td>3-0</td>\n",
              "      <td>4-7</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>16</th>\n",
              "      <td>((3, 7), (4, 7))</td>\n",
              "      <td>False</td>\n",
              "      <td>True</td>\n",
              "      <td>-0.0102</td>\n",
              "      <td>-0.2021</td>\n",
              "      <td>(3, 7)</td>\n",
              "      <td>(4, 7)</td>\n",
              "      <td>3-7</td>\n",
              "      <td>4-7</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "value            sorted    X|Y    Y|X   s_X|Y   s_Y|X   first  second    X  \\\n",
              "0      ((1, 1), (2, 0))   True   True -0.2611 -0.0261  (1, 1)  (2, 0)  1-1   \n",
              "1      ((1, 1), (3, 0))   True   True -0.2367 -0.0226  (1, 1)  (3, 0)  1-1   \n",
              "2      ((1, 1), (3, 7))   True  False -0.2495 -0.0140  (1, 1)  (3, 7)  1-1   \n",
              "3      ((1, 1), (4, 7))   True   True -0.2441 -0.2005  (1, 1)  (4, 7)  1-1   \n",
              "4      ((1, 2), (2, 0))   True   True -0.0949 -0.0398  (1, 2)  (2, 0)  1-2   \n",
              "5      ((1, 2), (3, 0))   True   True -0.0920 -0.0578  (1, 2)  (3, 0)  1-2   \n",
              "6      ((1, 2), (3, 7))   True  False -0.0542  0.0014  (1, 2)  (3, 7)  1-2   \n",
              "7      ((1, 2), (4, 7))   True   True -0.0881 -0.2244  (1, 2)  (4, 7)  1-2   \n",
              "8      ((1, 6), (2, 0))   True  False -0.1567  0.0060  (1, 6)  (2, 0)  1-6   \n",
              "9      ((1, 6), (3, 0))   True  False -0.1936 -0.0518  (1, 6)  (3, 0)  1-6   \n",
              "10     ((1, 6), (3, 7))   True   True -0.1802 -0.0170  (1, 6)  (3, 7)  1-6   \n",
              "11     ((1, 6), (4, 7))   True   True -0.1528 -0.1815  (1, 6)  (4, 7)  1-6   \n",
              "12     ((2, 0), (3, 0))  False  False -0.1252 -0.1461  (2, 0)  (3, 0)  2-0   \n",
              "13     ((2, 0), (3, 7))   True   True -0.1249 -0.1244  (2, 0)  (3, 7)  2-0   \n",
              "14     ((2, 0), (4, 7))  False   True -0.1005 -0.2919  (2, 0)  (4, 7)  2-0   \n",
              "15     ((3, 0), (4, 7))   True   True -0.5023 -0.6728  (3, 0)  (4, 7)  3-0   \n",
              "16     ((3, 7), (4, 7))  False   True -0.0102 -0.2021  (3, 7)  (4, 7)  3-7   \n",
              "\n",
              "value    Y  \n",
              "0      2-0  \n",
              "1      3-0  \n",
              "2      3-7  \n",
              "3      4-7  \n",
              "4      2-0  \n",
              "5      3-0  \n",
              "6      3-7  \n",
              "7      4-7  \n",
              "8      2-0  \n",
              "9      3-0  \n",
              "10     3-7  \n",
              "11     4-7  \n",
              "12     3-0  \n",
              "13     3-7  \n",
              "14     4-7  \n",
              "15     4-7  \n",
              "16     4-7  "
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 85,
      "metadata": {
        "scrolled": false
      }
    },
    {
      "cell_type": "code",
      "source": [
        "tw_cond_imp_df[['X', 'Y', 'X|Y', 'Y|X', 's_X|Y', 's_Y|X']]"
      ],
      "outputs": [],
      "execution_count": null,
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "tw_cond_imp_df.to_csv('../results/double_lesion_FASHION_importance_pairs_net1_k8.csv')"
      ],
      "outputs": [],
      "execution_count": 87,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "This a count for every type:"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "tw_cond_imp_df.groupby(['X|Y', 'Y|X']).size()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 80,
          "data": {
            "text/plain": [
              "X|Y    Y|X  \n",
              "False  False     1\n",
              "       True      2\n",
              "True   False     4\n",
              "       True     10\n",
              "dtype: int64"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 80,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "Let's see how $s_{i|j}$ is influnced by the type:"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "tw_cond_imp_type = (tw_cond_imp_df['X|Y'].astype(str) + '-' + tw_cond_imp_df['Y|X'].astype(str)).values\n",
        "\n",
        "ax = sns.scatterplot(tw_cond_imp_df['s_X|Y']*100,\n",
        "                     tw_cond_imp_df['s_Y|X']*100,\n",
        "                     hue=tw_cond_imp_type,\n",
        "                     s=100)\n",
        "\n",
        "sij_min, sij_max = double_conditional_df['s_i|j'].min() * 100 - 5, double_conditional_df['s_i|j'].max() * 110\n",
        "\nax.axis([sij_min, sij_max, sij_min, sij_max]);"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 20216 characters]\n"
            ],
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 81,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "Let's take only Type I, II and II: There is at least one False for X|Y and Y|X:"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "from src.lesion.output import draw_tw_cond_dependency_graph\n",
        "\n",
        "if tw_cond_imp_df.shape[0] > 0:\n",
        "    draw_tw_cond_dependency_graph(tw_cond_imp_df, single_df);"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 20420 characters]\n"
            ],
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 86,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### For comparison, let's see the \"dependency\" graph by the strongest weight (absolute sum) path between two layers"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "one_way_weighted_dist_mat = build_weighted_dist_mat(model_path, one_way_clustering_result)\n",
        "\ndraw_ow_weight_dependency_graph(one_way_weighted_dist_mat);"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 20240 characters]\n"
            ],
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 35,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Although the two graphs have shared edges, they are not the same, and they capture different stuff."
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "plot_weighted_dist_mat(one_way_weighted_dist_mat);"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": [
              "[encoded data, 27076 characters]\n"
            ],
            "text/plain": [
              "<Figure size 936x720 with 2 Axes>"
            ]
          },
          "metadata": {}
        }
      ],
      "execution_count": 36,
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### IGNORE ME - IT IS MORE COMPLEX THAN IT SEEMS\n",
        "### Validation: Transitivity\n",
        "\n",
        "We can try to valdidate our dependency graph, by checking whether it is transitive:\n",
        "\n",
        "#### If\n",
        "$$\n",
        "X \\rightarrow Z \\rightarrow Y\n",
        "$$\n",
        "\n",
        "### Then\n",
        "\n",
        "$$\n",
        "X \\rightarrow Y\n",
        "$$"
      ],
      "metadata": {}
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Store Data"
      ],
      "metadata": {
        "papermill": {
          "duration": 0.144045,
          "end_time": "2019-11-22T19:49:16.470397",
          "exception": false,
          "start_time": "2019-11-22T19:49:16.326352",
          "status": "completed"
        },
        "tags": []
      }
    },
    {
      "cell_type": "code",
      "source": [
        "if MODE == 'STORE':\n",
        "    with open(PICKLE_PATH, 'wb') as f:\n",
        "        pickle.dump((N_CLUSTERS, single_df,\n",
        "                     TWO_WAY_N_SHUFFLES, TWO_WAY_PVALUE_THRESHOLD,\n",
        "                     double_joint_df, joint_metadata,\n",
        "                     double_conditional_df, conditional_metadata),\n",
        "                   f)"
      ],
      "outputs": [],
      "execution_count": 50,
      "metadata": {
        "papermill": {
          "duration": 0.212036,
          "end_time": "2019-11-22T19:49:16.826485",
          "exception": false,
          "start_time": "2019-11-22T19:49:16.614449",
          "status": "completed"
        },
        "tags": []
      }
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python",
      "version": "3.8.3",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "papermill": {
      "output_path": "./notebooks/mlp-double-lesion-test.ipynb",
      "start_time": "2019-11-22T16:04:57.819234",
      "parameters": {},
      "environment_variables": {},
      "exception": null,
      "end_time": "2019-11-22T19:49:20.536312",
      "duration": 13462.717078,
      "version": "1.1.0",
      "input_path": "./notebooks/mlp-double-lesion-test.ipynb"
    },
    "pycharm": {
      "stem_cell": {
        "cell_type": "raw",
        "metadata": {
          "collapsed": false
        },
        "source": []
      }
    },
    "kernel_info": {
      "name": "python3"
    },
    "nteract": {
      "version": "0.12.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}