-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
MAINT: constants: recalculation of exact constants #17584
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
Conversation
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.
Thanks for the work! It's in good shape now, just some minor things with the linter.
@tupui Could you take a look at the linter/formatting issues? I don´t know how to handle them. |
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 did a few suggestions to fix the linter. For some lines too long in _codata
it's ok to let it complain.
scipy/constants/_codata.py
Outdated
elif is_exact: | ||
exact[name] = val | ||
else: | ||
assert not is_truncated |
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.
Here and elsewhere:
Is this for control flow? If yes, could you replace these with proper exceptions? The issue is that assertions are missing if someone use python -O
Co-authored-by: Pamphile Roy <roy.pamphile@gmail.com>
Co-authored-by: Pamphile Roy <roy.pamphile@gmail.com>
Co-authored-by: Pamphile Roy <roy.pamphile@gmail.com>
Co-authored-by: Pamphile Roy <roy.pamphile@gmail.com>
Co-authored-by: Pamphile Roy <roy.pamphile@gmail.com>
Co-authored-by: Pamphile Roy <roy.pamphile@gmail.com>
scipy/constants/_codata.py
Outdated
raise Exception("Parsing error") | ||
except Warning as w: | ||
raise Warning(f"{name} is truncated but not exact.") from w |
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.
About the exceptions, we should try to raise informative types of exceptions for the users, instead of Warning
, it's more something like a ValueError
that we might need.
For internal exceptions, we should also be more specific and not use a bare Exception
. This to help prevent catching something else.
Lastly and this is a bit more work, we should test any new exception which we add. Our current policy is to do the following in Pytest:
message = r"Message in the exception"
with pytest.raises(ValueError, match=message):
... # code that will raise
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.
Okay, I'll try to get to it at some other day.
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.
Thank you 🙇 No rush at all.
The tests show the intended error messages but I admit that the whole pull request became such a mess that I'm not sure if I made it better or worse. So, before you merge it, please perform a very, very thorough review. |
@lucascolley |
Thanks a lot for the quick response. I've bumped the milestone on the assumption that no maintainers are looking to give the "very, very thorough review" and finish this in the next few weeks. |
Probably not, and I'm hoping we don't have to wait weeks before I branch |
Don't worry, I'm currently occupied with other things with a higher priority for me. |
scipy/constants/tests/test_codata.py
Outdated
from numpy.testing import ( | ||
assert_equal, | ||
assert_, | ||
assert_almost_equal, | ||
assert_raises, | ||
) | ||
from scipy.constants import _codata |
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.
Let's not make unnnecessary formatting changes.
elif is_exact: | ||
exact[name] = val | ||
elif is_truncated: | ||
raise Warning("Parsing error") |
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.
Why raise
a Warning? Should this warn or raise
an exception?
|
||
physical_constants: dict[str, tuple[float, str, float]] = {} | ||
exact2022 = exact2018 |
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 haven't checked whether this is true, but for now...
h = exact['Planck constant'] | ||
e = exact['elementary charge'] | ||
k = exact['Boltzmann constant'] | ||
N_A = exact['Avogadro constant'] |
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 original PR defined some useful constants to reduce duplication. I'll add them back.
alpha_W = (3 + lambertw(-3 * math.exp(-3))).real | ||
x_W = (5 + lambertw(-5 * math.exp(-5))).real |
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 original PR defined numerical values. Fortunately, lambertw
gives exactly the same result as mpmath
, but it would probably be safer to pre-calculate these.
[lint only]
Thanks for working on this @jakobjakobson13. After merging main into gh-11345 and this PR, I checked this against the original, gh-11345. I've decided to close this one in favor of gh-11345 for a few reasons:
There were also some good ideas from this one that I merged into gh-11345 in 4a91f45, so when that gets merged, we'll add you as a coauthor. |
Reference issue
Closes gh-11341, closes gh-11345 and closes gh-14467.
What does this implement/fix?
Exact values of the CODATA constant set are calculated for bigger precision
Additional information
This is just a follow up on the abandoned gh-11345 and credits for the working parts go to @pv (The broken bits are my mistake.)