-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Dask friendly check in .weighted()
#4559
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
Changes from 8 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c3b47db
Use map_blocks for weighted init checks
jbusecke 36f83bc
added dask test
jbusecke 5cabe4a
Update xarray/core/weighted.py
jbusecke dc129bb
Update xarray/core/weighted.py
jbusecke a3b51e0
implement requires_dask
jbusecke 2f0cc53
Implement raise_if_dask_computes
jbusecke feb0f1c
Added logic to check for dask arrays
jbusecke 4da7120
applied isort
jbusecke 8886baa
Update xarray/core/weighted.py
jbusecke dc92dbe
Refactor dask mapping
jbusecke 681f34d
Try duck_array_ops.isnull
jbusecke 0852564
black formatting
jbusecke 64bef42
Add whatsnew
jbusecke e99b068
Merge branch 'master' into weighted_nancheck_dask
jbusecke c81a829
Remove numpy
jbusecke 6a85409
apply isort
jbusecke 07f5623
Merge branch 'master' into weighted_nancheck_dask
jbusecke a5b4cf3
Update xarray/core/weighted.py
dcherian 98c4e32
Update xarray/core/weighted.py
dcherian 8964608
black formatting
jbusecke c49d312
Update xarray/core/weighted.py
mathause b5d5a30
Merge branch 'master' into weighted_nancheck_dask
mathause 1d59e0f
Update xarray/core/weighted.py
mathause 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,10 @@ | ||||||
from typing import TYPE_CHECKING, Hashable, Iterable, Optional, Union, overload | ||||||
|
||||||
import numpy as np | ||||||
|
||||||
from .computation import dot | ||||||
from .options import _get_keep_attrs | ||||||
from .pycompat import is_duck_dask_array | ||||||
|
||||||
if TYPE_CHECKING: | ||||||
from .dataarray import DataArray, Dataset | ||||||
|
@@ -100,11 +103,22 @@ def __init__(self, obj, weights): | |||||
if not isinstance(weights, DataArray): | ||||||
raise ValueError("`weights` must be a DataArray") | ||||||
|
||||||
if weights.isnull().any(): | ||||||
raise ValueError( | ||||||
"`weights` cannot contain missing values. " | ||||||
"Missing values can be replaced by `weights.fillna(0)`." | ||||||
def _weight_check(w): | ||||||
if np.isnan(w).any(): | ||||||
jbusecke marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
raise ValueError( | ||||||
"`weights` cannot contain missing values. " | ||||||
"Missing values can be replaced by `weights.fillna(0)`." | ||||||
) | ||||||
return w.data | ||||||
|
||||||
if is_duck_dask_array(weights.data): | ||||||
import dask.array as dsa | ||||||
|
||||||
weights.data = dsa.map_blocks( | ||||||
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.
Suggested change
so we don't modify the original object. Could even do |
||||||
_weight_check, weights.data, dtype=weights.dtype | ||||||
) | ||||||
else: | ||||||
weights.data = _weight_check(weights.data) | ||||||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
self.obj = obj | ||||||
self.weights = weights | ||||||
|
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
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.
.isnull()
does a bit more than that:np.isnan
won't detectNaT
. @mathause, how likely is it to get datetime-like arrays here? They don't make much sense as weights, but as far as I can tell we don't check (I might be missing something, though)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.
There is no check for that. A
TimeDelta
may make some sense as weights.DateTime
not so much. I think we can get away with usingnp.isnan
. ADate*
array as weights containingNaT
should be super uncommon.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.
We could still operate on the dataarray instead of the dask/numpy array, but as @dcherian suggesred, that would be less efficient. I would be curious as to what penalties would actually occur when we use the
weights.map_blocks
compared todask.array.map_blocks
?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.
duck_array_ops.isnull
to account fortimedelta64
? It is weird to have it as a weight though. Does that work?