-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement cumulative aggregation (dask#433)
Co-authored-by: crusaderky <crusaderky@gmail.com>
- Loading branch information
1 parent
9f76576
commit fa515d2
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import functools | ||
|
||
from dask.dataframe import methods | ||
from dask.utils import M | ||
|
||
from dask_expr._expr import Blockwise, Expr, Projection | ||
|
||
|
||
class CumulativeAggregations(Expr): | ||
_parameters = ["frame", "axis", "skipna"] | ||
_defaults = {"axis": None} | ||
|
||
chunk_operation = None | ||
aggregate_operation = None | ||
|
||
def _divisions(self): | ||
return self.frame._divisions() | ||
|
||
@functools.cached_property | ||
def _meta(self): | ||
return self.frame._meta | ||
|
||
def _lower(self): | ||
chunks = CumulativeBlockwise( | ||
self.frame, self.axis, self.skipna, self.chunk_operation | ||
) | ||
chunks_last = TakeLast(chunks, self.skipna) | ||
return CumulativeFinalize(chunks, chunks_last, self.aggregate_operation) | ||
|
||
def _simplify_up(self, parent): | ||
if isinstance(parent, Projection): | ||
return type(self)(self.frame[parent.operand("columns")], *self.operands[1:]) | ||
|
||
|
||
class CumulativeBlockwise(Blockwise): | ||
_parameters = ["frame", "axis", "skipna", "operation"] | ||
_defaults = {"skipna": True, "axis": None} | ||
_projection_passthrough = True | ||
|
||
@functools.cached_property | ||
def _meta(self): | ||
return self.frame._meta | ||
|
||
@functools.cached_property | ||
def operation(self): | ||
return self.operand("operation") | ||
|
||
@functools.cached_property | ||
def _args(self) -> list: | ||
return self.operands[:-1] | ||
|
||
|
||
class TakeLast(Blockwise): | ||
_parameters = ["frame", "skipna"] | ||
_projection_passthrough = True | ||
|
||
@staticmethod | ||
def operation(a, skipna=True): | ||
if skipna: | ||
a = a.bfill() | ||
return a.tail(n=1).squeeze() | ||
|
||
|
||
class CumulativeFinalize(Expr): | ||
_parameters = ["frame", "previous_partitions", "aggregator"] | ||
|
||
def _divisions(self): | ||
return self.frame._divisions() | ||
|
||
@functools.cached_property | ||
def _meta(self): | ||
return self.frame._meta | ||
|
||
def _layer(self) -> dict: | ||
dsk = {} | ||
frame, previous_partitions = self.frame, self.previous_partitions | ||
dsk[(self._name, 0)] = (frame._name, 0) | ||
|
||
intermediate_name = self._name + "-intermediate" | ||
for i in range(1, self.frame.npartitions): | ||
if i == 1: | ||
dsk[(intermediate_name, i)] = (previous_partitions._name, i - 1) | ||
else: | ||
# aggregate with previous cumulation results | ||
dsk[(intermediate_name, i)] = ( | ||
methods._cum_aggregate_apply, | ||
self.aggregator, | ||
(intermediate_name, i - 1), | ||
(previous_partitions._name, i - 1), | ||
) | ||
dsk[(self._name, i)] = ( | ||
self.aggregator, | ||
(self.frame._name, i), | ||
(intermediate_name, i), | ||
) | ||
return dsk | ||
|
||
|
||
class CumSum(CumulativeAggregations): | ||
chunk_operation = M.cumsum | ||
aggregate_operation = staticmethod(methods.cumsum_aggregate) | ||
|
||
|
||
class CumProd(CumulativeAggregations): | ||
chunk_operation = M.cumprod | ||
aggregate_operation = staticmethod(methods.cumprod_aggregate) | ||
|
||
|
||
class CumMax(CumulativeAggregations): | ||
chunk_operation = M.cummax | ||
aggregate_operation = staticmethod(methods.cummax_aggregate) | ||
|
||
|
||
class CumMin(CumulativeAggregations): | ||
chunk_operation = M.cummin | ||
aggregate_operation = staticmethod(methods.cummin_aggregate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters