Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 22, 2023
1 parent 179f6aa commit 19efaf4
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 267 deletions.
20 changes: 10 additions & 10 deletions tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def test_spotpy_calibration():
"drainage_area": np.array([10]),
"model_name": "Dummy",
}

mask = np.array([0, 0, 0, 0, 1, 1])

best_parameters, best_simulation, best_objfun = perform_calibration(
model_config,
"mae",
Expand All @@ -38,16 +38,16 @@ def test_spotpy_calibration():
assert len(best_parameters) == len(bounds_high)

# Test that the objective function is calculated correctly
objfun = get_objective_function(
model_config["Qobs"],
best_simulation,
obj_func="mae",
mask=mask,
)
objfun = get_objective_function(
model_config["Qobs"],
best_simulation,
obj_func="mae",
mask=mask,
)

assert objfun == best_objfun

# Test dummy model response
model_config["parameters"] = [5, 5, 5]
Qsim = dummy_model(model_config)
assert Qsim[3] == 3500.00
assert Qsim[3] == 3500.00
68 changes: 35 additions & 33 deletions xhydro/modelling/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,27 @@
from spotpy.parameter import Uniform

from xhydro.modelling.hydrological_modelling import hydrological_model_selector
from xhydro.modelling.obj_funcs import (get_objective_function,
get_objfun_minimize_or_maximize,
get_optimizer_minimize_or_maximize,
)
from xhydro.modelling.obj_funcs import (
get_objective_function,
get_objfun_minimize_or_maximize,
get_optimizer_minimize_or_maximize,
)


class spot_setup:
"""Create the spotpy calibration system that is used for hydrological model calibration."""

def __init__(self,
model_config,
bounds_high,
bounds_low,
obj_func=None,
take_negative=False,
mask=None,
transform=None,
epsilon=None,
):
def __init__(
self,
model_config,
bounds_high,
bounds_low,
obj_func=None,
take_negative=False,
mask=None,
transform=None,
epsilon=None,
):
"""Initialize the spot_setup object.
The initialization of the spot_setup object includes a generic
Expand Down Expand Up @@ -115,7 +116,7 @@ def __init__(self,
- "rsr" : Ratio of RMSE to standard deviation.
- "volume_error": Total volume error over the period.
"""

# Gather the model_config dictionary and obj_func string, and other
# optional arguments.
self.model_config = model_config
Expand Down Expand Up @@ -176,16 +177,17 @@ def objectivefunction(
- simulation, observation : vectors of streamflow used to compute
the objective function
"""

obj_fun_val = get_objective_function(evaluation,
simulation,
obj_func=self.obj_func,
take_negative=self.take_negative,
mask=self.mask,
transform=self.transform,
epsilon=self.epsilon,
)


obj_fun_val = get_objective_function(
evaluation,
simulation,
obj_func=self.obj_func,
take_negative=self.take_negative,
mask=self.mask,
transform=self.transform,
epsilon=self.epsilon,
)

return obj_fun_val


Expand Down Expand Up @@ -215,7 +217,7 @@ def perform_calibration(
It will be up to the user to provide the data that the model requires.
obj_func : str
The objective function used for calibrating. Can be any one of these:
- "abs_bias" : Absolute value of the "bias" metric
- "abs_pbias": Absolute value of the "pbias" metric
- "abs_volume_error" : Absolute value of the volume_error metric
Expand All @@ -231,7 +233,7 @@ def perform_calibration(
- "rmse" : Root Mean Square Error
- "rrmse" : Relative Root Mean Square Error (RMSE-to-mean ratio)
- "rsr" : Ratio of RMSE to standard deviation.
bounds_high : np.array
High bounds for the model parameters to be calibrated. Spotpy will sample parameter sets from
within these bounds. The size must be equal to the number of parameters to calibrate.
Expand Down Expand Up @@ -259,13 +261,13 @@ def perform_calibration(
# for the objective function and the working direction of the algorithm.
of_maximize = get_objfun_minimize_or_maximize(obj_func)
algo_maximize = get_optimizer_minimize_or_maximize(algorithm)

# They are not working in the same direction. Take the negative of the OF.
if of_maximize != algo_maximize:
take_negative = True
else:
take_negative = False

# Set up the spotpy object to prepare the calibration
spotpy_setup = spot_setup(
model_config,
Expand All @@ -277,7 +279,7 @@ def perform_calibration(
transform=transform,
epsilon=epsilon,
)

# Select an optimization algorithm and parameterize it, then run the
# optimization process.
if algorithm == "DDS":
Expand Down Expand Up @@ -311,12 +313,12 @@ def perform_calibration(
# Reconvert objective function if required.
if take_negative:
bestobjf = bestobjf * -1

# Update the parameter set to put the best parameters in model_config...
model_config.update({"parameters": best_parameters})

# ... which can be used to run the hydrological model and get the best Qsim.
Qsim = hydrological_model_selector(model_config)

# Return the best parameters, Qsim and best objective function value.
return best_parameters, Qsim, bestobjf
return best_parameters, Qsim, bestobjf
Loading

0 comments on commit 19efaf4

Please sign in to comment.