Skip to content

Commit

Permalink
Merge pull request #339 from xarray-contrib/AS_fix_resample_idx_no_coord
Browse files Browse the repository at this point in the history
dim in resample_idx not coords
  • Loading branch information
raybellwaves committed Jul 26, 2021
2 parents ef0c0fd + 32f9df4 commit 3b96309
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Changelog History
xskillscore v0.0.23 (2021-xx-xx)
--------------------------------

Internal Changes
~~~~~~~~~~~~~~~~
- :py:func:`~xskillscore.resampling.resample_iterations_idx` do not break when ``dim`` is
not coordinate. (:issue:`303`, :pr:`339`) `Aaron Spring`_


xskillscore v0.0.22 (2021-06-29)
--------------------------------
Expand Down
11 changes: 10 additions & 1 deletion xskillscore/core/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def resample_iterations(forecast, iterations, dim="member", dim_max=None, replac
forecast_smp.append(forecast.isel({dim: idx}).assign_coords({dim: new_dim}))
forecast_smp = xr.concat(forecast_smp, dim="iteration", **CONCAT_KWARGS)
forecast_smp["iteration"] = np.arange(iterations)
if dim not in forecast.coords:
del forecast_smp.coords[dim]
return forecast_smp.transpose(..., "iteration")


Expand Down Expand Up @@ -172,7 +174,12 @@ def resample_iterations_idx(
for interannual-to-decadal predictions experiments. Climate Dynamics, 40(1–2),
245–272. https://doi.org/10/f4jjvf
"""
# equivalent to above
if dim not in forecast.coords:
forecast.coords[dim] = np.arange(0, forecast[dim].size)
dim_coord_set = True
else:
dim_coord_set = False

select_dim_items = forecast[dim].size
new_dim = forecast[dim]

Expand Down Expand Up @@ -205,4 +212,6 @@ def select_bootstrap_indices_ufunc(x, idx):
# return only dim_max members
if dim_max is not None and dim_max <= forecast[dim].size:
forecast_smp = forecast_smp.isel({dim: slice(None, dim_max)})
if dim_coord_set:
del forecast_smp.coords[dim]
return forecast_smp
11 changes: 11 additions & 0 deletions xskillscore/tests/test_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,14 @@ def test_resample_inputs(a_1d, func, input, chunk, replace):
assert is_dask_collection(actual) if chunk else not is_dask_collection(actual)
# input type preserved
assert type(actual) == type(a_1d)


@pytest.mark.parametrize("func", resample_iterations_funcs)
def test_resample_dim_no_coord(func):
"""resample_iterations doesnt fail when no dim coords"""
da = xr.DataArray(
np.random.rand(100, 3, 3),
coords=[("time", np.arange(100)), ("x", np.arange(3)), ("y", np.arange(3))],
)
del da.coords["time"]
assert "time" not in func(da, 2, dim="time").coords

0 comments on commit 3b96309

Please sign in to comment.