Esempio n. 1
0
 def real_mapper(prop):
     """Mapping methods for RealBounds."""
     if 'uncertainty' in prop['scalars'][0]:
         if prop['units'] == 'eV':  # Arbitrarily convert to attojoules
             mean = convert_units(value=float(prop['scalars'][0]['value']),
                                  starting_unit=prop['units'],
                                  final_unit='aJ'
                                  )
             std = convert_units(value=float(prop['scalars'][0]['value']),
                                 starting_unit=prop['units'],
                                 final_unit='aJ'
                                 )
             val = NormalReal(mean=mean,
                              units='aJ',
                              std=std
                              )
         else:
             val = NormalReal(mean=float(prop['scalars'][0]['value']),
                              units=prop['units'],
                              std=float(prop['scalars'][0]['uncertainty'])
                              )
     else:
         val = NominalReal(nominal=float(prop['scalars'][0]['value']),
                           units=prop['units']
                           )
     return val
def test_file_change():
    """Test that swapping units files works."""
    assert convert_units(1, 'm', 'cm') == 100
    with pytest.raises(UndefinedUnitError):
        assert convert_units(1, 'usd', 'usd') == 1
    with _change_units(filename=pkg_resources.resource_filename("gemd.units",
                                                                "tests/test_units.txt")):
        with pytest.raises(UndefinedUnitError):
            assert convert_units(1, 'm', 'cm') == 100
        assert convert_units(1, 'usd', 'usd') == 1
    assert convert_units(1, 'm', 'cm') == 100  # And verify we're back to normal
Esempio n. 3
0
    def _convert_bounds(self, target_units):
        """
        Convert the bounds to the target unit system, or None if not possible.

        Parameters
        ----------
        target_units: str
            The units to convert into.

        Returns
        -------
        tuple (float, float)
            A tuple of the (lower_bound, upper_bound) in the target units.

        """
        try:
            lower_bound = units.convert_units(self.lower_bound,
                                              self.default_units, target_units)
            upper_bound = units.convert_units(self.upper_bound,
                                              self.default_units, target_units)
            return lower_bound, upper_bound
        except units.IncompatibleUnitsError:
            return None, None
def test_conversion():
    """Tests that check if particular units are interoperable."""
    conversions = {"in_lb": "foot_pound"}
    for source, dest in conversions.items():
        assert convert_units(convert_units(1, source, dest), dest, source) == 1