diff --git a/Examples_from_manuscript.ipynb b/Examples_from_manuscript.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..2c5cca616959893bd4d5ed43b577f29c419ccaf2 --- /dev/null +++ b/Examples_from_manuscript.ipynb @@ -0,0 +1,213 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# MLAir (v1.0) - Examples\n", + "\n", + "This notebook contains all examples as provided in Leufen et al. (2020). \n", + "Please follow the installation instructions provided in the [README](https://gitlab.version.fz-juelich.de/toar/mlair/-/blob/master/README.md) on gitlab. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 1\n", + "\n", + "The following cell imports MLAir and executes a minimalistic toy experiment. This cell is equivalent to Figure 2 in the manuscript." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import mlair\n", + "\n", + "# just give it a dry run without any modifications\n", + "mlair.run()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 2 \n", + "\n", + "In the following cell we use other station IDs provided as a list of strings (see also [JOIN-Web interface](https://join.fz-juelich.de/services/rest/surfacedata/) of the TOAR database for more details).\n", + "Moreover, we expand the `window_history_size` to 14 days and run the experiment. This cell is equivalent to Figure 3 in the manuscript." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# our new stations to use\n", + "stations = ['DEBW030', 'DEBW037', 'DEBW031', 'DEBW015', 'DEBW107']\n", + "\n", + "# expanded temporal context to 14 (days, because of default sampling=\"daily\")\n", + "window_history_size = 14\n", + "\n", + "# restart the experiment with little customisation\n", + "mlair.run(stations=stations, \n", + " window_history_size=window_history_size)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 3 \n", + "\n", + "The following cell loads the trained model from Example 2 and generates predictions for the two specified stations. \n", + "To ensure that the model is not retrained the keywords `create_new_model` and `train_model` are set to `False`. This cell is equivalent to Figure 4 in the manuscript. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# our new stations to use\n", + "stations = ['DEBY002', 'DEBY079']\n", + "\n", + "# same setting for window_history_size\n", + "window_history_size = 14\n", + "\n", + "# run experiment without training\n", + "mlair.run(stations=stations, \n", + " window_history_size=window_history_size, \n", + " create_new_model=False, \n", + " train_model=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 4\n", + "\n", + "The following cell demonstrates how a user defined model can be implemented by inheriting from `AbstractModelClass`. Within the `__init__` method `super().__init__`, `set_model` and `set_compile_options` should be called. Moreover, it is possible to set custom objects by calling `set_custom_objects`. Those custom objects are used to re-load the model (see also Keras documentation). For demonstration, the loss is added as custom object which is not required because a Keras built-in function is used as loss.\n", + "\n", + "The Keras-model itself is defined in `set_model` by using the sequential or functional Keras API. All compile options can be defined in `set_compile_options`.\n", + "This cell is equivalent to Figure 5 in the manuscript." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Figure 5\n", + "import keras\n", + "from keras.losses import mean_squared_error as mse\n", + "from keras.layers import PReLU, Input, Conv2D, Flatten, Dropout, Dense\n", + "\n", + "from mlair.model_modules import AbstractModelClass\n", + "\n", + "class MyCustomisedModel(AbstractModelClass):\n", + "\n", + " \"\"\"\n", + " A customised model with a 1x1 Conv, and 2 Dense layers (16, \n", + " output shape). Dropout is used after Conv layer.\n", + " \"\"\"\n", + " def __init__(self, shape_inputs: list, shape_outputs: list):\n", + " \n", + " # set attributes shape_inputs and shape_outputs\n", + " super().__init__(shape_inputs[0], shape_outputs[0])\n", + "\n", + " # apply to model\n", + " self.set_model()\n", + " self.set_compile_options()\n", + " self.set_custom_objects(loss=self.compile_options['loss'])\n", + "\n", + " def set_model(self):\n", + " x_input = Input(shape=self.shape_inputs)\n", + " x_in = Conv2D(4, (1, 1))(x_input)\n", + " x_in = PReLU()(x_in)\n", + " x_in = Flatten()(x_in)\n", + " x_in = Dropout(0.1)(x_in)\n", + " x_in = Dense(16)(x_in)\n", + " x_in = PReLU()(x_in)\n", + " x_in = Dense(self.shape_outputs)(x_in)\n", + " out = PReLU()(x_in)\n", + " self.model = keras.Model(inputs=x_input, outputs=[out])\n", + "\n", + " def set_compile_options(self):\n", + " self.initial_lr = 1e-2\n", + " self.optimizer = keras.optimizers.SGD(lr=self.initial_lr, momentum=0.9)\n", + " self.loss = mse\n", + " self.compile_options = {\"metrics\": [\"mse\", \"mae\"]}\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Example 5 \n", + "\n", + "Embedding of a custom Run Module in a modified MLAir workflow. In comparison to examples 1 to 4, this code example works on a single step deeper regarding the level of abstraction. Instead of calling the run method of MLAir, the user needs to add all stages individually and is responsible for all dependencies between the stages. By using the `Workflow` class as context manager, all stages are automatically connected with the result that all stages can easily be plugged in. This cell is equivalent to Figure 6 in the manuscript." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import logging\n", + "\n", + "class CustomStage(mlair.RunEnvironment):\n", + " \"\"\"A custom MLAir stage for demonstration.\"\"\"\n", + " def __init__(self, test_string):\n", + " super().__init__() # always call super init method\n", + " self._run(test_string) # call a class method\n", + " \n", + " def _run(self, test_string):\n", + " logging.info(\"Just running a custom stage.\")\n", + " logging.info(\"test_string = \" + test_string)\n", + " epochs = self.data_store.get(\"epochs\")\n", + " logging.info(\"epochs = \" + str(epochs))\n", + " \n", + " \n", + "# create your custom MLAir workflow\n", + "CustomWorkflow = mlair.Workflow()\n", + "# provide stages without initialisation\n", + "CustomWorkflow.add(mlair.ExperimentSetup, epochs=128)\n", + "# add also keyword arguments for a specific stage\n", + "CustomWorkflow.add(CustomStage, test_string=\"Hello World\")\n", + "# finally execute custom workflow in order of adding\n", + "CustomWorkflow.run()\n", + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python (mlt_new)", + "language": "python", + "name": "venv" + }, + "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.6.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}