{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Working with the med3pa Comparaison module\n",
    "------------------------------------------\n",
    "\n",
    "The below function will help plotting the global metrics calculated for two compared experiments.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def plot_global_metrics(file_path, metric):\n",
    "    \n",
    "    with open(file_path, 'r') as f1:\n",
    "        data = json.load(f1)\n",
    "\n",
    "    # Initialize lists to hold values for plotting\n",
    "    dr_values = []\n",
    "    metrics_dr_1_values = []\n",
    "    metrics_dr_2_values = []\n",
    "\n",
    "    # Extract values from the JSON data\n",
    "    for dr in sorted(data.keys(), key=int, reverse=True):\n",
    "        dr_values.append(int(dr))\n",
    "        metrics_dr_1_values.append(data[dr]['metrics_dr_1']['metrics'][metric])\n",
    "        metrics_dr_2_values.append(data[dr]['metrics_dr_2']['metrics'][metric])\n",
    "\n",
    "    # Ensure DR values go from 100 to 0\n",
    "    dr_values = sorted(dr_values, reverse=True)\n",
    "\n",
    "    # Plot the metrics\n",
    "    plt.figure(figsize=(10, 6))\n",
    "    plt.plot(dr_values, metrics_dr_1_values, label='metrics_dr_1', marker='o')\n",
    "    plt.plot(dr_values, metrics_dr_2_values, label='metrics_dr_2', marker='x')\n",
    "\n",
    "    # Add labels and title\n",
    "    plt.xlabel('DR (descending from 100 to 0)')\n",
    "    plt.ylabel(metric)\n",
    "    plt.title(f'Comparison of {metric} for metrics_dr_1 and metrics_dr_2')\n",
    "    plt.legend()\n",
    "    plt.grid(True)\n",
    "    plt.gca().invert_xaxis()  # Invert the x-axis to ensure DR goes from 100 to 0\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use this function and pass the path to the global_metrics_comparaison.json file, alongside the metric we want to plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 70128 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "plot_global_metrics('./med3pa_comparaison_results/global_metrics_comparaison.json', 'Accuracy')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The below function will plot the metrics for each profile in the tree, give the two experiments had the same tree structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def plot_profile_metrics(file_path, samples_ratio, profile, metric):\n",
    "    \n",
    "    with open(file_path, 'r') as f1:\n",
    "        data = json.load(f1)\n",
    "        \n",
    "    # Initialize lists to hold values for plotting\n",
    "    dr_values = []\n",
    "    metrics_1_values = []\n",
    "    metrics_2_values = []\n",
    "\n",
    "    # Extract values from the JSON data\n",
    "    for dr in sorted(data[samples_ratio][profile].keys(), key=int, reverse=True):\n",
    "        dr_value = int(dr)\n",
    "        metric_1_value = data[samples_ratio][profile][dr].get('metrics_1', {}).get(metric)\n",
    "        metric_2_value = data[samples_ratio][profile][dr].get('metrics_2', {}).get(metric)\n",
    "        \n",
    "        if metric_1_value is not None:\n",
    "            dr_values.append(dr_value)\n",
    "            metrics_1_values.append(metric_1_value)\n",
    "        if metric_2_value is not None:\n",
    "            if dr_value not in dr_values:\n",
    "                dr_values.append(dr_value)\n",
    "            metrics_2_values.append(metric_2_value)\n",
    "\n",
    "    # Ensure DR values go from 100 to 0\n",
    "    dr_values = sorted(dr_values, reverse=True)\n",
    "\n",
    "    # Plot the metrics\n",
    "    plt.figure(figsize=(10, 6))\n",
    "    if metrics_1_values:\n",
    "        plt.plot(dr_values[:len(metrics_1_values)], metrics_1_values, label='metrics_1', marker='o')\n",
    "    if metrics_2_values:\n",
    "        plt.plot(dr_values[:len(metrics_2_values)], metrics_2_values, label='metrics_2', marker='x')\n",
    "\n",
    "    # Add labels and title\n",
    "    plt.xlabel('DR (descending from 100 to 0)')\n",
    "    plt.ylabel(metric)\n",
    "    plt.title(f'Comparison of {metric} for metrics_1 and metrics_2 (samples_ratio: {samples_ratio}, profile: {profile})')\n",
    "    plt.legend()\n",
    "    plt.grid(True)\n",
    "    plt.gca().invert_xaxis()  # Invert the x-axis to ensure DR goes from 100 to 0\n",
    "    plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 56604 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "file_path = './med3pa_comparaison_results/profiles_metrics_comparaison.json'\n",
    "profile = \"* / Age <= -0.8201243877410889\"\n",
    "\n",
    "plot_profile_metrics(file_path, \"0\", profile, 'Auc')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
