{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "753d084e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.insert(0, '../Results')\n",
    "sys.path.insert(0, '../../src')\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import pickle\n",
    "import os\n",
    "from visualization_utils import perc, SetPlotRC, ApplyFont"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "43109ad5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# if not os.path.exists(\"Figures\"):\n",
    "#     os.mkdir(\"Figures\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51e2cc87",
   "metadata": {},
   "source": [
    "# Simplex Mixing Scenario"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8d0b191b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_simplex_results = pd.read_pickle(r\"../Results/simulation_results_simplex_mixing_distributionV1.pkl\")\n",
    "# df_simplex_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f2fe0dda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mixing_dist</th>\n",
       "      <th>CorInfoMax</th>\n",
       "      <th>LDMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>laplace</td>\n",
       "      <td>24.704708</td>\n",
       "      <td>30.817942</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>normal</td>\n",
       "      <td>25.236073</td>\n",
       "      <td>31.087195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>uniform</td>\n",
       "      <td>25.585450</td>\n",
       "      <td>31.000608</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>uniform2</td>\n",
       "      <td>25.073613</td>\n",
       "      <td>30.896235</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  mixing_dist  CorInfoMax       LDMI\n",
       "0     laplace   24.704708  30.817942\n",
       "1      normal   25.236073  31.087195\n",
       "2     uniform   25.585450  31.000608\n",
       "3    uniform2   25.073613  30.896235"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = df_simplex_results[['Model', 'mixing_dist', 'SINR']].groupby(['Model', 'mixing_dist']).agg({'mixing_dist':'size', 'SINR':'mean'}).rename(columns={'mixing_dist':'count','SINR':'SINR'}).reset_index()\n",
    "# results\n",
    "\n",
    "table = pd.DataFrame(columns = ([\"mixing_dist\"] + list(df_simplex_results['Model'].unique())))\n",
    "table['mixing_dist'] = list(results['mixing_dist'].unique())\n",
    "for m in list(table.columns[1:]):\n",
    "    table[m] = results.loc[results['Model'] == m]['SINR'].values\n",
    "    \n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "df21df27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 56292 characters]\n",
      "text/plain": [
       "<Figure size 1584x576 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "xticks_fontsize = 25\n",
    "yticks_fontsize = 25\n",
    "xlabel_fontsize = 25\n",
    "ylabel_fontisize = 30\n",
    "title_fontsize = 30\n",
    "suptitle_fontsize = 30\n",
    "\n",
    "CorInfoMaxSINRResults = []\n",
    "LDMISINRResults = []\n",
    "for dist in ['normal', \"uniform\", \"uniform2\", \"laplace\"]:\n",
    "    CorInfoMaxSINRResults.append(df_simplex_results.loc[(df_simplex_results['Model'] == \"CorInfoMax\") & (df_simplex_results['mixing_dist'] == dist)]['SINR'].values)\n",
    "    LDMISINRResults.append(df_simplex_results.loc[(df_simplex_results['Model'] == \"LDMI\") & (df_simplex_results['mixing_dist'] == dist)]['SINR'].values)\n",
    "\n",
    "fig = plt.figure(figsize = (22,8))\n",
    "plt.subplot(1,2, 1)\n",
    "plt.boxplot(CorInfoMaxSINRResults, meanline = True, showmeans = True, widths = 0.6, patch_artist = True)\n",
    "plt.xticks([1, 2, 3, 4], [r'$\\mathcal{N}(0,\\,1)$', r'$\\mathcal{U}[-1,\\,1]$', r'$\\mathcal{U}[-2,\\,2]$', r'$\\mathcal{L}(0 ,1)$'])\n",
    "plt.grid(linewidth=2)\n",
    "plt.xticks(rotation = (45),fontsize=xticks_fontsize)\n",
    "plt.yticks(fontsize=yticks_fontsize)\n",
    "# plt.ylabel('SINR (dB)',fontsize=25)\n",
    "# plt.xlabel(r\"Distribution of the Entries of Mixing Matrix \",fontsize=25)\n",
    "plt.title(\"Online CorInfoMax Results\",fontsize=title_fontsize)\n",
    "plt.axis([0.0,5,20,34])\n",
    "\n",
    "# plt.figure(figsize = (12,7))\n",
    "plt.subplot(1,2, 2)\n",
    "plt.boxplot(LDMISINRResults, meanline = True, showmeans = True, \n",
    "            widths = 0.6, patch_artist = True)\n",
    "plt.xticks([1, 2, 3, 4], [r'$\\mathcal{N}(0,\\,1)$', r'$\\mathcal{U}[-1,\\,1]$', r'$\\mathcal{U}[-2,\\,2]$', r'$\\mathcal{L}(0 ,1)$'])\n",
    "plt.grid(linewidth=2)\n",
    "plt.xticks(rotation = (45),fontsize=xticks_fontsize)\n",
    "plt.yticks(fontsize=yticks_fontsize)\n",
    "# plt.ylabel('SINR (dB)',fontsize=25)\n",
    "# plt.xlabel(r\"Distribution of the Entries of Mixing Matrix \",fontsize=25)\n",
    "plt.title(\"LD-InfoMax Results\",fontsize=title_fontsize)\n",
    "plt.axis([0.0,5,20,34])\n",
    "fig.text(0.5, -0.06, \"Distribution of the Entries of Mixing Matrix \", ha='center', va='center',fontsize=xlabel_fontsize)\n",
    "fig.text(0.09, 0.5, 'SINR (dB)', ha='center', va='center', rotation='vertical', fontsize=ylabel_fontisize)\n",
    "\n",
    "# fig.suptitle(\"Simplex Source Separation Results\", fontsize = suptitle_fontsize)\n",
    "plt.plot()\n",
    "\n",
    "# plt.savefig('Figures/CorInfoMaxSimplex_MixingDistribution.pdf', format='pdf', bbox_inches = 'tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf3411f",
   "metadata": {},
   "source": [
    "# Simplex Noisy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d07cfd87",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_simplex_results = pd.read_pickle(r\"../Results/simulation_results_simplex_noisyV1.pkl\")\n",
    "# df_simplex_results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1aca80eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SNRlevel</th>\n",
       "      <th>CorInfoMax</th>\n",
       "      <th>LDMI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>30</td>\n",
       "      <td>25.007563</td>\n",
       "      <td>30.942486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>21.353160</td>\n",
       "      <td>25.660944</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20</td>\n",
       "      <td>17.168238</td>\n",
       "      <td>21.125244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15</td>\n",
       "      <td>12.320006</td>\n",
       "      <td>16.590291</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "      <td>7.746747</td>\n",
       "      <td>12.513439</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   SNRlevel  CorInfoMax       LDMI\n",
       "4        30   25.007563  30.942486\n",
       "3        25   21.353160  25.660944\n",
       "2        20   17.168238  21.125244\n",
       "1        15   12.320006  16.590291\n",
       "0        10    7.746747  12.513439"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = df_simplex_results[['Model', 'SNRlevel', 'SINR']].groupby(['Model', 'SNRlevel']).agg({'SNRlevel':'size', 'SINR':'mean'}).rename(columns={'SNRlevel':'count','SINR':'SINR'}).reset_index()\n",
    "# results\n",
    "\n",
    "table = pd.DataFrame(columns = ([\"SNRlevel\"] + list(df_simplex_results['Model'].unique())))\n",
    "table['SNRlevel'] = list(results['SNRlevel'].unique())\n",
    "for m in list(table.columns[1:]):\n",
    "    table[m] = results.loc[results['Model'] == m]['SINR'].values\n",
    "    \n",
    "table = table.iloc[::-1]\n",
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "74cb402c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.\n",
      "findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 40492 characters]\n",
      "text/plain": [
       "<Figure size 800x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "data = np.stack(df_simplex_results.loc[(df_simplex_results['SNRlevel'] == 30) & (df_simplex_results['Model'] == \"CorInfoMax\")]['SINRlist'].apply(lambda row: np.array(row)).to_numpy(), axis = 0)\n",
    "\n",
    "# compute the medians and 25/75 percentiles\n",
    "med, perc_25, perc_75, std_data = perc(data)\n",
    "\n",
    "x = np.arange(0, med.shape[0])\n",
    "fh2=plt.figure(figsize=(10, 6), dpi=80)\n",
    "plt.plot(x, med, linewidth=4, color='#006BB2')\n",
    "plt.fill_between(x, med + std_data, med - std_data, alpha=0.25, linewidth=0, color='#006BB2')\n",
    "\n",
    "\n",
    "gh=plt.grid(linewidth=2)\n",
    "plt.xticks(fontsize=20)\n",
    "plt.yticks(fontsize=20)\n",
    "plt.ylabel('SINR (dB)',fontsize=25)\n",
    "plt.xlabel(r\"Number of Iterations / 25000\",fontsize=25)\n",
    "plt.title(\"Online CorInfoMax Simplex SINR Convergence \")\n",
    "plt.axis([0.0,20.0,-0,28])\n",
    "ApplyFont(plt.gca())\n",
    "\n",
    "# plt.savefig('Figures/CorInfoMaxSimplex_convergence30dBSNR.pdf', format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bcf2b2b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 63884 characters]\n",
      "text/plain": [
       "<Figure size 800x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_simplex_results = df_simplex_results.iloc[:df_simplex_results.shape[0] - np.mod(df_simplex_results.shape[0], 10)]\n",
    "results = df_simplex_results[['Model', 'SNRlevel', 'SINR']].groupby(['Model', 'SNRlevel']).agg({'SNRlevel':'size', 'SINR':'mean'}).rename(columns={'SNRlevel':'count','SINR':'SINR'}).reset_index()\n",
    "\n",
    "SINRCorInfoMax = (results.loc[(results['Model'] == 'CorInfoMax')]['SINR'].to_numpy())\n",
    "SINRLDMI = (results.loc[(results['Model'] == 'LDMI')]['SINR'].to_numpy())\n",
    "SNRinp = (np.array([10, 15, 20, 25, 30]))\n",
    "\n",
    "dataCorInfo = df_simplex_results.loc[(df_simplex_results['Model'] == 'CorInfoMax')]['SINR'].to_numpy().reshape(df_simplex_results.shape[0]//(10),-1)\n",
    "dataLDMI = df_simplex_results.loc[(df_simplex_results['Model'] == 'LDMI')]['SINR'].to_numpy().reshape(df_simplex_results.shape[0]//(10),-1)\n",
    "\n",
    "medCorInfo, perc_25CorInfo, perc_75CorInfo, stdCorInfoMax = perc(dataCorInfo)\n",
    "# perc_25CorInfo = np.flipud(perc_25CorInfo)\n",
    "# perc_75CorInfo = np.flipud(perc_75CorInfo)\n",
    "\n",
    "medLDMI, perc_25LDMI, perc_75LDMI, stdLDMI = perc(dataLDMI)\n",
    "# perc_25LDMI= np.flipud(perc_25LDMI)\n",
    "# perc_75LDMI = np.flipud(perc_75LDMI)\n",
    "\n",
    "fh2=plt.figure(figsize=(10, 6), dpi=80)\n",
    "\n",
    "plt.plot(SNRinp,SINRCorInfoMax,\"-o\", linewidth=4, markersize = 13, label = \"CorInfoMax\")\n",
    "plt.fill_between(SNRinp, SINRCorInfoMax + stdCorInfoMax, SINRCorInfoMax - stdCorInfoMax, alpha=0.25, linewidth=0, color='#006BB2')\n",
    "\n",
    "plt.plot(SNRinp,SINRLDMI,\"-o\", linewidth=4, markersize = 13, label = 'LD-InfoMax', color='#F39C12')\n",
    "plt.fill_between(SNRinp, SINRLDMI + stdLDMI, SINRLDMI - stdLDMI, alpha=0.25, linewidth=0, color='orange')\n",
    "\n",
    "\n",
    "gh=plt.grid(linewidth=2)\n",
    "plt.xticks(fontsize=20)\n",
    "plt.yticks(fontsize=20)\n",
    "plt.ylabel('SINR (dB)',fontsize=25)\n",
    "plt.xlabel(r\"Input SNR (dB)\",fontsize=25)\n",
    "plt.axis([9.99,30,-0,33])\n",
    "plt.legend(loc='lower left',fontsize=20)\n",
    "plt.title('Simplex Source Separation SINR Results',fontsize=20)\n",
    "ApplyFont(plt.gca())\n",
    "\n",
    "# plt.savefig('Figures/CorInfoMaxSimplex_ComparisonNoisy.pdf', format='pdf')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
