Skip to content

Make constructing slices lazily. #1994

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

Merged
merged 5 commits into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions asv_bench/benchmarks/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,44 @@
from . import parameterized, randn, requires_dask

nx = 3000
long_nx = 30000000
ny = 2000
nt = 1000
window = 20

randn_xy = randn((nx, ny), frac_nan=0.1)
randn_xt = randn((nx, nt))
randn_t = randn((nt, ))
randn_long = randn((long_nx, ), frac_nan=0.1)


class Rolling(object):
def setup(self, *args, **kwargs):
self.ds = xr.Dataset(
{'var1': (('x', 'y'), randn((nx, ny), frac_nan=0.1)),
'var2': (('x', 't'), randn((nx, nt))),
'var3': (('t', ), randn(nt))},
{'var1': (('x', 'y'), randn_xy),
'var2': (('x', 't'), randn_xt),
'var3': (('t', ), randn_t)},
coords={'x': np.arange(nx),
'y': np.linspace(0, 1, ny),
't': pd.date_range('1970-01-01', periods=nt, freq='D'),
'x_coords': ('x', np.linspace(1.1, 2.1, nx))})
self.da_long = xr.DataArray(randn_long, dims='x',
coords={'x': np.arange(long_nx) * 0.1})

@parameterized(['func', 'center'],
(['mean', 'count'], [True, False]))
def time_rolling(self, func, center):
getattr(self.ds.rolling(x=window, center=center), func)()

@parameterized(['func', 'pandas'],
(['mean', 'count'], [True, False]))
def time_rolling_long(self, func, pandas):
if pandas:
se = self.da_long.to_series()
getattr(se.rolling(window=window), func)()
else:
getattr(self.da_long.rolling(x=window), func)()
Copy link
Member Author

@fujiisoup fujiisoup Mar 16, 2018

Choose a reason for hiding this comment

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

Also added a benchmark using pandas for long 1d-array, in order to make it easier to find the cause of the regression.
But is it too verbose as pandas might make the similar benchmark tests?

Copy link
Member

Choose a reason for hiding this comment

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

I'm OK with this.


@parameterized(['window_', 'min_periods'],
([20, 40], [5, None]))
def time_rolling_np(self, window_, min_periods):
Expand All @@ -47,3 +64,4 @@ def setup(self, *args, **kwargs):
requires_dask()
super(RollingDask, self).setup(**kwargs)
self.ds = self.ds.chunk({'x': 100, 'y': 50, 't': 50})
self.da_long = self.da_long.chunk({'x': 10000})
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Documentation
Enhancements
~~~~~~~~~~~~

- Some speed improvement to construct :py:class:`~xarray.DataArrayRolling`
object (:issue:`1993`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

Bug fixes
~~~~~~~~~

Expand Down
25 changes: 6 additions & 19 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,34 +151,21 @@ def __init__(self, obj, min_periods=None, center=False, **windows):
"""
super(DataArrayRolling, self).__init__(obj, min_periods=min_periods,
center=center, **windows)
self.window_indices = None
self.window_labels = None

self._setup_windows()
self.window_labels = self.obj[self.dim]

def __iter__(self):
for (label, indices) in zip(self.window_labels, self.window_indices):
window = self.obj.isel(**{self.dim: indices})
stops = np.arange(1, len(self.window_labels) + 1)
starts = stops - int(self.window)
starts[:int(self.window)] = 0
for (label, start, stop) in zip(self.window_labels, starts, stops):
window = self.obj.isel(**{self.dim: slice(start, stop)})

counts = window.count(dim=self.dim)
window = window.where(counts >= self._min_periods)

yield (label, window)

def _setup_windows(self):
"""
Find the indices and labels for each window
"""
self.window_labels = self.obj[self.dim]
window = int(self.window)
dim_size = self.obj[self.dim].size

stops = np.arange(dim_size) + 1
starts = np.maximum(stops - window, 0)

self.window_indices = [slice(start, stop)
for start, stop in zip(starts, stops)]

def construct(self, window_dim, stride=1, fill_value=dtypes.NA):
"""
Convert this rolling object to xr.DataArray,
Expand Down