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

improve typing of DataArray and Dataset reductions #6746

Merged
merged 9 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
58 changes: 29 additions & 29 deletions xarray/core/_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from xarray.core import duck_array_ops
from xarray.core.options import OPTIONS
from xarray.core.types import Dims
from xarray.core.types import Dims, Self
from xarray.core.utils import contains_only_chunked_or_numpy, module_available

if TYPE_CHECKING:
Expand All @@ -30,7 +30,7 @@ def reduce(
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
) -> Dataset:
) -> Self:
raise NotImplementedError()

def count(
Expand All @@ -39,7 +39,7 @@ def count(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``count`` along some dimension(s).

Expand Down Expand Up @@ -111,7 +111,7 @@ def all(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``all`` along some dimension(s).

Expand Down Expand Up @@ -183,7 +183,7 @@ def any(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``any`` along some dimension(s).

Expand Down Expand Up @@ -256,7 +256,7 @@ def max(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``max`` along some dimension(s).

Expand Down Expand Up @@ -343,7 +343,7 @@ def min(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``min`` along some dimension(s).

Expand Down Expand Up @@ -430,7 +430,7 @@ def mean(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``mean`` along some dimension(s).

Expand Down Expand Up @@ -522,7 +522,7 @@ def prod(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``prod`` along some dimension(s).

Expand Down Expand Up @@ -629,7 +629,7 @@ def sum(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``sum`` along some dimension(s).

Expand Down Expand Up @@ -736,7 +736,7 @@ def std(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``std`` along some dimension(s).

Expand Down Expand Up @@ -840,7 +840,7 @@ def var(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``var`` along some dimension(s).

Expand Down Expand Up @@ -943,7 +943,7 @@ def median(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``median`` along some dimension(s).

Expand Down Expand Up @@ -1034,7 +1034,7 @@ def cumsum(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``cumsum`` along some dimension(s).

Expand Down Expand Up @@ -1127,7 +1127,7 @@ def cumprod(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> Dataset:
) -> Self:
"""
Reduce this Dataset's data by applying ``cumprod`` along some dimension(s).

Expand Down Expand Up @@ -1226,7 +1226,7 @@ def reduce(
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
) -> DataArray:
) -> Self:
raise NotImplementedError()

def count(
Expand All @@ -1235,7 +1235,7 @@ def count(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``count`` along some dimension(s).

Expand Down Expand Up @@ -1301,7 +1301,7 @@ def all(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``all`` along some dimension(s).

Expand Down Expand Up @@ -1367,7 +1367,7 @@ def any(
*,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``any`` along some dimension(s).

Expand Down Expand Up @@ -1434,7 +1434,7 @@ def max(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``max`` along some dimension(s).

Expand Down Expand Up @@ -1513,7 +1513,7 @@ def min(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``min`` along some dimension(s).

Expand Down Expand Up @@ -1592,7 +1592,7 @@ def mean(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``mean`` along some dimension(s).

Expand Down Expand Up @@ -1676,7 +1676,7 @@ def prod(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``prod`` along some dimension(s).

Expand Down Expand Up @@ -1773,7 +1773,7 @@ def sum(
min_count: int | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``sum`` along some dimension(s).

Expand Down Expand Up @@ -1870,7 +1870,7 @@ def std(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``std`` along some dimension(s).

Expand Down Expand Up @@ -1964,7 +1964,7 @@ def var(
ddof: int = 0,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``var`` along some dimension(s).

Expand Down Expand Up @@ -2057,7 +2057,7 @@ def median(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``median`` along some dimension(s).

Expand Down Expand Up @@ -2140,7 +2140,7 @@ def cumsum(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``cumsum`` along some dimension(s).

Expand Down Expand Up @@ -2229,7 +2229,7 @@ def cumprod(
skipna: bool | None = None,
keep_attrs: bool | None = None,
**kwargs: Any,
) -> DataArray:
) -> Self:
"""
Reduce this DataArray's data by applying ``cumprod`` along some dimension(s).

Expand Down
4 changes: 2 additions & 2 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,14 +1402,14 @@ def _cov_corr(
) / (valid_count)

if method == "cov":
return cov # type: ignore[return-value]
return cov # type: ignore[return-value,unused-ignore]

else:
# compute std + corr
da_a_std = da_a.std(dim=dim)
da_b_std = da_b.std(dim=dim)
corr = cov / (da_a_std * da_b_std)
return corr # type: ignore[return-value]
return corr # type: ignore[return-value,unused-ignore]
headtr1ck marked this conversation as resolved.
Show resolved Hide resolved


def cross(
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def __setitem__(self, key, value) -> None:
self.dataset[dim_indexers] = value


class Dataset(
class Dataset( # type: ignore[misc,unused-ignore]
headtr1ck marked this conversation as resolved.
Show resolved Hide resolved
DataWithCoords,
DatasetAggregations,
DatasetArithmetic,
Expand Down
10 changes: 5 additions & 5 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import itertools
import math
import warnings
from collections.abc import Hashable, Iterator, Mapping
from collections.abc import Hashable, Iterator, Mapping, Sequence
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar

import numpy as np
Expand Down Expand Up @@ -467,9 +467,8 @@ def reduce(
obj, rolling_dim, keep_attrs=keep_attrs, fill_value=fillna
)

result = windows.reduce(
func, dim=list(rolling_dim.values()), keep_attrs=keep_attrs, **kwargs
)
dim: Sequence[Hashable] = list(rolling_dim.values())
result = windows.reduce(func, dim=dim, keep_attrs=keep_attrs, **kwargs)

# Find valid windows based on count.
counts = self._counts(keep_attrs=False)
Expand All @@ -486,14 +485,15 @@ def _counts(self, keep_attrs: bool | None) -> DataArray:
# array is faster to be reduced than object array.
# The use of skipna==False is also faster since it does not need to
# copy the strided array.
dim: Sequence[Hashable] = list(rolling_dim.values())
counts = (
self.obj.notnull(keep_attrs=keep_attrs)
.rolling(
{d: w for d, w in zip(self.dim, self.window)},
center={d: self.center[i] for i, d in enumerate(self.dim)},
)
.construct(rolling_dim, fill_value=False, keep_attrs=keep_attrs)
.sum(dim=list(rolling_dim.values()), skipna=False, keep_attrs=keep_attrs)
.sum(dim=dim, skipna=False, keep_attrs=keep_attrs)
)
return counts

Expand Down
6 changes: 3 additions & 3 deletions xarray/util/generate_aggregations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from xarray.core import duck_array_ops
from xarray.core.options import OPTIONS
from xarray.core.types import Dims
from xarray.core.types import Dims, Self
from xarray.core.utils import contains_only_chunked_or_numpy, module_available

if TYPE_CHECKING:
Expand All @@ -50,7 +50,7 @@ def reduce(
keep_attrs: bool | None = None,
keepdims: bool = False,
**kwargs: Any,
) -> {obj}:
) -> Self:
raise NotImplementedError()"""

GROUPBY_PREAMBLE = """
Expand Down Expand Up @@ -108,7 +108,7 @@ def {method}(
*,{extra_kwargs}
keep_attrs: bool | None = None,
**kwargs: Any,
) -> {obj}:
) -> Self:
"""
Reduce this {obj}'s data by applying ``{method}`` along some dimension(s).

Expand Down