Skip to content
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

create the context objects passed to custom combine_attrs functions #5668

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

keewis
Copy link
Collaborator

@keewis keewis commented Aug 3, 2021

Follow-up to #4896: this creates the context object in reduce methods and passes it to merge_attrs, with more planned.

Note that for now this is a bit inconvenient to use for provenance tracking (as discussed in the cf-xarray issue) because functions implementing that would still have to deal with merging the attrs.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 3, 2021

Unit Test Results

         6 files           6 suites   51m 18s ⏱️
16 200 tests 14 465 ✔️ 1 735 💤 0 ❌
90 396 runs  82 221 ✔️ 8 175 💤 0 ❌

Results for commit 8d23032.

♻️ This comment has been updated with latest results.

Comment on lines +1788 to +1789
if isinstance(keep_attrs, bool):
keep_attrs = "override" if keep_attrs else "drop"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this go in _get_keep_attrs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, because we need to support both of these:

Suggested change
if isinstance(keep_attrs, bool):
keep_attrs = "override" if keep_attrs else "drop"
with xr.set_options(keep_attrs=True):
ds.mean()
ds.mean(keep_attrs=True)

but I can move it to merge_attrs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge_attrs would be a good solution.

We could update _get_keep_attrs to _get_keep_attrs(keep_attrs, default=False) and put all the logic in one place but that's a much bigger change.

@@ -63,6 +63,9 @@ class Context:
def __init__(self, func):
self.func = func
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keewis do you have an idea of what to do for groupby.mean and similar calls?

Copy link
Collaborator Author

@keewis keewis Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be possible to pass the actual (bound?) function (e.g. ds.groupby(...).mean) instead, which would also allow accessing additional data in the combine_attrs function. Not sure how to get a nice repr for that but I guess that's a minor issue.

Edit: to avoid introspecting stack frames, I guess we could pass getattr(self, "name") (where that makes sense)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants