-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Optimize dask array equality checks. #3453
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
Changes from 1 commit
e84cc97
8739ddd
4a66e7c
e99148e
ee0d422
5e742e4
53c0f4e
08f7f74
6e4c11f
4ee2963
0711eb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -181,16 +181,49 @@ def allclose_or_equiv(arr1, arr2, rtol=1e-5, atol=1e-8): | |||||||||||||||||||||||||||||||||||
| arr2 = asarray(arr2) | ||||||||||||||||||||||||||||||||||||
| if arr1.shape != arr2.shape: | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||
| dask_array | ||||||||||||||||||||||||||||||||||||
| and isinstance(arr1, dask_array.Array) | ||||||||||||||||||||||||||||||||||||
| and isinstance(arr2, dask_array.Array) | ||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||
| # GH3068 | ||||||||||||||||||||||||||||||||||||
| if arr1.name == arr2.name: | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
| return bool(isclose(arr1, arr2, rtol=rtol, atol=atol, equal_nan=True).all()) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def lazy_array_equiv(arr1, arr2): | ||||||||||||||||||||||||||||||||||||
| """Like array_equal, but doesn't actually compare values | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| arr1 = asarray(arr1) | ||||||||||||||||||||||||||||||||||||
| arr2 = asarray(arr2) | ||||||||||||||||||||||||||||||||||||
| if arr1.shape != arr2.shape: | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||
| dask_array | ||||||||||||||||||||||||||||||||||||
| and isinstance(arr1, dask_array.Array) | ||||||||||||||||||||||||||||||||||||
| and isinstance(arr2, dask_array.Array) | ||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||
| # GH3068 | ||||||||||||||||||||||||||||||||||||
| if arr1.name == arr2.name: | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def array_equiv(arr1, arr2): | ||||||||||||||||||||||||||||||||||||
| """Like np.array_equal, but also allows values to be NaN in both arrays | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| arr1 = asarray(arr1) | ||||||||||||||||||||||||||||||||||||
| arr2 = asarray(arr2) | ||||||||||||||||||||||||||||||||||||
| if arr1.shape != arr2.shape: | ||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||
| dask_array | ||||||||||||||||||||||||||||||||||||
| and isinstance(arr1, dask_array.Array) | ||||||||||||||||||||||||||||||||||||
| and isinstance(arr2, dask_array.Array) | ||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||
| # GH3068 | ||||||||||||||||||||||||||||||||||||
| if arr1.name == arr2.name: | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| def equals(self, other, equiv=duck_array_ops.array_equiv): | |
| """True if two Variables have the same dimensions and values; | |
| otherwise False. | |
| Variables can still be equal (like pandas objects) if they have NaN | |
| values in the same locations. | |
| This method is necessary because `v1 == v2` for Variables | |
| does element-wise comparisons (like numpy.ndarrays). | |
| """ | |
| other = getattr(other, "variable", other) | |
| try: | |
| return self.dims == other.dims and ( | |
| self._data is other._data or equiv(self.data, other.data) | |
| ) | |
| except (TypeError, AttributeError): | |
| return False |
equiv=duck_array_ops.array_equiv by default which now calls duck_array_ops.lazy_array_equiv first so things are good?
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.
OK I've added the identity check to lazy_array_equiv too
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| from . import dtypes, pdcompat | ||
| from .alignment import deep_align | ||
| from .duck_array_ops import lazy_array_equiv | ||
| from .utils import Frozen, dict_equiv | ||
| from .variable import Variable, as_variable, assert_unique_multiindex_level_names | ||
|
|
||
|
|
@@ -123,16 +124,24 @@ def unique_variable( | |
| combine_method = "fillna" | ||
|
|
||
| if equals is None: | ||
| out = out.compute() | ||
| # first check without comparing values i.e. no computes | ||
| for var in variables[1:]: | ||
| equals = getattr(out, compat)(var) | ||
| equals = getattr(out, compat)(var, equiv=lazy_array_equiv) | ||
| if not equals: | ||
|
||
| break | ||
|
|
||
| # now compare values with minimum number of computes | ||
| if not equals: | ||
| out = out.compute() | ||
| for var in variables[1:]: | ||
| equals = getattr(out, compat)(var) | ||
| if not equals: | ||
| break | ||
|
|
||
| if not equals: | ||
| raise MergeError( | ||
| "conflicting values for variable {!r} on objects to be combined. " | ||
| "You can skip this check by specifying compat='override'.".format(name) | ||
| f"conflicting values for variable {name!r} on objects to be combined. " | ||
| "You can skip this check by specifying compat='override'." | ||
| ) | ||
|
|
||
| if combine_method: | ||
|
|
||
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.
same concern as below -- should this be checking against
Noneexplicitly, which I believelazy_array_equivcan return?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 think this is right. We are exiting early because
equals[k] is not Truei.e. either the shapes are not equal, or one (or both) of the arrays is a numpy array or the dask names are not equal