|
1 | 1 | { |
2 | 2 | "cells": [ |
3 | 3 | { |
4 | | - "metadata": {}, |
5 | 4 | "cell_type": "markdown", |
| 5 | + "id": "f8ebfe5589158bd0", |
| 6 | + "metadata": {}, |
6 | 7 | "source": [ |
7 | 8 | "# Saving and loading models\n", |
8 | 9 | "\n", |
9 | 10 | "This notebook introduces the concept of saving and loading `.gwref` files.\n", |
10 | 11 | "\n", |
11 | 12 | "This notebook can be downloaded from the source code [here](https://github.com/andersretznerSGU/gwrefpy/blob/main/docs/user_guide/5_io.ipynb).\n", |
12 | 13 | "\n", |
13 | | - "# Saving a model 💾\n", |
| 14 | + "## Saving a model 💾\n", |
14 | 15 | "\n", |
15 | 16 | "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." |
16 | | - ], |
17 | | - "id": "f8ebfe5589158bd0" |
| 17 | + ] |
18 | 18 | }, |
19 | 19 | { |
20 | | - "metadata": {}, |
21 | 20 | "cell_type": "markdown", |
22 | | - "source": "We will first create a simple model with one observed well and one reference well.", |
23 | | - "id": "a7d320daa081a770" |
| 21 | + "id": "a7d320daa081a770", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "We will first create a simple model with one observed well and one reference well." |
| 25 | + ] |
24 | 26 | }, |
25 | 27 | { |
26 | | - "metadata": { |
27 | | - "ExecuteTime": { |
28 | | - "end_time": "2025-09-19T06:19:23.202275Z", |
29 | | - "start_time": "2025-09-19T06:19:21.423475Z" |
30 | | - } |
31 | | - }, |
32 | 28 | "cell_type": "code", |
| 29 | + "execution_count": 1, |
| 30 | + "id": "ae7b6686cad2a72f", |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [], |
33 | 33 | "source": [ |
34 | 34 | "import gwrefpy as gr\n", |
35 | 35 | "import numpy as np\n", |
36 | 36 | "import pandas as pd" |
37 | | - ], |
38 | | - "id": "ae7b6686cad2a72f", |
39 | | - "outputs": [], |
40 | | - "execution_count": 3 |
| 37 | + ] |
41 | 38 | }, |
42 | 39 | { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 2, |
| 42 | + "id": "e9337241bbe35e1a", |
43 | 43 | "metadata": { |
44 | 44 | "tags": [ |
45 | 45 | "hide-input" |
46 | | - ], |
47 | | - "ExecuteTime": { |
48 | | - "end_time": "2025-09-19T06:19:23.878Z", |
49 | | - "start_time": "2025-09-19T06:19:23.865523Z" |
50 | | - } |
| 46 | + ] |
51 | 47 | }, |
52 | | - "cell_type": "code", |
| 48 | + "outputs": [], |
53 | 49 | "source": [ |
54 | 50 | "# Create some example data\n", |
55 | 51 | "n_days = 100\n", |
|
68 | 64 | " + np.random.normal(0, 0.05, n_days)\n", |
69 | 65 | ")\n", |
70 | 66 | "values_ref1 = pd.Series(values_ref1, index=dates)" |
71 | | - ], |
72 | | - "id": "e9337241bbe35e1a", |
73 | | - "outputs": [], |
74 | | - "execution_count": 4 |
| 67 | + ] |
75 | 68 | }, |
76 | 69 | { |
77 | | - "metadata": { |
78 | | - "ExecuteTime": { |
79 | | - "end_time": "2025-09-19T06:19:25.524811Z", |
80 | | - "start_time": "2025-09-19T06:19:25.522288Z" |
81 | | - } |
82 | | - }, |
83 | 70 | "cell_type": "code", |
| 71 | + "execution_count": 3, |
| 72 | + "id": "dd1b89e7b780e90d", |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
84 | 75 | "source": [ |
85 | 76 | "# Creat the observed and reference wells\n", |
86 | 77 | "obs1 = gr.Well(name=\"Obs. well\", is_reference=False)\n", |
|
92 | 83 | "model1 = gr.Model(name=\"IO Model\")\n", |
93 | 84 | "model1.add_well(obs1)\n", |
94 | 85 | "model1.add_well(ref1)" |
95 | | - ], |
96 | | - "id": "dd1b89e7b780e90d", |
97 | | - "outputs": [], |
98 | | - "execution_count": 5 |
| 86 | + ] |
99 | 87 | }, |
100 | 88 | { |
101 | | - "metadata": {}, |
102 | 89 | "cell_type": "markdown", |
| 90 | + "id": "3f437ef415865dca", |
| 91 | + "metadata": {}, |
103 | 92 | "source": [ |
104 | 93 | "We can now save the model to a `.gwref` file using the `save_project` function.\n", |
105 | 94 | "```{note}\n", |
106 | 95 | "The funciton will create the file in the current working directory if no path is specified.\n", |
107 | 96 | "```" |
108 | | - ], |
109 | | - "id": "3f437ef415865dca" |
| 97 | + ] |
110 | 98 | }, |
111 | 99 | { |
112 | | - "metadata": { |
113 | | - "ExecuteTime": { |
114 | | - "end_time": "2025-09-19T06:21:35.698499Z", |
115 | | - "start_time": "2025-09-19T06:21:35.680754Z" |
116 | | - } |
117 | | - }, |
118 | 100 | "cell_type": "code", |
119 | | - "source": [ |
120 | | - "# Save the model to a .gwref file\n", |
121 | | - "model1.save_project(\"my_model.gwref\")" |
122 | | - ], |
| 101 | + "execution_count": 4, |
123 | 102 | "id": "cff5a365dc580981", |
| 103 | + "metadata": {}, |
124 | 104 | "outputs": [ |
125 | 105 | { |
126 | 106 | "name": "stdout", |
127 | 107 | "output_type": "stream", |
128 | 108 | "text": [ |
129 | | - "Model 'Logging Example Model' saved to 'example_model.gwref'.\n" |
| 109 | + "Model 'IO Model' saved to 'my_model.gwref'.\n" |
130 | 110 | ] |
131 | 111 | } |
132 | 112 | ], |
133 | | - "execution_count": 9 |
| 113 | + "source": [ |
| 114 | + "# Save the model to a .gwref file\n", |
| 115 | + "model1.save_project(\"my_model.gwref\")" |
| 116 | + ] |
134 | 117 | }, |
135 | 118 | { |
136 | | - "metadata": {}, |
137 | 119 | "cell_type": "markdown", |
| 120 | + "id": "d2a3d40fe6011460", |
| 121 | + "metadata": {}, |
138 | 122 | "source": [ |
139 | 123 | "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", |
140 | 124 | "```{warning}\n", |
141 | 125 | "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", |
142 | 126 | "\n", |
143 | 127 | "The funciton will not create any folders in the specified path if they do not exist.\n", |
144 | 128 | "```" |
145 | | - ], |
146 | | - "id": "d2a3d40fe6011460" |
| 129 | + ] |
147 | 130 | }, |
148 | 131 | { |
149 | | - "metadata": { |
150 | | - "ExecuteTime": { |
151 | | - "end_time": "2025-09-19T06:21:50.470844Z", |
152 | | - "start_time": "2025-09-19T06:21:50.464055Z" |
153 | | - } |
154 | | - }, |
155 | 132 | "cell_type": "code", |
156 | | - "source": [ |
157 | | - "# Save the model to a .gwref file, allowing overwriting\n", |
158 | | - "model1.save_project(\"my_model.gwref\") # This will not overwrite the existing file and a warning will be logged\n", |
159 | | - "model1.save_project(\"my_model.gwref\", overwrite=True) # This will overwrite the existing file" |
160 | | - ], |
| 133 | + "execution_count": 5, |
161 | 134 | "id": "8927630e5d539af5", |
| 135 | + "metadata": {}, |
162 | 136 | "outputs": [ |
163 | 137 | { |
164 | 138 | "name": "stdout", |
165 | 139 | "output_type": "stream", |
166 | 140 | "text": [ |
167 | | - "The file 'example_model.gwref' already exists. To overwrite the existing file set the argument 'overwrite' to True.\n", |
168 | | - "Model 'Logging Example Model' saved to 'example_model.gwref'.\n" |
| 141 | + "The file 'my_model.gwref' already exists. To overwrite the existing file set the argument 'overwrite' to True.\n", |
| 142 | + "Model 'IO Model' saved to 'my_model.gwref'.\n" |
169 | 143 | ] |
170 | 144 | } |
171 | 145 | ], |
172 | | - "execution_count": 10 |
| 146 | + "source": [ |
| 147 | + "# Save the model to a .gwref file, allowing overwriting\n", |
| 148 | + "model1.save_project(\"my_model.gwref\") # This will not overwrite the existing file and a warning will be logged\n", |
| 149 | + "model1.save_project(\"my_model.gwref\", overwrite=True) # This will overwrite the existing file" |
| 150 | + ] |
173 | 151 | }, |
174 | 152 | { |
175 | | - "metadata": {}, |
176 | 153 | "cell_type": "markdown", |
| 154 | + "id": "ab0429e99fb8f13e", |
| 155 | + "metadata": {}, |
177 | 156 | "source": [ |
178 | 157 | "## Loading Models 📦\n", |
179 | 158 | "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", |
180 | 159 | "\n", |
181 | 160 | "There are two ways to load a model:\n", |
182 | 161 | "1. Create a new `Model` object and use the `open_project` method.\n", |
183 | 162 | "2. Specify the `.gwref` file directly when creating a new `Model` object." |
184 | | - ], |
185 | | - "id": "ab0429e99fb8f13e" |
| 163 | + ] |
186 | 164 | }, |
187 | 165 | { |
188 | | - "metadata": { |
189 | | - "ExecuteTime": { |
190 | | - "end_time": "2025-09-18T11:56:24.801814Z", |
191 | | - "start_time": "2025-09-18T11:56:24.769652Z" |
192 | | - } |
193 | | - }, |
194 | 166 | "cell_type": "code", |
195 | | - "source": [ |
196 | | - "# Method 1: Create a new Model object and load the project\n", |
197 | | - "loaded_model1 = gr.Model(name=\"Name will be overwritten\")\n", |
198 | | - "loaded_model1.open_project(\"my_model.gwref\")" |
199 | | - ], |
| 167 | + "execution_count": 6, |
200 | 168 | "id": "30f2208061681fe", |
| 169 | + "metadata": {}, |
201 | 170 | "outputs": [ |
202 | 171 | { |
203 | 172 | "name": "stdout", |
204 | 173 | "output_type": "stream", |
205 | 174 | "text": [ |
206 | | - "Model 'Logging Example Model' loaded from 'example_model.gwref'.\n" |
| 175 | + "Model 'IO Model' loaded from 'my_model.gwref'.\n" |
207 | 176 | ] |
208 | 177 | } |
209 | 178 | ], |
210 | | - "execution_count": 3 |
| 179 | + "source": [ |
| 180 | + "# Method 1: Create a new Model object and load the project\n", |
| 181 | + "loaded_model1 = gr.Model(name=\"Name will be overwritten\")\n", |
| 182 | + "loaded_model1.open_project(\"my_model.gwref\")" |
| 183 | + ] |
211 | 184 | }, |
212 | 185 | { |
213 | | - "metadata": { |
214 | | - "ExecuteTime": { |
215 | | - "end_time": "2025-09-18T11:56:24.887100Z", |
216 | | - "start_time": "2025-09-18T11:56:24.877035Z" |
217 | | - } |
218 | | - }, |
219 | 186 | "cell_type": "code", |
220 | | - "source": [ |
221 | | - "# Method 2: Directly load the project into a new Model object\n", |
222 | | - "loaded_model2 = gr.Model(\"my_model.gwref\")" |
223 | | - ], |
| 187 | + "execution_count": 7, |
224 | 188 | "id": "5b2b524c40b0d03", |
| 189 | + "metadata": {}, |
225 | 190 | "outputs": [ |
226 | 191 | { |
227 | 192 | "name": "stdout", |
228 | 193 | "output_type": "stream", |
229 | 194 | "text": [ |
230 | | - "Model 'Logging Example Model' loaded from 'example_model.gwref'.\n" |
| 195 | + "Model 'IO Model' loaded from 'my_model.gwref'.\n" |
231 | 196 | ] |
232 | 197 | } |
233 | 198 | ], |
234 | | - "execution_count": 4 |
| 199 | + "source": [ |
| 200 | + "# Method 2: Directly load the project into a new Model object\n", |
| 201 | + "loaded_model2 = gr.Model(\"my_model.gwref\")" |
| 202 | + ] |
235 | 203 | }, |
236 | 204 | { |
237 | | - "metadata": {}, |
238 | 205 | "cell_type": "markdown", |
239 | | - "source": "This concludes this notebook on saving and loading models in `gwrefpy`.", |
240 | | - "id": "e30b422c68de2ecf" |
| 206 | + "id": "e30b422c68de2ecf", |
| 207 | + "metadata": {}, |
| 208 | + "source": [ |
| 209 | + "This concludes this notebook on saving and loading models in `gwrefpy`." |
| 210 | + ] |
241 | 211 | } |
242 | 212 | ], |
243 | 213 | "metadata": { |
244 | 214 | "kernelspec": { |
245 | | - "display_name": "Python 3", |
| 215 | + "display_name": "Python 3 (ipykernel)", |
246 | 216 | "language": "python", |
247 | 217 | "name": "python3" |
248 | 218 | }, |
249 | 219 | "language_info": { |
250 | 220 | "codemirror_mode": { |
251 | 221 | "name": "ipython", |
252 | | - "version": 2 |
| 222 | + "version": 3 |
253 | 223 | }, |
254 | 224 | "file_extension": ".py", |
255 | 225 | "mimetype": "text/x-python", |
256 | 226 | "name": "python", |
257 | 227 | "nbconvert_exporter": "python", |
258 | | - "pygments_lexer": "ipython2", |
259 | | - "version": "2.7.6" |
| 228 | + "pygments_lexer": "ipython3", |
| 229 | + "version": "3.11.2" |
260 | 230 | } |
261 | 231 | }, |
262 | 232 | "nbformat": 4, |
|
0 commit comments