Exemple #1
0
    def test_issue121(self):
        sh = (2, 1)
        ureg = UnitRegistry()

        z, v = 0, 2.
        self.assertEqual(z + v * ureg.meter, v * ureg.meter)
        self.assertEqual(z - v * ureg.meter, -v * ureg.meter)
        self.assertEqual(v * ureg.meter + z, v * ureg.meter)
        self.assertEqual(v * ureg.meter - z, v * ureg.meter)

        self.assertEqual(sum([v * ureg.meter, v * ureg.meter]), 2 * v * ureg.meter)

        z, v = np.zeros(sh), 2. * np.ones(sh)
        self.assertQuantityEqual(z + v * ureg.meter, v * ureg.meter)
        self.assertQuantityEqual(z - v * ureg.meter, -v * ureg.meter)
        self.assertQuantityEqual(v * ureg.meter + z, v * ureg.meter)
        self.assertQuantityEqual(v * ureg.meter - z, v * ureg.meter)

        z, v = np.zeros((3, 1)), 2. * np.ones(sh)
        for x, y in ((z, v),
                     (z, v * ureg.meter),
                     (v * ureg.meter, z)
                     ):
            try:
                w = x + y
                self.assertTrue(False, "ValueError not raised")
            except ValueError:
                pass
            try:
                w = x - y
                self.assertTrue(False, "ValueError not raised")
            except ValueError:
                pass
    def test_issue121(self):
        sh = (2, 1)
        ureg = UnitRegistry()

        z, v = 0, 2.
        self.assertEqual(z + v * ureg.meter, v * ureg.meter)
        self.assertEqual(z - v * ureg.meter, -v * ureg.meter)
        self.assertEqual(v * ureg.meter + z, v * ureg.meter)
        self.assertEqual(v * ureg.meter - z, v * ureg.meter)

        self.assertEqual(sum([v * ureg.meter, v * ureg.meter]),
                         2 * v * ureg.meter)

        z, v = np.zeros(sh), 2. * np.ones(sh)
        self.assertQuantityEqual(z + v * ureg.meter, v * ureg.meter)
        self.assertQuantityEqual(z - v * ureg.meter, -v * ureg.meter)
        self.assertQuantityEqual(v * ureg.meter + z, v * ureg.meter)
        self.assertQuantityEqual(v * ureg.meter - z, v * ureg.meter)

        z, v = np.zeros((3, 1)), 2. * np.ones(sh)
        for x, y in ((z, v), (z, v * ureg.meter), (v * ureg.meter, z)):
            try:
                w = x + y
                self.assertTrue(False, "ValueError not raised")
            except ValueError:
                pass
            try:
                w = x - y
                self.assertTrue(False, "ValueError not raised")
            except ValueError:
                pass
Exemple #3
0
    def test_issue121b(self):
        sh = (2, 1)
        ureg = UnitRegistry()

        z, v = 0, 2.0
        self.assertEqual(z + v * ureg.meter, v * ureg.meter)
        self.assertEqual(z - v * ureg.meter, -v * ureg.meter)
        self.assertEqual(v * ureg.meter + z, v * ureg.meter)
        self.assertEqual(v * ureg.meter - z, v * ureg.meter)

        self.assertEqual(sum([v * ureg.meter, v * ureg.meter]),
                         2 * v * ureg.meter)

        z, v = np.zeros(sh), 2.0 * np.ones(sh)
        self.assertQuantityEqual(z + v * ureg.meter, v * ureg.meter)
        self.assertQuantityEqual(z - v * ureg.meter, -v * ureg.meter)
        self.assertQuantityEqual(v * ureg.meter + z, v * ureg.meter)
        self.assertQuantityEqual(v * ureg.meter - z, v * ureg.meter)

        z, v = np.zeros((3, 1)), 2.0 * np.ones(sh)
        for x, y in ((z, v), (z, v * ureg.meter), (v * ureg.meter, z)):
            with self.assertRaises(ValueError):
                x + y
            with self.assertRaises(ValueError):
                x - y
Exemple #4
0
 def test_converter_inplace(self):
     for c in (ScaleConverter(20.), OffsetConverter(20., 2)):
         fun1 = lambda x, y: c.from_reference(c.to_reference(x, y), y)
         fun2 = lambda x, y: c.to_reference(c.from_reference(x, y), y)
         for fun, (inplace, comp) in itertools.product((fun1, fun2),
                                                       ((True, self.assertIs), (False, self.assertIsNot))):
             a = np.ones((1, 10))
             ac = np.ones((1, 10))
             r = fun(a, inplace)
             np.testing.assert_allclose(r, ac)
             comp(a, r)
 def test_converter_inplace(self):
     for c in (ScaleConverter(20.0), OffsetConverter(20.0, 2)):
         fun1 = lambda x, y: c.from_reference(c.to_reference(x, y), y)
         fun2 = lambda x, y: c.to_reference(c.from_reference(x, y), y)
         for fun, (inplace, comp) in itertools.product(
             (fun1, fun2),
             ((True, self.assertIs), (False, self.assertIsNot))):
             a = np.ones((1, 10))
             ac = np.ones((1, 10))
             r = fun(a, inplace)
             np.testing.assert_allclose(r, ac)
             comp(a, r)
Exemple #6
0
    def test_convert(self):

        # Conversions with single units take a different codepath than
        # Conversions with more than one unit.
        src_dst1 = UnitsContainer(meter=1), UnitsContainer(inch=1)
        src_dst2 = UnitsContainer(meter=1, seconds=-1), UnitsContainer(inch=1, minutes=-1)
        for src, dst in (src_dst1, src_dst2):
            a = np.ones((3, 1))
            ac = np.ones((3, 1))

            q = self.Q_(a, src)
            qac = self.Q_(ac, src).to(dst)
            r = q.to(dst)
            self.assertQuantityAlmostEqual(qac, r)
            self.assertIsNot(r, q)
            self.assertIsNot(r._magnitude, a)
Exemple #7
0
    def test_equal(self):
        x = self.q.magnitude
        u = self.Q_(np.ones(x.shape))

        self.assertQuantityEqual(u, u)
        self.assertQuantityEqual(u == u, u.magnitude == u.magnitude)
        self.assertQuantityEqual(u == 1, u.magnitude == 1)
Exemple #8
0
    def test_array_interface(self):
        import numpy as np

        x = self.U_('m')
        arr = np.ones(10)
        self.assertQuantityEqual(arr*x, self.Q_(arr, 'm'))
        self.assertQuantityEqual(arr/x, self.Q_(arr, '1/m'))
        self.assertQuantityEqual(x/arr, self.Q_(arr, 'm'))
Exemple #9
0
 def test_reversible_op(self):
     """ """
     x = self.q.magnitude
     u = self.Q_(np.ones(x.shape))
     self.assertQuantityEqual(x / self.q, u * x / self.q)
     self.assertQuantityEqual(x * self.q, u * x * self.q)
     self.assertQuantityEqual(x + u, u + x)
     self.assertQuantityEqual(x - u, -(u - x))
Exemple #10
0
    def test_array_interface(self):
        import numpy as np

        x = self.U_('m')
        arr = np.ones(10)
        self.assertQuantityEqual(arr * x, self.Q_(arr, 'm'))
        self.assertQuantityEqual(arr / x, self.Q_(arr, '1/m'))
        self.assertQuantityEqual(x / arr, self.Q_(arr, 'm'))
Exemple #11
0
    def test_array_interface(self):
        import numpy as np

        x = self.U_("m")
        arr = np.ones(10)
        helpers.assert_quantity_equal(arr * x, self.Q_(arr, "m"))
        helpers.assert_quantity_equal(arr / x, self.Q_(arr, "1/m"))
        helpers.assert_quantity_equal(x / arr, self.Q_(arr, "m"))
Exemple #12
0
 def test_reversible_op(self):
     """
     """
     x = self.q.magnitude
     u = self.Q_(np.ones(x.shape))
     self.assertQuantityEqual(x / self.q, u * x / self.q)
     self.assertQuantityEqual(x * self.q, u * x * self.q)
     self.assertQuantityEqual(x + u, u + x)
     self.assertQuantityEqual(x - u, -(u - x))
Exemple #13
0
    def test_equal(self):
        x = self.q.magnitude
        u = self.Q_(np.ones(x.shape))
        true = np.ones_like(x, dtype=np.bool_)
        false = np.zeros_like(x, dtype=np.bool_)

        self.assertQuantityEqual(u, u)
        self.assertQuantityEqual(u == u, u.magnitude == u.magnitude)
        self.assertQuantityEqual(u == 1, u.magnitude == 1)

        v = self.Q_(np.zeros(x.shape), "m")
        w = self.Q_(np.ones(x.shape), "m")
        self.assertNDArrayEqual(v == 1, false)
        self.assertNDArrayEqual(
            self.Q_(np.zeros_like(x), "m") == self.Q_(np.zeros_like(x), "s"), false,
        )
        self.assertNDArrayEqual(v == v, true)
        self.assertNDArrayEqual(v == w, false)
        self.assertNDArrayEqual(v == w.to("mm"), false)
        self.assertNDArrayEqual(u == v, false)
Exemple #14
0
    def test_convert_inplace(self):
        ureg = self.ureg

        # Conversions with single units take a different codepath than
        # Conversions with more than one unit.
        src_dst1 = UnitsContainer(meter=1), UnitsContainer(inch=1)
        src_dst2 = UnitsContainer(meter=1, second=-1), UnitsContainer(inch=1, minute=-1)
        for src, dst in (src_dst1, src_dst2):
            v = (ureg.convert(1, src, dst),)

            a = np.ones((3, 1))
            ac = np.ones((3, 1))

            r1 = ureg.convert(a, src, dst)
            np.testing.assert_allclose(r1, v * ac)
            self.assertIsNot(r1, a)

            r2 = ureg.convert(a, src, dst, inplace=True)
            np.testing.assert_allclose(r2, v * ac)
            self.assertIs(r2, a)
Exemple #15
0
    def test_convert_inplace(self):
        ureg = self.ureg

        # Conversions with single units take a different codepath than
        # Conversions with more than one unit.
        src_dst1 = UnitsContainer(meter=1), UnitsContainer(inch=1)
        src_dst2 = UnitsContainer(meter=1, second=-1), UnitsContainer(inch=1, minute=-1)
        for src, dst in (src_dst1, src_dst2):
            v = ureg.convert(1, src, dst),

            a = np.ones((3, 1))
            ac = np.ones((3, 1))

            r1 = ureg.convert(a, src, dst)
            np.testing.assert_allclose(r1, v * ac)
            self.assertIsNot(r1, a)

            r2 = ureg.convert(a, src, dst, inplace=True)
            np.testing.assert_allclose(r2, v * ac)
            self.assertIs(r2, a)
    def test_log_converter_inplace(self):
        arb_value = 3.14
        c = LogarithmicConverter(scale=1, logbase=10, logfactor=1)

        from_to = lambda value, inplace: c.from_reference(
            c.to_reference(value, inplace), inplace)

        to_from = lambda value, inplace: c.to_reference(
            c.from_reference(value, inplace), inplace)

        for fun, (inplace, comp) in itertools.product(
            (from_to, to_from),
            ((True, self.assertIs), (False, self.assertIsNot))):
            arb_array = arb_value * np.ones((1, 10))
            result = fun(arb_array, inplace)
            np.testing.assert_allclose(result, arb_array)
            comp(arb_array, result)
Exemple #17
0
    def test_issue37(self):
        x = np.ma.masked_array([1, 2, 3], mask=[True, True, False])
        q = ureg.meter * x
        assert isinstance(q, ureg.Quantity)
        np.testing.assert_array_equal(q.magnitude, x)
        assert q.units == ureg.meter.units
        q = x * ureg.meter
        assert isinstance(q, ureg.Quantity)
        np.testing.assert_array_equal(q.magnitude, x)
        assert q.units == ureg.meter.units

        m = np.ma.masked_array(2 * np.ones(3, 3))
        qq = q * m
        assert isinstance(qq, ureg.Quantity)
        np.testing.assert_array_equal(qq.magnitude, x * m)
        assert qq.units == ureg.meter.units
        qq = m * q
        assert isinstance(qq, ureg.Quantity)
        np.testing.assert_array_equal(qq.magnitude, x * m)
        assert qq.units == ureg.meter.units
Exemple #18
0
    def test_issue39(self):
        x = np.matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
        q = ureg.meter * x
        assert isinstance(q, ureg.Quantity)
        np.testing.assert_array_equal(q.magnitude, x)
        assert q.units == ureg.meter.units
        q = x * ureg.meter
        assert isinstance(q, ureg.Quantity)
        np.testing.assert_array_equal(q.magnitude, x)
        assert q.units == ureg.meter.units

        m = np.matrix(2 * np.ones(3, 3))
        qq = q * m
        assert isinstance(qq, ureg.Quantity)
        np.testing.assert_array_equal(qq.magnitude, x * m)
        assert qq.units == ureg.meter.units
        qq = m * q
        assert isinstance(qq, ureg.Quantity)
        np.testing.assert_array_equal(qq.magnitude, x * m)
        assert qq.units == ureg.meter.units
Exemple #19
0
    def test_issue39(self):
        x = np.matrix([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
        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)
Exemple #20
0
    def test_issue37(self):
        x = np.ma.masked_array([1, 2, 3], mask=[True, True, False])
        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)
Exemple #21
0
    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)
Exemple #22
0
    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)
Exemple #23
0
 def test_nparray(self):
     self._test_numeric(np.ones((1, 3)), self._test_inplace)