{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Fixating parameters while fitting\n", " \n", "*Developed by Mark Bakker, TU Delft*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Required files to run this notebook (all available from the `data` subdirectory):\n", "\n", "* Head files: `head_nb1.csv`\n", "* Pricipitation files: `rain_nb1.csv`\n", "* Evaporation files: `evap_nb1.csv`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pastas\n", "Pastas is a computer program for hydrological time series analysis and is available from the [Pastas Github](https://github.com/pastas/pastas) . Pastas makes heavy use of `pandas` `timeseries`. An introduction to `pandas` `timeseries` can be found, for example, [here](http://nbviewer.jupyter.org/github/mbakker7/exploratory_computing_with_python/blob/master/notebook8_pandas/py_exploratory_comp_8_sol.ipynb). The Pastas documentation is available [here](http://pastas.readthedocs.io)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import pastas as ps\n", "import matplotlib.pyplot as plt\n", "\n", "ps.set_log_level(\"ERROR\")\n", "ps.show_versions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the head observations\n", "The first step in time series analysis is to load a time series of head observations. The time series needs to be stored as a `pandas.Series` object where the index is the date (and time, if desired). `pandas` provides many options to load time series data, depending on the format of the file that contains the time series. In this example, measured heads are stored in the csv file `head_nb1.csv`. \n", "The heads are read from a csv file with the `read_csv` function of `pandas` and are then squeezed to create a `pandas Series` object. To check if you have the correct data type, use the `type` command as shown below. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ho = pd.read_csv(\"data/head_nb1.csv\", parse_dates=[\"date\"], index_col=\"date\").squeeze()\n", "print(\"The data type of the oseries is:\", type(ho))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable `ho` is now a `pandas Series` object. To see the first five lines, type `ho.head()`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ho.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The series can be plotted as follows" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ho.plot(style=\".\", figsize=(12, 4))\n", "plt.ylabel(\"Head [m]\")\n", "plt.xlabel(\"Time [years]\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load the stresses\n", "The head variation shown above is believed to be caused by two stresses: rainfall and evaporation. Measured rainfall is stored in the file `rain_nb1.csv` and measured potential evaporation is stored in the file `evap_nb1.csv`. \n", "The rainfall and potential evaporation are loaded and plotted." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rain = pd.read_csv(\n", " \"data/rain_nb1.csv\", parse_dates=[\"date\"], index_col=\"date\"\n", ").squeeze()\n", "print(\"The data type of the rain series is:\", type(rain))\n", "\n", "evap = pd.read_csv(\n", " \"data/evap_nb1.csv\", parse_dates=[\"date\"], index_col=\"date\"\n", ").squeeze()\n", "print(\"The data type of the evap series is\", type(evap))\n", "\n", "plt.figure(figsize=(12, 4))\n", "rain.plot(label=\"rain\")\n", "evap.plot(label=\"evap\")\n", "plt.xlabel(\"Time [years]\")\n", "plt.ylabel(\"Rainfall/Evaporation (m/d)\")\n", "plt.legend(loc=\"best\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recharge\n", "As a first simple model, the recharge is approximated as the measured rainfall minus the measured potential evaporation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "recharge = rain - evap\n", "plt.figure(figsize=(12, 4))\n", "recharge.plot()\n", "plt.xlabel(\"Time [years]\")\n", "plt.ylabel(\"Recharge (m/d)\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### First time series model\n", "Once the time series are read from the data files, a time series model can be constructed by going through the following three steps:\n", "\n", "1. Create a `Model` object by passing it the observed head series. Store your model in a variable so that you can use it later on. \n", "2. Add the stresses that are expected to cause the observed head variation to the model. In this example, this is only the recharge series. For each stess, a `StressModel` object needs to be created. Each `StressModel` object needs three input arguments: the time series of the stress, the response function that is used to simulate the effect of the stress, and a name. In addition, it is recommended to specified the `kind` of series, which is used to perform a number of checks on the series and fix problems when needed. This checking and fixing of problems (for example, what to substitute for a missing value) depends on the kind of series. In this case, the time series of the stress is stored in the variable `recharge`, the Gamma function is used to simulate the response, the series will be called `'recharge'`, and the kind is `prec` which stands for precipitation. One of the other keyword arguments of the `StressModel` class is `up`, which means that a positive stress results in an increase (up) of the head. The default value is `True`, which we use in this case as a positive recharge will result in the heads going up. Each `StressModel` object needs to be stored in a variable, after which it can be added to the model. \n", "3. When everything is added, the model can be solved. The default option is to minimize the sum of the squares of the errors between the observed and modeled heads. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ml = ps.Model(ho)\n", "sm1 = ps.StressModel(recharge, ps.Gamma(), name=\"recharge\", settings=\"prec\")\n", "ml.add_stressmodel(sm1)\n", "ml.solve(tmin=\"1985\", tmax=\"2010\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `solve` function has a number of default options that can be specified with keyword arguments. One of these options is that by default a fit report is printed to the screen. The fit report includes a summary of the fitting procedure, the optimal values obtained by the fitting routine, and some basic statistics. The model contains five parameters: the parameters $A$, $n$, and $a$ of the Gamma function used as the response function for the recharge, the parameter $d$, which is a constant base level, and the parameter $\\alpha$ of the noise model, which will be explained a little later on in this notebook.\n", "The results of the model are plotted below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ml.plot(figsize=(12, 4));" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ml = ps.Model(ho)\n", "sm1 = ps.StressModel(recharge, ps.Gamma(), name=\"recharge\", settings=\"prec\")\n", "ml.add_stressmodel(sm1)\n", "ml.solve(tmin=\"1985\", tmax=\"2010\", solver=ps.LeastSquares())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ml = ps.Model(ho)\n", "sm1 = ps.StressModel(recharge, ps.Gamma(), name=\"recharge\", settings=\"prec\")\n", "ml.add_stressmodel(sm1)\n", "ml.set_parameter(\"recharge_n\", vary=False)\n", "ml.solve(tmin=\"1985\", tmax=\"2010\", solver=ps.LeastSquares())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ml.plot(figsize=(10, 4));" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "pastas_dev", "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.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:41:22) [MSC v.1929 64 bit (AMD64)]" }, "vscode": { "interpreter": { "hash": "29475f8be425919747d373d827cb41e481e140756dd3c75aa328bf3399a0138e" } }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 4 }