HBV-Lab 1.4.2__tar.gz → 1.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/HBV_model.py +5 -3
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/__init__.py +36 -36
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/calibration.py +94 -12
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/mcp_server.py +694 -686
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab.egg-info/PKG-INFO +36 -6
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab.egg-info/SOURCES.txt +1 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/PKG-INFO +36 -6
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/README.md +375 -345
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/setup.py +1 -1
- hbv_lab-1.5.0/tests/test_dds.py +77 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/hbv_step.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/response.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/routing.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/snow.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/soil.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab/uncertainty.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab.egg-info/dependency_links.txt +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab.egg-info/entry_points.txt +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab.egg-info/requires.txt +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/HBV_Lab.egg-info/top_level.txt +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/LICENSE +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/setup.cfg +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/tests/test_hbv_model.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/tests/test_hbv_step.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/tests/test_response.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/tests/test_snow.py +0 -0
- {hbv_lab-1.4.2 → hbv_lab-1.5.0}/tests/test_soil.py +0 -0
|
@@ -46,16 +46,18 @@ class HBVModel(uncertainty, calibration):
|
|
|
46
46
|
'CWH': {'min': 0.0, 'max': 0.2, 'default': 0.1}
|
|
47
47
|
},
|
|
48
48
|
'soil': {
|
|
49
|
-
|
|
49
|
+
# FC, PERC and K2 ranges follow Seibert (1997) since v1.5.0. The earlier, narrower
|
|
50
|
+
# ranges (FC <= 300, PERC <= 3, K2 >= 0.01) pinned dry and chalk catchments at the bounds.
|
|
51
|
+
'FC': {'min': 50.0, 'max': 500.0, 'default': 150.0},
|
|
50
52
|
'LP': {'min': 0.3, 'max': 1.0, 'default': 0.7},
|
|
51
53
|
'BETA': {'min': 1.0, 'max': 5.0, 'default': 2.0}
|
|
52
54
|
},
|
|
53
55
|
'response': {
|
|
54
56
|
'K0': {'min': 0.1, 'max': 0.9, 'default': 0.5},
|
|
55
57
|
'K1': {'min': 0.05, 'max': 0.5, 'default': 0.2},
|
|
56
|
-
'K2': {'min': 0.
|
|
58
|
+
'K2': {'min': 0.0005, 'max': 0.1, 'default': 0.05},
|
|
57
59
|
'UZL': {'min': 5.0, 'max': 50.0, 'default': 20.0},
|
|
58
|
-
'PERC': {'min': 0.5, 'max':
|
|
60
|
+
'PERC': {'min': 0.5, 'max': 10.0, 'default': 1.5},
|
|
59
61
|
'MAXBAS' :{'min': 1, 'max': 10, 'default': 3}
|
|
60
62
|
}
|
|
61
63
|
}
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
"""
|
|
2
|
-
HBV_Lab — an intuitive, object-oriented Python implementation of a lumped
|
|
3
|
-
conceptual HBV hydrological model for education and research.
|
|
4
|
-
|
|
5
|
-
Typical usage::
|
|
6
|
-
|
|
7
|
-
from HBV_Lab import HBVModel
|
|
8
|
-
model = HBVModel()
|
|
9
|
-
model.load_data(data=df, ...)
|
|
10
|
-
model.run()
|
|
11
|
-
model.calibrate()
|
|
12
|
-
model.evaluate_uncertainty()
|
|
13
|
-
|
|
14
|
-
The individual process routines are also importable for advanced use::
|
|
15
|
-
|
|
16
|
-
from HBV_Lab import snow_routine, soil_routine, response_routine_two_tanks
|
|
17
|
-
from HBV_Lab import hbv_step, route_with_maxbas
|
|
18
|
-
"""
|
|
19
|
-
|
|
20
|
-
from .HBV_model import HBVModel
|
|
21
|
-
from .hbv_step import hbv_step
|
|
22
|
-
from .snow import snow_routine
|
|
23
|
-
from .soil import soil_routine
|
|
24
|
-
from .response import response_routine_two_tanks
|
|
25
|
-
from .routing import route_with_maxbas
|
|
26
|
-
|
|
27
|
-
__version__ = "1.
|
|
28
|
-
|
|
29
|
-
__all__ = [
|
|
30
|
-
"HBVModel",
|
|
31
|
-
"hbv_step",
|
|
32
|
-
"snow_routine",
|
|
33
|
-
"soil_routine",
|
|
34
|
-
"response_routine_two_tanks",
|
|
35
|
-
"route_with_maxbas",
|
|
36
|
-
]
|
|
1
|
+
"""
|
|
2
|
+
HBV_Lab — an intuitive, object-oriented Python implementation of a lumped
|
|
3
|
+
conceptual HBV hydrological model for education and research.
|
|
4
|
+
|
|
5
|
+
Typical usage::
|
|
6
|
+
|
|
7
|
+
from HBV_Lab import HBVModel
|
|
8
|
+
model = HBVModel()
|
|
9
|
+
model.load_data(data=df, ...)
|
|
10
|
+
model.run()
|
|
11
|
+
model.calibrate()
|
|
12
|
+
model.evaluate_uncertainty()
|
|
13
|
+
|
|
14
|
+
The individual process routines are also importable for advanced use::
|
|
15
|
+
|
|
16
|
+
from HBV_Lab import snow_routine, soil_routine, response_routine_two_tanks
|
|
17
|
+
from HBV_Lab import hbv_step, route_with_maxbas
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from .HBV_model import HBVModel
|
|
21
|
+
from .hbv_step import hbv_step
|
|
22
|
+
from .snow import snow_routine
|
|
23
|
+
from .soil import soil_routine
|
|
24
|
+
from .response import response_routine_two_tanks
|
|
25
|
+
from .routing import route_with_maxbas
|
|
26
|
+
|
|
27
|
+
__version__ = "1.5.0"
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"HBVModel",
|
|
31
|
+
"hbv_step",
|
|
32
|
+
"snow_routine",
|
|
33
|
+
"soil_routine",
|
|
34
|
+
"response_routine_two_tanks",
|
|
35
|
+
"route_with_maxbas",
|
|
36
|
+
]
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
class calibration:
|
|
2
2
|
|
|
3
3
|
def calibrate(self, method='Nelder-Mead', objective='NSE', iterations=100,
|
|
4
|
-
verbose=True, plot_results=True, progress_callback=None
|
|
4
|
+
verbose=True, plot_results=True, progress_callback=None,
|
|
5
|
+
seed=None, dds_r=0.2):
|
|
5
6
|
"""
|
|
6
7
|
Calibrate an HBV model's parameters to optimize the objective function.
|
|
7
8
|
|
|
@@ -17,6 +18,11 @@ class calibration:
|
|
|
17
18
|
as 'SLSQP' or 'L-BFGS-B' to terminate immediately without improving
|
|
18
19
|
on the starting values. Gradient-free options that respect the
|
|
19
20
|
parameter bounds include 'Nelder-Mead' and 'Powell'.
|
|
21
|
+
'DDS' selects Dynamically Dimensioned Search (Tolson and Shoemaker,
|
|
22
|
+
2007, Water Resources Research 43, W01413), a global single-solution
|
|
23
|
+
search designed for watershed model calibration. With 'DDS',
|
|
24
|
+
``iterations`` is the number of model evaluations, and the search
|
|
25
|
+
starts from the current parameter values.
|
|
20
26
|
objective : str, default 'NSE'
|
|
21
27
|
Objective function to maximize. Options are:
|
|
22
28
|
- 'NSE': Nash-Sutcliffe Efficiency (higher is better)
|
|
@@ -35,6 +41,11 @@ class calibration:
|
|
|
35
41
|
where the values are in human-facing objective terms (e.g. NSE). Useful for
|
|
36
42
|
driving an external progress UI (e.g. the MCP server). Exceptions raised by
|
|
37
43
|
the callback are swallowed so they cannot interrupt calibration.
|
|
44
|
+
seed : int, optional
|
|
45
|
+
Random seed for 'DDS', for reproducible runs. Ignored by other methods.
|
|
46
|
+
dds_r : float, default 0.2
|
|
47
|
+
DDS neighbourhood perturbation size, as a fraction of each parameter
|
|
48
|
+
range (0.2 is the value recommended by Tolson and Shoemaker).
|
|
38
49
|
|
|
39
50
|
Returns:
|
|
40
51
|
--------
|
|
@@ -173,9 +184,11 @@ class calibration:
|
|
|
173
184
|
# (e.g. NSE/KGE as-is, RMSE/MAE as-is). Returned to the caller.
|
|
174
185
|
trajectory = []
|
|
175
186
|
|
|
176
|
-
def callback(params):
|
|
187
|
+
def callback(params, current_value=None):
|
|
188
|
+
# DDS passes the value it has already computed, which saves a model run
|
|
177
189
|
num_iter[0] += 1
|
|
178
|
-
current_value
|
|
190
|
+
if current_value is None:
|
|
191
|
+
current_value = objective_function(params)
|
|
179
192
|
|
|
180
193
|
# For NSE and KGE, we're minimizing the negative value
|
|
181
194
|
if objective in ['NSE', 'KGE']:
|
|
@@ -215,14 +228,18 @@ class calibration:
|
|
|
215
228
|
|
|
216
229
|
try:
|
|
217
230
|
# Run optimization
|
|
218
|
-
|
|
219
|
-
objective_function,
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
231
|
+
if str(method).upper() == 'DDS':
|
|
232
|
+
result = _dds(objective_function, initial_params, param_bounds, iterations,
|
|
233
|
+
r=dds_r, seed=seed, callback=callback)
|
|
234
|
+
else:
|
|
235
|
+
result = opt.minimize(
|
|
236
|
+
objective_function,
|
|
237
|
+
initial_params,
|
|
238
|
+
method=method,
|
|
239
|
+
bounds=param_bounds,
|
|
240
|
+
callback=callback,
|
|
241
|
+
options={'maxiter': iterations}
|
|
242
|
+
)
|
|
226
243
|
|
|
227
244
|
# Get optimized parameters
|
|
228
245
|
opt_params = result.x
|
|
@@ -272,4 +289,69 @@ class calibration:
|
|
|
272
289
|
# Restore original parameters on error
|
|
273
290
|
self.params = original_params
|
|
274
291
|
print(f"Calibration failed with error: {str(e)}")
|
|
275
|
-
raise
|
|
292
|
+
raise
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def _dds(fun, x0, bounds, max_evals, r=0.2, seed=None, callback=None):
|
|
296
|
+
"""Dynamically Dimensioned Search (Tolson and Shoemaker, 2007) for minimisation.
|
|
297
|
+
|
|
298
|
+
Starts from ``x0`` and spends ``max_evals`` evaluations of ``fun`` in total
|
|
299
|
+
(the first one on ``x0``). At evaluation i, each parameter is perturbed with
|
|
300
|
+
probability 1 - ln(i)/ln(max_evals), so the search moves from global to
|
|
301
|
+
local as the budget is used up. Perturbations are Gaussian with standard
|
|
302
|
+
deviation ``r`` times the parameter range and are reflected at the bounds.
|
|
303
|
+
``callback(x_best, f_best)`` is called after every evaluation.
|
|
304
|
+
|
|
305
|
+
Returns a ``scipy.optimize.OptimizeResult`` (fields x, fun, nit, nfev,
|
|
306
|
+
success, message, history), so callers can treat it like ``minimize``.
|
|
307
|
+
"""
|
|
308
|
+
import numpy as np
|
|
309
|
+
from scipy.optimize import OptimizeResult
|
|
310
|
+
|
|
311
|
+
rng = np.random.default_rng(seed)
|
|
312
|
+
lb = np.array([b[0] for b in bounds], dtype=float)
|
|
313
|
+
ub = np.array([b[1] for b in bounds], dtype=float)
|
|
314
|
+
span = ub - lb
|
|
315
|
+
free = np.flatnonzero(span > 0) # parameters with a fixed value are never perturbed
|
|
316
|
+
max_evals = max(1, int(max_evals))
|
|
317
|
+
|
|
318
|
+
def f(x):
|
|
319
|
+
v = float(fun(x))
|
|
320
|
+
return v if np.isfinite(v) else np.inf
|
|
321
|
+
|
|
322
|
+
x_best = np.clip(np.asarray(x0, dtype=float), lb, ub)
|
|
323
|
+
f_best = f(x_best)
|
|
324
|
+
history = [f_best]
|
|
325
|
+
if callback is not None:
|
|
326
|
+
callback(x_best.copy(), f_best)
|
|
327
|
+
|
|
328
|
+
for i in range(1, max_evals):
|
|
329
|
+
x_new = x_best.copy()
|
|
330
|
+
if free.size:
|
|
331
|
+
p = 1.0 - np.log(i) / np.log(max_evals)
|
|
332
|
+
pick = free[rng.random(free.size) < p]
|
|
333
|
+
if pick.size == 0:
|
|
334
|
+
pick = np.array([rng.choice(free)])
|
|
335
|
+
for j in pick:
|
|
336
|
+
v = x_best[j] + r * span[j] * rng.standard_normal()
|
|
337
|
+
if v < lb[j]:
|
|
338
|
+
v = lb[j] + (lb[j] - v)
|
|
339
|
+
if v > ub[j]:
|
|
340
|
+
v = lb[j]
|
|
341
|
+
elif v > ub[j]:
|
|
342
|
+
v = ub[j] - (v - ub[j])
|
|
343
|
+
if v < lb[j]:
|
|
344
|
+
v = ub[j]
|
|
345
|
+
x_new[j] = v
|
|
346
|
+
f_new = f(x_new)
|
|
347
|
+
if f_new <= f_best:
|
|
348
|
+
x_best, f_best = x_new, f_new
|
|
349
|
+
history.append(f_best)
|
|
350
|
+
if callback is not None:
|
|
351
|
+
callback(x_best.copy(), f_best)
|
|
352
|
+
|
|
353
|
+
return OptimizeResult(
|
|
354
|
+
x=x_best, fun=f_best, nit=max_evals, nfev=max_evals, success=False,
|
|
355
|
+
message=f"DDS used its full budget of {max_evals} evaluations "
|
|
356
|
+
f"(maximum number of iterations reached).",
|
|
357
|
+
history=history)
|