Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hawkins sutton smoke tests, linting #1769

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tests/test_partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import xarray as xr

from xclim.ensembles import fractional_uncertainty, hawkins_sutton, lafferty_sriver
from xclim.ensembles._filters import _concat_hist, _model_in_all_scens, _single_member
from xclim.ensembles._filters import ( # noqa: F401
_concat_hist,
_model_in_all_scens,
_single_member,
)


def test_hawkins_sutton_smoke(open_dataset):
Expand Down
37 changes: 20 additions & 17 deletions xclim/ensembles/_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
import xarray as xr


def _concat_hist(da, **hist):
"""Concatenate historical scenario with future scenarios along time.
def _concat_hist(da: xr.DataArray, **hist) -> xr.DataArray:
r"""Concatenate historical scenario with future scenarios along the time dimension.

Parameters
----------
da : xr.DataArray
Input data where the historical scenario is stored alongside other, future, scenarios.
hist: {str: str}
Mapping of the scenario dimension name to the historical scenario coordinate, e.g. `scenario="historical"`.
Input data where the historical scenario is stored alongside other, future, scenarios.
\*\*hist : dict
Mapping of the scenario dimension name to the historical scenario coordinate, e.g. `scenario="historical"`.

Returns
-------
xr.DataArray
Data with the historical scenario is stacked in time before each one of the other scenarios.
Data with the historical scenario is stacked in time before each one of the other scenarios.

Notes
-----
Expand Down Expand Up @@ -51,6 +51,7 @@ def _concat_hist(da, **hist):

# Select historical scenario and drop it from the data
h = da.sel(**hist).dropna("time", how="all")
h = h.drop_vars(dim)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relevant changes here.

ens = da.drop_sel(**hist)

index = ens[dim]
Expand All @@ -59,15 +60,17 @@ def _concat_hist(da, **hist):
return xr.concat([h, bare], dim="time").assign_coords({dim: index})


def _model_in_all_scens(da, dimensions=None):
def _model_in_all_scens(
da: xr.DataArray, dimensions: dict | None = None
) -> xr.DataArray:
"""Return data with only simulations that have at least one member in each scenario.

Parameters
----------
da: xr.DataArray
Input data with dimensions for time, member, model and scenario.
dimensions: dict
Mapping from original dimension names to standard dimension names: scenario, model, member.
da : xr.DataArray
Input data with dimensions for time, member, model and scenario.
dimensions : dict, optional
Mapping from original dimension names to standard dimension names: scenario, model, member.

Returns
-------
Expand Down Expand Up @@ -100,20 +103,20 @@ def _model_in_all_scens(da, dimensions=None):
return da.sel(model=ok).rename(dimensions)


def _single_member(da, dimensions=None):
def _single_member(da: xr.DataArray, dimensions: dict | None = None) -> xr.DataArray:
"""Return data for a single member per model.

Parameters
----------
da : xr.DataArray
Input data with dimensions for time, member, model and scenario.
dimensions: dict
Mapping from original dimension names to standard dimension names: scenario, model, member.
Input data with dimensions for time, member, model and scenario.
dimensions : dict
Mapping from original dimension names to standard dimension names: scenario, model, member.

Returns
-------
xr.DataArray
Data with only one member per model.
Data with only one member per model.

Notes
-----
Expand Down Expand Up @@ -147,6 +150,6 @@ def _single_member(da, dimensions=None):
return out.rename(dimensions)


def reverse_dict(d):
def reverse_dict(d: dict) -> dict:
"""Reverse dictionary."""
return {v: k for (k, v) in d.items()}
Loading