Skip to content
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

Merged

Conversation

neutrinoceros
Copy link
Member

@neutrinoceros neutrinoceros commented Apr 2, 2023

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

@neutrinoceros neutrinoceros force-pushed the hotfix_temperature_units_comparison branch from 8481eb8 to 0e8afb5 Compare April 2, 2023 16:36
@neutrinoceros neutrinoceros added the bug Something isn't working label Apr 2, 2023
unyt/unit_object.py Outdated Show resolved Hide resolved
Copy link
Member

@ngoldbaum ngoldbaum left a 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.

if not isinstance(u, Unit):
return True
if not math.isclose(self.base_value, u.base_value):
if not self == u:
Copy link
Member

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__.

return (
math.isclose(self.base_value, u.base_value)
isinstance(u, Unit)
and self.dimensions == u.dimensions
Copy link
Member

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.

unyt/unit_object.py Show resolved Hide resolved
@neutrinoceros
Copy link
Member Author

Thanks @ngoldbaum for this early review !
Two,very similar tests are currently failing, that raise a FutureWarning, see

def test_delta_degC():
t1 = 10 * degC
t2 = 1 * K
assert t1 + t2 == 11 * degC
# In the current implementation 1*K + 10*degC = 11*K
# In the future, this will generate UnitOperationError
with pytest.warns(FutureWarning):
t2 + t1
assert t2 + t1 == 11 * K
t3 = 1 * delta_degC
assert t1 + t3 == 11 * degC

Am I understanding correctly that 1K + 1degC is deprecated but 1degC + 1K isn't ?
In any case, I missed this deprecation in #402, should it be expired for unyt 3.0 ?

@ngoldbaum
Copy link
Member

Am I understanding correctly that 1K + 1degC is deprecated but 1degC + 1K isn't

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.

@neutrinoceros
Copy link
Member Author

neutrinoceros commented Apr 3, 2023

Bugs have to come from somewhere though :D
I'll make a separate (hopefully easier to ship) PR to expire the deprecation (edit: for posterity, this is #410)

@neutrinoceros neutrinoceros force-pushed the hotfix_temperature_units_comparison branch from 0e8afb5 to 861e6f0 Compare April 8, 2023 15:32
@neutrinoceros neutrinoceros changed the title BUG: fix unit comparison for K VS degC BUG: fix unit comparison for K VS degC (⏰ wait for #410) Apr 8, 2023
@neutrinoceros
Copy link
Member Author

I've revamped this.

@neutrinoceros
Copy link
Member Author

There's now a new error in doc tests, where the results of adding two scalar temperatures with dtype == int64 has dtype == float64, so the repr is a little different. This is actually consistent with how other unitful scalars have been behaving for a while, so I think the correct fix is to just change the test's expectation, but maybe I'm misguided, so I'll wait for inputs on this point.

@ngoldbaum
Copy link
Member

Thanks so much for looking closer at this! Changing the doctest is fine.

@neutrinoceros neutrinoceros force-pushed the hotfix_temperature_units_comparison branch from ecc3c3e to 0369be3 Compare April 8, 2023 18:25
@neutrinoceros neutrinoceros force-pushed the hotfix_temperature_units_comparison branch from 0369be3 to d0a4017 Compare April 10, 2023 15:48
@neutrinoceros neutrinoceros changed the title BUG: fix unit comparison for K VS degC (⏰ wait for #410) BUG: fix unit comparison for K VS degC Apr 10, 2023
@neutrinoceros
Copy link
Member Author

rebased on main now that #410 is merged

@neutrinoceros neutrinoceros marked this pull request as ready for review April 10, 2023 15:49
Copy link
Member

@ngoldbaum ngoldbaum left a 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_")
Copy link
Member

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.

Copy link
Member Author

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 :)

@ngoldbaum ngoldbaum merged commit 0cb8ae4 into yt-project:main Apr 10, 2023
@neutrinoceros neutrinoceros deleted the hotfix_temperature_units_comparison branch April 10, 2023 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants