{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "from data import DataProcessor, DataConfig\n",
    "from trainers.quantile_trainer import QuantileTrainer\n",
    "from trainers.autoregressive_trainer import AutoRegressiveTrainer\n",
    "from trainers.trainer import Trainer\n",
    "from utils.clearml import ClearMLHelper\n",
    "from models import *\n",
    "from losses import *\n",
    "import torch\n",
    "import numpy as np\n",
    "from torch.nn import MSELoss, L1Loss\n",
    "from datetime import datetime\n",
    "import pytz\n",
    "import torch.nn as nn\n",
    "\n",
    "# auto reload\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Non-AutoRegressive"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "InsecureRequestWarning: Certificate verification is disabled! Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ClearML Task: created new task id=ae9d93e6bf3f4d228832378707574777\n",
      "ClearML results page: http://192.168.1.182:8080/projects/2e46d4af6f1e4c399cf9f5aa30bc8795/experiments/ae9d93e6bf3f4d228832378707574777/output/log\n",
      "2023-11-08 21:46:32,338 - clearml.Task - INFO - Storing jupyter notebook directly as code\n",
      "2023-11-08 21:46:32,342 - clearml.Repository Detection - WARNING - Can't get url information for git repo in /workspaces/Thesis/src/notebooks\n",
      "2023-11-08 21:46:35,244 - clearml.model - WARNING - 500 model found when searching for `file:///workspaces/Thesis/src/notebooks/checkpoint.pt`\n",
      "2023-11-08 21:46:35,245 - clearml.model - WARNING - Selected model `Untitled Task` (id=bc0cb0d7fc614e2e8b0edf5b85348646)\n",
      "2023-11-08 21:46:35,254 - clearml.frameworks - INFO - Found existing registered model id=bc0cb0d7fc614e2e8b0edf5b85348646 [/workspaces/Thesis/src/notebooks/checkpoint.pt] reusing it.\n",
      "2023-11-08 21:46:35,748 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.ae9d93e6bf3f4d228832378707574777/models/checkpoint.pt\n",
      "2023-11-08 21:46:37,673 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.ae9d93e6bf3f4d228832378707574777/models/checkpoint.pt\n",
      "2023-11-08 21:46:41,215 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.ae9d93e6bf3f4d228832378707574777/models/checkpoint.pt\n",
      "2023-11-08 21:46:43,004 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.ae9d93e6bf3f4d228832378707574777/models/checkpoint.pt\n",
      "2023-11-08 21:46:44,745 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.ae9d93e6bf3f4d228832378707574777/models/checkpoint.pt\n",
      "Early stopping triggered\n"
     ]
    }
   ],
   "source": [
    "#### Hyperparameters ####\n",
    "inputDim = 96 + 96\n",
    "learningRate = 0.0003\n",
    "epochs = 150\n",
    "\n",
    "# model = LinearRegression(inputDim, 96)\n",
    "model = NonLinearRegression(inputDim, 96, hiddenSize=1024, numLayers=5)\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=learningRate)\n",
    "\n",
    "#### Data Processor ####\n",
    "data_config = DataConfig()\n",
    "data_processor = DataProcessor(data_config)\n",
    "data_processor.set_batch_size(1024)\n",
    "\n",
    "\n",
    "data_processor.set_train_range((datetime(year=2015, month=1, day=1, tzinfo=pytz.UTC), datetime(year=2022, month=11, day=30, tzinfo=pytz.UTC)))\n",
    "data_processor.set_test_range((datetime(year=2023, month=1, day=1, tzinfo=pytz.UTC), datetime(year=2023, month=6, day=20, tzinfo=pytz.UTC)))\n",
    "\n",
    "#### ClearML ####\n",
    "clearml_helper = ClearMLHelper(project_name=\"Thesis/NrvForecast\")\n",
    "\n",
    "#### Trainer ####\n",
    "trainer = Trainer(model, optimizer, nn.MSELoss(), data_processor, \"cuda\", debug=False, clearml_helper=clearml_helper)\n",
    "trainer.add_metrics_to_track([MSELoss(), L1Loss()])\n",
    "trainer.plot_every(10)\n",
    "trainer.early_stopping(patience=40)\n",
    "trainer.train(epochs=epochs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AutoRegressive Simple Linear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "InsecureRequestWarning: Certificate verification is disabled! Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ClearML Task: created new task id=e4983af7aad748e8ae71e5fa46767d60\n",
      "ClearML results page: http://192.168.1.182:8080/projects/2e46d4af6f1e4c399cf9f5aa30bc8795/experiments/e4983af7aad748e8ae71e5fa46767d60/output/log\n",
      "2023-11-08 22:51:38,769 - clearml.Task - INFO - Storing jupyter notebook directly as code\n",
      "2023-11-08 22:51:38,773 - clearml.Repository Detection - WARNING - Can't get url information for git repo in /workspaces/Thesis/src/notebooks\n",
      "2023-11-08 22:51:41,704 - clearml.model - WARNING - 500 model found when searching for `file:///workspaces/Thesis/src/notebooks/checkpoint.pt`\n",
      "2023-11-08 22:51:41,705 - clearml.model - WARNING - Selected model `Untitled Task` (id=bc0cb0d7fc614e2e8b0edf5b85348646)\n",
      "2023-11-08 22:51:41,713 - clearml.frameworks - INFO - Found existing registered model id=bc0cb0d7fc614e2e8b0edf5b85348646 [/workspaces/Thesis/src/notebooks/checkpoint.pt] reusing it.\n",
      "2023-11-08 22:51:42,239 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.e4983af7aad748e8ae71e5fa46767d60/models/checkpoint.pt\n",
      "2023-11-08 22:51:43,980 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.e4983af7aad748e8ae71e5fa46767d60/models/checkpoint.pt\n",
      "2023-11-08 22:51:45,684 - clearml.Task - INFO - Completed model upload to http://192.168.1.182:8081/Thesis/NrvForecast/Untitled%20Task.e4983af7aad748e8ae71e5fa46767d60/models/checkpoint.pt\n",
      "Early stopping triggered\n"
     ]
    }
   ],
   "source": [
    "#### Hyperparameters ####\n",
    "inputDim = 96\n",
    "learningRate = 0.0003\n",
    "epochs = 50\n",
    "\n",
    "# model = LinearRegression(inputDim, 1)\n",
    "model = NonLinearRegression(inputDim, 1, hiddenSize=1024, numLayers=5)\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=learningRate)\n",
    "\n",
    "#### Data Processor ####\n",
    "data_config = DataConfig()\n",
    "data_config.LOAD_FORECAST = False\n",
    "data_processor = DataProcessor(data_config)\n",
    "data_processor.set_batch_size(1024)\n",
    "\n",
    "\n",
    "data_processor.set_train_range((datetime(year=2015, month=1, day=1, tzinfo=pytz.UTC), datetime(year=2022, month=11, day=30, tzinfo=pytz.UTC)))\n",
    "data_processor.set_test_range((datetime(year=2023, month=1, day=1, tzinfo=pytz.UTC), np.inf))\n",
    "\n",
    "#### ClearML ####\n",
    "clearml_helper = ClearMLHelper(project_name=\"Thesis/NrvForecast\")\n",
    "\n",
    "#### Trainer ####\n",
    "trainer = AutoRegressiveTrainer(model, optimizer, nn.MSELoss(), data_processor, \"cuda\", debug=False, clearml_helper=clearml_helper)\n",
    "trainer.add_metrics_to_track([MSELoss(), L1Loss()])\n",
    "trainer.plot_every(10)\n",
    "trainer.early_stopping(patience=10)\n",
    "trainer.train(epochs=epochs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantile Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "DataProcessor.__init__() missing 1 required positional argument: 'data_config'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m/workspaces/Thesis/src/notebooks/training.ipynb Cell 7\u001b[0m line \u001b[0;36m1\n\u001b[1;32m     <a href='vscode-notebook-cell://dev-container%2B7b22686f737450617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f546865736973222c226c6f63616c446f636b6572223a66616c73652c22636f6e66696746696c65223a7b22246d6964223a312c2270617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f5468657369732f2e646576636f6e7461696e65722f646576636f6e7461696e65722e6a736f6e222c22736368656d65223a227673636f64652d66696c65486f7374227d7d@ssh-remote%2Bvictormylle.be/workspaces/Thesis/src/notebooks/training.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=13'>14</a>\u001b[0m data_config \u001b[39m=\u001b[39m DataConfig()\n\u001b[1;32m     <a href='vscode-notebook-cell://dev-container%2B7b22686f737450617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f546865736973222c226c6f63616c446f636b6572223a66616c73652c22636f6e66696746696c65223a7b22246d6964223a312c2270617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f5468657369732f2e646576636f6e7461696e65722f646576636f6e7461696e65722e6a736f6e222c22736368656d65223a227673636f64652d66696c65486f7374227d7d@ssh-remote%2Bvictormylle.be/workspaces/Thesis/src/notebooks/training.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=14'>15</a>\u001b[0m data_config\u001b[39m.\u001b[39mLOAD_FORECAST \u001b[39m=\u001b[39m \u001b[39mFalse\u001b[39;00m\n\u001b[0;32m---> <a href='vscode-notebook-cell://dev-container%2B7b22686f737450617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f546865736973222c226c6f63616c446f636b6572223a66616c73652c22636f6e66696746696c65223a7b22246d6964223a312c2270617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f5468657369732f2e646576636f6e7461696e65722f646576636f6e7461696e65722e6a736f6e222c22736368656d65223a227673636f64652d66696c65486f7374227d7d@ssh-remote%2Bvictormylle.be/workspaces/Thesis/src/notebooks/training.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=15'>16</a>\u001b[0m data_processor \u001b[39m=\u001b[39m DataProcessor()\n\u001b[1;32m     <a href='vscode-notebook-cell://dev-container%2B7b22686f737450617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f546865736973222c226c6f63616c446f636b6572223a66616c73652c22636f6e66696746696c65223a7b22246d6964223a312c2270617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f5468657369732f2e646576636f6e7461696e65722f646576636f6e7461696e65722e6a736f6e222c22736368656d65223a227673636f64652d66696c65486f7374227d7d@ssh-remote%2Bvictormylle.be/workspaces/Thesis/src/notebooks/training.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=16'>17</a>\u001b[0m data_processor\u001b[39m.\u001b[39mset_batch_size(\u001b[39m1024\u001b[39m)\n\u001b[1;32m     <a href='vscode-notebook-cell://dev-container%2B7b22686f737450617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f546865736973222c226c6f63616c446f636b6572223a66616c73652c22636f6e66696746696c65223a7b22246d6964223a312c2270617468223a222f686f6d652f766963746f726d796c6c652f53656144726976652f4d79204c69627261726965732f4750552d7365727665722f5468657369732f2e646576636f6e7461696e65722f646576636f6e7461696e65722e6a736f6e222c22736368656d65223a227673636f64652d66696c65486f7374227d7d@ssh-remote%2Bvictormylle.be/workspaces/Thesis/src/notebooks/training.ipynb#X10sdnNjb2RlLXJlbW90ZQ%3D%3D?line=18'>19</a>\u001b[0m data_processor\u001b[39m.\u001b[39mset_train_range((\u001b[39m-\u001b[39mnp\u001b[39m.\u001b[39minf, datetime(year\u001b[39m=\u001b[39m\u001b[39m2022\u001b[39m, month\u001b[39m=\u001b[39m\u001b[39m11\u001b[39m, day\u001b[39m=\u001b[39m\u001b[39m30\u001b[39m, tzinfo\u001b[39m=\u001b[39mpytz\u001b[39m.\u001b[39mUTC)))\n",
      "\u001b[0;31mTypeError\u001b[0m: DataProcessor.__init__() missing 1 required positional argument: 'data_config'"
     ]
    }
   ],
   "source": [
    "#### Hyperparameters ####\n",
    "inputDim = 96\n",
    "learningRate = 0.0003\n",
    "epochs = 100\n",
    "\n",
    "quantiles = torch.tensor([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]).to(\"cuda\")\n",
    "\n",
    "# model = LinearRegression(inputDim, len(quantiles))\n",
    "model = NonLinearRegression(inputDim, len(quantiles), hiddenSize=1024, numLayers=5, dropout=0.2)\n",
    "model.output_size = 1\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=learningRate)\n",
    "\n",
    "#### Data Processor ####\n",
    "data_config = DataConfig()\n",
    "data_config.LOAD_FORECAST = False\n",
    "data_processor = DataProcessor(data_config)\n",
    "data_processor.set_batch_size(1024)\n",
    "\n",
    "data_processor.set_train_range((-np.inf, datetime(year=2022, month=11, day=30, tzinfo=pytz.UTC)))\n",
    "data_processor.set_test_range((datetime(year=2023, month=1, day=1, tzinfo=pytz.UTC), np.inf))\n",
    "\n",
    "#### ClearML ####\n",
    "clearml_helper = ClearMLHelper(project_name=\"Thesis/NrvForecast\")\n",
    "\n",
    "#### Trainer ####\n",
    "trainer = QuantileTrainer(model, optimizer, data_processor, quantiles, \"cuda\", debug=True, clearml_helper=clearml_helper)\n",
    "trainer.add_metrics_to_track([PinballLoss(quantiles), MSELoss(), L1Loss()])\n",
    "trainer.early_stopping(patience=10)\n",
    "trainer.plot_every(10)\n",
    "trainer.train(epochs=epochs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
