Skip to content

Commit bd4f914

Browse files
authored
Merge pull request #178 from CitrineInformatics/maintain/update-pint
Update Pint interactions to be compatible with latest release
2 parents 17a7752 + 948c778 commit bd4f914

File tree

5 files changed

+100
-67
lines changed

5 files changed

+100
-67
lines changed

gemd/units/citrine_en.txt

+62-25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Citrine customized units input file for Pint, based explicitly on the Pint defaults as
2-
# included release 0.9 (https://github.com/hgrecco/pint/releases/tag/0.9). The Pint Authors hold
3-
# copyright and are documented in https://github.com/hgrecco/pint/blob/0.9/AUTHORS.
2+
# included release 0.20 (https://github.com/hgrecco/pint/releases/tag/0.20). The Pint Authors hold
3+
# copyright and are documented in https://github.com/hgrecco/pint/blob/0.20/AUTHORS.
44
#
55
# The original copyright statement for the constants file reads:
66
#
@@ -80,7 +80,7 @@
8080
# [density] = [mass] / [volume]
8181
#
8282
# Note that primary dimensions don't need to be declared; they can be
83-
# defined or the first time in a unit definition.
83+
# defined for the first time in a unit definition.
8484
# E.g. see below `meter = [length]`
8585
#
8686
#
@@ -112,7 +112,9 @@ atto- = 1e-18 = a-
112112
femto- = 1e-15 = f-
113113
pico- = 1e-12 = p-
114114
nano- = 1e-9 = n-
115-
micro- = 1e-6 = µ- = u-
115+
# The micro (U+00B5) and Greek mu (U+03BC) are both valid prefixes,
116+
# and they often use the same glyph.
117+
micro- = 1e-6 = µ- = μ- = u-
116118
milli- = 1e-3 = m-
117119
centi- = 1e-2 = c-
118120
deci- = 1e-1 = d-
@@ -152,7 +154,6 @@ gram = [mass] = g = Gram
152154
mole = [substance] = mol = Mole
153155
kelvin = [temperature]; offset: 0 = K = Kelvin = degK = °K = degree_Kelvin = degreeK # older names supported for compatibility
154156
radian = [] = rad = Radian
155-
neper = [] = Np = Neper
156157
bit = [] = _ = Bit
157158
count = [] = _ = Count
158159

@@ -180,13 +181,13 @@ mil = π / 32000 * radian = Mil
180181
steradian = radian ** 2 = sr = Steradian
181182
square_degree = (π / 180) ** 2 * sr = sq_deg = sqdeg = Square_Degree
182183

183-
# Logarithmic ratio
184-
bel = 0.5 * ln10 * neper = Bel
185-
186184
# Information
187-
byte = 8 * bit = B = octet = Byte = Octet
188185
baud = bit / second = Bd = bps = Baud = BPS
189186

187+
byte = 8 * bit = B = octet = Byte = Octet
188+
# byte = 8 * bit = _ = octet
189+
## NOTE: B (byte) symbol can conflict with Bell
190+
190191
# Length
191192
angstrom = 1e-10 * meter = Å = ångström = Angstrom = Ångstrom = Å
192193
micron = micrometer = µ = Micron
@@ -198,7 +199,7 @@ nautical_mile = 1852 * meter = nmi = Nautical_Mile
198199
bohr = hbar / (alpha * m_e * c) = a_0 = a0 = Bohr = bohr_radius = atomic_unit_of_length = a_u_length
199200
x_unit_Cu = K_alpha_Cu_d_220 * d_220 / 1537.4 = Xu_Cu
200201
x_unit_Mo = K_alpha_Mo_d_220 * d_220 / 707.831 = Xu_Mo
201-
angstrom_star = K_alpha_W_d_220 * d_220 / 0.2090100 = Å_star = Angstrom_Star
202+
angstrom_star = K_alpha_W_d_220 * d_220 / 0.2090100 = Å_star = Angstrom_Star = Ångstrom_Star
202203
planck_length = (hbar * gravitational_constant / c ** 3) ** 0.5 = Planck_length = Planck_Length
203204

204205
# Mass
@@ -218,7 +219,10 @@ week = 7 * day = Week
218219
fortnight = 2 * week = Fortnight
219220
year = 365.25 * day = yr = julian_year = Year = Julian_year = Julian_Year
220221
month = year / 12 = _ = Month
221-
decade = 10 * year = _ = Decade
222+
223+
# decade = 10 * year = _ = Decade
224+
## NOTE: decade [time] can conflict with decade [dimensionless]
225+
222226
century = 100 * year = _ = centuries = Century = Centuries
223227
millennium = 1e3 * year = _ = millennia = Millennium = Millennia
224228
eon = 1e9 * year = _ = Eon
@@ -262,21 +266,27 @@ stere = meter ** 3 = _ = Stere
262266
[frequency] = 1 / [time]
263267
hertz = 1 / second = Hz = Hertz
264268
revolutions_per_minute = revolution / minute = rpm = Revolutions_Per_Minute = RPM
269+
revolutions_per_second = revolution / second = rps = Revolutions_Per_Second = RPS
265270
counts_per_second = count / second = cps = Counts_Per_Second = CPS
266271

267272
# Wavenumber
268273
[wavenumber] = 1 / [length]
269274
reciprocal_centimeter = 1 / cm = cm_1 = kayser = Kayser = Reciprocal_Centimeter
270275

271276
# Velocity
272-
[velocity] = [length] / [time] = [speed]
277+
[velocity] = [length] / [time]
278+
[speed] = [velocity]
273279
knot = nautical_mile / hour = kt = knot_international = international_knot = Knot = International_Knot
274280
mile_per_hour = mile / hour = mph = MPH = Mile_Per_Hour = Miles_Per_Hour = miles_per_hour
275281
kilometer_per_hour = kilometer / hour = kph = KPH = kilometers_per_hour = Kilometer_Per_Hour = Kilometers_Per_Hour
276282
kilometer_per_second = kilometer / second = kps = kilometers_per_second = Kilometer_Per_Second
277283
meter_per_second = meter / second = mps = Meter_Per_Second = Meters_Per_Second
278284
foot_per_second = foot / second = fps = feet_per_second = Foot_Per_Second = Feet_Per_Second
279285

286+
# Volumetric Flow Rate
287+
[volumetric_flow_rate] = [volume] / [time]
288+
sverdrup = 1e6 * meter ** 3 / second = sv
289+
280290
# Acceleration
281291
[acceleration] = [velocity] / [time]
282292
galileo = centimeter / second ** 2 = Gal = Galileo
@@ -296,7 +306,7 @@ joule = newton * meter = J = Joule
296306
erg = dyne * centimeter = _ = Erg
297307
watt_hour = watt * hour = Wh = watthour = Whr = Watt_Hour = Watthour
298308
electron_volt = e * volt = eV
299-
rydberg = h * c * R_inf = Ry = Rydberg
309+
rydberg = * c * R_inf = Ry = Rydberg
300310
hartree = 2 * rydberg = E_h = Eh = Hartree = hartree_energy = atomic_unit_of_energy = a_u_energy
301311
calorie = 4.184 * joule = cal = thermochemical_calorie = cal_th = Calorie = Thermochemical_Calorie
302312
international_calorie = 4.1868 * joule = cal_it = international_steam_table_calorie = International_Calorie
@@ -320,9 +330,13 @@ boiler_horsepower = 33475 * Btu / hour # unclear whic
320330
metric_horsepower = 75 * force_kilogram * meter / second
321331
electrical_horsepower = 746 * watt
322332
refrigeration_ton = 12e3 * Btu / hour = _ = ton_of_refrigeration # approximate, no exact definition
333+
cooling_tower_ton = 1.25 * refrigeration_ton # approximate, no exact definition
323334
standard_liter_per_minute = atmosphere * liter / minute = slpm = slm
324335
conventional_watt_90 = K_J90 ** 2 * R_K90 / (K_J ** 2 * R_K) * watt = W_90
325336

337+
# Momentum
338+
[momentum] = [length] * [mass] / [time]
339+
326340
# Density (as auxiliary for pressure)
327341
[density] = [mass] / [volume]
328342
mercury = 13.5951 * kilogram / liter = Hg = Hg_0C = Hg_32F = conventional_mercury
@@ -348,6 +362,7 @@ inch_H2O_39F = inch * water_39F * g_0
348362
inch_H2O_60F = inch * water_60F * g_0
349363
foot_H2O = foot * water * g_0 = ftH2O = feet_H2O
350364
centimeter_H2O = centimeter * water * g_0 = cmH2O = cm_H2O
365+
sound_pressure_level = 20e-6 * pascal = SPL
351366

352367
# Torque
353368
[torque] = [force] * [length]
@@ -381,7 +396,7 @@ enzyme_unit = micromole / minute = U = enzymeunit
381396

382397
# Entropy
383398
[entropy] = [energy] / [temperature]
384-
clausius = calorie / kelvin = _ = Clausius
399+
clausius = calorie / kelvin = Cl = Clausius
385400

386401
# Molar entropy
387402
[molar_entropy] = [entropy] / [substance]
@@ -474,17 +489,17 @@ farad = coulomb / volt = F
474489
abfarad = 1e9 * farad = abF
475490
conventional_farad_90 = R_K90 / R_K * farad = F_90
476491

492+
# Magnetic flux
493+
[magnetic_flux] = [electric_potential] * [time]
494+
weber = volt * second = Wb = Weber
495+
unit_pole = µ_0 * biot * centimeter
496+
477497
# Inductance
478498
[inductance] = [magnetic_flux] / [current]
479499
henry = weber / ampere = H = Henry
480500
abhenry = 1e-9 * henry = abH
481501
conventional_henry_90 = R_K / R_K90 * henry = H_90
482502

483-
# Magnetic flux
484-
[magnetic_flux] = [electric_potential] * [time]
485-
weber = volt * second = Wb = Weber
486-
unit_pole = µ_0 * biot * centimeter
487-
488503
# Magnetic field
489504
[magnetic_field] = [magnetic_flux] / [area]
490505
tesla = weber / meter ** 2 = T = Tesla
@@ -494,7 +509,7 @@ gamma = 1e-9 * tesla = γ
494509
[magnetomotive_force] = [current]
495510
ampere_turn = ampere = At
496511
biot_turn = biot
497-
gilbert = 1 / (4 * π) * biot_turn = _ = Gilbert
512+
gilbert = 1 / (4 * π) * biot_turn = _ = Gilbert # Remove Gb for risk of collision with GB
498513

499514
# Magnetic field strength
500515
[magnetic_field_strength] = [current] / [length]
@@ -512,6 +527,27 @@ buckingham = debye * angstrom = Buckingham
512527
bohr_magneton = e * hbar / (2 * m_e) = µ_B = mu_B
513528
nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N
514529

530+
# Logaritmic Unit Definition
531+
# Unit = scale; logbase; logfactor
532+
# x_dB = [logfactor] * log( x_lin / [scale] ) / log( [logbase] )
533+
534+
# Logaritmic Units of dimensionless quantity: [ https://en.wikipedia.org/wiki/Level_(logarithmic_quantity) ]
535+
536+
decibelmilliwatt = 1e-3 watt; logbase: 10; logfactor: 10 = dBm
537+
decibelmicrowatt = 1e-6 watt; logbase: 10; logfactor: 10 = dBu
538+
539+
decibel = 1 ; logbase: 10; logfactor: 10 = dB
540+
# bell = 1 ; logbase: 10; logfactor: = B
541+
## NOTE: B (Bell) symbol conflicts with byte
542+
543+
decade = 1 ; logbase: 10; logfactor: 1
544+
## NOTE: decade [time] can conflict with decade [dimensionless]
545+
546+
octave = 1 ; logbase: 2; logfactor: 1 = oct
547+
548+
neper = 1 ; logbase: 2.71828182845904523536028747135266249775724709369995; logfactor: 0.5 = Np = Neper
549+
# neper = 1 ; logbase: eulers_number; logfactor: 0.5 = Np
550+
## NOTE: using eulers_number in a logbase definition seems broken under Pint 0.19.2, 0.20.0
515551

516552
#### UNIT GROUPS ####
517553
# Mostly for length, area, volume, mass, force
@@ -580,7 +616,7 @@ nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N
580616
teaspoon = fluid_ounce / 6 = tsp
581617
tablespoon = fluid_ounce / 2 = tbsp = Tbsp
582618
shot = 3 * tablespoon = jig = US_shot
583-
cup = pint / 2 = _ = liquid_cup = US_liquid_cup
619+
cup = pint / 2 = _ = liquid_cup = US_liquid_cup # Removed because of risk of collision with cP
584620
barrel = 31.5 * gallon = bbl
585621
oil_barrel = 42 * gallon = oil_bbl
586622
beer_barrel = 31 * gallon = beer_bbl
@@ -599,6 +635,7 @@ nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N
599635
ton = 2e3 * pound = _ = short_ton
600636
long_ton = 2240 * pound
601637
slug = g_0 * pound * second ** 2 / foot
638+
slinch = g_0 * pound * second ** 2 / inch = blob = slugette
602639

603640
force_ounce = g_0 * ounce = ozf = ounce_force
604641
force_pound = g_0 * pound = lbf = pound_force
@@ -621,9 +658,9 @@ nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N
621658
@end
622659

623660
@group Troy
624-
pennyweight = 24 * grain = _ = pennyweight
625-
troy_ounce = 480 * grain = _ = troy_ounce
626-
troy_pound = 12 * troy_ounce = _ = troy_pound
661+
pennyweight = 24 * grain = dwt
662+
troy_ounce = 480 * grain = toz = ozt
663+
troy_pound = 12 * troy_ounce = tlb = lbt
627664
@end
628665

629666
@group Apothecary
@@ -650,7 +687,7 @@ nuclear_magneton = e * hbar / (2 * m_p) = µ_N = mu_N
650687

651688
@group Printer
652689
pica = inch / 6 = _ = printers_pica
653-
point = pica / 12 = _ = printers_point = big_point
690+
point = pica / 12 = pp = printers_point = big_point = bp
654691
didot = 1 / 2660 * m
655692
cicero = 12 * didot
656693
tex_point = inch / 72.27

gemd/units/constants_en.txt

+17-17
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@
5252
#### MATHEMATICAL CONSTANTS ####
5353
# As computed by Maxima with fpprec:50
5454

55-
pi = 3.1415926535897932384626433832795028841971693993751 = π # pi
56-
# ^^^^^ Uncommented pi definition
55+
pi = 3.1415926535897932384626433832795028841971693993751 = π # pi
5756
tansec = 4.8481368111333441675396429478852851658848753880815e-6 # tangent of 1 arc-second ~ arc_second/radian
5857
ln10 = 2.3025850929940456840179914546843642076011014886288 # natural logarithm of 10
5958
wien_x = 4.9651142317442763036987591313228939440555849867973 # solution to (x-5)*exp(x)+5 = 0 => x = W(5/exp(5))+5
6059
wien_u = 2.8214393721220788934031913302944851953458817440731 # solution to (u-3)*exp(u)+3 = 0 => u = W(3/exp(3))+3
60+
eulers_number = 2.71828182845904523536028747135266249775724709369995
6161

6262
#### DEFINED EXACT CONSTANTS ####
6363

6464
speed_of_light = 299792458 m/s = c = c_0 # since 1983
65-
planck_constant = 6.62607015e-34 J s = h # since May 2019
65+
planck_constant = 6.62607015e-34 J s = # since May 2019
6666
elementary_charge = 1.602176634e-19 C = e # since May 2019
6767
avogadro_number = 6.02214076e23 # since May 2019
6868
boltzmann_constant = 1.380649e-23 J K^-1 = k = k_B # since May 2019
@@ -75,19 +75,19 @@ conventional_von_klitzing_constant = 2.5812807e4 ohm = R_K90 # since Jan 1990
7575
# Floating-point conversion may introduce inaccuracies
7676

7777
zeta = c / (cm/s) = ζ
78-
dirac_constant = h / (2 * π) = ħ = hbar = atomic_unit_of_action = a_u_action
78+
dirac_constant = / (2 * π) = ħ = hbar = atomic_unit_of_action = a_u_action
7979
avogadro_constant = avogadro_number * mol^-1 = N_A
8080
molar_gas_constant = k * N_A = R
8181
faraday_constant = e * N_A
82-
conductance_quantum = 2 * e ** 2 / h = G_0
83-
magnetic_flux_quantum = h / (2 * e) = Φ_0 = Phi_0
84-
josephson_constant = 2 * e / h = K_J
85-
von_klitzing_constant = h / e ** 2 = R_K
86-
stefan_boltzmann_constant = 2 / 15 * π ** 5 * k ** 4 / (h ** 3 * c ** 2) = σ = sigma
87-
first_radiation_constant = 2 * π * h * c ** 2 = c_1
88-
second_radiation_constant = h * c / k = c_2
89-
wien_wavelength_displacement_law_constant = h * c / (k * wien_x)
90-
wien_frequency_displacement_law_constant = wien_u * k / h
82+
conductance_quantum = 2 * e ** 2 / = G_0
83+
magnetic_flux_quantum = / (2 * e) = Φ_0 = Phi_0
84+
josephson_constant = 2 * e / = K_J
85+
von_klitzing_constant = / e ** 2 = R_K
86+
stefan_boltzmann_constant = 2 / 15 * π ** 5 * k ** 4 / ( ** 3 * c ** 2) = σ = sigma
87+
first_radiation_constant = 2 * π * * c ** 2 = c_1
88+
second_radiation_constant = * c / k = c_2
89+
wien_wavelength_displacement_law_constant = * c / (k * wien_x)
90+
wien_frequency_displacement_law_constant = wien_u * k /
9191

9292
#### MEASURED CONSTANTS ####
9393
# Recommended CODATA-2018 values
@@ -109,10 +109,10 @@ K_alpha_W_d_220 = 0.108852175
109109

110110
#### DERIVED CONSTANTS ####
111111

112-
fine_structure_constant = (2 * h * R_inf / (m_e * c)) ** 0.5 = α = alpha
113-
vacuum_permeability = 2 * α * h / (e ** 2 * c) = µ_0 = mu_0 = mu0 = magnetic_constant
114-
vacuum_permittivity = e ** 2 / (2 * α * h * c) = ε_0 = epsilon_0 = eps_0 = eps0 = electric_constant
115-
impedance_of_free_space = 2 * α * h / e ** 2 = Z_0 = characteristic_impedance_of_vacuum
112+
fine_structure_constant = (2 * * R_inf / (m_e * c)) ** 0.5 = α = alpha
113+
vacuum_permeability = 2 * α * / (e ** 2 * c) = µ_0 = mu_0 = mu0 = magnetic_constant
114+
vacuum_permittivity = e ** 2 / (2 * α * * c) = ε_0 = epsilon_0 = eps_0 = eps0 = electric_constant
115+
impedance_of_free_space = 2 * α * / e ** 2 = Z_0 = characteristic_impedance_of_vacuum
116116
coulomb_constant = α * hbar * c / e ** 2 = k_C
117117
classical_electron_radius = α * hbar / (m_e * c) = r_e
118118
thomson_cross_section = 8 / 3 * π * r_e ** 2 = σ_e = sigma_e

gemd/units/impl.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
"""Implementation of units."""
2-
import pint
3-
from pint import UnitRegistry
4-
from pint.unit import _Unit
2+
from pint import UnitRegistry, Unit
53
import pkg_resources
64

75
import functools
86
from typing import Union
97

8+
# alias the error that is thrown when units are incompatible
9+
# this helps to isolate the dependence on pint
10+
from pint.errors import DimensionalityError as IncompatibleUnitsError # noqa Import
11+
from pint.errors import UndefinedUnitError
1012

1113
# use the default unit registry for now
1214
DEFAULT_FILE = pkg_resources.resource_filename("gemd.units", "citrine_en.txt")
13-
_ureg = UnitRegistry(filename=DEFAULT_FILE)
14-
15-
16-
# alias the error that is thrown when units are incompatible
17-
# this helps to isolate the dependence on pint
18-
IncompatibleUnitsError = pint.errors.DimensionalityError
19-
UndefinedUnitError = pint.errors.UndefinedUnitError
15+
registry = UnitRegistry(filename=DEFAULT_FILE)
2016

2117

2218
@functools.lru_cache(maxsize=None)
23-
def parse_units(units: Union[str, _Unit, None]) -> Union[str, _Unit, None]:
19+
def parse_units(units: Union[str, Unit, None]) -> Union[str, Unit, None]:
2420
"""
25-
Parse a string or _Unit into a standard string representation of the unit.
21+
Parse a string or Unit into a standard string representation of the unit.
2622
2723
Parameters
2824
----------
29-
units: Union[str, _Unit, None]
30-
The string or _Unit representation of the object we wish to display
25+
units: Union[str, Unit, None]
26+
The string or Unit representation of the object we wish to display
3127
3228
Returns
3329
-------
34-
[Union[str, _Unit, None]]
30+
[Union[str, Unit, None]]
3531
The representation; note that the same type that was passed is returned
3632
3733
"""
@@ -40,8 +36,8 @@ def parse_units(units: Union[str, _Unit, None]) -> Union[str, _Unit, None]:
4036
elif units == '':
4137
return 'dimensionless'
4238
elif isinstance(units, str):
43-
return str(_ureg(units).units)
44-
elif isinstance(units, _Unit):
39+
return str(registry(units).units)
40+
elif isinstance(units, Unit):
4541
return units
4642
else:
4743
raise UndefinedUnitError("Units must be given as a recognized unit string or Units object")
@@ -70,7 +66,7 @@ def convert_units(value: float, starting_unit: str, final_unit: str) -> float:
7066
if starting_unit == final_unit:
7167
return value # skip computation
7268
else:
73-
return _ureg.Quantity(value, starting_unit).to(final_unit).magnitude
69+
return registry.Quantity(value, starting_unit).to(final_unit).magnitude
7470

7571

7672
def change_definitions_file(filename: str = None):
@@ -83,8 +79,8 @@ def change_definitions_file(filename: str = None):
8379
The file to use
8480
8581
"""
86-
global _ureg
82+
global registry
8783
convert_units.cache_clear() # Units will change
8884
if filename is None:
8985
filename = DEFAULT_FILE
90-
_ureg = UnitRegistry(filename=filename)
86+
registry = UnitRegistry(filename=filename)

0 commit comments

Comments
 (0)