-
Notifications
You must be signed in to change notification settings - Fork 48
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
BUG: fix unit comparison for K VS degC #408
BUG: fix unit comparison for K VS degC #408
Conversation
8481eb8
to
0e8afb5
Compare
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 change seems fine in principle, thanks for catching it and working on a fix.
That said, we have to be super careful about performance since this is a hot path. I would suggest doing some %timeit
benchmarks (or something fancier if you wish). Unfortunately I'm pretty sure the pyperf benchmarks in the repo have bitrotted at this point so they might not be the best place to start.
Also one thing to keep in mind when doing benchmarks that depend on sympy is to install the fastcache
module, which implements a fast C LRU cache that sympy will use if it's installed.
unyt/unit_object.py
Outdated
if not isinstance(u, Unit): | ||
return True | ||
if not math.isclose(self.base_value, u.base_value): | ||
if not self == u: |
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 don't remember exactly why but I'm pretty sure there was a reason __ne__
wasn't implemented in terms of ==
. Speed maybe? Avoids one extra function call and the indirection of going from ==
to __eq__
in CPython.
If there isn't a big speed difference and you do end up doing it this way there's no need to keep the rest of the implementation of __ne__
.
unyt/unit_object.py
Outdated
return ( | ||
math.isclose(self.base_value, u.base_value) | ||
isinstance(u, Unit) | ||
and self.dimensions == u.dimensions |
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.
The dimension comparison should come last, for performance reasons.
Thanks @ngoldbaum for this early review ! unyt/unyt/tests/test_unyt_array.py Lines 2470 to 2480 in fd98eea
Am I understanding correctly that |
Yes, because 1 K is the same as 1 delta_degC, so treating it as a temperature delta is well-defined. Good call on the deprecation, it’s probably worth making it raise an error. TBH temperature units are such a pain I kinda wish we had stuck with just supporting Kelvin but that ship sailed a long time ago. |
Bugs have to come from somewhere though :D |
0e8afb5
to
861e6f0
Compare
I've revamped this.
|
There's now a new error in doc tests, where the results of adding two scalar temperatures with |
Thanks so much for looking closer at this! Changing the doctest is fine. |
ecc3c3e
to
0369be3
Compare
0369be3
to
d0a4017
Compare
rebased on main now that #410 is merged |
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.
Just one issue otherwise LGTM
if ( | ||
offset is not None | ||
and u1.base_offset != 0.0 | ||
and not repr(u0).startswith("delta_") |
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'm pretty sure this last conditional will always be True
because delta_degF
and delta_degC
have base_offset=0.0
.
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.
the second condition in on u1
while this one is on u0
. I don't like it very much, I think I reached the point where I was more or less typing random conditions until I got one that passed tests, but I can guarantee that it's needed :)
This resolves the first problem in #407
It seems to break other tests (nothing that seems unfixable though), so I thought I'd expose early in case anyone wants to provide feedback