-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #472
- Loading branch information
Showing
5 changed files
with
76 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import numpy as np | ||
import pandas as pd | ||
from xarray.groupers import EncodedGroups, Grouper | ||
|
||
|
||
class FlagGrouper(Grouper): | ||
def factorize(self, group) -> EncodedGroups: | ||
assert "flag_values" in group.attrs | ||
assert "flag_meanings" in group.attrs | ||
|
||
values = np.array(group.attrs["flag_values"]) | ||
full_index = pd.Index(group.attrs["flag_meanings"].split(" ")) | ||
|
||
if group.dtype.kind in "iu" and (np.diff(values) == 1).all(): | ||
# optimize | ||
codes = group.data - group.data[0].astype(int) | ||
else: | ||
codes, _ = pd.factorize(group.data.ravel()) | ||
|
||
codes_da = group.copy(data=codes.reshape(group.shape)) | ||
codes_da.attrs.pop("flag_values") | ||
codes_da.attrs.pop("flag_meanings") | ||
|
||
return EncodedGroups(codes=codes_da, full_index=full_index) | ||
|
||
def reset(self): | ||
pass |
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,15 @@ | ||
import numpy as np | ||
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") | ||
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) |
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