{ "cells": [ { "cell_type": "markdown", "id": "41296cc6-9d84-47c5-8a92-2d292f6f3c4a", "metadata": { "papermill": { "duration": 0.01523, "end_time": "2024-01-31T17:49:38.190595", "exception": false, "start_time": "2024-01-31T17:49:38.175365", "status": "completed" }, "tags": [] }, "source": [ "# Plots" ] }, { "cell_type": "code", "execution_count": 1, "id": "9286e0b8-3c78-4b0f-943c-d219e9840dfe", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.399978600Z", "start_time": "2023-11-22T14:30:12.220250Z" }, "papermill": { "duration": 0.015281, "end_time": "2024-01-31T17:49:38.214428", "exception": false, "start_time": "2024-01-31T17:49:38.199147", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# Import to be able to import python package from src\n", "import sys\n", "sys.path.insert(0, '../src')" ] }, { "cell_type": "code", "execution_count": 2, "id": "2028eed7-b1c3-4c9e-b6a0-00433caa7d0f", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.438915100Z", "start_time": "2023-11-22T14:30:12.223262200Z" }, "papermill": { "duration": 2.494197, "end_time": "2024-01-31T17:49:40.714680", "exception": false, "start_time": "2024-01-31T17:49:38.220483", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "The `LightGBM` module could not be imported. To enable LightGBM support in Darts, follow the detailed instructions in the installation guide: https://github.com/unit8co/darts/blob/master/INSTALL.md\n", "The `Prophet` module could not be imported. To enable Prophet support in Darts, follow the detailed instructions in the installation guide: https://github.com/unit8co/darts/blob/master/INSTALL.md\n" ] } ], "source": [ "import pandas as pd\n", "import ontime as on\n", "from darts.datasets import EnergyDataset" ] }, { "cell_type": "markdown", "id": "e24da8ab-6a83-4c2f-9ff0-c633d4693a91", "metadata": { "papermill": { "duration": 0.004156, "end_time": "2024-01-31T17:49:40.723198", "exception": false, "start_time": "2024-01-31T17:49:40.719042", "status": "completed" }, "tags": [] }, "source": [ "## Load data" ] }, { "cell_type": "code", "execution_count": 3, "id": "db08372d-8ab2-4290-9196-76eb0c275629", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.886891300Z", "start_time": "2023-11-22T14:30:12.235870500Z" }, "papermill": { "duration": 0.070649, "end_time": "2024-01-31T17:49:40.797936", "exception": false, "start_time": "2024-01-31T17:49:40.727287", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "ts = EnergyDataset().load()" ] }, { "cell_type": "markdown", "id": "789865e9-e840-4267-994a-f10743e46279", "metadata": { "papermill": { "duration": 0.004193, "end_time": "2024-01-31T17:49:40.806517", "exception": false, "start_time": "2024-01-31T17:49:40.802324", "status": "completed" }, "tags": [] }, "source": [ "Complete TimeSeries" ] }, { "cell_type": "code", "execution_count": 4, "id": "009bcbe3-b73c-4355-b280-1bcb3d98e113", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.888403100Z", "start_time": "2023-11-22T14:30:12.382164600Z" }, "papermill": { "duration": 0.018375, "end_time": "2024-01-31T17:49:40.829148", "exception": false, "start_time": "2024-01-31T17:49:40.810773", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "df = ts.pd_dataframe()\n", "df = df.interpolate()\n", "cols = ['generation biomass', 'generation solar', 'generation nuclear']\n", "df = df[cols]" ] }, { "cell_type": "code", "execution_count": 5, "id": "7baaa1c5-c460-44bb-a375-f2eff4509cee", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.888403100Z", "start_time": "2023-11-22T14:30:12.406885100Z" }, "papermill": { "duration": 0.008708, "end_time": "2024-01-31T17:49:40.842048", "exception": false, "start_time": "2024-01-31T17:49:40.833340", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "ts = on.TimeSeries.from_dataframe(df)" ] }, { "cell_type": "markdown", "id": "07386885-6139-43ed-a695-c2fa3cf23904", "metadata": { "papermill": { "duration": 0.004101, "end_time": "2024-01-31T17:49:40.850253", "exception": false, "start_time": "2024-01-31T17:49:40.846152", "status": "completed" }, "tags": [] }, "source": [ "Prepare data" ] }, { "cell_type": "code", "execution_count": 6, "id": "07b54c1f-4215-43c0-8c81-bb337b9f50fb", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.888403100Z", "start_time": "2023-11-22T14:30:12.414642100Z" }, "papermill": { "duration": 0.010221, "end_time": "2024-01-31T17:49:40.864515", "exception": false, "start_time": "2024-01-31T17:49:40.854294", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "ts_uni = ts['generation solar'].slice(pd.Timestamp('2015'), pd.Timestamp('2016'))\n", "ts_multi = ts.slice(pd.Timestamp('2015'), pd.Timestamp('2016'))" ] }, { "cell_type": "markdown", "id": "a1a7ecfe-894b-42a8-8a94-5163f83b26a4", "metadata": {}, "source": [ "## Primitive Plots" ] }, { "cell_type": "markdown", "id": "a438a78b-3ed5-4501-a316-2414e881dbb3", "metadata": {}, "source": [ "### Line(s)" ] }, { "cell_type": "markdown", "id": "8c60d9ea-e027-4ba0-8fc6-87bfd8059084", "metadata": { "papermill": { "duration": 0.00435, "end_time": "2024-01-31T17:49:40.963591", "exception": false, "start_time": "2024-01-31T17:49:40.959241", "status": "completed" }, "tags": [] }, "source": [ "With univariate TimeSeries" ] }, { "cell_type": "code", "execution_count": 7, "id": "ad694d28-07ff-4563-81a6-84ffe6591b70", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_uni.head(400))\\\n", " .add(on.marks.line)\\\n", " .properties(width=600)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "4de943c7-b0fd-4943-8cb1-8ce496599864", "metadata": { "papermill": { "duration": 0.006692, "end_time": "2024-01-31T17:49:41.118951", "exception": false, "start_time": "2024-01-31T17:49:41.112259", "status": "completed" }, "tags": [] }, "source": [ "with multivariate TimeSeries" ] }, { "cell_type": "code", "execution_count": 8, "id": "2a4a0582-209b-461a-815b-7d4d4698c0f7", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.940457Z", "start_time": "2023-11-22T14:30:12.570624900Z" }, "papermill": { "duration": 0.105309, "end_time": "2024-01-31T17:49:41.230778", "exception": false, "start_time": "2024-01-31T17:49:41.125469", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_multi.head(400))\\\n", " .add(on.marks.line)\\\n", " .properties(width=600)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "2fd66619-22c4-43b5-b827-39b01d16e73b", "metadata": {}, "source": [ "### Dots" ] }, { "cell_type": "markdown", "id": "e1e97a4e-352d-446f-a70f-66c83e6754e1", "metadata": {}, "source": [ "With univariate TimeSeries" ] }, { "cell_type": "code", "execution_count": 9, "id": "f25a59e9-98d3-4519-bc03-feb9f6cd4d2a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_uni.head(400))\\\n", " .add(on.marks.dots)\\\n", " .properties(width=600)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "8b7e5730-9341-404d-93a9-f6811d2775d9", "metadata": {}, "source": [ "with multivariate TimeSeries" ] }, { "cell_type": "code", "execution_count": 10, "id": "e02804dd-0c24-40e4-9acc-8ed817f03a12", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_multi.head(400))\\\n", " .add(on.marks.dots)\\\n", " .properties(width=600)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "f9b6835a-2bbf-48d6-935d-fa6226cf45c1", "metadata": {}, "source": [ "### Areas" ] }, { "cell_type": "markdown", "id": "8c8f8fc6-ccfd-4c3f-b513-e14842e7849c", "metadata": {}, "source": [ "With a single time series" ] }, { "cell_type": "code", "execution_count": 11, "id": "7db16759-fbf2-46ef-a0da-4eea94f8ca60", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_uni.head(400))\\\n", " .add(on.marks.area)\\\n", " .properties(width=600)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "54c8348a-fafe-49c4-939f-77f1a7a2c9d6", "metadata": {}, "source": [ "With a multivariate time series it works with exactly two" ] }, { "cell_type": "code", "execution_count": 12, "id": "d6b18ef8-7954-4780-8e9a-dd6e8afd6d15", "metadata": {}, "outputs": [], "source": [ "from darts import concatenate" ] }, { "cell_type": "code", "execution_count": 13, "id": "70178386-e035-4c7a-91c2-ee206131635d", "metadata": {}, "outputs": [], "source": [ "# First we create the series with two components\n", "ts_ci = concatenate([\n", " ts_multi.univariate_component(0), \n", " ts_multi.univariate_component(1)\n", "], axis=1)\n", "ts_ci = on.TimeSeries.from_darts(ts_ci)" ] }, { "cell_type": "code", "execution_count": 14, "id": "98ca8c64-bcde-4e93-a0ec-df1410242f36", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Then we plot it\n", "on.Plot(ts_ci.head(200))\\\n", " .add(on.marks.area, title='Diff. between solar and biomass generation')\\\n", " .properties(width=600, height=200)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "f436ba6f-aa4f-47e8-9673-754c9c02e1b4", "metadata": { "papermill": { "duration": 0.013993, "end_time": "2024-01-31T17:49:41.259259", "exception": false, "start_time": "2024-01-31T17:49:41.245266", "status": "completed" }, "tags": [] }, "source": [ "### Heatmaps" ] }, { "attachments": {}, "cell_type": "markdown", "id": "94b74653-0b50-442f-a666-9bd0984f9781", "metadata": { "papermill": { "duration": 0.014072, "end_time": "2024-01-31T17:49:41.320059", "exception": false, "start_time": "2024-01-31T17:49:41.305987", "status": "completed" }, "tags": [] }, "source": [ "with univariate TimeSeries" ] }, { "cell_type": "code", "execution_count": 15, "id": "8a70db18-2736-4b52-8a49-a09908048535", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_uni.head(1000))\\\n", " .add(on.marks.heatmap)\\\n", " .properties(width=600, height=50)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "bb43360b-c5ff-4e95-9af8-7ced3fb66d7d", "metadata": { "papermill": { "duration": 0.01433, "end_time": "2024-01-31T17:49:41.415862", "exception": false, "start_time": "2024-01-31T17:49:41.401532", "status": "completed" }, "tags": [] }, "source": [ "with multivariate Heatmap" ] }, { "cell_type": "code", "execution_count": 23, "id": "95e245b4-d594-45f7-ae7a-04db01fc860d", "metadata": { "ExecuteTime": { "end_time": "2023-11-22T14:30:12.943458900Z", "start_time": "2023-11-22T14:30:12.778200200Z" }, "papermill": { "duration": 0.052047, "end_time": "2024-01-31T17:49:41.386466", "exception": false, "start_time": "2024-01-31T17:49:41.334419", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot(ts_multi.head(1000))\\\n", " .add(on.marks.heatmap)\\\n", " .properties(width=600, height=150)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "3b07d001-bef6-4b9b-86b6-105ad318adab", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "id": "8a52c3c7-3adf-454e-9b6d-b0959adcd8f6", "metadata": {}, "source": [ "## Combined Plots" ] }, { "cell_type": "markdown", "id": "c29b1243-bc85-4a8d-899a-710cbfc2d77b", "metadata": {}, "source": [ "Most of the plots in onTime can be combined as they are based on Altair layered charts. For instance, you can do the following to have a dots on a line." ] }, { "cell_type": "code", "execution_count": 24, "id": "e18041c8-4c74-4f63-be92-56ad1d971bf7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "" ], "text/plain": [ "alt.LayerChart(...)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "on.Plot()\\\n", " .add(on.marks.dots, ts_multi.univariate_component(1).head(400))\\\n", " .add(on.marks.line, ts_multi.univariate_component(0).head(400))\\\n", " .properties(width=600, height=200)\\\n", " .show()" ] }, { "cell_type": "markdown", "id": "8c1f4d05-d5b2-4d86-8cf0-6165175354eb", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "id": "e87e999c-4ffa-4f3a-92a8-db894982a2ae", "metadata": {}, "source": [ "## Thematic Plots" ] }, { "cell_type": "markdown", "id": "b65f9e0b-381a-4085-b919-3074b9b18b0a", "metadata": {}, "source": [ "### Forecasts" ] }, { "cell_type": "code", "execution_count": 25, "id": "79328857-d576-43d4-a54a-29196312448b", "metadata": {}, "outputs": [], "source": [ "ts_train, ts_test = ts_uni.split_before(0.9)" ] }, { "cell_type": "code", "execution_count": 26, "id": "53bdff9e-f198-4a24-9968-95c3efaf5ebb", "metadata": {}, "outputs": [], "source": [ "from ontime.context import common" ] }, { "cell_type": "code", "execution_count": 27, "id": "c592bccc-95bf-4b2d-9952-f1b4075288da", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "