{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python packages used in this code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import pickle\n",
    "import platform\n",
    "import sys\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Environments\n",
    "\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",
    "seaborn : 0.11.2\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__)\n",
    "print('seaborn :', sns.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Main codes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make dataframe for results "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result = pd.read_csv('../30_Output/30_csv/300_TransferLearning_Result.csv')\n",
    "df_result.columns = ['Unnamed: 0', 'data_name', 'Number of samples', 'n_itr', 'type', 'Mean squared error', 'Corr', 'MAE', 'R2']\n",
    "df_result = df_result.replace({'Using only source features':'Only source',\n",
    "                   'With source features':'Argumented',\n",
    "                   'Without transfer':'No transfer',\n",
    "                   'Leraning the residuals':'HTL-offset',\n",
    "                   'Learning the rates':'HTL-scale'})\n",
    "df_result['Root mean squared error'] = np.sqrt(df_result['Mean squared error'])\n",
    "\n",
    "n_sample_list=[5, 15, 10, 20, 30, 50]\n",
    "target_name_list = ['Torque1','Torque2','Torque3','Torque4','Torque5','Torque6','Torque7']\n",
    "\n",
    "results = pd.DataFrame(columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'])\n",
    "\n",
    "for target_name in target_name_list:\n",
    "    for type_name in ['No transfer', 'Only source', 'Argumented', 'HTL-offset', 'HTL-scale', 'Proposed method', 'Proposed method 2', 'Proposed method 3']:\n",
    "        tmp_data = df_result[(df_result['data_name']==target_name)&(df_result['type']==type_name)]\n",
    "        df_add = pd.DataFrame(np.array([target_name, \n",
    "                               type_name, \n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].std()]).reshape(1,-1), \n",
    "                              columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'],\n",
    "                              index=[target_name+'_'+type_name])\n",
    "        results = pd.concat([results, df_add], axis=0)\n",
    "\n",
    "# Save\n",
    "results.to_csv('../30_Output/30_csv/420_MakeResults_Table.csv')\n",
    "\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FineTuning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result = pd.read_csv('../30_Output/30_csv/310_TransferLearning_Result.csv')\n",
    "df_result.columns = ['Unnamed: 0', 'data_name', 'Number of samples', 'n_itr', 'type', 'Mean squared error', 'Corr', 'MAE', 'R2']\n",
    "df_result = df_result.replace({'Using only source features':'Only source',\n",
    "                   'With source features':'Argumented',\n",
    "                   'Without transfer':'No transfer',\n",
    "                   'Leraning the residuals':'HTL-offset',\n",
    "                   'Learning the rates':'HTL-scale'})\n",
    "df_result['Root mean squared error'] = np.sqrt(df_result['Mean squared error'])\n",
    "\n",
    "n_sample_list=[5, 15, 10, 20, 30, 50]\n",
    "target_name_list = ['Torque1','Torque2','Torque3','Torque4','Torque5','Torque6','Torque7']\n",
    "\n",
    "results = pd.DataFrame(columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'])\n",
    "\n",
    "for target_name in target_name_list:\n",
    "    # for type_name in ['No transfer', 'Only source', 'Argumented', 'HTL-offset', 'HTL-scale', 'Proposed method', 'Proposed method 2', 'Proposed method 3']:\n",
    "    for type_name in ['Fine tuning']:\n",
    "        tmp_data = df_result[(df_result['data_name']==target_name)&(df_result['type']==type_name)]\n",
    "        df_add = pd.DataFrame(np.array([target_name, \n",
    "                               type_name, \n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].std()]).reshape(1,-1), \n",
    "                              columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'],\n",
    "                              index=[target_name+'_'+type_name])\n",
    "        results = pd.concat([results, df_add], axis=0)\n",
    "\n",
    "# Save\n",
    "results.to_csv('../30_Output/30_csv/410_MakeResults_Table_FT.csv')\n",
    "\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MAML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result = pd.read_csv('../30_Output/30_csv/320_TransferLearning_Result.csv')\n",
    "df_result.columns = ['Unnamed: 0', 'data_name', 'Number of samples', 'n_itr', 'type', 'Mean squared error', 'Corr', 'MAE', 'R2', 'epochs']\n",
    "df_result = df_result.replace({'Using only source features':'Only source',\n",
    "                   'With source features':'Argumented',\n",
    "                   'Without transfer':'No transfer',\n",
    "                   'Leraning the residuals':'HTL-offset',\n",
    "                   'Learning the rates':'HTL-scale'})\n",
    "df_result['Root mean squared error'] = np.sqrt(df_result['Mean squared error'])\n",
    "\n",
    "n_sample_list=[5, 15, 10, 20, 30, 50]\n",
    "target_name_list = ['Torque1','Torque2','Torque3','Torque4','Torque5','Torque6','Torque7']\n",
    "\n",
    "results = pd.DataFrame(columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'])\n",
    "\n",
    "for target_name in target_name_list:\n",
    "    # for type_name in ['No transfer', 'Only source', 'Argumented', 'HTL-offset', 'HTL-scale', 'Proposed method', 'Proposed method 2', 'Proposed method 3']:\n",
    "    for type_name in ['MAML']:\n",
    "        tmp_data = df_result[(df_result['data_name']==target_name)&(df_result['type']==type_name)]\n",
    "        df_add = pd.DataFrame(np.array([target_name, \n",
    "                               type_name, \n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].std()]).reshape(1,-1), \n",
    "                              columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'],\n",
    "                              index=[target_name+'_'+type_name])\n",
    "        results = pd.concat([results, df_add], axis=0)\n",
    "\n",
    "# Save\n",
    "results.to_csv('../30_Output/30_csv/420_MakeResults_Table_MAML.csv')\n",
    "\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## L2-SP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result = pd.read_csv('../30_Output/30_csv/330_TransferLearning_Result.csv')\n",
    "df_result.columns = ['Unnamed: 0', 'data_name', 'Number of samples', 'n_itr', 'type', 'Mean squared error', 'Corr', 'MAE', 'R2']\n",
    "df_result = df_result.replace({'Using only source features':'Only source',\n",
    "                   'With source features':'Argumented',\n",
    "                   'Without transfer':'No transfer',\n",
    "                   'Leraning the residuals':'HTL-offset',\n",
    "                   'Learning the rates':'HTL-scale'})\n",
    "df_result['Root mean squared error'] = np.sqrt(df_result['Mean squared error'])\n",
    "\n",
    "n_sample_list=[5, 15, 10, 20, 30, 50]\n",
    "target_name_list = ['Torque1','Torque2','Torque3','Torque4','Torque5','Torque6','Torque7']\n",
    "\n",
    "results = pd.DataFrame(columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'])\n",
    "\n",
    "for target_name in target_name_list:\n",
    "    # for type_name in ['No transfer', 'Only source', 'Argumented', 'HTL-offset', 'HTL-scale', 'Proposed method', 'Proposed method 2', 'Proposed method 3']:\n",
    "    for type_name in ['L2-SP']:\n",
    "        tmp_data = df_result[(df_result['data_name']==target_name)&(df_result['type']==type_name)]\n",
    "        df_add = pd.DataFrame(np.array([target_name, \n",
    "                               type_name, \n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].std()]).reshape(1,-1), \n",
    "                              columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'],\n",
    "                              index=[target_name+'_'+type_name])\n",
    "        results = pd.concat([results, df_add], axis=0)\n",
    "\n",
    "# Save\n",
    "results.to_csv('../30_Output/30_csv/430_MakeResults_Table_L2SP.csv')\n",
    "\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## PAC-Net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_result = pd.read_csv('../30_Output/30_csv/340_TransferLearning_Result.csv')\n",
    "df_result.columns = ['Unnamed: 0', 'data_name', 'Number of samples', 'n_itr', 'type', 'Mean squared error', 'Corr', 'MAE', 'R2']\n",
    "df_result = df_result.replace({'Using only source features':'Only source',\n",
    "                   'With source features':'Argumented',\n",
    "                   'Without transfer':'No transfer',\n",
    "                   'Leraning the residuals':'HTL-offset',\n",
    "                   'Learning the rates':'HTL-scale'})\n",
    "df_result['Root mean squared error'] = np.sqrt(df_result['Mean squared error'])\n",
    "\n",
    "n_sample_list=[5, 15, 10, 20, 30, 50]\n",
    "target_name_list = ['Torque1','Torque2','Torque3','Torque4','Torque5','Torque6','Torque7']\n",
    "\n",
    "results = pd.DataFrame(columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'])\n",
    "\n",
    "for target_name in target_name_list:\n",
    "    # for type_name in ['No transfer', 'Only source', 'Argumented', 'HTL-offset', 'HTL-scale', 'Proposed method', 'Proposed method 2', 'Proposed method 3']:\n",
    "    for type_name in ['PAC']:\n",
    "        tmp_data = df_result[(df_result['data_name']==target_name)&(df_result['type']==type_name)]\n",
    "        df_add = pd.DataFrame(np.array([target_name, \n",
    "                               type_name, \n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==5]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==10]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==15]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==20]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==30]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==40]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==50]['Root mean squared error'].std(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].mean(),\n",
    "                               tmp_data[tmp_data['Number of samples']==100]['Root mean squared error'].std()]).reshape(1,-1), \n",
    "                              columns=['target', 'type', '5_mean', '5_std', '10_mean', '10_std', '15_mean', '15_std', '20_mean', '20_std', '30_mean', '30_std', '40_mean', '40_std', '50_mean', '50_std', '100_mean', '100_std'],\n",
    "                              index=[target_name+'_'+type_name])\n",
    "        results = pd.concat([results, df_add], axis=0)\n",
    "\n",
    "# Save\n",
    "results.to_csv('../30_Output/30_csv/440_MakeResults_Table_PAC.csv')\n",
    "\n",
    "results"
   ]
  }
 ],
 "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": 4
}
