-
Notifications
You must be signed in to change notification settings - Fork 468
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
Suggestion: Add unitless dimensions #1277
Comments
I would be ok with thi. |
note that "ppb", "ppt", and "ppq" are ambiguous ("billion" might mean either 109 or 1012, depending on the language / country), and that "ppthousand" should probably be an alias of "permille" |
@keewis True, this is ambiguos. I think we should not add it. |
From the SI Brochure BIPM : https://www.bipm.org/utils/common/pdf/si-brochure/SI-Brochure-9-EN.pdf |
In view of the above info, in my opinion percent and ppm should definitely be supported out of the box (and ppthousand / permille maybe too), while ppb, ppt, ppq are better left out. I also think that the |
Any updates on adding ppm and other concentration units to pint? |
@hCraker - Note that "ppm" is not a unit for concentration accoding to the citation from the SI Brochure BIPM above. Converting between concentration units is complicated (at least for concentrations in chemistry). pint would need a lot more information to do the coversion (molar mass, density, non-ideal mixing effects etc.). So this is best handled in a seperate chemistry / chemical engineering package that may build upon pint (like https://thermo.readthedocs.io/index.html does). |
Thanks for the response @dalito. It sounds like this issue should be closed since these sorts of unit conversions belong in an extension to pint and not in pint itself. |
Anyone want to contribute to this? It's gonna be a breaking change since % is interpreted as modulo by the parser. Thought @hgrecco |
Yes thats what I'm thinking ! @hgrecco agreed ? |
So, I have this preliminary scratch that runs without issues: from pint import UnitRegistry
from pint.testing import assert_equal
pct_preproc = lambda string: string.replace("%", "percent")
ureg = UnitRegistry(preprocessors=[pct_preproc])
ureg.define("percent = 0.01 = %")
ureg.define("ppm = 1e-6")
assert ureg("%") == ureg("percent") == ureg.percent
assert ureg("ppm") == ureg.ppm
a = ureg.Quantity("10 %")
b = ureg.Quantity("100 ppm")
c = ureg.Quantity("0.5")
assert f"{a}" == "10 percent"
assert f"{a:~}" == "10 %"
assert f"{b}" == "100 ppm"
assert f"{b:~}" == "100 ppm"
assert_equal(a, 0.1)
assert_equal(1000 * b, a)
assert_equal(c, 5 * a)
assert_equal ((1 * ureg.meter)/(1 * ureg.kilometer), 0.1 * ureg.percent)
assert c.to("percent").m == 50
# assert c.to("%").m == 50 # <-- this raises an exception As you can see, it all works as expected except for the last (commented) line, where I check the use of
It seems that the registry preprocessors are not used when calling |
Also, a question about design: do we want i.e, do we want |
I don't think it should be based on |
We can use what has been defined in MetPy both preprocessors & percent definition. |
I think we can treat the bug you found in another issue @cpascual |
Good, then I'll submit a PR with the approach that I drafted in my example (which is essentially the same as in MetPy, AFAICT) and leave the support of Is that ok with you @jules-ch ? |
Not sure if this would be difficult to implement internally, but one common thing I do with units is convert between various unitless representations (i.e. I want to convert a percent to ppm).
Suggested additional dimensionless units:
The text was updated successfully, but these errors were encountered: