-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add units #35
Conversation
Codecov Report
@@ Coverage Diff @@
## master #35 +/- ##
==========================================
+ Coverage 59.01% 77.11% +18.1%
==========================================
Files 5 6 +1
Lines 61 118 +57
Branches 0 11 +11
==========================================
+ Hits 36 91 +55
Misses 25 25
- Partials 0 2 +2
Continue to review full report at Codecov.
|
Haha, before i can even comment the CEDS stuff is gone :-) |
To discuss: Maybe "N2O_N" N2O-N is not possible I think, they need to be valid Python variable names. |
My vote would be neither. Removed ambiguity about whether an underscore is
really an underscore or whether it's meant to be a hyphen. Option b is to
have a convenience function for conversion which understands hyphens,
strips them, gets the conversions and then returns the results with hyphens
back in.
…On Tue, 30 Oct 2018 at 11:31 am, Robert Gieseke ***@***.***> wrote:
To discuss:
Hyphens or Underscores in unit names:
Maybe "N2O_N"
N2O-N is not possible I think, they need to be valid Python variable names.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#35 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AWh-m0ms3Ov_CgEAbfkMz1lE_1mIq95Zks5uqCpogaJpZM4YAUT6>
.
|
In general, are the units we would actually use so complex we need a sophisticated library for that? |
Hmmm, having looked into it, I am not that happy with using Pint. If I have two units and want to do lots of conversions between the two, I cannot get just a (pre-/post-) offset and a scaling factor (without hacking Pint or resorting to calculating these myself from some chosen points). Anything else would be inefficient, at least in the low-level API... What can we do about that? |
Alternatively, we could have a look at unyt, which seems to have the functionality I want: https://unyt.readthedocs.io/en/latest/modules/unyt.unit_object.html#unyt.unit_object.Unit.get_conversion_factor |
@swillner What unit conversion is happening in the low-level API? |
I would argue yes. People (me) have cocked them up enough to mean that if we can just do the unit handling once, in a fairly clever way, we should. |
It's the only unit library with Pandas support. For me that's reason enough to use it.
I'm happy to make a PR to Pint this week which gives you a convenience function to Pint which does this (or put it first in OpenSCM, then make a PR in Pint) >>> import pint
>>> ureg = pint.UnitRegistry()
>>> a = 1*ureg.m
>>> a
<Quantity(1, 'meter')>
>>> a.to("km").magnitude
0.001 It would look like this def get_conversion_factor(start_units, other_units):
# type checking etc goes here
pint_quantity = 1 * ureg(start_units)
return pint_quantity.to(other_units).magnitude or as a method on a quantity def get_conversion_factor(self, other_units):
# type checking etc goes here
pint_quantity = 1 * ureg(self) # or something like this
return pint_quantity.to(other_units).magnitude We could then cache those values or whatever you wanted. |
If people want to make plots in different units, that should be easy (I can't think of any other use cases...).
All of the conversion when we get requests from models e.g. MAGICC's adaptor says, give me CO2 emissions in GtC/yr but the user set them as MtC / month, the low-level API has to do that conversion. |
Ideally all the emissions units in that file, plus conversions for all the key IPCC metrics (AR5GWP20, AR5GWP100, AR5GTP20, AR5GTP100, AR4GWP100, AR4GWP20 etc.), concentrations units (ppm, ppb, ppt), physical units (K, C, W/m^2, kg, m, s) etc. plus all the conversions between them as, including prefixes and different times for the fluxes (e.g. per year, per month, per day, per second). |
Hmm, ok, we shouldn't use two different unit libraries... Then I will use such a function getting offset and scaling from points, though far from optimal. But including something like that in Pint would probably better rely on Pint internals... |
I don't understand why? If we implement something in Pint which is exactly the same as the unyt functionality, isn't that fine? |
unyt directly exposes the offset and scaling that are used internally (in Pint these are hidden much deeper). Using two points (e.g. 1 unit and 10 unit) to calculate offset and scaling is just a work-around. Maybe it's just my desire for asthetic code then ;) |
Ok, I will have a look if we can make a PR in Pint with something like that, what do you think? |
I like it. Good procrastination ;) |
@rgieseke to you for final say and then merge? Is this all we needed in this PR? |
@swillner @znicholls In general, is this ready to be merged or are you still tweaking? |
Current failure is from the notebook. |
Nope. Let's let Sven do his additions then I'll do the usual:
|
I'm not yet done with the units... Since it's just adding two lines (g and t) for each gas, should'nt we resort to defining them programmaitcally, e.g.
and then add the conversions between them? |
Co-Authored-By: znicholls <zebedee.nicholls@climate-energy-college.org>
Co-Authored-By: znicholls <zebedee.nicholls@climate-energy-college.org>
Co-Authored-By: znicholls <zebedee.nicholls@climate-energy-college.org>
Co-Authored-By: znicholls <zebedee.nicholls@climate-energy-college.org>
Co-Authored-By: znicholls <zebedee.nicholls@climate-energy-college.org>
np.testing.assert_allclose
…On Sun, 4 Nov 2018 at 11:37 am, Sven Willner ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In tests/unit/test_units.py
<#35 (comment)>
:
> @@ -0,0 +1,120 @@
+import numpy as np
+import pytest
+
+from openscm.units import unit_registry
+from openscm.units import DimensionalityError, UndefinedUnitError, UnitConverter
+
+
+def test_unit_registry():
+ CO2 = unit_registry("CO2")
+ assert CO2.to("C").magnitude == 12.0 / 44.0
yeah, I used explicit round somewhere where I thougt it's necessery...
There is also an assertAlmostEqual in unittest standard package. Should
we agree on a standard here?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#35 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AWh-m3raQZ8XLCgy3GrTkaDV4RndKc_sks5urtF8gaJpZM4YAUT6>
.
|
@rgieseke Have you disabled the "normal" merge option? |
yep merge commits are annoying, I prefer rebase... |
ah, ok. will you have the honor then? ;) |
Nah in general I think authors shouldn't merge, for you or robert
…On Sun, 4 Nov 2018 at 12:31 pm, Sven Willner ***@***.***> wrote:
ah, ok. will you have the honor then? ;)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#35 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AWh-m0V5Cum5QIfR4yXSwKCNYIGe_C03ks5urt4PgaJpZM4YAUT6>
.
|
notebooks/emissions-units-with-pint.ipynb
gives an example of how this works with Pint