-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Coarsen keep attrs 3376 #3801
Coarsen keep attrs 3376 #3801
Conversation
Thanks for the PR and tests @amcnicho . Are you up for adding the code to pass through |
I added code to pass the keyword through and handle it in the constructor of the This function
makes me suspicious that this keep_attrs keyword shouldn't be reaching this section of the code though. Any guidance would be appreciated. |
Recent commits look good @amcnicho ! |
|
(sorry, I didn't see your message before my message prior) Do you know what's calling |
It appears to be the mean function operating on the coarsen object xarray/tests/test_dataset.py#l.5676. The FWIW the dataset object returned by that call in the test looks like this:
|
Right, yes, this does get a bit nested... Appreciate you working through it. At first glance,
Does that make sense? |
...but then potentially it does conflict with the existing use of CC @fujiisoup if you're more familiar with this code. And @dcherian has done some work on it. |
Does that make sense? |
I couldn't find a test that checks attribute persistence across rolling window operations, but when I tested it interactively e.g.
it still doesn't seem to work. I agree that it is better to focus on
Yes, I seem not to have included the assignment, or it got lost along the way. I added it with a new commit. Thank you for noticing that.
I think I see now. |
Assign keep_attrs keyword value to Coarsen objects in constructor Add conditional inside _reduce_method.wrapped_func branching on self.keep_attrs and pass back to returned Dataset
Sorry, I changed this from a draft to ready for review accidentally, and it doesn't look like there is a way to reverse that operation. The tests still fail after this latest change, but I think we are converging on a fix. The variable.coarsen method is applied to each of the arrays inside |
xarray/core/rolling.py
Outdated
@@ -626,6 +649,11 @@ def _reduce_method(cls, func: Callable, include_skipna: bool, numeric_only: bool | |||
def wrapped_func(self, **kwargs): | |||
from .dataset import Dataset | |||
|
|||
if self.keep_attrs == True: |
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.
if self.keep_attrs == True: | |
if self.keep_attrs: |
That's looking good re coarsen! Do tests pass? |
Thank you for the review, I’ll incorporate those changes. |
Handle global keep_attrs setting inside Variable._coarsen_reshape Pass attrs through consistently inside DatasetCoarsen._reduce_method Don't pass Variable.coarsen a keyword argument it doesn't expect inside DataArrayCoarsen._reduce_method
The recent changes I made to both the code and the tests result in a pass condition for both the dataset and variable version of test_coarsen_keep_attrs. I think adding the keep_attrs keyword argument to the Note that there are at least two failure modes remaining:
|
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.
This looks great!
Re the 2nd 'failure mode': IIUC the current tests should still pass, and that's a separate (and correct) case; is that right?
We can merge the Coarsen
changes alone, or we should add the same tests for Rolling
; whichever you prefer.
If you merge master into this, it'll fix the unrelated failing tests.
xarray/tests/test_dataset.py
Outdated
ds = Dataset( | ||
data_vars={"var1": ("coord", var1), "var2": ("coord", var2)}, | ||
coords={"coord": coords}, | ||
attrs={'units':'test', 'long_name':'testing'} |
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.
attrs={'units':'test', 'long_name':'testing'} | |
attrs=_attrs |
Then we know we're testing for the correct value
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.
I will make this change.
xarray/tests/test_dataset.py
Outdated
assert dat.attrs == _attrs | ||
|
||
# # Test kept attrs using wrapper function keyword | ||
# dat = ds.coarsen(coord=5).mean(keep_attrs=True) |
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.
Yeah, I don't think this should work; keep_attrs
needs to be passed specifically to coarsen
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.
I am still building intuition for this package so thank you for clarifying. I will remove the comment.
That's right. The original test used test_reduce_keep_attrs as a template. Now, instead of defining one Variable object and passing it to both test conditions, a new Variable is defined and coarsened to check each setting of the global option. I will merge master in and add similar tests for |
Remove commented-out test from test_coarsen_keep_attrs Add test_rolling_keep_attrs
Return a Dataset object that results in test_rolling_keep_attrs Passing
xarray/core/variable.py
Outdated
keep_attrs = _get_keep_attrs(default=False) | ||
if keep_attrs is None: | ||
keep_attrs = _get_keep_attrs(default=False) |
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.
keep_attrs = _get_keep_attrs(default=False) | |
if keep_attrs is None: | |
keep_attrs = _get_keep_attrs(default=False) | |
if keep_attrs is None: | |
keep_attrs = _get_keep_attrs(default=False) |
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.
If the assignment on L.1952 is removed, the Variable.coarsen
tests fail with UnboundLocalError: local variable 'keep_attrs' referenced before assignment
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.
Ah yes; then we don't need the bottom two lines; just keep_attrs = _get_keep_attrs(default=False)
is enough
This looks excellent! Could we add the change to Then I think we're ready to merge! |
Looks great! Any feedback from others? Otherwise will merge later today Thanks @amcnicho ! |
Thanks a lot @amcnicho ! Glad to have you as a contributor! |
…e-size * upstream/master: (24 commits) Fix alignment with join="override" when some dims are unindexed (pydata#3839) Fix CFTimeIndex-related errors stemming from updates in pandas (pydata#3764) Doctests fixes (pydata#3846) add xpublish to related projects (pydata#3850) update installation instruction (pydata#3849) Pint support for top-level functions (pydata#3611) un-xfail tests that append to netCDF files with scipy (pydata#3805) remove panel conversion (pydata#3845) Add nxarray to related-projects.rst (pydata#3848) Implement skipna kwarg in xr.quantile (pydata#3844) Allow `where` to receive a callable (pydata#3827) update macos image (pydata#3838) Label "Installed Versions" item in Issue template (pydata#3832) Add note on diff's n differing from pandas (pydata#3822) DOC: Add rioxarray and other external examples (pydata#3757) Use stable RTD image. removed mention that 'dims' are inferred from 'coords'-dict when omit… (pydata#3821) Coarsen keep attrs 3376 (pydata#3801) Turn on html repr by default (pydata#3812) Fix zarr append with groups (pydata#3610) ...
isort -rc . && black . && mypy . && flake8
whats-new.rst
for all changes andapi.rst
for new APII also noticed missing attributes when attempting certain operations, so I have created this pull request to attempt a fix. I modified code in
DataWithCoords.coarsen()
to try applying the same logic used for other methods since #2482 was successfully merged.I added two new tests that adapt the bug reported by @jejjohnson. I believe they should pass when this is fixed. One uses
Dataset.coarsen()
and the other usesVariable.coarsen
. Both tests currently fail.xarray/tests/test_dataset.py::TestDataset::test_coarsen_keep_attrs
xarray/tests/test_variable.py::TestVariable::test_coarsen_keep_attrs