diff --git a/notebooks/plotting.ipynb b/notebooks/plotting.ipynb index 4825ab4bbbc442da9a562bb283d96d1b42153c2d..66914d34eff2c98015a51ff0827583d1e5ba7283 100644 --- a/notebooks/plotting.ipynb +++ b/notebooks/plotting.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "e0d6a2e3", "metadata": {}, "outputs": [], @@ -41,14 +41,222 @@ " accelerator = 'cpu'" ] }, + { + "cell_type": "markdown", + "id": "b1aec6bf", + "metadata": {}, + "source": [ + "# Neuer Versuch" + ] + }, + { + "cell_type": "code", + "execution_count": 153, + "id": "13b1bcc5", + "metadata": {}, + "outputs": [], + "source": [ + "from utils.helpers import load_model\n", + "from torch.utils.data import DataLoader\n", + "from data_tools.datasets import TimeSeriesDataSet\n", + "from plotly.subplots import make_subplots\n", + "\n", + "model_dir = Path('../KIWaVo/models/lfu/sehmsdorf/lightning_logs/version_10/')\n", + "data_file = Path('../KIWaVo/data/input/MergedSehmsdorfAll.csv')" + ] + }, + { + "cell_type": "code", + "execution_count": 166, + "id": "c53eb9aa", + "metadata": {}, + "outputs": [], + "source": [ + "def load_df_pred(data_file : Path,model):\n", + " df = pd.read_csv(data_file, index_col=0, parse_dates=True)\n", + " if not isinstance(df.index,pd.core.indexes.datetimes.DatetimeIndex):\n", + " df = pd.read_csv(data_file,index_col=0,parse_dates=True,date_format='%d.%m.%Y %H:00')\n", + "\n", + " df = ut.fill_missing_values(df)\n", + "\n", + " if model.hparams.differencing == 1:\n", + " df[\"d1\"] = df[df.columns[model.hparams.gauge_idx]].diff()\n", + "\n", + " if model.hparams.embed_time:\n", + " hours = df.index.hour\n", + " hour_rad = torch.tensor(2*torch.pi*hours/24)\n", + " df[\"h_sin\"] = torch.sin(hour_rad)\n", + " df[\"h_cos\"] = torch.cos(hour_rad)\n", + "\n", + " days = df.index.day_of_year + hours / 24\n", + " days_rad = torch.tensor(2*torch.pi*days/365)\n", + " df[\"d_sin\"] = torch.sin(days_rad)\n", + " df[\"d_cos\"] = torch.cos(days_rad)\n", + " columns = ['h_sin', 'h_cos', 'd_sin', 'd_cos'] + [col for col in df.columns if col not in ['h_sin', 'h_cos', 'd_sin', 'd_cos']]\n", + " df = df[columns]\n", + "\n", + " return df[1:]\n", + "\n", + "def load_data_pred(data_file,model):\n", + " df = load_df_pred(data_file,model)\n", + " df_scaled = pd.DataFrame(model.scaler.transform(df),index=df.index,columns=df.columns)\n", + " x = torch.Tensor(df_scaled.values) #[start:end]\n", + " y = torch.Tensor(df_scaled[df.columns[model.hparams.target_idx]].values)#[start:end]]\n", + "\n", + " dataset = TimeSeriesDataSet(x,y,model.in_size,model.out_size)\n", + "\n", + " y_true = df[df.columns[model.hparams.gauge_idx]][model.in_size-1:-model.out_size-1]\n", + "\n", + " data_loader = DataLoader(dataset, batch_size=256,num_workers=8)\n", + "\n", + " return df, df_scaled, data_loader,y_true\n", + "\n", + "def plot(y_pred,start=None,end=None,cols=None):\n", + " if start is None:\n", + " start = y_pred.index[0]\n", + " elif isinstance(start,str):\n", + " start = pd.to_datetime(start)\n", + " if end is None:\n", + " end = y_pred.index[-1]\n", + " elif isinstance(end,str):\n", + " end = pd.to_datetime(end)\n", + " if cols is None:\n", + " cols = [1,12,24,48]\n", + "\n", + " y_pred_sel = y_pred[start:end]\n", + "\n", + " fig = go.Figure()\n", + " fig.add_trace(go.Scatter(x=y_pred_sel.index,y=y_pred_sel[0],name='True',line=dict(width=3)))\n", + "\n", + " for i in cols:\n", + " fig.add_trace(go.Scatter(x=y_pred_sel.index[i:], y=y_pred_sel[i][:-i], mode='lines', name=f\"Pred {i}\"))\n", + " return fig\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "43fc20f5", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, - "id": "e4ac7073", + "id": "044c3bbb", + "metadata": {}, + "outputs": [], + "source": [ + "model = load_model(model_dir)\n", + "df, df_scaled, data_loader,y_true= load_data_pred(data_file,model)\n", + "y_pred = hp.get_pred(model,data_loader,y_true)" + ] + }, + { + "cell_type": "code", + "execution_count": 143, + "id": "54c6c264", "metadata": {}, "outputs": [], "source": [] }, + { + "cell_type": "code", + "execution_count": 169, + "id": "ab939e21", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "def plot_cont(y_pred,start=None,end=None,hours=None):\n", + " if start is None:\n", + " start = y_pred.index[0]\n", + " elif isinstance(start,str):\n", + " start = pd.to_datetime(start)\n", + " if end is None:\n", + " end = y_pred.index[-1]\n", + " elif isinstance(end,str):\n", + " end = pd.to_datetime(end)\n", + " if hours is None:\n", + " hours = [0]\n", + "\n", + " y_pred_sel = y_pred[start:end]\n", + " y_pred_sel2 = y_pred_sel[y_pred_sel.index.hour.isin(hours)]\n", + "\n", + " fig = go.Figure()\n", + " fig.add_trace(go.Scatter(x=y_pred_sel.index,y=y_pred_sel[0],name='True',line=dict(width=3)))\n", + "\n", + " # Create figure with secondary y-axis\n", + " fig = make_subplots(specs=[[{\"secondary_y\": True}]])\n", + "\n", + " fig.add_trace(go.Scatter(x=y_pred_sel.index,y=y_pred_sel[0],name='True',line=dict(width=5)))\n", + " for idx, row in y_pred_sel2.iterrows():\n", + "\n", + " x = pd.date_range(idx,periods=48+1,freq='h')[1:]\n", + " y = row[1:]\n", + " fig.add_trace(go.Scatter(x=x,y=y,name=idx.strftime('%Y-%m-%d %h')))\n", + "\n", + "\n", + " return fig\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf4f39fa", + "metadata": {}, + "outputs": [], + "source": [ + "start=pd.to_datetime('2024-01-01')\n", + "end=pd.to_datetime('2024-06-02')\n", + "fig = plot_cont(y_pred,start,end,hours=[0,6,12,18])\n", + "\n", + "fig.add_trace(go.Scatter(x=df[start:end].index,y=df[\"NSehmsdorf_mm\"][start:end],name='Rain'),secondary_y=True)\n", + "fig" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9d86847e", + "metadata": {}, + "outputs": [], + "source": [ + "plot(y_pred,start='2024-01-01',end='2024-06-02',cols=[1,12,24,48])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e204fc12", + "metadata": {}, + "outputs": [], + "source": [ + "start=pd.to_datetime('2024-01-01')\n", + "end=pd.to_datetime('2024-06-02')\n", + "\n", + "y_pred_sel = y_pred[start:end]\n", + "\n", + "fig = go.Figure()\n", + "\n", + "fig.add_trace(go.Scatter(x=y_pred_sel.index,y=y_pred_sel[0],name='True',line=dict(width=3)))\n", + "\n", + "for i in [1,24,48]:\n", + " fig.add_trace(go.Scatter(x=y_pred_sel.index[i:],y=y_pred_sel[i][:-i],name=f'Pred {i}'))\n", + "\n", + "fig" + ] + }, + { + "cell_type": "markdown", + "id": "11ae694c", + "metadata": {}, + "source": [ + "# Alter Versuch" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1573,6 +1781,34 @@ "fig.update_layout(xaxis_title=\"Zeit\",yaxis_title=\"Wasserstand [cm]\")\n", "fig.write_html(\"pötrau.html\")" ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f1d5c345", + "metadata": {}, + "outputs": [], + "source": [ + "temp = {\"a\":1,\"b\":2}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "892f855c", + "metadata": {}, + "outputs": [], + "source": [ + "temp.get(\"c\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d96cf6a1", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {