Commit 055f47ee authored by Thibault Hallouin's avatar Thibault Hallouin
Browse files

update to new evalhyd API

i.e. where metrics is put as last positional parameter
No related merge requests found
Showing with 28 additions and 28 deletions
+28 -28
Subproject commit 8b952cebfdc8a25a3d54f340b56fd123e8b8a277
Subproject commit e4b61c01421a021c9d7c89139c0c0ef23d6524ed
%% Cell type:code id: tags:
``` python
import numpy
import evalhyd
```
%% Cell type:markdown id: tags:
## Calculate Nash-Sutcliffe efficiency on 1D arrays
%% Cell type:code id: tags:
``` python
# streamflow observations
obs = numpy.array(
[4.7, 4.3, 5.5, 2.7]
)
# streamflow simulations
sim = numpy.array(
[5.3, 4.2, 5.7, 2.3]
)
print(obs.shape, sim.shape)
```
%% Output
(4,) (4,)
%% Cell type:code id: tags:
``` python
nse, = evalhyd.evald(['NSE'], obs, sim)
nse, = evalhyd.evald(obs, sim, ['NSE'])
print(nse)
```
%% Output
[0.86298077]
%% Cell type:markdown id: tags:
## Calculate Nash-Sutcliffe efficiency on 2D arrays
%% Cell type:code id: tags:
``` python
# streamflow observations
obs = numpy.array(
[[4.7, 4.3, 5.5, 2.7]]
)
# streamflow simulations
sim = numpy.array(
[[5.3, 4.2, 5.7, 2.3],
[4.3, 4.2, 4.7, 4.3],
[5.3, 5.2, 5.7, 2.3]]
)
print(obs.shape, sim.shape)
```
%% Output
(1, 4) (3, 4)
%% Cell type:code id: tags:
``` python
nse, = evalhyd.evald(['NSE'], obs, sim)
nse, = evalhyd.evald(obs, sim, ['NSE'])
print(nse)
```
%% Output
[[0.86298077]
[0.18990385]
[0.67067308]]
......
%% Cell type:code id: tags:
``` python
import numpy
import pandas
import evalhyd
```
%% Cell type:markdown id: tags:
## Calculate Brier score and Brier skill score at once
%% Cell type:code id: tags:
``` python
# streamflow observations
obs = numpy.array(
[[4.7, 4.3, 5.5, 2.7, 4.1]]
)
# streamflow forecasts (3 members)
frc = numpy.array(
[[5.3, 4.2, 5.7, 2.3, 3.1],
[4.3, 4.2, 4.7, 4.3, 3.3],
[5.3, 5.2, 5.7, 2.3, 3.9]]
)
# streamflow thresholds (2)
thr = [4., 5.]
print(obs.shape, frc.shape)
```
%% Output
(1, 5) (3, 5)
%% Cell type:code id: tags:
``` python
bs, bss = evalhyd.evalp(['BS', 'BSS'], obs, frc, thr)
bs, bss = evalhyd.evalp(obs, frc, ['BS', 'BSS'], thr)
```
%% Cell type:code id: tags:
``` python
print(
pandas.DataFrame(
bs, [f"q > {thr[0]}", f"q > {thr[1]}"],
["Brier score"]
)
)
```
%% Output
Brier score
q > 4.0 0.222222
q > 5.0 0.133333
%% Cell type:code id: tags:
``` python
print(
pandas.DataFrame(
bss, [f"q > {thr[0]}", f"q > {thr[1]}"],
["Brier skill score"]
)
)
```
%% Output
Brier skill score
q > 4.0 -0.388889
q > 5.0 0.166667
%% Cell type:markdown id: tags:
## Calculate decompositions of Brier score
%% Cell type:code id: tags:
``` python
bs_crd, bs_lbd = evalhyd.evalp(['BS_CRD', 'BS_LBD'], obs, frc, [4., 5.])
bs_crd, bs_lbd = evalhyd.evalp(obs, frc, ['BS_CRD', 'BS_LBD'], [4., 5.])
```
%% Cell type:markdown id: tags:
### calibration-refinement decomposition
%% Cell type:code id: tags:
``` python
print(
pandas.DataFrame(
bs_crd, [f"q > {thr[0]}", f"q > {thr[1]}"],
["reliability", "resolution", "uncertainty"]
)
)
```
%% Output
reliability resolution uncertainty
q > 4.0 0.222222 0.16 0.16
q > 5.0 0.033333 0.06 0.16
%% Cell type:markdown id: tags:
#### bs = rel - res + unc?
%% Cell type:code id: tags:
``` python
numpy.allclose(
numpy.squeeze(bs),
bs_crd[:, 0] - bs_crd[:, 1] + bs_crd[:, 2]
)
```
%% Output
True
%% Cell type:markdown id: tags:
### likelihood-base rate decomposition
%% Cell type:code id: tags:
``` python
print(
pandas.DataFrame(
bs_lbd, [f"q > {thr[0]}", f"q > {thr[1]}"],
["type 2 bias", "discrimination", "sharpness"]
)
)
```
%% Output
type 2 bias discrimination sharpness
q > 4.0 0.072222 0.027778 0.177778
q > 5.0 0.072222 0.027778 0.088889
%% Cell type:markdown id: tags:
#### bs = t2b - dis + shr?
%% Cell type:code id: tags:
``` python
numpy.allclose(
numpy.squeeze(bs),
bs_lbd[:, 0] - bs_lbd[:, 1] + bs_lbd[:, 2]
)
```
%% Output
True
......
......@@ -118,7 +118,7 @@ setup(
author_email='thibault.hallouin@inrae.fr',
url='https://gitlab.irstea.fr/evalhyd/evalhyd-python',
description='Python bindings for EvalHyd',
long_description='An evaluator for hydrological simulations/forecasts.',
long_description='An evaluator for streamflow predictions.',
ext_modules=ext_modules,
install_requires=['pybind11>=2.0.1', 'numpy'],
cmdclass={'build_ext': BuildExt},
......
......@@ -29,18 +29,18 @@ PYBIND11_MODULE(evalhyd, m)
m.def(
"evald", evalhyd::evald<xt::pytensor<double, 1>>,
R"pbdoc(
Function to evaluate deterministic streamflow evaluation.
Function to evaluate deterministic streamflow predictions.
:Parameters:
metrics: `List[str]`
The sequence of evaluation metrics to be computed.
q_obs: `numpy.ndarray`
1D array of streamflow observations.
q_prd: `numpy.ndarray`
1D array of streamflow observations.
1D array of streamflow predictions.
metrics: `List[str]`
The sequence of evaluation metrics to be computed.
:Returns:
......@@ -59,31 +59,31 @@ PYBIND11_MODULE(evalhyd, m)
... [5.3, 4.2, 5.7, 2.3, 3.1]
... )
>>> nse, = evalhyd.evalp(['NSE'], obs, prd)
>>> nse, = evalhyd.evalp(obs, prd, ['NSE'])
>>> print(nse)
[0.6254771]
)pbdoc",
py::arg("metrics"), py::arg("q_obs"), py::arg("q_prd")
py::arg("q_obs"), py::arg("q_prd"), py::arg("metrics")
);
m.def(
"evald", evalhyd::evald<xt::pytensor<double, 2>>,
R"pbdoc(
Function to evaluate deterministic streamflow evaluation.
Function to evaluate deterministic streamflow predictions.
:Parameters:
metrics: `List[str]`
The sequence of evaluation metrics to be computed.
q_obs: `numpy.ndarray`
2D array of streamflow observations (with its temporal
dimension on axis 1).
q_prd: `numpy.ndarray`
2D array of streamflow observations (with its temporal
2D array of streamflow predictions (with its temporal
dimension on axis 1).
metrics: `List[str]`
The sequence of evaluation metrics to be computed.
:Returns:
`List[numpy.ndarray]`
......@@ -103,36 +103,36 @@ PYBIND11_MODULE(evalhyd, m)
... [5.3, 5.2, 5.7, 2.3, 3.9]]
... )
>>> nse, = evalhyd.evalp(['NSE'], obs, prd)
>>> nse, = evalhyd.evalp(obs, prd, ['NSE'])
>>> print(nse)
[[0.6254771 ]
[0.04341603]
[0.66364504]]
)pbdoc",
py::arg("metrics"), py::arg("q_obs"), py::arg("q_prd")
py::arg("q_obs"), py::arg("q_prd"), py::arg("metrics")
);
// probabilistic evaluation
m.def(
"evalp", evalhyd::evalp,
R"pbdoc(
Function to evaluate probabilistic streamflow evaluation.
Function to evaluate probabilistic streamflow predictions.
:Parameters:
metrics: `List[str]`
The sequence of evaluation metrics to be computed.
q_obs: `numpy.ndarray`
2D array of streamflow observations (with size 1 for
axis 0, and with the temporal dimension on axis 1).
q_prd: `numpy.ndarray`
2D array of streamflow observations (with the ensemble
2D array of streamflow predictions (with the ensemble
members on axis 0, and with the temporal dimension on
axis 1).
metrics: `List[str]`
The sequence of evaluation metrics to be computed.
q_thr: `List[float]`, optional
The streamflow threshold(s) to consider for the *metrics*
assessing the prediction of exceedance events. If not
......@@ -157,7 +157,7 @@ PYBIND11_MODULE(evalhyd, m)
... [5.3, 5.2, 5.7, 2.3, 3.9]]
... )
>>> bs, bs_lbd = evalhyd.evalp(['BS', 'BS_LBD'], obs, prd, [4., 5.])
>>> bs, bs_lbd = evalhyd.evalp(obs, prd, ['BS', 'BS_LBD'], [4., 5.])
>>> print(bs)
[[0.22222222]
[0.13333333]]
......@@ -165,12 +165,12 @@ PYBIND11_MODULE(evalhyd, m)
[[0.07222222 0.02777778 0.17777778]
[0.07222222 0.02777778 0.08888889]]
>>> qs, = evalhyd.evalp(['QS'], obs, prd)
>>> qs, = evalhyd.evalp(obs, prd, ['QS'])
>>> print(qs)
[[257.412129]]
)pbdoc",
py::arg("metrics"), py::arg("q_obs"), py::arg("q_prd"),
py::arg("q_obs"), py::arg("q_prd"), py::arg("metrics"),
py::arg("q_thr") = py::list()
);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment