From 30cbfc0c9fafbe78e64bed5720da1183d8102bf5 Mon Sep 17 00:00:00 2001 From: leufen1 <l.leufen@fz-juelich.de> Date: Fri, 18 Sep 2020 18:09:14 +0200 Subject: [PATCH] add example for data handlers to docs too --- docs/_source/customise.rst | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docs/_source/customise.rst b/docs/_source/customise.rst index 24b9492f..4c9ee538 100644 --- a/docs/_source/customise.rst +++ b/docs/_source/customise.rst @@ -248,6 +248,53 @@ Custom Data Handler * (optionally) Overwrite the base class :py:`self.get_coordinates()` method to return coordinates as dictionary with keys *lon* and *lat*. +.. code-block:: python + + import datetime as dt + import numpy as np + import pandas as pd + import xarray as xr + + from mlair.data_handler import AbstractDataHandler + + class DummyDataHandler(AbstractDataHandler): + + def __init__(self, name, number_of_samples=None): + """This data handler takes a name argument and the number of samples to generate. If not provided, a random + number between 100 and 150 is set.""" + super().__init__() + self.name = name + self.number_of_samples = number_of_samples if number_of_samples is not None else np.random.randint(100, 150) + self._X = self.create_X() + self._Y = self.create_Y() + + def create_X(self): + """Inputs are random numbers between 0 and 10 with shape (no_samples, window=14, variables=5).""" + X = np.random.randint(0, 10, size=(self.number_of_samples, 14, 5)) # samples, window, variables + datelist = pd.date_range(dt.datetime.today().date(), periods=self.number_of_samples, freq="H").tolist() + return xr.DataArray(X, dims=['datetime', 'window', 'variables'], coords={"datetime": datelist, + "window": range(14), + "variables": range(5)}) + + def create_Y(self): + """Targets are normal distributed random numbers with shape (no_samples, window=5, variables=1).""" + Y = np.round(0.5 * np.random.randn(self.number_of_samples, 5, 1), 1) # samples, window, variables + datelist = pd.date_range(dt.datetime.today().date(), periods=self.number_of_samples, freq="H").tolist() + return xr.DataArray(Y, dims=['datetime', 'window', 'variables'], coords={"datetime": datelist, + "window": range(5), + "variables": range(1)}) + + def get_X(self, upsampling=False, as_numpy=False): + """Upsampling parameter is not used for X.""" + return np.copy(self._X) if as_numpy is True else self._X + + def get_Y(self, upsampling=False, as_numpy=False): + """Upsampling parameter is not used for Y.""" + return np.copy(self._Y) if as_numpy is True else self._Y + + def __str__(self): + return self.name + Customised Run Module and Workflow ---------------------------------- -- GitLab