{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import preprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "cifar10_test_acc = []\n",
    "cifar10_flops = []\n",
    "cifar10_paras = []\n",
    "\n",
    "cifar100_test_acc = []\n",
    "cifar100_flops = []\n",
    "cifar100_paras = []\n",
    "\n",
    "ImageNet16_120_test_acc = []\n",
    "ImageNet16_120_flops = []\n",
    "ImageNet16_120_paras = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('all_model.txt') as f:\n",
    "    for line in f:\n",
    "        line = line.rstrip()\n",
    "        # print(line[0:19])\n",
    "        if line[0:20] == 'cifar10        FLOP=':\n",
    "            cifar10_flops.append(float(line[20:25]))\n",
    "            cifar10_paras.append(float(line[37:41]))\n",
    "        elif line[0:20] == 'cifar10        train':\n",
    "            cifar10_test_acc.append(float(line[-7:-2]))\n",
    "        \n",
    "        elif line[0:20] == 'cifar100       FLOP=':\n",
    "            cifar100_flops.append(float(line[20:25]))\n",
    "            cifar100_paras.append(float(line[37:41]))\n",
    "        elif line[0:20] == 'cifar100       train':\n",
    "            cifar100_test_acc.append(float(line[-7:-2]))\n",
    "\n",
    "        elif line[0:20] == 'ImageNet16-120 FLOP=':\n",
    "            ImageNet16_120_flops.append(float(line[21:25]))\n",
    "            ImageNet16_120_paras.append(float(line[37:41]))\n",
    "        elif line[0:20] == 'ImageNet16-120 train':\n",
    "            ImageNet16_120_test_acc.append(float(line[-7:-2]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15625"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(cifar100_flops)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15625"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ImageNet16_120_test_acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 27944 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(cifar10_flops, cifar10_test_acc, 'g.')\n",
    "plt.xlabel('FLOPs')\n",
    "plt.ylabel('CIFAR-10_TEST_ACC')\n",
    "plt.title('CIFAR-10 TOP1')\n",
    "plt.plot(121.82, 93.99, 'r*')\n",
    "plt.text(121.82, 93.99,'λ = 0.25')\n",
    "plt.plot(58.9, 92.31, 'r*')\n",
    "plt.text(58.9, 92.31,'λ = 0.5')\n",
    "plt.plot(19.58, 90.21, 'r*')\n",
    "plt.text(19.58, 90.21,'λ = 0.75')\n",
    "plt.grid(True)\n",
    "plt.ylim([85, 95])\n",
    "plt.savefig(\"myimg_1.svg\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "cifar10_flops_normalized = preprocessing.normalize(np.array(cifar10_flops).reshape(1, -1))\n",
    "cifar100_flops_normalized = preprocessing.normalize(np.array(cifar100_flops).reshape(1, -1))\n",
    "ImageNet16_120_flops_normalized = preprocessing.normalize(np.array(ImageNet16_120_flops).reshape(1, -1))\n",
    "Normlized_overall_flops = np.add(cifar10_flops_normalized, cifar100_flops_normalized, ImageNet16_120_flops_normalized)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 15625)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Normlized_overall_flops.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "cifar10_test_acc_normalized = preprocessing.normalize(np.array(cifar10_test_acc).reshape(1, -1))\n",
    "cifar100_test_acc_normalized = preprocessing.normalize(np.array(cifar100_test_acc).reshape(1, -1))\n",
    "ImageNet16_120_test_acc_normalized = preprocessing.normalize(np.array(ImageNet16_120_test_acc).reshape(1, -1))\n",
    "Normlized_overall_test_acc = np.add(cifar10_test_acc_normalized, cifar100_test_acc_normalized, ImageNet16_120_test_acc_normalized)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36568 characters]",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(Normlized_overall_flops, Normlized_overall_test_acc, 'g.')\n",
    "plt.xlabel('Normlized_overall_flops')\n",
    "plt.ylabel('Normlized_overall_test_acc')\n",
    "plt.title('Normlized Model Evaluation')\n",
    "plt.plot(Normlized_overall_flops[:,857], Normlized_overall_test_acc[:,857], 'r*')\n",
    "plt.text(Normlized_overall_flops[:,857], Normlized_overall_test_acc[:,857],'λ = 0/0.25')\n",
    "\n",
    "plt.plot(Normlized_overall_flops[:,2564], Normlized_overall_test_acc[:,2564], 'r*')\n",
    "plt.text(Normlized_overall_flops[:,2564], Normlized_overall_test_acc[:,2564],'λ = 0.5')\n",
    "\n",
    "plt.plot(Normlized_overall_flops[:,485], Normlized_overall_test_acc[:,485], 'r*')\n",
    "plt.text(Normlized_overall_flops[:,485], Normlized_overall_test_acc[:,485],'λ = 0.75')\n",
    "plt.grid(True)\n",
    "plt.ylim([0.0155, 0.01825])\n",
    "plt.savefig(\"Normlized_Model_Evaluation_zoom_in.svg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 222864 characters]",
      "text/plain": [
       "<Figure size 1800x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# Define row and column labels\n",
    "row_labels = ['plain', 'grasp', 'fisher', 'epe_nas', 'grad_norm',\n",
    "              'snip', 'synflow', 'l2_norm', 'params', 'zen',\n",
    "              'jacov', 'flops', 'nwot', 'TGNAS']\n",
    "\n",
    "col_labels = [\"TNB101_MICRO-AUTOENC\", \"TNB101_MICRO-ROOM\", \"TNB101_MICRO-JIGSAW\",\n",
    "              \"TNB101_MICRO-NORMAL\", \"TNB101_MICRO-OBJECT\", \"TNB101_MICRO-SEGMENT\",\n",
    "              \"TNB101_MICRO-SCENE\", \"NB201-IMGNT\", \"NB201-CF10\", \"NB201-CF100\", \"NB301-CF10\"]\n",
    "\n",
    "# Fill in values manually (each sublist is a row, each number is a column)\n",
    "data = [\n",
    "    [0.07, 0.36, 0.35, 0.03, 0.34, -0.02, 0.24, -0.22,  -0.26, -0.21, -0.32],  # plain\n",
    "    [-0.12, -0.29, -0.12, 0.01, -0.22, 0.00, -0.27,0.55,  0.51, 0.54,  0.34],  # grasp\n",
    "    [-0.58, 0.30, 0.30, 0.16, 0.44, 0.12, 0.66, 0.48, 0.50,  0.54,  -0.28],  # fisher\n",
    "    [0.00, 0.40, 0.16,  0.00,  0.39,  0.00, 0.51, 0.33,  0.70,  0.60,  0.00],   # epe_nas\n",
    "    [-0.32, 0.25, 0.36, 0.36, 0.39,  0.60, 0.65, 0.57,  0.58,  0.63,  -0.04],  # grad_norm\n",
    "    [-0.27, 0.32, 0.41, 0.49,  0.45, 0.68, 0.70,  0.57,  0.58,  0.63,  -0.05],  # snip\n",
    "    [0.00,  0.30, 0.47,  0.00,  0.48,  0.00,  0.72,  0.75,  0.73,  0.76,  0.18],   # synflow\n",
    "    [0.04,  0.18, 0.35,  0.50,  0.32, 0.48,  0.53,  0.69,  0.68,  0.72,  0.45],   # l2_norm\n",
    "    [-0.01,  0.30, 0.44,  0.62,  0.45, 0.68,  0.64,  0.69,  0.72,  0.73,  0.46],  # params\n",
    "    [0.14,  0.38, 0.51,  0.71,  0.54, 0.67,  0.72,  0.39,  0.35,  0.35,  0.43],   # zen\n",
    "    [0.18,  0.40,  0.56, 0.75, 0.51,  0.80,  0.75, 0.71,  0.75,  0.71,  -0.04],   # jacov\n",
    "    [-0.02,  0.30,  0.45,  0.64,  0.46,0.69,  0.65,  0.67,  0.69,  0.71,  0.42],  # flops\n",
    "    [0.03,  0.25,  0.42,  0.57,  0.39,0.53, 0.60, 0.77, 0.77, 0.80, 0.47],   # nwot\n",
    "    [0.4193, 0.2630, 0.3915, 0.5225, 0.4376, 0.5906, 0.5105, 0.6895, 0.7953, 0.7068, 0.6556]  # TGNAS\n",
    "]\n",
    "\n",
    "# Create the DataFrame\n",
    "df = pd.DataFrame(data, index=row_labels, columns=col_labels)\n",
    "\n",
    "# Plot the heatmap\n",
    "plt.figure(figsize=(18, 6))\n",
    "sns.heatmap(df, annot=True, fmt=\".2f\", cmap=\"viridis_r\", linewidths=0.5, cbar=True)\n",
    "plt.title(\"Spearman rank correlations between ZC proxy values and validation accuracies\", fontsize=16)\n",
    "plt.xticks(rotation=45, ha='right')\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 242528 characters]",
      "text/plain": [
       "<Figure size 2000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Spearman_Correlations_Heatmap_normalized.pdf'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# Define row and column labels\n",
    "row_labels = ['plain', 'grasp', 'fisher', 'epe_nas', 'grad_norm',\n",
    "              'snip', 'synflow', 'l2_norm', 'params', 'zen',\n",
    "              'jacov', 'flops', 'nwot', 'TGNAS']\n",
    "\n",
    "col_labels = [\"TNB101_MICRO-AUTOENC\", \"TNB101_MICRO-ROOM\", \"TNB101_MICRO-JIGSAW\",\n",
    "              \"TNB101_MICRO-NORMAL\", \"TNB101_MICRO-OBJECT\", \"TNB101_MICRO-SEGMENT\",\n",
    "              \"TNB101_MICRO-SCENE\", \"NB201-IMGNT\", \"NB201-CF10\", \"NB201-CF100\", \"NB301-CF10\"]\n",
    "\n",
    "# Data values\n",
    "data = [\n",
    "    [0.07, 0.36, 0.35, 0.03, 0.34, -0.02, 0.24, -0.22,  -0.26, -0.21, -0.32],  # plain\n",
    "    [-0.12, -0.29, -0.12, 0.01, -0.22, 0.00, -0.27,0.55,  0.51, 0.54,  0.34],  # grasp\n",
    "    [-0.58, 0.30, 0.30, 0.16, 0.44, 0.12, 0.66, 0.48, 0.50,  0.54,  -0.28],  # fisher\n",
    "    [0.00, 0.40, 0.16,  0.00,  0.39,  0.00, 0.51, 0.33,  0.70,  0.60,  0.00],   # epe_nas\n",
    "    [-0.32, 0.25, 0.36, 0.36, 0.39,  0.60, 0.65, 0.57,  0.58,  0.63,  -0.04],  # grad_norm\n",
    "    [-0.27, 0.32, 0.41, 0.49,  0.45, 0.68, 0.70,  0.57,  0.58,  0.63,  -0.05],  # snip\n",
    "    [0.00,  0.30, 0.47,  0.00,  0.48,  0.00,  0.72,  0.75,  0.73,  0.76,  0.18],   # synflow\n",
    "    [0.04,  0.18, 0.35,  0.50,  0.32, 0.48,  0.53,  0.69,  0.68,  0.72,  0.45],   # l2_norm\n",
    "    [-0.01,  0.30, 0.44,  0.62,  0.45, 0.68,  0.64,  0.69,  0.72,  0.73,  0.46],  # params\n",
    "    [0.14,  0.38, 0.51,  0.71,  0.54, 0.67,  0.72,  0.39,  0.35,  0.35,  0.43],   # zen\n",
    "    [0.18,  0.40,  0.56, 0.75, 0.51,  0.80,  0.75, 0.71,  0.75,  0.71,  -0.04],   # jacov\n",
    "    [-0.02,  0.30,  0.45,  0.64,  0.46,0.69,  0.65,  0.67,  0.69,  0.71,  0.42],  # flops\n",
    "    [0.03,  0.25,  0.42,  0.57,  0.39,0.53, 0.60, 0.77, 0.77, 0.80, 0.47],   # nwot\n",
    "    [0.4193, 0.2630, 0.3915, 0.5225, 0.4376, 0.5906, 0.5105, 0.6895, 0.7953, 0.7068, 0.6556]  # TGNAS\n",
    "]\n",
    "\n",
    "# DataFrame creation\n",
    "df = pd.DataFrame(data, index=row_labels, columns=col_labels)\n",
    "\n",
    "# Compute average correlations\n",
    "df['Avg'] = df.mean(axis=1)\n",
    "\n",
    "# Compute raw difference vs FLOPS\n",
    "flops_avg = df.loc['flops', 'Avg']\n",
    "df['Diff vs FLOPS'] = df['Avg'] - flops_avg\n",
    "\n",
    "# Prepare heatmap data (excluding Avg column)\n",
    "heatmap_df = df[col_labels + ['Diff vs FLOPS']]\n",
    "\n",
    "annot_data = heatmap_df.round(2).astype(str)\n",
    "tgnas_idx = 'TGNAS'\n",
    "annot_data.loc[tgnas_idx] = annot_data.loc[tgnas_idx].apply(lambda x: f\"$\\\\bf{{{x}}}$\")\n",
    "\n",
    "# Plot heatmap\n",
    "plt.figure(figsize=(20, 6))\n",
    "sns.heatmap(heatmap_df, annot=True, fmt=\".2f\", cmap=\"viridis_r\", linewidths=0.5, cbar=True)\n",
    "plt.title(\"Spearman rank correlations between ZC proxy values and validation accuracies\", fontsize=16)\n",
    "plt.xticks(rotation=45, ha='right')\n",
    "plt.tight_layout()\n",
    "\n",
    "\n",
    "output_path = \"Spearman_Correlations_Heatmap_normalized.pdf\"\n",
    "plt.savefig(output_path, format='pdf')\n",
    "plt.show()\n",
    "plt.close()\n",
    "\n",
    "output_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.07' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.36' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.35' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.03' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.34' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '-0.02' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.24' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '-0.22' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '-0.26' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '-0.21' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:49: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '-0.32' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
      "/tmp/ipykernel_1402451/3019593296.py:47: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '62%' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.\n",
      "  annot_data.loc[row, col] = f\"{val:.0f}%\"\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 280180 characters]",
      "text/plain": [
       "<Figure size 2200x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Spearman_Correlations_with_Relative_Avg.pdf'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# Data setup (unchanged)\n",
    "row_labels = ['plain', 'grasp', 'fisher', 'epe_nas', 'grad_norm',\n",
    "              'snip', 'synflow', 'l2_norm', 'params', 'zen',\n",
    "              'jacov', 'flops', 'nwot', 'TG-NAS']\n",
    "\n",
    "col_labels = [\"TNB101_MICRO-AUTOENC\", \"TNB101_MICRO-ROOM\", \"TNB101_MICRO-JIGSAW\",\n",
    "              \"TNB101_MICRO-NORMAL\", \"TNB101_MICRO-OBJECT\", \"TNB101_MICRO-SEGMENT\",\n",
    "              \"TNB101_MICRO-SCENE\", \"NB201-IMGNT\", \"NB201-CF10\", \"NB201-CF100\", \"NB301-CF10\"]\n",
    "\n",
    "data = [\n",
    "    [0.07, 0.36, 0.35, 0.03, 0.34, -0.02, 0.24, -0.22,  -0.26, -0.21, -0.32],\n",
    "    [-0.12, -0.29, -0.12, 0.01, -0.22, 0.00, -0.27, 0.55, 0.51, 0.54, 0.34],\n",
    "    [-0.58, 0.30, 0.30, 0.16, 0.44, 0.12, 0.66, 0.48, 0.50, 0.54, -0.28],\n",
    "    [0.00, 0.40, 0.16, 0.00, 0.39, 0.00, 0.51, 0.33, 0.70, 0.60, 0.00],\n",
    "    [-0.32, 0.25, 0.36, 0.36, 0.39, 0.60, 0.65, 0.57, 0.58, 0.63, -0.04],\n",
    "    [-0.27, 0.32, 0.41, 0.49, 0.45, 0.68, 0.70, 0.57, 0.58, 0.63, -0.05],\n",
    "    [0.00, 0.30, 0.47, 0.00, 0.48, 0.00, 0.72, 0.75, 0.73, 0.76, 0.18],\n",
    "    [0.04, 0.18, 0.35, 0.50, 0.32, 0.48, 0.53, 0.69, 0.68, 0.72, 0.45],\n",
    "    [-0.01, 0.30, 0.44, 0.62, 0.45, 0.68, 0.64, 0.69, 0.72, 0.73, 0.46],\n",
    "    [0.14, 0.38, 0.51, 0.71, 0.54, 0.67, 0.72, 0.39, 0.35, 0.35, 0.43],\n",
    "    [0.18, 0.40, 0.56, 0.75, 0.51, 0.80, 0.75, 0.71, 0.75, 0.71, -0.04],\n",
    "    [-0.02, 0.30, 0.45, 0.64, 0.46, 0.69, 0.65, 0.67, 0.69, 0.71, 0.42],\n",
    "    [0.03, 0.25, 0.42, 0.57, 0.39, 0.53, 0.60, 0.77, 0.77, 0.80, 0.47],\n",
    "    [0.4193, 0.2630, 0.3915, 0.5225, 0.4376, 0.5906, 0.5105, 0.6895, 0.7953, 0.7068, 0.6556]\n",
    "]\n",
    "\n",
    "# DataFrame creation\n",
    "df = pd.DataFrame(data, index=row_labels, columns=col_labels)\n",
    "\n",
    "# Compute stabilized relative differences and relative avg\n",
    "rel_diff_df = (df[col_labels] - df.loc['flops'][col_labels]) / abs(df.loc['flops'][col_labels])\n",
    "df['Relative_Avg'] = rel_diff_df.mean(axis=1)\n",
    "\n",
    "# Prepare heatmap data\n",
    "heatmap_df = df[col_labels + ['Relative_Avg']]\n",
    "\n",
    "# Format annotations: raw for benchmarks, percentage only for Relative_Avg\n",
    "annot_data = heatmap_df.copy()\n",
    "for row in annot_data.index:\n",
    "    for col in annot_data.columns:\n",
    "        if col == 'Relative_Avg':\n",
    "            val = (annot_data.loc[row, col] + 1) * 100  # convert to %\n",
    "            annot_data.loc[row, col] = f\"{val:.0f}%\"\n",
    "        else:\n",
    "            annot_data.loc[row, col] = f\"{annot_data.loc[row, col]:.2f}\"\n",
    "\n",
    "# Plot heatmap\n",
    "plt.figure(figsize=(22, 6))\n",
    "sns.heatmap(heatmap_df, annot=annot_data, fmt='', cmap=\"viridis_r\", linewidths=0.5,\n",
    "            cbar=True, annot_kws={'size': 13})\n",
    "plt.title(\"Spearman rank correlations between ZC proxy values and validation accuracies\", fontsize=16)\n",
    "plt.xticks(rotation=45, ha='right', fontsize=10)\n",
    "\n",
    "# Bold TG-NAS ytick\n",
    "yticks = plt.gca().get_yticklabels()\n",
    "for tick in yticks:\n",
    "    if tick.get_text() == 'TG-NAS':\n",
    "        tick.set_fontweight('bold')\n",
    "    tick.set_fontsize(13)\n",
    "\n",
    "plt.tight_layout()\n",
    "\n",
    "# Save and show\n",
    "output_path = \"Spearman_Correlations_with_Relative_Avg.pdf\"\n",
    "plt.savefig(output_path, format='pdf')\n",
    "plt.show()\n",
    "plt.close()\n",
    "\n",
    "output_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_1402451/810376878.py:21: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.\n",
      "  annot_data = df.applymap(lambda x: f\"{x:.2f}\")\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 55332 characters]",
      "text/plain": [
       "<Figure size 400x400 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'TGNAS_4x4_Heatmap_Bold.pdf'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# Data setup (unchanged)\n",
    "row_labels = ['grasp', 'fisher', 'epe_nas', 'flops', 'TG-NAS']\n",
    "\n",
    "col_labels = [\"AUTOENC\", \"NB201-IMGNT\",  \"NB201-CF10\", \"NB301-CF10\"]\n",
    "\n",
    "data = [\n",
    "    [-0.12, 0.55, 0.51, 0.34],  # grasp\n",
    "    [-0.58, 0.48, 0.50, -0.28],  # fisher\n",
    "    [0.00, 0.33, 0.70, 0.00],   # epe_nas\n",
    "    [-0.02, 0.67, 0.69, 0.42],  # flops\n",
    "    [0.4193, 0.6895, 0.7953, 0.6556]  # TGNAS\n",
    "    ]\n",
    "\n",
    "df = pd.DataFrame(data, index=row_labels, columns=col_labels)\n",
    "\n",
    "# Format annotations: 2 decimals + bold TGNAS\n",
    "annot_data = df.applymap(lambda x: f\"{x:.2f}\")\n",
    "annot_data.loc['TG-NAS'] = annot_data.loc['TG-NAS'].apply(lambda x: f\"$\\\\bf{{{x}}}$\")\n",
    "\n",
    "# Plot heatmap\n",
    "plt.figure(figsize=(4, 4))\n",
    "sns.heatmap(df, annot=annot_data, fmt='', cmap=\"viridis_r\", linewidths=0.5, cbar=True, annot_kws={'size': 12})\n",
    "plt.title(\"TGNAS vs Proxies on 4 Datasets\", fontsize=12)\n",
    "plt.xticks(rotation=45, ha='right', fontsize=10)  # x-axis tick font size\n",
    "# plt.yticks(fontsize=10)\n",
    "yticks = plt.gca().get_yticklabels()\n",
    "for tick in yticks:\n",
    "    if tick.get_text() == 'TG-NAS':\n",
    "        tick.set_fontweight('bold')\n",
    "    tick.set_fontsize(10)\n",
    "plt.tight_layout()\n",
    "\n",
    "# Save and show\n",
    "output_path = \"TGNAS_4x4_Heatmap_Bold.pdf\"\n",
    "plt.savefig(output_path, format='pdf')\n",
    "plt.show()\n",
    "plt.close()\n",
    "\n",
    "output_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 263564 characters]",
      "text/plain": [
       "<Figure size 1400x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output saved to Spearman_Correlations_with_Relative_Avg_bolded_row_fixed.pdf\n",
      "\n",
      "Formatted and Bolded 'TG-NAS' row for annotation (corrected):\n",
      "TNB101_MICRO-AUTOENC     $\\bf{0.42}$\n",
      "TNB101_MICRO-ROOM        $\\bf{0.26}$\n",
      "TNB101_MICRO-JIGSAW      $\\bf{0.39}$\n",
      "TNB101_MICRO-NORMAL      $\\bf{0.52}$\n",
      "TNB101_MICRO-OBJECT      $\\bf{0.44}$\n",
      "TNB101_MICRO-SEGMENT     $\\bf{0.59}$\n",
      "TNB101_MICRO-SCENE       $\\bf{0.51}$\n",
      "NB201-IMGNT              $\\bf{0.69}$\n",
      "NB201-CF10               $\\bf{0.80}$\n",
      "NB201-CF100              $\\bf{0.71}$\n",
      "NB301-CF10               $\\bf{0.66}$\n",
      "Relative_Avg            $\\bf{299\\%}$\n",
      "Name: TG-NAS, dtype: object\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# Data setup (unchanged)\n",
    "row_labels = ['plain', 'grasp', 'fisher', 'epe_nas', 'grad_norm',\n",
    "              'snip', 'synflow', 'l2_norm', 'params', 'zen',\n",
    "              'jacov', 'flops', 'nwot', 'TG-NAS']\n",
    "\n",
    "col_labels = [\"TNB101_MICRO-AUTOENC\", \"TNB101_MICRO-ROOM\", \"TNB101_MICRO-JIGSAW\",\n",
    "              \"TNB101_MICRO-NORMAL\", \"TNB101_MICRO-OBJECT\", \"TNB101_MICRO-SEGMENT\",\n",
    "              \"TNB101_MICRO-SCENE\", \"NB201-IMGNT\", \"NB201-CF10\", \"NB201-CF100\", \"NB301-CF10\"]\n",
    "\n",
    "data = [\n",
    "    [0.07, 0.36, 0.35, 0.03, 0.34, -0.02, 0.24, -0.22,  -0.26, -0.21, -0.32],\n",
    "    [-0.12, -0.29, -0.12, 0.01, -0.22, 0.00, -0.27, 0.55, 0.51, 0.54, 0.34],\n",
    "    [-0.58, 0.30, 0.30, 0.16, 0.44, 0.12, 0.66, 0.48, 0.50, 0.54, -0.28],\n",
    "    [0.00, 0.40, 0.16, 0.00, 0.39, 0.00, 0.51, 0.33, 0.70, 0.60, 0.00],\n",
    "    [-0.32, 0.25, 0.36, 0.36, 0.39, 0.60, 0.65, 0.57, 0.58, 0.63, -0.04],\n",
    "    [-0.27, 0.32, 0.41, 0.49, 0.45, 0.68, 0.70, 0.57, 0.58, 0.63, -0.05],\n",
    "    [0.00, 0.30, 0.47, 0.00, 0.48, 0.00, 0.72, 0.75, 0.73, 0.76, 0.18],\n",
    "    [0.04, 0.18, 0.35, 0.50, 0.32, 0.48, 0.53, 0.69, 0.68, 0.72, 0.45],\n",
    "    [-0.01, 0.30, 0.44, 0.62, 0.45, 0.68, 0.64, 0.69, 0.72, 0.73, 0.46],\n",
    "    [0.14, 0.38, 0.51, 0.71, 0.54, 0.67, 0.72, 0.39, 0.35, 0.35, 0.43],\n",
    "    [0.18, 0.40, 0.56, 0.75, 0.51, 0.80, 0.75, 0.71, 0.75, 0.71, -0.04],\n",
    "    [-0.02, 0.30, 0.45, 0.64, 0.46, 0.69, 0.65, 0.67, 0.69, 0.71, 0.42], # flops\n",
    "    [0.03, 0.25, 0.42, 0.57, 0.39, 0.53, 0.60, 0.77, 0.77, 0.80, 0.47],\n",
    "    [0.4193, 0.2630, 0.3915, 0.5225, 0.4376, 0.5906, 0.5105, 0.6895, 0.7953, 0.7068, 0.6556] # TG-NAS\n",
    "]\n",
    "\n",
    "# DataFrame creation\n",
    "df = pd.DataFrame(data, index=row_labels, columns=col_labels)\n",
    "\n",
    "# Compute stabilized relative differences and relative avg\n",
    "rel_diff_df = (df[col_labels] - df.loc['flops'][col_labels]) / abs(df.loc['flops'][col_labels])\n",
    "df['Relative_Avg'] = rel_diff_df.mean(axis=1)\n",
    "\n",
    "# Prepare heatmap data (this is the numeric data that determines cell colors)\n",
    "heatmap_df = df[col_labels + ['Relative_Avg']]\n",
    "\n",
    "# --- ANNOTATION FORMATTING ---\n",
    "# Start with annot_data as a copy of numeric heatmap_df to easily access values for formatting.\n",
    "# We will convert these to strings.\n",
    "annot_data_strings = pd.DataFrame(index=heatmap_df.index, columns=heatmap_df.columns, dtype=object)\n",
    "\n",
    "for r_label in heatmap_df.index:\n",
    "    for c_label in heatmap_df.columns:\n",
    "        numeric_value = heatmap_df.loc[r_label, c_label]\n",
    "        if c_label == 'Relative_Avg':\n",
    "            # Add 1 and convert to percentage string for 'Relative_Avg' column\n",
    "            transformed_value = (numeric_value + 1) * 100\n",
    "            annot_data_strings.loc[r_label, c_label] = f\"{transformed_value:.0f}%\"\n",
    "        else:\n",
    "            # Format other columns to 2 decimal places\n",
    "            annot_data_strings.loc[r_label, c_label] = f\"{numeric_value:.2f}\"\n",
    "\n",
    "# Now, bold all annotations in the 'TG-NAS' row (last row)\n",
    "# This operates on the DataFrame that already contains formatted strings.\n",
    "if 'TG-NAS' in annot_data_strings.index:\n",
    "    annot_data_strings.loc['TG-NAS'] = annot_data_strings.loc['TG-NAS'].apply(\n",
    "        lambda x_str: f\"$\\\\bf{{{x_str.replace('%', '\\\\%')}}}$\" # Corrected line\n",
    "    )\n",
    "# --- ANNOTATION FORMATTING END ---\n",
    "\n",
    "# Plot heatmap\n",
    "plt.figure(figsize=(14, 8))\n",
    "sns.heatmap(heatmap_df, annot=annot_data_strings, fmt='', cmap=\"viridis_r\", linewidths=0.5,\n",
    "            cbar=True, annot_kws={'size': 13}) # Use annot_data_strings here\n",
    "plt.title(\"Spearman rank correlations between ZC proxy values and validation accuracies\", fontsize=16)\n",
    "plt.xticks(rotation=45, ha='right', fontsize=10)\n",
    "\n",
    "# Bold TG-NAS ytick and set all ytick fontsizes\n",
    "yticks = plt.gca().get_yticklabels()\n",
    "for tick in yticks:\n",
    "    if tick.get_text() == 'TG-NAS':\n",
    "        tick.set_fontweight('bold')\n",
    "    tick.set_fontsize(13) # Set fontsize for all yticks\n",
    "\n",
    "plt.tight_layout()\n",
    "\n",
    "# Save and show\n",
    "output_path = \"Spearman_Correlations_with_Relative_Avg_bolded_row_fixed.pdf\" # Changed filename\n",
    "plt.savefig(output_path, format='pdf')\n",
    "plt.show()\n",
    "plt.close()\n",
    "\n",
    "print(f\"Output saved to {output_path}\")\n",
    "print(\"\\nFormatted and Bolded 'TG-NAS' row for annotation (corrected):\")\n",
    "if 'TG-NAS' in annot_data_strings.index:\n",
    "    print(annot_data_strings.loc['TG-NAS'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "zerocost",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
