Skip to content

Commit

Permalink
fix doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Bell committed Mar 10, 2024
1 parent c8920c7 commit 2ac0527
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 94 deletions.
18 changes: 9 additions & 9 deletions xskillscore/core/comparative.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ def sign_test(
>>> (-1 * confidence).plot(color="gray") # doctest: +ELLIPSIS
[<matplotlib.lines.Line2D object at 0x...>]
>>> walk
<xarray.DataArray (time: 30)>
<xarray.DataArray (time: 30)> Size: 240B
array([ 1, 0, 1, 2, 1, 2, 3, 4, 5, 6, 5, 6, 7, 6, 7, 8, 9,
10, 9, 10, 11, 12, 13, 12, 11, 12, 13, 14, 15, 14])
Coordinates:
* time (time) int64 0 1 2 3 4 5 6 7 8 9 ... 20 21 22 23 24 25 26 27 28 29
* time (time) int64 240B 0 1 2 3 4 5 6 7 8 ... 21 22 23 24 25 26 27 28 29
>>> significantly_different
<xarray.DataArray (time: 30)>
<xarray.DataArray (time: 30)> Size: 30B
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False, True, True, True,
True, True, True, True, True, True, True, True, True,
True, True, True])
Coordinates:
* time (time) int64 0 1 2 3 4 5 6 7 8 9 ... 20 21 22 23 24 25 26 27 28 29
alpha float64 0.05
* time (time) int64 240B 0 1 2 3 4 5 6 7 8 ... 21 22 23 24 25 26 27 28 29
alpha float64 8B 0.05
References
----------
Expand Down Expand Up @@ -274,13 +274,13 @@ def halfwidth_ci_test(
... f1, f2, o, "mae", time_dim="time", dim=[], alpha=0.05
... )
>>> significantly_different
<xarray.DataArray ()>
<xarray.DataArray ()> Size: 1B
array(False)
>>> diff
<xarray.DataArray ()>
<xarray.DataArray ()> Size: 8B
array(-0.01919449)
>>> hwci
<xarray.DataArray ()>
<xarray.DataArray ()> Size: 8B
array(0.38729387)
>>> # absolute magnitude of difference is smaller than half-width of
>>> # confidence interval, therefore not significant at level alpha=0.05
Expand All @@ -289,7 +289,7 @@ def halfwidth_ci_test(
... f1, f2 + 2.0, o, "mae", time_dim="time", dim=[], alpha=0.05
... )
>>> significantly_different
<xarray.DataArray ()>
<xarray.DataArray ()> Size: 1B
array(True)
References
Expand Down
38 changes: 19 additions & 19 deletions xskillscore/core/deterministic.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def linslope(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.linslope(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[-0.30948771, -0.21562529, -0.63141304],
[ 0.31446077, 2.23858011, 0.44743617],
[-0.22243944, 0.47034784, 1.08512859]])
Expand Down Expand Up @@ -205,7 +205,7 @@ def pearson_r(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.pearson_r(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[-0.17455755, -0.26648379, -0.74265833],
[ 0.32535918, 0.42496646, 0.1940647 ],
[-0.3203094 , 0.33207755, 0.89250429]])
Expand Down Expand Up @@ -275,7 +275,7 @@ def pearson_r_p_value(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.pearson_r_p_value(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.77888033, 0.66476199, 0.15051516],
[0.59316935, 0.47567465, 0.75446898],
[0.59925464, 0.58509064, 0.04161894]])
Expand Down Expand Up @@ -357,7 +357,7 @@ def effective_sample_size(a, b, dim="time", skipna=False, keep_attrs=False):
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.effective_sample_size(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[4., 0., 4.],
[3., 4., 4.],
[3., 4., 2.]])
Expand Down Expand Up @@ -465,7 +465,7 @@ def pearson_r_eff_p_value(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.pearson_r_eff_p_value(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.82544245, nan, 0.25734167],
[0.78902959, 0.57503354, 0.8059353 ],
[0.79242625, 0.66792245, nan]])
Expand Down Expand Up @@ -547,7 +547,7 @@ def spearman_r(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.spearman_r(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[-0.6, -0.5, -0.7],
[ 0.4, 0.3, 0.3],
[-0.3, -0.1, 0.9]])
Expand Down Expand Up @@ -616,7 +616,7 @@ def spearman_r_p_value(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.spearman_r_p_value(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.28475698, 0.39100222, 0.1881204 ],
[0.50463158, 0.62383766, 0.62383766],
[0.62383766, 0.87288857, 0.03738607]])
Expand Down Expand Up @@ -716,7 +716,7 @@ def spearman_r_eff_p_value(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.spearman_r_eff_p_value(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.4 , nan, 0.3 ],
[0.73802024, 0.7 , 0.7 ],
[0.80602663, 0.9 , nan]])
Expand Down Expand Up @@ -819,8 +819,8 @@ def r2(
--------
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> r2(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
>>> xs.r2(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[ -3.77828319, -1.25687543, -2.52495914],
[ -0.67280201, -39.45271514, -5.78241791],
[ -1.66615797, -1.56749317, 0.09843265]])
Expand Down Expand Up @@ -888,8 +888,8 @@ def me(
--------
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> me(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
>>> xs.me(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[ 0.01748202, -0.14165293, 0.22455357],
[ 0.13893709, -0.23513353, -0.18174132],
[-0.29317762, 0.16887445, -0.17297527]])
Expand Down Expand Up @@ -963,7 +963,7 @@ def rmse(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.rmse(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.30366872, 0.5147618 , 0.57410211],
[0.2963848 , 0.37177283, 0.40563885],
[0.55686111, 0.38189299, 0.21317579]])
Expand Down Expand Up @@ -1038,7 +1038,7 @@ def mse(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.mse(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.09221469, 0.26497971, 0.32959323],
[0.08784395, 0.13821504, 0.16454288],
[0.31009429, 0.14584225, 0.04544392]])
Expand Down Expand Up @@ -1111,8 +1111,8 @@ def mae(
--------
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> mae(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
>>> xs.mae(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.26014863, 0.40137207, 0.48871634],
[0.18809417, 0.30197826, 0.2984658 ],
[0.52934554, 0.19820357, 0.17335851]])
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def median_absolute_error(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.median_absolute_error(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.28798217, 0.23322591, 0.62067468],
[0.12146232, 0.20314509, 0.23442927],
[0.59041981, 0.03289321, 0.21343494]])
Expand Down Expand Up @@ -1261,7 +1261,7 @@ def mape(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.mape(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.6268041 , 9.45134297, 3.28717608],
[0.27099746, 1.58105176, 1.48258713],
[6.55806162, 0.22271096, 0.39302745]])
Expand Down Expand Up @@ -1338,7 +1338,7 @@ def smape(
>>> a = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> b = xr.DataArray(np.random.rand(5, 3, 3), dims=["time", "x", "y"])
>>> xs.smape(a, b, dim="time")
<xarray.DataArray (x: 3, y: 3)>
<xarray.DataArray (x: 3, y: 3)> Size: 72B
array([[0.35591619, 0.43662087, 0.55372571],
[0.1864336 , 0.45831965, 0.38473469],
[0.58730494, 0.18081757, 0.14960832]])
Expand Down
70 changes: 35 additions & 35 deletions xskillscore/core/probabilistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ def crps_gaussian(
... )
>>> mu = forecasts.mean("member")
>>> sig = forecasts.std("member")
>>> crps_gaussian(observations, mu, sig, dim="x")
<xarray.DataArray (y: 3)>
>>> xs.crps_gaussian(observations, mu, sig, dim="x")
<xarray.DataArray (y: 3)> Size: 24B
array([1.0349773 , 0.36521376, 0.39017126])
Coordinates:
* y (y) int64 0 1 2
* y (y) int64 24B 0 1 2
See Also
--------
Expand Down Expand Up @@ -178,10 +178,10 @@ def crps_quadrature(
... )
>>> from scipy.stats import norm
>>> xs.crps_quadrature(observations, norm, dim="x")
<xarray.DataArray (y: 3)>
<xarray.DataArray (y: 3)> Size: 24B
array([0.80280921, 0.31818197, 0.32364912])
Coordinates:
* y (y) int64 0 1 2
* y (y) int64 24B 0 1 2
See Also
--------
Expand Down Expand Up @@ -259,11 +259,11 @@ def crps_ensemble(
... np.random.normal(size=(3, 3, 3)),
... coords=[("x", np.arange(3)), ("y", np.arange(3)), ("member", np.arange(3))],
... )
>>> crps_ensemble(observations, forecasts, dim="x")
<xarray.DataArray (y: 3)>
>>> xs.crps_ensemble(observations, forecasts, dim="x")
<xarray.DataArray (y: 3)> Size: 24B
array([1.04497153, 0.48997746, 0.47994095])
Coordinates:
* y (y) int64 0 1 2
* y (y) int64 24B 0 1 2
See Also
--------
Expand Down Expand Up @@ -347,10 +347,10 @@ def brier_score(
... coords=[("x", np.arange(3)), ("y", np.arange(3)), ("member", np.arange(3))],
... )
>>> xs.brier_score(observations > 0.5, (forecasts > 0.5).mean("member"), dim="y")
<xarray.DataArray (x: 3)>
<xarray.DataArray (x: 3)> Size: 24B
array([0.37037037, 0.14814815, 0.51851852])
Coordinates:
* x (x) int64 0 1 2
* x (x) int64 24B 0 1 2
See Also
--------
Expand Down Expand Up @@ -451,10 +451,10 @@ def threshold_brier_score(
... )
>>> threshold = [0.2, 0.5, 0.8]
>>> xs.threshold_brier_score(observations, forecasts, threshold)
<xarray.DataArray (threshold: 3)>
<xarray.DataArray (threshold: 3)> Size: 24B
array([0.27160494, 0.34567901, 0.18518519])
Coordinates:
* threshold (threshold) float64 0.2 0.5 0.8
* threshold (threshold) float64 24B 0.2 0.5 0.8
See Also
--------
Expand Down Expand Up @@ -643,12 +643,12 @@ def rps(
... )
>>> category_edges = np.array([0.33, 0.66])
>>> xs.rps(o, f, category_edges, dim="x")
<xarray.DataArray (y: 3)>
<xarray.DataArray (y: 3)> Size: 24B
array([0.37037037, 0.81481481, 1. ])
Coordinates:
* y (y) int64 0 1 2
observations_category_edge <U45 '[-np.inf, 0.33), [0.33, 0.66), [0.66, n...
forecasts_category_edge <U45 '[-np.inf, 0.33), [0.33, 0.66), [0.66, n...
* y (y) int64 24B 0 1 2
observations_category_edge <U45 180B '[-np.inf, 0.33), [0.33, 0.66), [0....
forecasts_category_edge <U45 180B '[-np.inf, 0.33), [0.33, 0.66), [0....
You can also define multi-dimensional ``category_edges``, e.g. with xr.quantile.
However, you still need to ensure that ``category_edges`` covers the forecasts
Expand All @@ -658,12 +658,12 @@ def rps(
... {"quantile": "category_edge"}
... )
>>> xs.rps(o, f, category_edges, dim="x")
<xarray.DataArray (y: 3)>
<xarray.DataArray (y: 3)> Size: 24B
array([0.37037037, 0.81481481, 0.88888889])
Coordinates:
* y (y) int64 0 1 2
observations_category_edge <U45 '[-np.inf, 0.33), [0.33, 0.66), [0.66, n...
forecasts_category_edge <U45 '[-np.inf, 0.33), [0.33, 0.66), [0.66, n...
* y (y) int64 24B 0 1 2
observations_category_edge <U45 180B '[-np.inf, 0.33), [0.33, 0.66), [0....
forecasts_category_edge <U45 180B '[-np.inf, 0.33), [0.33, 0.66), [0....
If you have probabilistic forecasts, i.e. without a ``member`` dimension but
different ``category`` probabilities, you can also provide probability
Expand Down Expand Up @@ -705,21 +705,21 @@ def rps(
... ).mean("member")
>>> assert (f_p.sum("category") == 1).all()
>>> xs.rps(o_p, f_p, category_edges=None, dim="x", input_distributions="p")
<xarray.DataArray (y: 3)>
<xarray.DataArray (y: 3)> Size: 24B
array([0.37037037, 0.81481481, 0.88888889])
Coordinates:
* y (y) int64 0 1 2
* y (y) int64 24B 0 1 2
Providing cumulative distribution inputs yields identical results, where highest
category equals 1 by default and can be ignored:
>>> o_c = o < category_edges
>>> f_c = (f < category_edges).mean("member")
>>> xs.rps(o_c, f_c, category_edges=None, dim="x", input_distributions="c")
<xarray.DataArray (y: 3)>
<xarray.DataArray (y: 3)> Size: 24B
array([0.37037037, 0.81481481, 0.88888889])
Coordinates:
* y (y) int64 0 1 2
* y (y) int64 24B 0 1 2
References
----------
Expand Down Expand Up @@ -915,13 +915,13 @@ def rank_histogram(
... coords=[("x", np.arange(3)), ("y", np.arange(3)), ("member", np.arange(3))],
... )
>>> xs.rank_histogram(observations, forecasts, dim="x")
<xarray.DataArray 'histogram_rank' (y: 3, rank: 4)>
<xarray.DataArray 'histogram_rank' (y: 3, rank: 4)> Size: 96B
array([[0, 0, 1, 2],
[0, 1, 2, 0],
[0, 0, 2, 1]])
Coordinates:
* y (y) int64 0 1 2
* rank (rank) float64 1.0 2.0 3.0 4.0
* y (y) int64 24B 0 1 2
* rank (rank) float64 32B 1.0 2.0 3.0 4.0
Notes
-----
Expand Down Expand Up @@ -1025,12 +1025,12 @@ def discrimination(
>>> forecast_event_likelihood = (forecasts > 0).mean("member")
>>> observed_event = observations > 0
>>> xs.discrimination(observed_event, forecast_event_likelihood, dim=["x", "y"])
<xarray.DataArray (event: 2, forecast_probability: 5)>
<xarray.DataArray (event: 2, forecast_probability: 5)> Size: 80B
array([[0.00437637, 0.15536105, 0.66739606, 0.12472648, 0.04814004],
[0.00451467, 0.16704289, 0.66365688, 0.1241535 , 0.04063205]])
Coordinates:
* forecast_probability (forecast_probability) float64 0.1 0.3 0.5 0.7 0.9
* event (event) bool True False
* forecast_probability (forecast_probability) float64 40B 0.1 0.3 0.5 0.7 0.9
* event (event) bool 2B True False
References
----------
Expand Down Expand Up @@ -1108,14 +1108,14 @@ def reliability(
... coords=[("x", np.arange(3)), ("y", np.arange(3))],
... )
>>> xs.reliability(observations > 0.1, (forecasts > 0.1).mean("member"), dim="x")
<xarray.DataArray (y: 3, forecast_probability: 5)>
<xarray.DataArray (y: 3, forecast_probability: 5)> Size: 120B
array([[nan, 0. , nan, 1. , nan],
[1. , 0.5, nan, nan, nan],
[nan, 0. , nan, 0. , nan]])
Coordinates:
* y (y) int64 0 1 2
* forecast_probability (forecast_probability) float64 0.1 0.3 0.5 0.7 0.9
samples (y, forecast_probability) float64 0.0 2.0 ... 1.0 0.0
* y (y) int64 24B 0 1 2
* forecast_probability (forecast_probability) float64 40B 0.1 0.3 0.5 0.7 0.9
samples (y, forecast_probability) float64 120B 0.0 2.0 ... 0.0
Notes
-----
Expand Down Expand Up @@ -1264,7 +1264,7 @@ def roc(
>>> o = f.copy()
>>> category_edges = np.linspace(-2, 2, 5)
>>> xs.roc(o, f, category_edges, dim=["time"])
<xarray.DataArray 'histogram_observations_forecasts' ()>
<xarray.DataArray 'histogram_observations_forecasts' ()> Size: 8B
array(1.)
See also
Expand Down
Loading

0 comments on commit 2ac0527

Please sign in to comment.