本文整理汇总了Python中pint.UnitRegistry方法的典型用法代码示例。如果您正苦于以下问题:Python pint.UnitRegistry方法的具体用法?Python pint.UnitRegistry怎么用?Python pint.UnitRegistry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pint
的用法示例。
在下文中一共展示了pint.UnitRegistry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_known_context
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_known_context(self):
ureg = UnitRegistry()
add_ctxs(ureg)
with ureg.context('lc'):
self.assertTrue(ureg._active_ctx)
self.assertTrue(ureg._active_ctx.graph)
self.assertFalse(ureg._active_ctx)
self.assertFalse(ureg._active_ctx.graph)
with ureg.context('lc', n=1):
self.assertTrue(ureg._active_ctx)
self.assertTrue(ureg._active_ctx.graph)
self.assertFalse(ureg._active_ctx)
self.assertFalse(ureg._active_ctx.graph)
示例2: test_known_context_enable
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_known_context_enable(self):
ureg = UnitRegistry()
add_ctxs(ureg)
ureg.enable_contexts('lc')
self.assertTrue(ureg._active_ctx)
self.assertTrue(ureg._active_ctx.graph)
ureg.disable_contexts(1)
self.assertFalse(ureg._active_ctx)
self.assertFalse(ureg._active_ctx.graph)
ureg.enable_contexts('lc', n=1)
self.assertTrue(ureg._active_ctx)
self.assertTrue(ureg._active_ctx.graph)
ureg.disable_contexts(1)
self.assertFalse(ureg._active_ctx)
self.assertFalse(ureg._active_ctx.graph)
示例3: test_unknown_nested_context
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_unknown_nested_context(self):
ureg = UnitRegistry()
add_ctxs(ureg)
with ureg.context('lc'):
x = dict(ureg._active_ctx)
y = dict(ureg._active_ctx.graph)
try:
with ureg.context('la'):
pass
except KeyError as e:
value = True
except Exception as e:
value = False
self.assertTrue(value)
self.assertEqual(x, ureg._active_ctx)
self.assertEqual(y, ureg._active_ctx.graph)
self.assertFalse(ureg._active_ctx)
self.assertFalse(ureg._active_ctx.graph)
示例4: test_nested_context
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_nested_context(self):
ureg = UnitRegistry()
add_ctxs(ureg)
q = 500 * ureg.meter
s = (ureg.speed_of_light / q).to('Hz')
self.assertRaises(ValueError, q.to, 'Hz')
with ureg.context('lc'):
self.assertEqual(q.to('Hz'), s)
with ureg.context('ab'):
self.assertEqual(q.to('Hz'), s)
self.assertEqual(q.to('Hz'), s)
with ureg.context('ab'):
self.assertRaises(ValueError, q.to, 'Hz')
with ureg.context('lc'):
self.assertEqual(q.to('Hz'), s)
self.assertRaises(ValueError, q.to, 'Hz')
示例5: _test_ctx
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def _test_ctx(self, ctx):
ureg = UnitRegistry()
q = 500 * ureg.meter
s = (ureg.speed_of_light / q).to('Hz')
nctx = len(ureg._contexts)
self.assertNotIn(ctx.name, ureg._contexts)
ureg.add_context(ctx)
self.assertIn(ctx.name, ureg._contexts)
self.assertEqual(len(ureg._contexts), nctx + 1 + len(ctx.aliases))
with ureg.context(ctx.name):
self.assertEqual(q.to('Hz'), s)
self.assertEqual(s.to('meter'), q)
ureg.remove_context(ctx.name)
self.assertNotIn(ctx.name, ureg._contexts)
self.assertEqual(len(ureg._contexts), nctx)
示例6: test_warnings
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_warnings(self):
ureg = UnitRegistry()
with self.capture_log() as buffer:
add_ctxs(ureg)
d = Context('ab')
ureg.add_context(d)
self.assertEqual(len(buffer), 1)
self.assertIn("ab", str(buffer[-1]))
d = Context('ab1', aliases=('ab',))
ureg.add_context(d)
self.assertEqual(len(buffer), 2)
self.assertIn("ab", str(buffer[-1]))
示例7: test_issue104
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_issue104(self):
ureg = UnitRegistry()
x = [ureg('1 meter'), ureg('1 meter'), ureg('1 meter')]
y = [ureg('1 meter')] * 3
def summer(values):
if not values:
return 0
total = values[0]
for v in values[1:]:
total += v
return total
self.assertQuantityAlmostEqual(summer(x), ureg.Quantity(3, 'meter'))
self.assertQuantityAlmostEqual(x[0], ureg.Quantity(1, 'meter'))
self.assertQuantityAlmostEqual(summer(y), ureg.Quantity(3, 'meter'))
self.assertQuantityAlmostEqual(y[0], ureg.Quantity(1, 'meter'))
示例8: test_issue37
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_issue37(self):
x = np.ma.masked_array([1, 2, 3], mask=[True, True, False])
ureg = UnitRegistry()
q = ureg.meter * x
self.assertIsInstance(q, ureg.Quantity)
np.testing.assert_array_equal(q.magnitude, x)
self.assertEqual(q.units, ureg.meter.units)
q = x * ureg.meter
self.assertIsInstance(q, ureg.Quantity)
np.testing.assert_array_equal(q.magnitude, x)
self.assertEqual(q.units, ureg.meter.units)
m = np.ma.masked_array(2 * np.ones(3,3))
qq = q * m
self.assertIsInstance(qq, ureg.Quantity)
np.testing.assert_array_equal(qq.magnitude, x * m)
self.assertEqual(qq.units, ureg.meter.units)
qq = m * q
self.assertIsInstance(qq, ureg.Quantity)
np.testing.assert_array_equal(qq.magnitude, x * m)
self.assertEqual(qq.units, ureg.meter.units)
示例9: test_issue39
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_issue39(self):
x = np.matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
ureg = UnitRegistry()
q = ureg.meter * x
self.assertIsInstance(q, ureg.Quantity)
np.testing.assert_array_equal(q.magnitude, x)
self.assertEqual(q.units, ureg.meter.units)
q = x * ureg.meter
self.assertIsInstance(q, ureg.Quantity)
np.testing.assert_array_equal(q.magnitude, x)
self.assertEqual(q.units, ureg.meter.units)
m = np.matrix(2 * np.ones(3,3))
qq = q * m
self.assertIsInstance(qq, ureg.Quantity)
np.testing.assert_array_equal(qq.magnitude, x * m)
self.assertEqual(qq.units, ureg.meter.units)
qq = m * q
self.assertIsInstance(qq, ureg.Quantity)
np.testing.assert_array_equal(qq.magnitude, x * m)
self.assertEqual(qq.units, ureg.meter.units)
示例10: test_issue74
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_issue74(self):
ureg = UnitRegistry()
v1 = np.asarray([1., 2., 3.])
v2 = np.asarray([3., 2., 1.])
q1 = v1 * ureg.ms
q2 = v2 * ureg.ms
np.testing.assert_array_equal(q1 < q2, v1 < v2)
np.testing.assert_array_equal(q1 > q2, v1 > v2)
np.testing.assert_array_equal(q1 <= q2, v1 <= v2)
np.testing.assert_array_equal(q1 >= q2, v1 >= v2)
q2s = np.asarray([0.003, 0.002, 0.001]) * ureg.s
v2s = q2s.to('ms').magnitude
np.testing.assert_array_equal(q1 < q2s, v1 < v2s)
np.testing.assert_array_equal(q1 > q2s, v1 > v2s)
np.testing.assert_array_equal(q1 <= q2s, v1 <= v2s)
np.testing.assert_array_equal(q1 >= q2s, v1 >= v2s)
示例11: convert_units
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def convert_units(dataset, in_unit, out_unit):
"""Convert units of *dataset*."""
from pint import UnitRegistry
ureg = UnitRegistry()
# Commented because buggy: race condition ?
# ureg.define("degree_Celsius = degC = Celsius = C = CELSIUS")
in_unit = ureg.parse_expression(in_unit, False)
if out_unit in ['CELSIUS', 'C', 'Celsius', 'celsius']:
dest_unit = ureg.degC
else:
dest_unit = ureg.parse_expression(out_unit, False)
data = ureg.Quantity(dataset, in_unit)
attrs = dataset.attrs
dataset = data.to(dest_unit).magnitude
dataset.attrs = attrs
dataset.attrs["units"] = out_unit
return dataset
示例12: __init__
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def __init__(self, database_path):
# Initiate pint for unit conversions
self.ureg = pint.UnitRegistry()
self.cfg_energy_unit = cfg.getParam('calculation_energy_unit')
self.cfg_currency = cfg.getParam('currency_name')
self.cfg_currency_year = cfg.getParamAsInt('currency_year')
db = get_database(database_path)
self.currency_table = db.get_table("CurrenciesConversion").data
self.currency_table = self.currency_table.set_index(['currency', 'year']).sort_index()
self.inflation_table = db.get_table("InflationConversion").data
self.inflation_table = self.inflation_table.set_index(['currency', 'year']).sort_index()
for unit_def in UnitConverter._unit_defs:
unit_name = unit_def.split(' = ')[0]
if hasattr(self.ureg, unit_name):
logging.debug('pint already has unit {}, unit is not being redefined'.format(unit_name))
continue
self.ureg.define(unit_def)
示例13: test_convert_unit_with_custom_registry
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def test_convert_unit_with_custom_registry(test_df):
# unit conversion with custom UnitRegistry
df = get_units_test_df(test_df).rename(unit={'EJ/yr': 'foo'})
# check that conversion fails with application registry
with pytest.raises(pint.UndefinedUnitError):
df.convert_unit('foo', 'baz')
# define a custom unit registry
ureg = pint.UnitRegistry()
ureg.define('baz = [custom]')
ureg.define('foo = 3 * baz')
exp = pd.Series([1., 6., 1.5, 9, 6, 21], name='value')
assert_converted_units(df, 'foo', 'baz', exp, registry=ureg)
# This test is parametrized as the product of three sets:
# 1. The test_df fixture.
# 2. Current species, context, and expected output magnitude.
# 3. Input and output expressions, and any factor on the output magnitude due
# to differences in units between these.
示例14: _build_unit_registry
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def _build_unit_registry():
try:
from pint import UnitRegistry
registry = UnitRegistry()
registry.define('FPS = 1 * hertz')
except ImportError:
class NoUnitRegistry:
def __init__(self):
pass
def __getattr__(self, item):
return 1
registry = NoUnitRegistry()
return registry
示例15: load_data
# 需要导入模块: import pint [as 别名]
# 或者: from pint import UnitRegistry [as 别名]
def load_data(self):
super(SoundSpeedPlotter, self).load_data()
self.pressure = [seawater.pres(self.temperature_depths[idx], ll[0])
for idx, ll in enumerate(self.points)]
ureg = pint.UnitRegistry()
try:
u = ureg.parse_units(self.variable_units[0].lower())
except:
u = ureg.dimensionless
if u == ureg.boltzmann_constant:
u = ureg.kelvin
if u == ureg.kelvin:
temperature_c = ureg.Quantity(
self.temperature, u).to(ureg.celsius).magnitude
else:
temperature_c = self.temperature
self.sspeed = seawater.svel(
self.salinity, temperature_c, self.pressure
)