{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": [ "# Saving and loading models\n", "\n", "This notebook introduces the concept of saving and loading `.gwref` files.\n", "\n", "This notebook can be downloaded from the source code [here](https://github.com/andersretznerSGU/gwrefpy/blob/main/docs/user_guide/5_io.ipynb).\n", "\n", "# Saving a model 💾\n", "\n", "You can save a `gwrefpy` model to a `.gwref` file using the `save_project` function of the `Model` class. This function saves the model, including all wells and fits, to a specified file." ], "id": "f8ebfe5589158bd0" }, { "metadata": {}, "cell_type": "markdown", "source": "We will first create a simple model with one observed well and one reference well.", "id": "a7d320daa081a770" }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-19T06:19:23.202275Z", "start_time": "2025-09-19T06:19:21.423475Z" } }, "cell_type": "code", "source": [ "import gwrefpy as gr\n", "import numpy as np\n", "import pandas as pd" ], "id": "ae7b6686cad2a72f", "outputs": [], "execution_count": 3 }, { "metadata": { "tags": [ "hide-input" ], "ExecuteTime": { "end_time": "2025-09-19T06:19:23.878Z", "start_time": "2025-09-19T06:19:23.865523Z" } }, "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": "e9337241bbe35e1a", "outputs": [], "execution_count": 4 }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-19T06:19:25.524811Z", "start_time": "2025-09-19T06:19:25.522288Z" } }, "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)\n", "ref1 = gr.Well(name=\"Ref. well\", is_reference=True)\n", "ref1.add_timeseries(values_ref1)\n", "\n", "# Create the model and add the wells\n", "model1 = gr.Model(name=\"IO Model\")\n", "model1.add_well(obs1)\n", "model1.add_well(ref1)" ], "id": "dd1b89e7b780e90d", "outputs": [], "execution_count": 5 }, { "metadata": {}, "cell_type": "markdown", "source": [ "We can now save the model to a `.gwref` file using the `save_project` function.\n", "```{note}\n", "The funciton will create the file in the current working directory if no path is specified.\n", "```" ], "id": "3f437ef415865dca" }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-19T06:21:35.698499Z", "start_time": "2025-09-19T06:21:35.680754Z" } }, "cell_type": "code", "source": [ "# Save the model to a .gwref file\n", "model1.save_project(\"my_model.gwref\")" ], "id": "cff5a365dc580981", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model 'Logging Example Model' saved to 'example_model.gwref'.\n" ] } ], "execution_count": 9 }, { "metadata": {}, "cell_type": "markdown", "source": [ "You can now find the `my_model.gwref` files in your working directory. You can also give a path with the filename and the fild will be saved to that path, e.g. `\"path//to//folder//my_model.gwref\"`. \n", "```{warning}\n", "If the file already exists, a warning will be logged and the file will not be overwritten. You can set the ``overwrite`` variable to `True` to overwrite the existing file.\n", "\n", "The funciton will not create any folders in the specified path if they do not exist.\n", "```" ], "id": "d2a3d40fe6011460" }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-19T06:21:50.470844Z", "start_time": "2025-09-19T06:21:50.464055Z" } }, "cell_type": "code", "source": [ "# Save the model to a .gwref file, allowing overwriting\n", "model1.save_project(\"my_model.gwref\") # This will not overwrite the existing file and a warning will be logged\n", "model1.save_project(\"my_model.gwref\", overwrite=True) # This will overwrite the existing file" ], "id": "8927630e5d539af5", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The file 'example_model.gwref' already exists. To overwrite the existing file set the argument 'overwrite' to True.\n", "Model 'Logging Example Model' saved to 'example_model.gwref'.\n" ] } ], "execution_count": 10 }, { "metadata": {}, "cell_type": "markdown", "source": [ "## Loading Models 📦\n", "You can load a model from a `.gwref` file using the `open_project` function from the `Model` class. This function reads the model from the specified file and saves it to the `Model` object. The model will include all wells and fits that were saved in the file.\n", "\n", "There are two ways to load a model:\n", "1. Create a new `Model` object and use the `open_project` method.\n", "2. Specify the `.gwref` file directly when creating a new `Model` object." ], "id": "ab0429e99fb8f13e" }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-18T11:56:24.801814Z", "start_time": "2025-09-18T11:56:24.769652Z" } }, "cell_type": "code", "source": [ "# Method 1: Create a new Model object and load the project\n", "loaded_model1 = gr.Model(name=\"Name will be overwritten\")\n", "loaded_model1.open_project(\"my_model.gwref\")" ], "id": "30f2208061681fe", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model 'Logging Example Model' loaded from 'example_model.gwref'.\n" ] } ], "execution_count": 3 }, { "metadata": { "ExecuteTime": { "end_time": "2025-09-18T11:56:24.887100Z", "start_time": "2025-09-18T11:56:24.877035Z" } }, "cell_type": "code", "source": [ "# Method 2: Directly load the project into a new Model object\n", "loaded_model2 = gr.Model(\"my_model.gwref\")" ], "id": "5b2b524c40b0d03", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Model 'Logging Example Model' loaded from 'example_model.gwref'.\n" ] } ], "execution_count": 4 }, { "metadata": {}, "cell_type": "markdown", "source": "This concludes this notebook on saving and loading models in `gwrefpy`.", "id": "e30b422c68de2ecf" } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }