def test_convert(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, -273.15)
     ])
     assert cup.convert(42, Unit('km'),
                        to_unit=Unit('m')) == 42000  # source to ref
     assert cup.convert(42, Unit('m'),
                        to_unit=Unit('m')) == 42  # ref to ref
     assert cup.convert(42, Unit('m'),
                        to_unit=Unit('km')) == 0.042  # ref to source
     assert cup.convert(
         42, Unit('km'),
         to_unit=Unit('mm')) == 42000000  # source to other source
     with raises(ValueError):
         cup.convert(42, Unit('km'),
                     to_unit=Unit('K'))  # Don't share same ref unit
     with raises(ValueError):
         cup.convert(42, Unit('m'),
                     to_unit=Unit('C'))  # Don't share same ref unit
     with raises(ValueError):
         cup.convert(42, Unit('m'),
                     to_unit=Unit('bogus'))  # to_unit doesn't exist
     with raises(ValueError):
         cup.convert(42, Unit('bogus'),
                     to_unit=Unit('C'))  # from_unit doesn't exist
     with raises(ValueError):
         cup.convert(42, Unit('fake'),
                     to_unit=Unit('bogus'))  # neither unit exists
 def test_eq(self):
     assert ScaleUnitConversion(Unit('mm'), Unit('m'),
                                0.001) == ScaleUnitConversion(
                                    Unit('mm'), Unit('m'), 0.001)
     assert ScaleUnitConversion(Unit('bs'), Unit('m'),
                                0.123) != ScaleUnitConversion(
                                    Unit('mm'), Unit('m'), 0.001)
 def test_conflicting_roles(self):
     with raises(ValueError):
         CustomUnitPolicy([
             ScaleUnitConversion(Unit('m'), Unit('km'), 0.001),
             ScaleUnitConversion(Unit('km'), Unit('mm'), 1000000)
         ],
                          duplicates='overwrite')
 def test_duplicate_overwrite(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('km'), Unit('mm'), 1000000)
     ],
                            duplicates='overwrite')
     assert cup.ref_unit(Unit('km')) == Unit('mm')
 def test_duplicate_raise(self):
     with raises(ValueError):
         CustomUnitPolicy([
             ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
             ScaleUnitConversion(Unit('km'), Unit('mm'), 1000000)
         ],
                          duplicates='raise')
 def test_iter(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, 273.15)
     ])
     assert Unit('mm') in cup
     assert Unit('m') in cup
     assert set(cup) == {'km', 'mm', 'm', 'C', 'K'}
 def test_convert_to_ref(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, 273.15)
     ])
     assert cup.convert_to_ref(42, Unit('km')) == 42000  # source to ref
     assert cup.convert_to_ref(42, Unit('m')) == 42  # ref to ref
     with raises(ValueError):
         cup.convert_to_ref(42, Unit('bogus'))  # unit doesn't exist
 def test_convert_to_ref_units(self):
     df = pd.DataFrame([
         [11, 12, 13],
         [21, 22, 23]], columns=['a', 'b', 'c'])
     cs = {n: ColumnMetadata(Unit(u)) for n, u in zip(['a', 'b', 'c'], ['m', 'mm', 'km'])}
     t = Table(df, name='Fool', col_specs=cs)
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000)])
     t_ref = t.convert_to_ref_units(cup, inplace=False)
     assert t_ref.col_units == [Unit('m')] * 3
     assert (np.array(t_ref.df) == np.array([[11, 0.012, 13000],
                                             [21, 0.022, 23000]])).all()
 def test_convert_to_home_units(self):
     df = pd.DataFrame([
         [11, 12, 13],
         [21, 22, 23]], columns=['a', 'b', 'c'])
     cs = {n: ColumnMetadata(Unit(u), Unit(hu)) for n, u, hu in zip(
         ['a', 'b', 'c'], ['m', 'm', 'm'], ['m', 'mm', 'km'])}
     t = Table(df, name='Fool', col_specs=cs)
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000)])
     t_home = t.convert_to_home_units(cup)
     assert t_home.col_units == [Unit('m'), Unit('mm'), Unit('km')]
     assert (np.array(t_home.df) == np.array([[11, 12000, 0.013],
                                              [21, 22000, 0.023]])).all()
 def test_can_convert(self):
     cup = CustomUnitPolicy([
         ScaleUnitConversion(Unit('km'), Unit('m'), 1000),
         ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001),
         AffineUnitConversion(Unit('C'), Unit('K'), 1, 273.15)
     ])
     assert cup.can_convert(from_unit=Unit('km'), to_unit=Unit('m'))
     assert cup.can_convert(from_unit=Unit('m'), to_unit=Unit('km'))
     assert cup.can_convert(from_unit=Unit('km'), to_unit=Unit('mm'))
     assert not cup.can_convert(
         from_unit=Unit('km'),
         to_unit=Unit('C'))  # Don't share same ref unit
     assert not cup.can_convert(
         from_unit=Unit('bogus'),
         to_unit=Unit('m'))  # from_unit doesn't exist
 def test_ref_unit(self):
     cup = CustomUnitPolicy(
         [ScaleUnitConversion(Unit('km'), Unit('m'), 1000)])
     assert cup.ref_unit(Unit('km')) == Unit('m')
     assert cup.ref_unit(Unit('m')) == Unit('m')
     with raises(ValueError):
         cup.ref_unit(Unit('furlong'))  # unit doesn't exist
    def test_convert_to_ref_units_unknown_unit(self):
        df = pd.DataFrame([
            [11, 12, 13],
            [21, 22, 23]], columns=['a', 'b', 'c'])
        cs = {n: ColumnMetadata(Unit(u)) for n, u in zip(['a', 'b', 'c'], ['m', 'mm', 'km'])}
        t = Table(df, name='Fool', col_specs=cs)
        cup_no_km = CustomUnitPolicy([ScaleUnitConversion(Unit('mm'), Unit('m'), 0.001)])
        t_ref_no_km = t.convert_to_ref_units(cup_no_km, inplace=False, units_not_in_policy='ignore')
        assert t_ref_no_km.col_units == [Unit('m'), Unit('m'), Unit('km')]
        assert (np.array(t_ref_no_km.df) == np.array([[11, 0.012, 13],
                                                      [21, 0.022, 23]])).all()

        with raises(ValueError):
            t.convert_to_ref_units(cup_no_km, inplace=False, units_not_in_policy='raise')
 def test_alias(self):
     alias = ScaleUnitConversion(Unit('mm'), Unit('m'),
                                 0.001).alias(Unit('millimetre'))
     assert alias == ScaleUnitConversion(Unit('millimetre'), Unit('m'),
                                         0.001)
     assert alias.to_ref(1000) == 1
 def test_reverse(self):
     assert ScaleUnitConversion(Unit('mm'), Unit('m'),
                                0.001).reverse().to_ref(42) == approx(42000)
 def test_from_ref(self):
     assert ScaleUnitConversion(Unit('mm'), Unit('m'),
                                0.001).from_ref(42) == 42000
 def test_to_ref(self):
     assert ScaleUnitConversion(Unit('mm'), Unit('m'),
                                0.001).to_ref(42) == 0.042