-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Use cumsum from flox #10987
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
Draft
Illviljan
wants to merge
31
commits into
pydata:main
Choose a base branch
from
Illviljan:cumsum_flox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−35
Draft
Use cumsum from flox #10987
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
776bc5a
use cumsum from flox
Illviljan ae27632
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a5f9326
Update groupby.py
Illviljan 50ccca4
Update groupby.py
Illviljan f55531e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 06ac372
Update groupby.py
Illviljan 31244e6
Merge branch 'cumsum_flox' of https://github.com/Illviljan/xarray int…
Illviljan dd47536
Update groupby.py
Illviljan e867f12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 88e0ebc
Update groupby.py
Illviljan 181d4a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a82ec39
use apply_ufunc for dataset and dataarray handling
Illviljan 6c6abed
Merge branch 'cumsum_flox' of https://github.com/Illviljan/xarray int…
Illviljan 24c3f1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d8d0eaa
Update groupby.py
Illviljan 55ff46a
Merge branch 'cumsum_flox' of https://github.com/Illviljan/xarray int…
Illviljan 33d1360
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c97ae98
sync protocols with each other
Illviljan 06b52ae
Merge branch 'cumsum_flox' of https://github.com/Illviljan/xarray int…
Illviljan 84f9b44
typing
Illviljan 2978877
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0a9adee
add dataset and version requirement
Illviljan ae9a3d8
Merge branch 'cumsum_flox' of https://github.com/Illviljan/xarray int…
Illviljan c056d1f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d4873b9
Update _aggregations.py
Illviljan 21cbde2
Update xarray/core/groupby.py
Illviljan 4aebc47
Update groupby.py
Illviljan f4cab24
Update groupby.py
Illviljan 23d9d50
Update groupby.py
Illviljan 9b64db2
Update generate_aggregations.py
Illviljan 928b158
Renove workaround in test
Illviljan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| from packaging.version import Version | ||
|
|
||
| from xarray.computation import ops | ||
| from xarray.computation.apply_ufunc import apply_ufunc | ||
| from xarray.computation.arithmetic import ( | ||
| DataArrayGroupbyArithmetic, | ||
| DatasetGroupbyArithmetic, | ||
|
|
@@ -1028,6 +1029,26 @@ def _maybe_unstack(self, obj): | |
|
|
||
| return obj | ||
|
|
||
| def _parse_dim(self, dim: Dims) -> tuple[Hashable, ...]: | ||
| parsed_dim: tuple[Hashable, ...] | ||
| if isinstance(dim, str): | ||
| parsed_dim = (dim,) | ||
| elif dim is None: | ||
| parsed_dim_list = list() | ||
| # preserve order | ||
| for dim_ in itertools.chain( | ||
| *(grouper.codes.dims for grouper in self.groupers) | ||
| ): | ||
| if dim_ not in parsed_dim_list: | ||
| parsed_dim_list.append(dim_) | ||
| parsed_dim = tuple(parsed_dim_list) | ||
| elif dim is ...: | ||
| parsed_dim = tuple(self._original_obj.dims) | ||
| else: | ||
| parsed_dim = tuple(dim) | ||
|
|
||
| return parsed_dim | ||
|
|
||
| def _flox_reduce( | ||
| self, | ||
| dim: Dims, | ||
|
|
@@ -1088,22 +1109,7 @@ def _flox_reduce( | |
| # set explicitly to avoid unnecessarily accumulating count | ||
| kwargs["min_count"] = 0 | ||
|
|
||
| parsed_dim: tuple[Hashable, ...] | ||
| if isinstance(dim, str): | ||
| parsed_dim = (dim,) | ||
| elif dim is None: | ||
| parsed_dim_list = list() | ||
| # preserve order | ||
| for dim_ in itertools.chain( | ||
| *(grouper.codes.dims for grouper in self.groupers) | ||
| ): | ||
| if dim_ not in parsed_dim_list: | ||
| parsed_dim_list.append(dim_) | ||
| parsed_dim = tuple(parsed_dim_list) | ||
| elif dim is ...: | ||
| parsed_dim = tuple(obj.dims) | ||
| else: | ||
| parsed_dim = tuple(dim) | ||
| parsed_dim = self._parse_dim(dim) | ||
|
|
||
| # Do this so we raise the same error message whether flox is present or not. | ||
| # Better to control it here than in flox. | ||
|
|
@@ -1202,6 +1208,61 @@ def _flox_reduce( | |
|
|
||
| return result | ||
|
|
||
| def _flox_scan( | ||
| self, | ||
| dim: Dims, | ||
| *, | ||
| func: str, | ||
| skipna: bool | None = None, | ||
| keep_attrs: bool | None = None, | ||
| **kwargs: Any, | ||
| ) -> DataArray: | ||
| from flox import groupby_scan | ||
|
|
||
| obj = self._original_obj | ||
|
|
||
| parsed_dim = self._parse_dim(dim) | ||
|
|
||
| axis = obj.get_axis_num(parsed_dim) | ||
Illviljan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # axis = (axis_,) if isinstance(axis_, int) else axis_ | ||
| codes = tuple(g.codes for g in self.groupers) | ||
|
|
||
| def wrapper(array, *by, func: str, skipna: bool | None, **kwargs): | ||
| if skipna or (skipna is None and obj.dtype.kind in "cfO"): | ||
| if "nan" not in func: | ||
| func = f"nan{func}" | ||
|
|
||
| return groupby_scan(array, *codes, func=func, **kwargs) | ||
|
|
||
| actual = apply_ufunc( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this is the way. eventually I'd like the |
||
| wrapper, | ||
| obj, | ||
| *codes, | ||
| # input_core_dims=input_core_dims, | ||
| # for xarray's test_groupby_duplicate_coordinate_labels | ||
| # exclude_dims=set(dim_tuple), | ||
| # output_core_dims=[output_core_dims], | ||
| dask="allowed", | ||
| # dask_gufunc_kwargs=dict( | ||
| # output_sizes=output_sizes, | ||
| # output_dtypes=[dtype] if dtype is not None else None, | ||
| # ), | ||
| keep_attrs=( | ||
| _get_keep_attrs(default=True) if keep_attrs is None else keep_attrs | ||
| ), | ||
| kwargs=dict( | ||
| func=func, | ||
| skipna=skipna, | ||
| expected_groups=None, | ||
| axis=axis, | ||
| dtype=None, | ||
| method=None, | ||
| engine=None, | ||
| ), | ||
| ) | ||
|
|
||
| return actual | ||
|
|
||
| def fillna(self, value: Any) -> T_Xarray: | ||
| """Fill missing values in this object by group. | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably requires a version guard.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, now I see:
groupby_scanwas added in: https://github.com/xarray-contrib/flox/releases/tag/v0.9.9 - OKcumsumwas added in: https://github.com/xarray-contrib/flox/releases/tag/v0.10.5 - NOK