{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1e8d827f-19a7-4add-86f7-d4ac10f503ac",
   "metadata": {},
   "source": [
    "### Python packages used in this code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e02347b-b60b-4885-bd32-8292b63cd826",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import platform\n",
    "import sys\n",
    "import seaborn as sns\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2e14b54-e053-488a-9af0-2e6b1cb55900",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Environments\n",
    "\n",
    "--Platform--\n",
    "OS : Windows-10-10.0.19044-SP0\n",
    "--Version--\n",
    "python :  3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]\n",
    "numpy : 1.23.1\n",
    "pandas : 1.4.3\n",
    "\"\"\"\n",
    "\n",
    "print('--Platform--')\n",
    "print('OS :', platform.platform())\n",
    "print('--Version--')\n",
    "print('python : ', sys.version)\n",
    "print('numpy :', np.__version__)\n",
    "print('pandas :', pd.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6105045-fe35-4aef-bf20-02f9134fc011",
   "metadata": {},
   "source": [
    "## Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7307fa80-e2d7-4f5f-b2b0-69d15ae1fa7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "Results = pd.read_csv('../30_Output/30_csv/200_TransferLearning/300_TransferLearning_Result.csv', index_col=0)\n",
    "Results2 = Results.copy()[[\n",
    "    'No. source model', \n",
    "    'Itr', \n",
    "    'MSE (test, Wotl)', \n",
    "    'MSE (test, Fine)',\n",
    "    'MSE (test, Ext-1)', \n",
    "    'MSE (test, Ext-2)', \n",
    "    'MSE (test, Ext-3)', \n",
    "    'MSE (test, Inv-1)', \n",
    "    'MSE (test, Inv-2)', \n",
    "    'MSE (test, Inv-3)'\n",
    "]]\n",
    "\n",
    "Results2['RMSE (test, Wotl)'] = np.sqrt(Results2['MSE (test, Wotl)']).values\n",
    "Results2['RMSE (test, Fine)'] = np.sqrt(Results2['MSE (test, Fine)']).values\n",
    "Results2['RMSE (test, Ext-1)'] = np.sqrt(Results2['MSE (test, Ext-1)']).values\n",
    "Results2['RMSE (test, Ext-2)'] = np.sqrt(Results2['MSE (test, Ext-2)']).values\n",
    "Results2['RMSE (test, Ext-3)'] = np.sqrt(Results2['MSE (test, Ext-3)']).values\n",
    "Results2['RMSE (test, Inv-1)'] = np.sqrt(Results2['MSE (test, Inv-1)']).values\n",
    "Results2['RMSE (test, Inv-2)'] = np.sqrt(Results2['MSE (test, Inv-2)']).values\n",
    "Results2['RMSE (test, Inv-3)'] = np.sqrt(Results2['MSE (test, Inv-3)']).values\n",
    "\n",
    "Results_plt = Results2[['RMSE (test, Ext-1)','RMSE (test, Ext-2)','RMSE (test, Ext-3)','RMSE (test, Inv-1)','RMSE (test, Inv-2)','RMSE (test, Inv-3)']]\n",
    "Results2.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7c3feb3-3428-4f94-86f9-6c6df42558b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "Results2.std()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95b02a94-2a38-481a-895b-9c68e82d6d99",
   "metadata": {},
   "source": [
    "## Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03d51f15-452b-4efd-9133-2d359f48cecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dataframe for plot\n",
    "Results2_plt = Results_plt.melt()\n",
    "for i_plt in Results2_plt.index:\n",
    "    if Results2_plt.loc[[i_plt],['variable']].values=='RMSE (test, Ext-1)':\n",
    "        Results2_plt.loc[[i_plt],['Layer for plot']] = 0.97\n",
    "        Results2_plt.loc[[i_plt],['Type']] = 'Feature extractor'\n",
    "    if Results2_plt.loc[[i_plt],['variable']].values=='RMSE (test, Ext-2)':\n",
    "        Results2_plt.loc[[i_plt],['Layer for plot']] = 1.97\n",
    "        Results2_plt.loc[[i_plt],['Type']] = 'Feature extractor'\n",
    "    if Results2_plt.loc[[i_plt],['variable']].values=='RMSE (test, Ext-3)':\n",
    "        Results2_plt.loc[[i_plt],['Layer for plot']] = 2.97\n",
    "        Results2_plt.loc[[i_plt],['Type']] = 'Feature extractor'\n",
    "    if Results2_plt.loc[[i_plt],['variable']].values=='RMSE (test, Inv-1)':\n",
    "        Results2_plt.loc[[i_plt],['Layer for plot']] = 1.03\n",
    "        Results2_plt.loc[[i_plt],['Type']] = 'Affine transfer'\n",
    "    if Results2_plt.loc[[i_plt],['variable']].values=='RMSE (test, Inv-2)':\n",
    "        Results2_plt.loc[[i_plt],['Layer for plot']] = 2.03\n",
    "        Results2_plt.loc[[i_plt],['Type']] = 'Affine transfer'\n",
    "    if Results2_plt.loc[[i_plt],['variable']].values=='RMSE (test, Inv-3)':\n",
    "        Results2_plt.loc[[i_plt],['Layer for plot']] = 3.03\n",
    "        Results2_plt.loc[[i_plt],['Type']] = 'Affine transfer'\n",
    "        \n",
    "Results2_plt2 = Results2_plt.groupby(['variable', 'Type']).mean()\n",
    "Results2_plt2 = Results2_plt.groupby(['variable']).mean()\n",
    "Results2_plt2['Type'] = [\n",
    "    'RMSE (test, Ext-1)',\n",
    "    'RMSE (test, Ext-2)',\n",
    "    'RMSE (test, Ext-3)',\n",
    "    'RMSE (test, Inv-1)',\n",
    "    'RMSE (test, Inv-2)',\n",
    "    'RMSE (test, Inv-3)'\n",
    "]\n",
    "Results2_plt2['point_Type'] = [\n",
    "    'Ext',\n",
    "    'Ext',\n",
    "    'Ext',\n",
    "    'Inv',\n",
    "    'Inv',\n",
    "    'Inv'\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a05ac46f-95c0-4fa1-8bea-a915a6bf3095",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "if not os.path.isdir('../30_Output/20_Plot/300_MakeResult'):\n",
    "    os.makedirs('../30_Output/20_Plot/300_MakeResult')\n",
    "\n",
    "plt.figure(figsize=(10,4))\n",
    "# snsfig = \n",
    "snsfig = sns.lineplot(\n",
    "    data=Results2_plt, \n",
    "    x='Layer for plot', \n",
    "    y='value', \n",
    "    hue='Type', \n",
    "    markers=False, \n",
    "    ci=90, \n",
    "    style='Type', \n",
    "    err_style=\"bars\", \n",
    "    dashes=False, \n",
    "    palette=['steelblue', 'indianred'], \n",
    "    lw=2.5, \n",
    "    err_kws={'lw':2.5, 'zorder':6},\n",
    "    zorder = 4\n",
    ")\n",
    "sns.scatterplot(\n",
    "    data=Results2_plt2, \n",
    "    x='Layer for plot', \n",
    "    y='value', \n",
    "    hue='point_Type', \n",
    "    s=[75,75,75,120,120,120], \n",
    "    legend=False, \n",
    "    style='point_Type', \n",
    "    # palette=(['steelblue']*3+['indianred']*3),\n",
    "    palette=(['steelblue']+['indianred']),\n",
    "    zorder = 5\n",
    ")\n",
    "x_lim = snsfig.get_xlim()\n",
    "plt.hlines(y=Results2.mean()['RMSE (test, Wotl)'], xmin=-1, xmax=4, color='gray', linestyles='dashed', lw=1.5, label='Without transfer', zorder=2)\n",
    "plt.hlines(y=Results2.mean()['RMSE (test, Fine)'], xmin=-1, xmax=4, color='gray', linestyles='dashdot', lw=1.5, label='Fine-tuning', zorder=3)\n",
    "plt.xlim(x_lim)\n",
    "plt.xticks([1,2,3],['1st layer','2nd layer','3rd layer'], size=11)\n",
    "plt.xlabel('Extracted layer', size=13)\n",
    "plt.ylabel('Root mean squared error (W/mK)', size=13)\n",
    "plt.legend(bbox_to_anchor=(1.01, 0.99), loc='upper left', borderaxespad=0, fontsize=13)\n",
    "plt.grid(color='gray', linestyle='dotted', linewidth=1, alpha=0.4, zorder=1)\n",
    "plt.tight_layout()\n",
    "plt.savefig('../30_Output/20_Plot/300_MakeResult/300_Lineplot.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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
