{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "# from pyts.image import GramianAngularField\n",
    "from pyts.image import GramianAngularField, MarkovTransitionField\n",
    "from tqdm import tqdm\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def standardize(df):\n",
    "    df_standardize = ((df - df.mean()) / df.std())\n",
    "    return df_standardize\n",
    "\n",
    "def normalize(df):\n",
    "    df_normalize = ((df - df.min()) / (df.max() - df.min()))\n",
    "    return df_normalize\n",
    "\n",
    "\n",
    "def normalize_global(df, min_val, max_val):\n",
    "    df_normalize = ((df - min_val) / (max_val - min_val))\n",
    "    return df_normalize\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 89/89 [00:04<00:00, 19.61it/s]\n"
     ]
    }
   ],
   "source": [
    "# find global min and max\n",
    "files = [file for file in range(1,89+1)]\n",
    "global_min = float('inf')\n",
    "global_max = float('-inf')\n",
    "\n",
    "for img_num in tqdm(files):\n",
    "    data_path = f\"../data/testlog{img_num}.csv\"\n",
    "    df = pd.read_csv(data_path, header=0)\n",
    "\n",
    "\n",
    "    region = (df[\"Step_Count\"] == 6)\n",
    "    min = region.idxmax()\n",
    "    max = region[::-1].idxmax()\n",
    "\n",
    "    data = (df[\"RF1_RefPwr\"][min:max].fillna(0) + df[\"RF2_RefPwr\"][min:max].fillna(0) + df[\"RF3_RefPwr\"][min:max].fillna(0))/3\n",
    "    data_min = np.min(data)\n",
    "    data_max = np.max(data)\n",
    "\n",
    "    if data_min < global_min:\n",
    "        global_min = data_min\n",
    "\n",
    "    if data_max > global_max:\n",
    "        global_max = data_max\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(147.33333333333334), np.float64(0.0))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "global_max, global_min"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "import plotly.graph_objs as go\n",
    "\n",
    "files = [file for file in range(1,89+1)]\n",
    "segments = []\n",
    "\n",
    "for img_num in tqdm(files):\n",
    "    data_path = f\"../data/testlog{img_num}.csv\"\n",
    "    df = pd.read_csv(data_path, header=0)\n",
    "\n",
    "\n",
    "    region = (df[\"Step_Count\"] == 6)\n",
    "    min = region.idxmax()\n",
    "    max = region[::-1].idxmax()\n",
    "\n",
    "    data = (df[\"RF1_RefPwr\"][min:max].fillna(0) + df[\"RF2_RefPwr\"][min:max].fillna(0) + df[\"RF3_RefPwr\"][min:max].fillna(0))/3\n",
    "    segments.append(normalize_global(data, global_min, global_max))\n",
    "\n",
    "data = []\n",
    "for i, segment in enumerate(segments, start=1):\n",
    "    data.append(go.Scatter(y=segment, mode='lines', name=i))\n",
    "\n",
    "fig = go.Figure(data=data)\n",
    "fig.update_traces(visible='legendonly')\n",
    "fig.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Step_Count', 'Press_Set', 'Press_Value', 'Press_Position', 'Top_Temp',\n",
       "       'Bottom_Temp', 'RF1_FwdPwr', 'RF1_RefPwr', 'RF2_FwdPwr', 'RF2_RefPwr',\n",
       "       'RF3_FwdPwr', 'RF3_RefPwr', 'RF1_Vpp', 'RF1_Imag', 'RF1_Phase',\n",
       "       'RF1_ESC_Vrms', 'RF2_Vpp', 'RF2_Imag', 'RF2_Phase', 'RF2_ESC_Vrms',\n",
       "       'RF3_Vpp', 'RF3_Imag', 'RF3_Phase', 'RF3_ESC_Vrms', 'RF1_Cap1Pos',\n",
       "       'RF1_Cap2Pos', 'RF2_Cap1Pos', 'RF2_Cap2Pos', 'RF3_Cap1Pos',\n",
       "       'RF3_Cap2Pos', 'Wall_SetTemp', 'Wall_Temp', 'Wall_Output',\n",
       "       'GDP_SetTemp', 'GDP_Temp', 'GDP_Output', 'Liner1_SetTemp',\n",
       "       'Liner1_Temp', 'Liner1_Output', 'BC1_SetPress', 'BC1_Press', 'BC1_Flow',\n",
       "       'BC1_LeakFlow', 'BC2_SetPress', 'BC2_Press', 'BC2_Flow', 'BC2_LeakFlow',\n",
       "       'Chiller1_SetTemp', 'Chiller1_Temp', 'Chiller1_Press',\n",
       "       'Chiller1_FlowRate', 'Chiller2_SetTemp', 'Chiller2_Temp',\n",
       "       'Chiller2_Press', 'Chiller2_FlowRate', 'Valve_Position', 'eESC_Volt',\n",
       "       'eESC_PosOut', 'EB_Cap', 'EB_Volt', 'EB_Current', 'EB_Phase',\n",
       "       'EPD_Trend1', 'EPD_Trend2', 'EPD_Trend3', 'EPD_Trend4', 'EPD_Trend5',\n",
       "       'CB_Leak'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Perform on all"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_combined_image_as_img(gaf_image, mtf_image, file_path):\n",
    "    # Stack GAF and MTF images along the channel dimension\n",
    "    combined_image = np.stack([gaf_image, mtf_image], axis=2)\n",
    "    \n",
    "    # Convert to uint8 format (if necessary)\n",
    "    combined_image = (combined_image * 255).astype(np.uint8)\n",
    "    \n",
    "    # Convert numpy array to Image\n",
    "    combined_image_pil = Image.fromarray(combined_image)\n",
    "    \n",
    "    # Save the image\n",
    "    combined_image_pil.save(file_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_combined_image_as_np(gaf_image, mtf_image, file_path):\n",
    "    # Stack GAF and MTF images along the channel dimension\n",
    "    combined_image = np.stack([gaf_image, mtf_image], axis=2).astype(np.float32)\n",
    "    \n",
    "    # Convert to uint8 format (if necessary)\n",
    "    # combined_image = (combined_image * 255).astype(np.uint8)\n",
    "    \n",
    "    # Convert numpy array to Image\n",
    "    # combined_image_pil = Image.fromarray(combined_image)\n",
    "    \n",
    "    # Save the image\n",
    "    np.save(file_path,combined_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 89/89 [00:04<00:00, 18.06it/s]\n"
     ]
    }
   ],
   "source": [
    "import warnings\n",
    "from tqdm import tqdm\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "image_size = 400\n",
    "gsaf = GramianAngularField(image_size=image_size, method=\"summation\")\n",
    "mtf = MarkovTransitionField(image_size=image_size, n_bins=5)\n",
    "\n",
    "files = [file for file in range(1,89+1)]\n",
    "\n",
    "for img_num in tqdm(files):\n",
    "    data_path = f\"../data/testlog{img_num}.csv\"\n",
    "    df = pd.read_csv(data_path, header=0)\n",
    "\n",
    "\n",
    "    region = (df[\"Step_Count\"] == 6)\n",
    "    min = region.idxmax()\n",
    "    max = region[::-1].idxmax()\n",
    "\n",
    "    data = (df[\"RF1_RefPwr\"][min:max].fillna(0) + df[\"RF2_RefPwr\"][min:max].fillna(0) + df[\"RF3_RefPwr\"][min:max].fillna(0))/3\n",
    "    # data_min = np.min(data_norm)\n",
    "    # if (np.max(data_norm) == 0):\n",
    "    #     data_max = 1\n",
    "    # else:\n",
    "    #     data_max = np.max(data_norm)\n",
    "\n",
    "    data_array = np.array(data)\n",
    "    segment_size = 400\n",
    "    num_segments = len(data_array) // segment_size\n",
    "    remainder = len(data_array) - num_segments * segment_size\n",
    "    # remove some portion of data to fit into nice length segments\n",
    "    segments = np.array_split(data_array[remainder:], num_segments)\n",
    "    for i, segment in enumerate(segments):\n",
    "        data = normalize_global(segment, global_min, global_max)\n",
    "        # data = data.reshape((1,-1))\n",
    "        # values = np.array([0, 1])\n",
    "\n",
    "        # data = np.concatenate((values, data))\n",
    "\n",
    "\n",
    "        # gaf_image = gsaf.transform(data)[0]\n",
    "        # gaf_image = normalize_global(gsaf.transform(data), data_min, data_max)[0]\n",
    "        # mtf_image = gaf_image # mtf works terribly on images of nearly empty data\n",
    "        # mtf_image = normalize_global(mtf.transform(data), data_min, data_max)[0]\n",
    "\n",
    "        file_path = f\"../gaf_data/train/0/img{img_num}_{i}\"\n",
    "        # save_combined_image_as_np(gaf_image, mtf_image, file_path)\n",
    "        np.savez_compressed(file_path, data=data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## visualize output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "# from pyts.image import GramianAngularField\n",
    "from pyts.image import GramianAngularField, MarkovTransitionField\n",
    "from tqdm import tqdm\n",
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = np.load('../gaf_data/vpp/train/0/img7_21.npz')['data']\n",
    "data2 = np.load('../gaf_data/vpp/train/0/img22_30.npz')['data']\n",
    "\n",
    "values = np.array([0, 1])\n",
    "\n",
    "# Add the values to the front\n",
    "data1 = np.concatenate((values, data1))\n",
    "data2 = np.concatenate((values, data2))\n",
    "\n",
    "# data_norm = normalize_global(data, global_min, global_max)\n",
    "# data = data_norm.fillna(0).to_numpy().reshape((1,-1))\n",
    "data1 = data1.reshape((1,-1))\n",
    "data2 = data2.reshape((1,-1))\n",
    "image_size = 400\n",
    "gsaf = GramianAngularField(image_size=image_size, \n",
    "                           method=\"summation\",\n",
    "\t\t\t\t\t\t   sample_range=(0,1))\n",
    "# mtf = MarkovTransitionField(image_size=image_size, n_bins=5)\n",
    "gaf_image1 = gsaf.transform(data1)[0]\n",
    "gaf_image2 = gsaf.transform(data2)[0]\n",
    "# gaf_image = normalize(gsaf.transform(data))[0]\n",
    "# mtf_image = normalize(mtf.transform(data))[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7c66b971cd90>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12528 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(gaf_image1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7c66b8c6f810>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14164 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(data1.reshape((-1,1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7c66b97fdf10>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12212 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(gaf_image2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7c66b992c090>]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 16504 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(data2.reshape((-1,1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-0.61363122, -0.61363122,  0.24691345, -0.49482223, -0.61363122,\n",
       "        -0.61363122, -0.61363122, -0.61363122, -0.61363122, -0.61363122],\n",
       "       [-0.98978992, -0.98978992, -0.49482223, -0.95936815, -0.98978992,\n",
       "        -0.98978992, -0.98978992, -0.98978992, -0.98978992, -0.98978992],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ],\n",
       "       [-1.        , -1.        , -0.61363122, -0.98978992, -1.        ,\n",
       "        -1.        , -1.        , -1.        , -1.        , -1.        ]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gaf_image1[310:320, 310:320]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.        ,  0.        , -1.        , -0.99999744, -1.        ,\n",
       "        -1.        , -0.99998976, -1.        , -1.        , -0.99999744],\n",
       "       [ 0.        ,  1.        ,  0.        ,  0.00226244,  0.        ,\n",
       "         0.        ,  0.00452489,  0.        ,  0.        ,  0.00226244],\n",
       "       [-1.        ,  0.        , -1.        , -0.99999744, -1.        ,\n",
       "        -1.        , -0.99998976, -1.        , -1.        , -0.99999744],\n",
       "       [-0.99999744,  0.00226244, -0.99999744, -0.99998976, -0.99999744,\n",
       "        -0.99999744, -0.99997697, -0.99999744, -0.99999744, -0.99998976],\n",
       "       [-1.        ,  0.        , -1.        , -0.99999744, -1.        ,\n",
       "        -1.        , -0.99998976, -1.        , -1.        , -0.99999744],\n",
       "       [-1.        ,  0.        , -1.        , -0.99999744, -1.        ,\n",
       "        -1.        , -0.99998976, -1.        , -1.        , -0.99999744],\n",
       "       [-0.99998976,  0.00452489, -0.99998976, -0.99997697, -0.99998976,\n",
       "        -0.99998976, -0.99995905, -0.99998976, -0.99998976, -0.99997697],\n",
       "       [-1.        ,  0.        , -1.        , -0.99999744, -1.        ,\n",
       "        -1.        , -0.99998976, -1.        , -1.        , -0.99999744],\n",
       "       [-1.        ,  0.        , -1.        , -0.99999744, -1.        ,\n",
       "        -1.        , -0.99998976, -1.        , -1.        , -0.99999744],\n",
       "       [-0.99999744,  0.00226244, -0.99999744, -0.99998976, -0.99999744,\n",
       "        -0.99999744, -0.99997697, -0.99999744, -0.99999744, -0.99998976]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gaf_image2[0:10, 0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
