-
-
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
Pint support for variables #3706
Conversation
I fixed the tests that were easier, but I have no idea about Edit: seems there are some issues with |
I'm not sure why, but ValueError: operands could not be broadcast together with shapes (2,0) (2,2) Edit: Seems we can just bump |
the easiest way to fix def units_equiv(a, b):
if hasattr(a, "units") or hasattr(b, "units"):
units_a = getattr(a, "units", None)
units_b = getattr(b, "units", None)
registry = getattr(units_a, "_REGISTRY", None) or getattr(b, "_REGISTRY", None)
units_a = units_a or registry.dimensionless
units_b = units_b or registry.dimensionless
return units_a == units_b
else:
# no units at all
return True
def equiv_with_units(a, b):
return duck_array_ops.array_equiv(a, b) and units_equiv(a, b) However, I'm out of ideas on how to automatically use that (I'd like to avoid having to call There is also There seems to have been some work to add a function named |
I think your
This may be necessary but perhaps not the most important thing right now? |
that could work for re |
xarray/xarray/core/duck_array_ops.py Lines 215 to 227 in 6d1434e
|
Fine by me. :) |
hrmm... I had investigated this before and thought I remembered correctly. I can't put it in |
I guess we need to either pass a kwarg regarding checking units/dimensionality down to xarray/xarray/core/variable.py Lines 1645 to 1648 in 6d1434e
I think a separate Note that we explicitly use Lines 194 to 208 in 6d1434e
|
I agree. However, I'm reluctant to add that function since that would be the first I'm inclined to provide some kind of hook for wrapped libraries instead (allow them to define a method like def metadata_identical(arr1, arr2):
if hasattr(arr1, "metadata_identical"):
return arr1.metadata_identical(arr2)
elif hasattr(arr2, "metadata_identical"):
return arr2.metadata_identical(arr1)
else:
return True
return (
self.dims == other.dims
and (self._data is other._data or equiv(self.data, other.data))
and metadata_identical(self.data, other.data)
)
I don't think that's a problem since what calls |
OK let's see what @shoyer thinks |
gentle ping, @shoyer |
these issues ( |
👍 to smaller PRs! |
xarray/core/variable.py
Outdated
if isinstance(mask, bool): | ||
mask = not mask | ||
else: | ||
mask = ~mask |
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.
Could you flip the argument order rather than adding this? I’m a little puzzles here.
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 concern here is about consistency when applying ~
to bool
objects and boolean dtype arrays, explicitly calling np.logical_not
is a good alternative.
But it does feel a little weird to me to see this here. Maybe changing duck_array_ops.notnull
would have the same effect?
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 as the fillna
issue above, in order to get the results in the units of data
, we need to flip the arguments and for that I need invert the mask (if there is a different way to flip the arguments without inverting, please do tell me).
I tried to use mask = ~mask
, but ~
does not work as expected for bool
. I'll use np.logical_not
instead.
Thanks for pinging me again here (I get a lot of GitHub notifications). I think the current behavior (1 meter is identical to 100 centimeters) is arguably consistent with how Right now, Did this come up in the context of some other downstream use-case, or is this just something that occurred to you for the sake of consistency? |
when writing the unit tests, we decided the definition of As far as I know there was no real use case (except from being able to use cc @jthielen |
@keewis For what it is worth, as far as |
which is what I've been doing with Also, concerning the commutative operations: should we wait for hgrecco/pint#1019 and remove the flipped parameters or should we merge as is and possibly revert once |
I don't anticipate any performance cost to this, just a small decrease in readability. So I think this is fine to merge for now with comments in the relevant sections and we can revert it later. My only suggestion is to add a note like |
I was wrong; I should have at least realized I didn't know. Apologies if that caused wasted time @jthielen Separately: should |
@shoyer, I added the notes @max-sixty, @jthielen: the |
Test failures look unrelated. Thanks @keewis |
…under * upstream/master: (71 commits) Optimize isel for lazy array equality checking (pydata#3588) pin msgpack (pydata#3793) concat now handles non-dim coordinates only present in one dataset (pydata#3769) Add new h5netcdf backend phony_dims kwarg (pydata#3753) always use dask_array_type for isinstance calls (pydata#3787) allow formatting the diff of ndarray attributes (pydata#3728) Pint support for variables (pydata#3706) Format issue template comment as md comment (pydata#3790) Avoid running test_open_mfdataset_list_attr without dask (pydata#3780) remove seaborn.apionly compatibility (pydata#3749) Python 3.8 CI (pydata#3727) PKG: Explicitly add setuptools dependency (pydata#3628) update whats-new Typo in Universal Functions section (pydata#3663) Release v0.15.0 fix setup.cfg Documentation fixes (pydata#3732) Remove extra && in PR template (pydata#3730) Remove garbage text inserted in DASK_LICENSE (pydata#3729) Avoid unsafe use of pip (pydata#3726) ...
I realized that most of the operations depend on
Variable
, which means that this should be the first step to making the other tests pass. This PR copies from #3611, making that PR a work-in-progress again until I remove those parts.black . && mypy . && flake8
whats-new.rst
for all changes andapi.rst
for new APIThese are the current failures:
np.prod
: not implemented bypint
yet, but should work once it isidentical
fails to detect same value, but in different units (like1 m
and100 cm
) as different. This is hard to implement in a way that does not includeisinstance
orhasattr
checks.rank
: not implemented for non-ndarrays, so maybe we should mark this asskip
?rolling_window
:nputils._rolling_window
usesnp.lib.stride_tricks.as_strided
, which cannot be overridden bypint
. We probably have to use something different?shift
:this tries to trim, then concatenate a filled array, but I think this should useit usesnp.pad
after trimming? Are there any disadvantages to that?numpy.pad
now which is supported sincedask==0.18
ordask==0.19
concat
:this was a misconception on my part, I didn't realize this was a classmethod. After fixing that this still fails because I assumedI rewrote the test to pass arrays that don't cause the failureVariable.concat
used the dimension names to reshape the arrays. It does not, so this fails:pad_with_fill_value
:I think this is a bug infixed on pint masterpint
(see non-quantity constant_values parameter to pad hgrecco/pint#992)Does anyone have any comments on these before I start fixing them? @dcherian?