Skip to content

Commit

Permalink
Make functions local with underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
richardarsenault committed Jan 16, 2024
1 parent 92f75c5 commit aec3460
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
4 changes: 2 additions & 2 deletions tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from xhydro.modelling.calibration import perform_calibration
from xhydro.modelling.hydrological_modelling import dummy_model
from xhydro.modelling.hydrological_modelling import _dummy_model
from xhydro.modelling.obj_funcs import get_objective_function, transform_flows


Expand Down Expand Up @@ -49,7 +49,7 @@ def test_spotpy_calibration():

# Test dummy model response
model_config["parameters"] = [5, 5, 5]
qsim = dummy_model(model_config)
qsim = _dummy_model(model_config)
assert qsim["qsim"].values[3] == 3500.00

# Also test to ensure SCEUA and take_minimize is required.
Expand Down
10 changes: 5 additions & 5 deletions tests/test_objective_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pytest

from xhydro.modelling.obj_funcs import (
_get_objfun_minimize_or_maximize,
get_objective_function,
get_objfun_minimize_or_maximize,
)


Expand Down Expand Up @@ -128,7 +128,7 @@ def test_maximizer_objfun_failure_modes_bias():
Use of bias objfun which is unbounded
"""
with pytest.raises(ValueError) as pytest_wrapped_e:
_ = get_objfun_minimize_or_maximize(obj_func="bias")
_ = _get_objfun_minimize_or_maximize(obj_func="bias")
assert pytest_wrapped_e.type == ValueError


Expand All @@ -137,7 +137,7 @@ def test_maximizer_objfun_failure_modes_pbias():
Use of pbias objfun which is unbounded
"""
with pytest.raises(ValueError) as pytest_wrapped_e:
_ = get_objfun_minimize_or_maximize(obj_func="pbias")
_ = _get_objfun_minimize_or_maximize(obj_func="pbias")
assert pytest_wrapped_e.type == ValueError


Expand All @@ -146,7 +146,7 @@ def test_maximizer_objfun_failure_modes_volume_error():
Use of volume_error objfun which is unbounded
"""
with pytest.raises(ValueError) as pytest_wrapped_e:
_ = get_objfun_minimize_or_maximize(obj_func="volume_error")
_ = _get_objfun_minimize_or_maximize(obj_func="volume_error")
assert pytest_wrapped_e.type == ValueError


Expand All @@ -155,5 +155,5 @@ def test_maximizer_objfun_failure_modes_unknown_metric():
Use of unknown objfun
"""
with pytest.raises(NotImplementedError) as pytest_wrapped_e:
_ = get_objfun_minimize_or_maximize(obj_func="unknown_of")
_ = _get_objfun_minimize_or_maximize(obj_func="unknown_of")
assert pytest_wrapped_e.type == NotImplementedError
8 changes: 4 additions & 4 deletions xhydro/modelling/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@

from xhydro.modelling.hydrological_modelling import run_hydrological_model
from xhydro.modelling.obj_funcs import (
_get_objfun_minimize_or_maximize,
_get_optimizer_minimize_or_maximize,
get_objective_function,
get_objfun_minimize_or_maximize,
get_optimizer_minimize_or_maximize,
)

__all__ = ["perform_calibration"]
Expand Down Expand Up @@ -374,8 +374,8 @@ def perform_calibration(
# to ensure that the algorithm is optimizing in the correct direction
# (maximizing or minimizing). This code determines the required direction
# 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)
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:
Expand Down
4 changes: 2 additions & 2 deletions xhydro/modelling/hydrological_modelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def run_hydrological_model(model_config: dict):
Simulated streamflow from the model, in xarray Dataset format.
"""
if model_config["model_name"] == "Dummy":
qsim = dummy_model(model_config)
qsim = _dummy_model(model_config)

elif model_config["model_name"] == "ADD_OTHER_HERE":
# ADD OTHER MODELS HERE
Expand All @@ -74,7 +74,7 @@ def run_hydrological_model(model_config: dict):
return qsim


def dummy_model(model_config: dict):
def _dummy_model(model_config: dict):
"""Dummy model.
Dummy model to show the implementation we should be aiming for. Each model
Expand Down
88 changes: 44 additions & 44 deletions xhydro/modelling/obj_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,24 @@ def get_objective_function(
"""
# List of available objective functions
obj_func_dict = {
"abs_bias": abs_bias,
"abs_pbias": abs_pbias,
"abs_volume_error": abs_volume_error,
"agreement_index": agreement_index,
"bias": bias,
"correlation_coeff": correlation_coeff,
"kge": kge,
"kge_mod": kge_mod,
"mae": mae,
"mare": mare,
"mse": mse,
"nse": nse,
"pbias": pbias,
"r2": r2,
"rmse": rmse,
"rrmse": rrmse,
"rsr": rsr,
"volume_error": volume_error,
"abs_bias": _abs_bias,
"abs_pbias": _abs_pbias,
"abs_volume_error": _abs_volume_error,
"agreement_index": _agreement_index,
"bias": _bias,
"correlation_coeff": _correlation_coeff,
"kge": _kge,
"kge_mod": _kge_mod,
"mae": _mae,
"mare": _mare,
"mse": _mse,
"nse": _nse,
"pbias": _pbias,
"r2": _r2,
"rmse": _rmse,
"rrmse": _rrmse,
"rsr": _rsr,
"volume_error": _volume_error,
}

# If we got a dataset, change to np.array
Expand Down Expand Up @@ -204,7 +204,7 @@ def get_objective_function(
return obj_fun_val


def get_objfun_minimize_or_maximize(obj_func: str):
def _get_objfun_minimize_or_maximize(obj_func: str):
"""Check whether the objective function needs to be maximized or minimized.
Returns a boolean value, where True means it should be maximized, and False
Expand Down Expand Up @@ -258,7 +258,7 @@ def get_objfun_minimize_or_maximize(obj_func: str):
return maximize


def get_optimizer_minimize_or_maximize(algorithm: str):
def _get_optimizer_minimize_or_maximize(algorithm: str):
"""Find the direction in which the optimizer searches.
Some optimizers try to maximize the objective function value, and others
Expand Down Expand Up @@ -364,7 +364,7 @@ def transform_flows(
"""


def abs_bias(qsim: np.array, qobs: np.array):
def _abs_bias(qsim: np.array, qobs: np.array):
"""
Absolute bias metric.
Expand All @@ -387,10 +387,10 @@ def abs_bias(qsim: np.array, qobs: np.array):
-----
The abs_bias should be MINIMIZED.
"""
return np.abs(bias(qsim, qobs))
return np.abs(_bias(qsim, qobs))


def abs_pbias(qsim: np.array, qobs: np.array):
def _abs_pbias(qsim: np.array, qobs: np.array):
"""
Absolute pbias metric.
Expand All @@ -413,10 +413,10 @@ def abs_pbias(qsim: np.array, qobs: np.array):
-----
The abs_pbias should be MINIMIZED.
"""
return np.abs(pbias(qsim, qobs))
return np.abs(_pbias(qsim, qobs))


def abs_volume_error(qsim: np.array, qobs: np.array):
def _abs_volume_error(qsim: np.array, qobs: np.array):
"""
Absolute value of the volume error metric.
Expand All @@ -440,10 +440,10 @@ def abs_volume_error(qsim: np.array, qobs: np.array):
-----
The abs_volume_error should be MINIMIZED.
"""
return np.abs(volume_error(qsim, qobs))
return np.abs(_volume_error(qsim, qobs))


def agreement_index(qsim: np.array, qobs: np.array):
def _agreement_index(qsim: np.array, qobs: np.array):
"""
Index of agreement metric.
Expand Down Expand Up @@ -471,7 +471,7 @@ def agreement_index(qsim: np.array, qobs: np.array):
return 1 - (a / c)


def bias(qsim: np.array, qobs: np.array):
def _bias(qsim: np.array, qobs: np.array):
"""
The bias metric.
Expand Down Expand Up @@ -499,7 +499,7 @@ def bias(qsim: np.array, qobs: np.array):
return np.mean(qsim - qobs)


def correlation_coeff(qsim: np.array, qobs: np.array):
def _correlation_coeff(qsim: np.array, qobs: np.array):
"""
Correlation coefficient metric.
Expand All @@ -522,7 +522,7 @@ def correlation_coeff(qsim: np.array, qobs: np.array):
return np.corrcoef(qobs, qsim)[0, 1]


def kge(qsim: np.array, qobs: np.array):
def _kge(qsim: np.array, qobs: np.array):
"""
Kling-Gupta efficiency metric (2009 version).
Expand Down Expand Up @@ -560,7 +560,7 @@ def kge(qsim: np.array, qobs: np.array):
return kge


def kge_mod(qsim: np.array, qobs: np.array):
def _kge_mod(qsim: np.array, qobs: np.array):
"""
Kling-Gupta efficiency metric (2012 version).
Expand Down Expand Up @@ -598,7 +598,7 @@ def kge_mod(qsim: np.array, qobs: np.array):
return kge_mod


def mae(qsim: np.array, qobs: np.array):
def _mae(qsim: np.array, qobs: np.array):
"""
Mean absolute error metric.
Expand All @@ -622,7 +622,7 @@ def mae(qsim: np.array, qobs: np.array):
return np.mean(np.abs(qsim - qobs))


def mare(qsim: np.array, qobs: np.array):
def _mare(qsim: np.array, qobs: np.array):
"""
Mean absolute relative error metric.
Expand All @@ -646,7 +646,7 @@ def mare(qsim: np.array, qobs: np.array):
return np.sum(np.abs(qobs - qsim)) / np.sum(qobs)


def mse(qsim: np.array, qobs: np.array):
def _mse(qsim: np.array, qobs: np.array):
"""
Mean square error metric.
Expand All @@ -671,7 +671,7 @@ def mse(qsim: np.array, qobs: np.array):
return np.mean((qobs - qsim) ** 2)


def nse(qsim: np.array, qobs: np.array):
def _nse(qsim: np.array, qobs: np.array):
"""
Nash-Sutcliffe efficiency metric.
Expand All @@ -698,7 +698,7 @@ def nse(qsim: np.array, qobs: np.array):
return 1 - (num / den)


def pbias(qsim: np.array, qobs: np.array):
def _pbias(qsim: np.array, qobs: np.array):
"""
Percent bias metric.
Expand Down Expand Up @@ -726,7 +726,7 @@ def pbias(qsim: np.array, qobs: np.array):
return (np.sum(qsim - qobs) / np.sum(qobs)) * 100


def r2(qsim: np.array, qobs: np.array):
def _r2(qsim: np.array, qobs: np.array):
"""
The r-squred metric.
Expand All @@ -747,10 +747,10 @@ def r2(qsim: np.array, qobs: np.array):
-----
The r2 should be MAXIMIZED.
"""
return correlation_coeff(qsim, qobs) ** 2
return _correlation_coeff(qsim, qobs) ** 2


def rmse(qsim: np.array, qobs: np.array):
def _rmse(qsim: np.array, qobs: np.array):
"""
Root mean square error metric.
Expand All @@ -774,7 +774,7 @@ def rmse(qsim: np.array, qobs: np.array):
return np.sqrt(np.mean((qobs - qsim) ** 2))


def rrmse(qsim: np.array, qobs: np.array):
def _rrmse(qsim: np.array, qobs: np.array):
"""
Relative root mean square error (ratio of rmse to mean) metric.
Expand All @@ -797,10 +797,10 @@ def rrmse(qsim: np.array, qobs: np.array):
-----
The rrmse should be MINIMIZED.
"""
return rmse(qsim, qobs) / np.mean(qobs)
return _rmse(qsim, qobs) / np.mean(qobs)


def rsr(qsim: np.array, qobs: np.array):
def _rsr(qsim: np.array, qobs: np.array):
"""
Ratio of root mean square error to standard deviation metric.
Expand All @@ -822,10 +822,10 @@ def rsr(qsim: np.array, qobs: np.array):
-----
The rsr should be MINIMIZED.
"""
return rmse(qobs, qsim) / np.std(qobs)
return _rmse(qobs, qsim) / np.std(qobs)


def volume_error(qsim: np.array, qobs: np.array):
def _volume_error(qsim: np.array, qobs: np.array):
"""
Volume error metric.
Expand Down

0 comments on commit aec3460

Please sign in to comment.