{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4e4ec888",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Project_Root: /Users/mguillermo/Desktop/MGPH/Maestria/Hydrogen_Exp/Analysis/2nd_AMIDIQ_Analysis\n",
      "Datos Cargados: 8\n"
     ]
    }
   ],
   "source": [
    "from    pathlib import Path\n",
    "import  sys\n",
    "\n",
    "\n",
    "PROJECT_ROOT    =   Path.cwd().parent\n",
    "sys.path.insert(0, str(PROJECT_ROOT))\n",
    "\n",
    "print(\"Project_Root:\", PROJECT_ROOT)\n",
    "\n",
    "\n",
    "from    src.visualizacion import (plot_xy, comparar_rondas, comparar_experimentos, plot_con_ajuste\n",
    "                                  , plot_3D, plot_color_map, plot_dual_axis)\n",
    "from    src.data_loader import apply_filter\n",
    "from    src.data_loader import load_all\n",
    "\n",
    "DATA_DIR    =   PROJECT_ROOT / \"data\"\n",
    "data        =   load_all(DATA_DIR)\n",
    "print(\"Datos Cargados:\", len(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "afc1db64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 46040 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 45224 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 67108 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 56820 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 41604 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 46760 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 45052 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from    pathlib                 import   Path\n",
    "from    src.data_loader         import  apply_filter\n",
    "import  matplotlib.pyplot       as       plt\n",
    "\n",
    "## Put the path for import the data \n",
    "#PROJECT_ROOT    =   Path.cwd().parent \n",
    "#DATA_DIR        =   PROJECT_ROOT    /   \"data\"\n",
    "\n",
    "#data    =   load_all(DATA_DIR)\n",
    "\n",
    "def plot_xy(df, x_col, y_col, label=None, ax=None):\n",
    "    if ax is None:\n",
    "        fig, ax = plt.subplots(figsize=(7,5))\n",
    "    \n",
    "    ax.plot(df[x_col], df[y_col], marker='o', linestyle='-', label=label)\n",
    "    ax.set_xlabel(x_col)\n",
    "    ax.set_ylabel(y_col)\n",
    "    ax.grid(True)\n",
    "\n",
    "    if label:\n",
    "        ax.legend()\n",
    "    \n",
    "    return ax\n",
    "\n",
    "def comparar_rondas(data, ronda, experimento, x_col, y_col,\n",
    "                    row_start=None, row_end=None,\n",
    "                    t_min=None, t_max=None, title=None):\n",
    "    \n",
    "    fig, ax = plt.subplots(figsize=(7,5))\n",
    "\n",
    "    if ronda == None:\n",
    "        rnd =   [1,2]\n",
    "    else:\n",
    "        rnd =   ronda\n",
    "\n",
    "    for ronda in rnd:\n",
    "        df  =   data[(ronda, experimento)]\n",
    "\n",
    "        # Data Filtering \n",
    "        df_f    =   apply_filter(df, row_start=row_start, row_end=row_end,\n",
    "                                 t_min=t_min, t_max=t_max)\n",
    "\n",
    "        plot_xy(df_f, x_col, y_col, label=f\"Ronda {ronda}\", ax=ax)\n",
    "    \n",
    "    if title == None:\n",
    "        ax.set_title(f\"Experiment {experimento} - Comparatives Rounds {rnd}\")\n",
    "    else:\n",
    "        ax.set_title(title)\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "def comparar_experimentos(data, ronda, expe, x_col, y_col,\n",
    "                          row_start=None, row_end=None,\n",
    "                          t_min=None, t_max=None, title=None):\n",
    "    \n",
    "    fig, ax = plt.subplots(figsize=(7,5))\n",
    "\n",
    "    if expe == None:\n",
    "        no_expe =   [1,2,3,4]\n",
    "    else:\n",
    "        no_expe =   expe\n",
    "\n",
    "    for exp in no_expe:\n",
    "        df  =   data[(ronda, exp)]\n",
    "\n",
    "        # Data Filtering \n",
    "        df_f    =   apply_filter(df, row_start=row_start, row_end=row_end,\n",
    "                                 t_min=t_min, t_max=t_max)\n",
    "        \n",
    "        plot_xy(df_f, x_col, y_col, label=f\"Experiment {exp}\", ax=ax)\n",
    "\n",
    "    if title == None:\n",
    "        ax.set_title(f\"Round {ronda} - Comparatives Experiments {no_expe}\")\n",
    "    else:\n",
    "        ax.set_title(title)\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "\n",
    "expe_1 = [1,4]\n",
    "\n",
    "comparar_rondas(data, ronda=None, experimento=1, x_col=\"Temperatura\", y_col=\"Resistencia\", row_start=35, \n",
    "                row_end=66, title=\"Comparative between Temperature vs Resistance in Experiment 1\")\n",
    "comparar_rondas(data, ronda=None, experimento=2, x_col=\"Voltaje Electrodos (V)\", y_col=\"Temperatura\", \n",
    "                row_start=35, row_end=66)\n",
    "comparar_experimentos(data, ronda=1, expe=None, x_col=\"Voltaje Electrodos (V)\", y_col=\"Resistencia\", \n",
    "                row_start=35, row_end=66)\n",
    "comparar_experimentos(data, ronda=1, expe=None, x_col=\"Voltaje Electrodos (V)\", y_col=\"Corriente Electrodos (A)\", \n",
    "                row_start=10, row_end=66)\n",
    "comparar_experimentos(data, ronda=1, expe=expe_1, x_col=\"Voltaje Electrodos (V)\",\n",
    "                      y_col=\"Corriente Electrodos (A)\", row_start=10, row_end=66)\n",
    "comparar_experimentos(data, ronda=1, expe=expe_1, x_col=\"Voltaje Electrodos (V)\",\n",
    "                      y_col=\"Resistencia\", row_start=35, row_end=66)\n",
    "comparar_experimentos(data, ronda=1, expe=[2,3], x_col=\"Voltaje Electrodos (V)\",\n",
    "                      y_col=\"Resistencia\", row_start=35, row_end=66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db0e8d5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39028 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 38812 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 37912 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Items rondas: [1]\n",
      "Items experimento: [1, 4]\n",
      "True\n",
      "Pendientes:\n",
      "Ronda 1: 2.190472\n",
      "Ronda 4: 2.038875\n",
      "{1: 1, 2: 4}\n",
      "2\n",
      "% Cambio Resultado 2 respecto Resultado 1: -6.92%\n"
     ]
    }
   ],
   "source": [
    "import numpy as np  \n",
    "\n",
    "def calcular_pendiente(df, x_col, y_col):\n",
    "    x       =   df[x_col].values\n",
    "    y       =   df[y_col].values\n",
    "\n",
    "    m, b    =   np.polyfit(x, y, 1)\n",
    "\n",
    "    return m, b\n",
    "\n",
    "def comparar_pendientes(data, experimento, ronda,\n",
    "                        x_col, y_col, row_start=None,\n",
    "                        row_end=None):\n",
    "    resultados  =   {}\n",
    "\n",
    "    print(f\"Items rondas: {ronda}\")\n",
    "    print(f\"Items experimento: {experimento}\")\n",
    "\n",
    "    if len(ronda) > len(experimento):\n",
    "        mayor   =   False\n",
    "    else:\n",
    "        mayor   =   True\n",
    "    print(mayor)\n",
    "\n",
    "    for rnd in ronda:\n",
    "        for exp in experimento:\n",
    "            df      =   data[(rnd, exp)]\n",
    "            df_f    =   apply_filter(df, row_start=row_start, row_end=row_end)\n",
    "            m, _    =   calcular_pendiente(df_f, x_col, y_col)\n",
    "            if mayor == False:\n",
    "                resultados[rnd] = m\n",
    "            else:\n",
    "                resultados[exp] = m\n",
    "    \n",
    "    print(\"Pendientes:\")\n",
    "    cont = 0\n",
    "    result = {}\n",
    "    for r, m in resultados.items():\n",
    "        cont = cont + 1\n",
    "        result[cont]    =   r\n",
    "        print(f\"Ronda {r}: {m:.6f}\")\n",
    "    \n",
    "    print(result)\n",
    "    print(len(resultados))\n",
    "    \n",
    "    cambio  =   ((resultados[result[2]] -   resultados[result[1]]) / resultados[result[1]]) * 100\n",
    "    print(f\"% Cambio Resultado 2 respecto Resultado 1: {cambio:.2f}%\")\n",
    "\n",
    "def plot_con_ajuste(data, x_col, y_col, ronda, exp,\n",
    "                    row_start=None, row_end= None,\n",
    "                    t_min=None, t_max=None):\n",
    "    \n",
    "    df      =   data[(ronda, exp)]\n",
    "    df_f    =   apply_filter(df, row_start=row_start, row_end=row_end,\n",
    "                             t_max=t_max, t_min=t_min)\n",
    "\n",
    "    x       =   df_f[x_col].values\n",
    "    y       =   df_f[y_col].values\n",
    "\n",
    "    m, b    =   np.polyfit(x, y, 1)\n",
    "\n",
    "    plt.figure(figsize=(7,5))\n",
    "    plt.scatter(x, y, label=f\"Datos-Experimento {exp}\")\n",
    "    plt.plot(x, m*x + b, color=\"red\", label=f\"Ajuste lineal (m={m:.4f})\")\n",
    "    plt.xlabel(x_col)\n",
    "    plt.ylabel(y_col)\n",
    "    plt.legend()\n",
    "    plt.grid(True)\n",
    "    plt.show()\n",
    "\n",
    "    return m \n",
    "\n",
    "plot_con_ajuste(data, x_col=\"Voltaje Electrodos (V)\", y_col=\"Resistencia\",\n",
    "                ronda=1, exp=1, row_start=36, row_end=66)\n",
    "plot_con_ajuste(data, x_col=\"Voltaje Electrodos (V)\", y_col=\"Corriente Electrodos (A)\",\n",
    "                ronda=1, exp=1, row_start=10, row_end=66)\n",
    "plot_con_ajuste(data, x_col=\"Voltaje Electrodos (V)\", y_col=\"Corriente Electrodos (A)\",\n",
    "                ronda=1, exp=4, row_start=10, row_end=66)\n",
    "\n",
    "expr    =   [1,4]\n",
    "rnd     =   [1]\n",
    "\n",
    "comparar_pendientes(data, experimento=expr, ronda=rnd, x_col=\"Voltaje Electrodos (V)\",\n",
    "                    y_col=\"Corriente Electrodos (A)\", row_start=10, row_end=66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa9e89fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 36968 characters]",
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(np.float64(1.724478653786506),\n",
       " np.float64(-2.3681436972054284),\n",
       " <Figure size 700x500 with 1 Axes>,\n",
       " <Axes: xlabel='Voltaje Electrodos (V)', ylabel='Corriente Electrodos (A)'>)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = data[(1,1)]\n",
    "plot_con_ajuste(df, x_col=\"Voltaje Electrodos (V)\", y_col=\"Corriente Electrodos (A)\")\n",
    "\n",
    "comparar_rondas(data, 2, )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad3e9c22",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python_Data_Analysis",
   "language": "python",
   "name": "python_data_analysis"
  },
  "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.14.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
