def test_convert_gwp(units, metric, species_in, species_out, expected_value): # Bare masses can be converted qty = registry.Quantity(1.0, units.format("")) expected = registry(f"{expected_value} {units}") observed = convert_gwp(metric, qty, species_in, species_out) assert observed.units == expected.units np.testing.assert_almost_equal(observed.magnitude, expected.magnitude) # '[mass] [speciesname] (/ [time])' can be converted; the input species is extracted # from the *qty* argument qty = "1.0 " + units.format(species_in) expected = registry(f"{expected_value} {units}") observed = convert_gwp(metric, qty, species_out) assert observed.units == expected.units np.testing.assert_almost_equal(observed.magnitude, expected.magnitude) # Tuple of (vector magnitude, unit expression) can be converted where the # the unit expression contains the input species name arr = np.array([1.0, 2.5, 0.1]) qty = (arr, units.format(species_in)) expected = arr * expected_value # Conversion works result = convert_gwp(metric, qty, species_out) # Magnitudes are as expected assert_array_almost_equal(result.magnitude, expected)
def test_convert_units(recwarn): """:func:`.convert_units` and :func:`.series_of_pint_quantity` work.""" # Common arguments args = [ pd.Series([1.1, 10.2, 100.3], name="bar"), dict(bar=(10.0, "lb", "kg")) ] exp = series_of_pint_quantity([ registry("4.9895 kg"), registry("46.2664 kg"), registry("454.9531 kg") ], ) # With store="quantity", a series of pint.Quantity is returned result = convert_units(*args, store="quantity") assert all( np.isclose(a, b, atol=1e-4) for a, b in zip(exp.values, result.values)) # With store="magnitude", a series of floats exp = pd.Series([q.magnitude for q in exp.values], name="bar") assert_series_equal(exp, convert_units(*args, store="magnitude"), check_dtype=False) # Other values for store= are errors with pytest.raises(ValueError, match="store='foo'"): convert_units(*args, store="foo") # series_of_pint_quantity() successfully caught warnings assert 0 == len(recwarn)
def test_emissions_internal(): # Dummy units can be created registry("0.5 _gwp").dimensionality == {"[_GWP]": 1.0} # Context can be enabled with registry.context("AR5GWP100", __a=1.0) as r: # Context-specific values are activated assert r("a_CH4 * a_N2O").to_base_units().magnitude == 28 * 265 # Context-specific conversion rules are available r("0.5 t").to("_gwp")
def test_commodities(self): data = get_codes("commodity") # Some expected commodities are present for check in "coal", "electr": assert check in data # Units for one commodity can be retrieved and parsed coal = data[data.index("coal")] registry(str(coal.get_annotation(id="unit").text)) # Descriptions are parsed without new lines crudeoil = data[data.index("crudeoil")] assert "\n" not in str(crudeoil.description)
def test_kt(): # The registry should correctly interpret `kt` as a weight (not velocity) assert str(registry("1000 kt").to("Mt")) == "1.0 megametric_ton" # A default UnitRegistry should interpret `kt` as velocity with pytest.raises(pint.DimensionalityError): pint.UnitRegistry()("kt").to("Mt")
def process_commodity_codes(codes): """Process a list of codes for ``commodity``. The function warns for commodities missing units or with non-:mod:`pint`-compatible units. """ for code in codes: unit = eval_anno(code, "unit") if unit is None: log.warning(f"Commodity {code} lacks defined units") continue try: # Check that the unit can be parsed by the pint.UnitRegistry registry(unit) except Exception: # pragma: no cover # No coverage: code that triggers this exception should never be committed log.warning(f"Unit {unit} for commodity {code} not pint compatible")
def test_units(unit_str, dim, new_def): if new_def: # Units defined in dimensions.txt are not recognized by base pint with pytest.raises(pint.UndefinedUnitError): DEFAULTS("1 " + unit_str) # Physical quantity of units is recognized dim = registry.get_dimensionality(dim) # Units can be parsed and have the correct dimensionality assert registry("1 " + unit_str).dimensionality == dim
def test_convert_gwp_carbon(context): # CO2 can be converted to C qty = (44.0 / 12, "tonne CO2") result = convert_gwp(context, qty, "C") assert result.units == registry("tonne") assert_almost_equal(result.magnitude, 1.0) # C can be converted to CO2 qty = (1, "tonne C") expected = registry.Quantity(44.0 / 12, "tonne") assert convert_gwp(context, qty, "CO2e") == expected
def test_convert_gwp_carbon(): # CO2 can be converted to C qty = (44. / 12, 'tonne CO2') result = convert_gwp('AR5GWP100', qty, 'C') assert result.units == registry('tonne') assert_almost_equal(result.magnitude, 1.0) # C can be converted to CO2 qty = (1, 'tonne C') expected = registry.Quantity(44. / 12, 'tonne') assert convert_gwp('AR5GWP100', qty, 'CO2e') == expected
def test_orders_of_magnitude(): # The registry recognizes units prefixed by an order of magnitude assert registry("1.2 billion EUR").to("million EUR").magnitude == 1.2e3