{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": [ "# Logging 📄\n", "\n", "This notebook introduces the concept of logging using Python's built-in logging module. Logging is essential for tracking events that happen when some software runs. The module provides a way to configure different log handlers and severity levels.\n", "\n", "This notebook can be downloaded from the source code [here](https://github.com/andersretznerSGU/gwrefpy/blob/main/docs/user_guide/6_logging.ipynb).\n", "\n", "The logging levels available in `gwrefpy` are:\n", "- `DEBUG`: Detailed information, typically of interest only when diagnosing problems.\n", "- `INFO`: Confirmation that things are working as expected. This is the default logging level.\n", "- `WARNING`: An indication that something unexpected happened, or indicative of some problem in the near future\n", "- `ERROR`: Due to a more serious problem, the software has not been able to perform some function. Typically, these are issues will also raise exceptions." ], "id": "7089c522dababc77" }, { "metadata": {}, "cell_type": "markdown", "source": "Let's start by importing the necessary libraries and setting the logging level to `DEBUG` using the `set_logging_level` function.", "id": "e90d19164555a7c1" }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-25T18:54:08.122694Z", "start_time": "2025-09-25T18:54:07.395160Z" } }, "cell_type": "code", "source": [ "import gwrefpy as gr\n", "import numpy as np\n", "import pandas as pd\n", "\n", "gr.set_log_level(\"DEBUG\") # Set logging level to DEBUG" ], "id": "8055b90446a20e26", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Log level set to DEBUG\n" ] } ], "execution_count": 1 }, { "metadata": {}, "cell_type": "markdown", "source": "We can now create some well objects and a model to see the logging in action.", "id": "820ba12d41db682c" }, { "metadata": { "tags": [ "hide-input" ], "ExecuteTime": { "end_time": "2025-09-25T18:54:08.147597Z", "start_time": "2025-09-25T18:54:08.142975Z" } }, "cell_type": "code", "source": [ "# Create some example data\n", "n_days = 100\n", "dates = pd.date_range(\"2020-01-01\", periods=n_days, freq=\"D\")\n", "\n", "# Observed and reference values with some noise\n", "values_obs1 = (\n", " 25.75\n", " + 0.7 * np.sin(np.linspace(0, 4 * np.pi, n_days))\n", " + np.random.normal(0, 0.1, n_days)\n", ")\n", "values_obs1 = pd.Series(values_obs1, index=dates)\n", "values_ref1 = (\n", " 18.75\n", " + 0.3 * np.sin(np.linspace(0, 4 * np.pi, n_days))\n", " + np.random.normal(0, 0.05, n_days)\n", ")\n", "values_ref1 = pd.Series(values_ref1, index=dates)" ], "id": "1aa3bef7234032fc", "outputs": [], "execution_count": 2 }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-25T18:54:08.164174Z", "start_time": "2025-09-25T18:54:08.158764Z" } }, "cell_type": "code", "source": [ "# Creat the observed and reference wells\n", "obs1 = gr.Well(name=\"Obs. well\", is_reference=False)\n", "obs1.add_timeseries(values_obs1) # <-- Here we will see logging messages\n", "ref1 = gr.Well(name=\"Ref. well\", is_reference=True)\n", "ref1.add_timeseries(values_ref1) # <-- Here we will see logging messages\n", "\n", "# Create the model and add the wells\n", "model1 = gr.Model(name=\"Logging Example Model\")\n", "model1.add_well(obs1) # <-- Here we will see logging messages\n", "model1.add_well(ref1) # <-- Here we will see logging messages" ], "id": "d4cf851212fa7c3b", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added timeseries to well Obs. well\n", "Added timeseries to well Ref. well\n", "Well 'Obs. well' added to model 'Logging Example Model'.\n", "Well 'Ref. well' added to model 'Logging Example Model'.\n" ] } ], "execution_count": 3 }, { "metadata": {}, "cell_type": "markdown", "source": "As you can see, we got logging messages in the console when adding the wells to the model. Now let's fit the model to see more logging messages.", "id": "96d264e429d5e067" }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-25T18:54:09.468831Z", "start_time": "2025-09-25T18:54:08.925472Z" } }, "cell_type": "code", "source": [ "# Fit the model\n", "model1.fit(\n", " obs1,\n", " ref1,\n", " offset=\"0D\",\n", " tmin=dates[0],\n", " tmax=dates[-21],\n", ")" ], "id": "c97e4e0ebc9559fc", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using linear regression method for fitting.\n", "Fit completed for model 'Logging Example Model' with RMSE 0.15953090249600974.\n", "Fitting model 'Logging Example Model' using reference well 'Ref. well' and observation well 'Obs. well'.\n", "Fit Results: Obs. well ~ Ref. well\n", "==================================\n", "Statistic Value Description\n", "--------------------------------------------------\n", "RMSE 0.1595 Root Mean Square Error\n", "R² 0.8928 Coefficient of Determination\n", "R-value 0.9449 Correlation Coefficient\n", "Slope 2.2887 Linear Regression Slope\n", "Intercept -17.1899 Linear Regression Intercept\n", "P-value 0.0000 Statistical Significance\n", "N 80 Number of Data Points\n", "Std Error 0.1616 Standard Error\n", "Confidence 95.0 % Confidence Level\n", "\n", "Calibration Period: 2020-01-01 00:00:00 to 2020-03-20 00:00:00\n", "Time Offset: 0D\n", "Aggregation Method: mean\n" ] }, { "data": { "text/plain": [ "FitResultData(ref_well='Ref. well', obs_well='Obs. well', rmse=0.1595, r²=0.8928, n=80)" ], "text/html": [ "\n", "
| Statistic | \n", "Value | \n", "Description | \n", "
|---|---|---|
| RMSE | \n", "0.1595 | \n", "Root Mean Square Error | \n", "
| R² | \n", "0.8928 | \n", "Coefficient of Determination | \n", "
| R-value | \n", "0.9449 | \n", "Correlation Coefficient | \n", "
| Slope | \n", "2.2887 | \n", "Linear Regression Slope | \n", "
| Intercept | \n", "-17.1899 | \n", "Linear Regression Intercept | \n", "
| P-value | \n", "0.0000 | \n", "Statistical Significance | \n", "
| N | \n", "80 | \n", "Number of Data Points | \n", "
| Std Error | \n", "0.1616 | \n", "Standard Error | \n", "
| Confidence | \n", "95.0% | \n", "Confidence Level | \n", "
\n",
" Calibration Period: 2020-01-01 00:00:00 to 2020-03-20 00:00:00
\n",
" Time Offset: 0D
\n",
" Aggregation Method: mean\n",
"
| Statistic | \n", "Value | \n", "Description | \n", "
|---|---|---|
| RMSE | \n", "0.1712 | \n", "Root Mean Square Error | \n", "
| R² | \n", "0.8609 | \n", "Coefficient of Determination | \n", "
| R-value | \n", "0.9278 | \n", "Correlation Coefficient | \n", "
| Slope | \n", "2.1421 | \n", "Linear Regression Slope | \n", "
| Intercept | \n", "-14.3748 | \n", "Linear Regression Intercept | \n", "
| P-value | \n", "0.0000 | \n", "Statistical Significance | \n", "
| N | \n", "80 | \n", "Number of Data Points | \n", "
| Std Error | \n", "0.1734 | \n", "Standard Error | \n", "
| Confidence | \n", "95.0% | \n", "Confidence Level | \n", "
\n",
" Calibration Period: 2020-01-01 00:00:00 to 2020-03-20 00:00:00
\n",
" Time Offset: 0D
\n",
" Aggregation Method: mean\n",
"