-
Notifications
You must be signed in to change notification settings - Fork 41
Add FlagGrouper #556
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
Add FlagGrouper #556
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
475cb06
Add FlagGrouper
dcherian e6c8392
Merge branch 'main' into flag-grouper
dcherian 1f51297
fix
dcherian 63ea00e
fix doc build
dcherian 3141a05
update
dcherian 36a647e
cleanup
dcherian fe6fe34
try again
dcherian 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from dataclasses import dataclass | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from xarray.groupers import EncodedGroups, UniqueGrouper | ||
|
||
|
||
@dataclass | ||
class FlagGrouper(UniqueGrouper): | ||
def factorize(self, group) -> EncodedGroups: | ||
if "flag_values" not in group.attrs or "flag_meanings" not in group.attrs: | ||
raise ValueError( | ||
"FlagGrouper can only be used with flag variables that have" | ||
"`flag_values` and `flag_meanings` specified in attrs." | ||
) | ||
|
||
values = np.array(group.attrs["flag_values"]) | ||
full_index = pd.Index(group.attrs["flag_meanings"].split(" ")) | ||
|
||
self.labels = values | ||
|
||
# TODO: we could optimize here, since `group` is already factorized, | ||
# but there are subtleties. For example, the attrs must be up to date, | ||
# any value that is not in flag_values will cause an error, etc. | ||
ret = super().factorize(group) | ||
|
||
ret.codes.attrs.pop("flag_values") | ||
ret.codes.attrs.pop("flag_meanings") | ||
|
||
return EncodedGroups( | ||
codes=ret.codes, | ||
full_index=full_index, | ||
group_indices=ret.group_indices, | ||
) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import numpy as np | ||
import pytest | ||
import xarray as xr | ||
from xarray.testing import assert_identical | ||
|
||
from cf_xarray.datasets import flag_excl | ||
from cf_xarray.groupers import FlagGrouper | ||
|
||
|
||
def test_flag_grouper(): | ||
ds = flag_excl.to_dataset().set_coords("flag_var").copy(deep=True) | ||
ds["foo"] = ("time", np.arange(8)) | ||
actual = ds.groupby(flag_var=FlagGrouper()).mean() | ||
expected = ds.groupby("flag_var").mean() | ||
expected["flag_var"] = ["flag_1", "flag_2", "flag_3"] | ||
expected["flag_var"].attrs["standard_name"] = "flag_mutual_exclusive" | ||
assert_identical(actual, expected) | ||
|
||
del ds.flag_var.attrs["flag_values"] | ||
with pytest.raises(ValueError): | ||
ds.groupby(flag_var=FlagGrouper()) | ||
|
||
ds.flag_var.attrs["flag_values"] = [0, 1, 2] | ||
del ds.flag_var.attrs["flag_meanings"] | ||
with pytest.raises(ValueError): | ||
ds.groupby(flag_var=FlagGrouper()) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"values", | ||
[ | ||
[1, 2], | ||
[1, 2, 3], # value out of range of flag_values | ||
], | ||
) | ||
def test_flag_grouper_optimized(values): | ||
ds = xr.Dataset( | ||
{"foo": ("x", values, {"flag_values": [0, 1, 2], "flag_meanings": "a b c"})} | ||
) | ||
ret = FlagGrouper().factorize(ds.foo) | ||
expected = ds.foo | ||
expected.data[ds.foo.data > 2] = -1 | ||
del ds.foo.attrs["flag_meanings"] | ||
del ds.foo.attrs["flag_values"] | ||
assert_identical(ret.codes, ds.foo) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.