Skip to content

Commit

Permalink
Explore moving functions from reduce to apply_ufunc
Browse files Browse the repository at this point in the history
  • Loading branch information
max-sixty committed Sep 17, 2023
1 parent fb9c9b3 commit 5ea724b
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions xarray/core/rolling_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np

from xarray.core.computation import apply_ufunc
from xarray.core.options import _get_keep_attrs
from xarray.core.pdcompat import count_not_none
from xarray.core.pycompat import is_duck_dask_array
Expand Down Expand Up @@ -128,9 +129,18 @@ def mean(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)

return self.obj.reduce(
move_exp_nanmean, dim=self.dim, alpha=self.alpha, keep_attrs=keep_attrs
)
dim_order = self.obj.dims

return apply_ufunc(
move_exp_nanmean,
self.obj,
input_core_dims=[[self.dim]],
kwargs=dict(alpha=self.alpha, axis=-1),
output_core_dims=[[self.dim]],
exclude_dims={self.dim},
keep_attrs=keep_attrs,
on_missing_core_dim="copy",
).transpose(*dim_order)

def sum(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
"""
Expand All @@ -155,6 +165,15 @@ def sum(self, keep_attrs: bool | None = None) -> T_DataWithCoords:
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)

return self.obj.reduce(
move_exp_nansum, dim=self.dim, alpha=self.alpha, keep_attrs=keep_attrs
)
dim_order = self.obj.dims

return apply_ufunc(
move_exp_nansum,
self.obj,
input_core_dims=[[self.dim]],
kwargs=dict(alpha=self.alpha, axis=-1),
output_core_dims=[[self.dim]],
exclude_dims={self.dim},
keep_attrs=keep_attrs,
on_missing_core_dim="copy",
).transpose(*dim_order)

0 comments on commit 5ea724b

Please sign in to comment.