8000 Optimize `ffill`, `bfill` with dask when `limit` is specified by josephnowak · Pull Request #9771 · pydata/xarray · GitHub
[go: up one dir, main page]

Skip to content

Optimize ffill, bfill with dask when limit is specified #9771

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 12, 2024
commit fde767b75702e27595fbf967c9ecd7961e3e38aa
42 changes: 17 additions & 25 deletions xarray/core/dask_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,7 @@ def _fill_with_last_one(a, b):

def _last_one(a, keepdims, axis):
# Find a faster way to find the last valid element of an array without ffill
return np.take(
_push(a, axis=axis),
axis=axis,
indices=[-1]
)
return np.take(_push(a, axis=axis), axis=axis, indices=[-1])

def _dtype_push(a, axis, dtype=None):
# Not sure why the blelloch algorithm force to receive a dtype
Expand All @@ -93,40 +89,36 @@ def _dtype_push(a, axis, dtype=None):
axis=axis,
dtype=array.dtype,
method=method,
preop=_last_one
preop=_last_one,
)

if n is not None and 0 < n < array.shape[axis] - 1:

def reset_cumsum(a, axis, dtype=None):
cumsum = np.cumsum(a, axis=axis)
reset_points = np.maximum.accumulate(
np.where(a == 0, cumsum, 0), axis=axis
)
reset_points = np.maximum.accumulate(np.where(a == 0, cumsum, 0), axis=axis)
return cumsum - reset_points

def last_reset_cumsum(a, axis, keepdims=None):
return np.take(
reset_cumsum(a, axis=axis),
axis=axis,
indices=[-1]
)
return np.take(reset_cumsum(a, axis=axis), axis=axis, indices=[-1])

def combine_reset_cumsum(a, b):
bitmask = np.cumprod(b != 0, axis=axis)
return np.where(bitmask, b + a, b)

valid_positions = da.reductions.cumreduction(
func=reset_cumsum,
binop=combine_reset_cumsum,
ident=0,
x=da.isnan(array, dtype=int),
axis=axis,
dtype=int,
method=method,
preop=last_reset_cumsum
) <= n
valid_positions = (
da.reductions.cumreduction(
func=reset_cumsum,
binop=combine_reset_cumsum,
ident=0,
x=da.isnan(array, dtype=int),
axis=axis,
dtype=int,
method=method,
preop=last_reset_cumsum,
)
<= n
)
pushed_array = da.where(valid_positions, pushed_array, np.nan)

return pushed_array

7 changes: 2 additions & 5 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,10 +1020,7 @@ def test_push_dask(method):
for c in range(1, 11):
with raise_if_dask_computes():
actual = push(
dask.array.from_array(array, chunks=c),
axis=0,
n=n,
method=method
dask.array.from_array(array, chunks=c), axis=0, n=n, method=method
)
np.testing.assert_equal(actual, expected)

Expand All @@ -1033,7 +1030,7 @@ def test_push_dask(method):
dask.array.from_array(array, chunks=(1, 2, 3, 2, 2, 1, 1)),
axis=0,
n=n,
method=method
method=method,
)
np.testing.assert_equal(actual, expected)

Expand Down
Loading
0