Пример #1
0
 def test_compress(self):
     helpers.assert_quantity_equal(
         self.q.compress([False, True], axis=0), [[3, 4]] * self.ureg.m
     )
     helpers.assert_quantity_equal(
         self.q.compress([False, True], axis=1), [[2], [4]] * self.ureg.m
     )
Пример #2
0
 def test_unwrap(self):
     helpers.assert_quantity_equal(
         np.unwrap([0, 3 * np.pi] * self.ureg.radians), [0, np.pi]
     )
     helpers.assert_quantity_equal(
         np.unwrap([0, 540] * self.ureg.deg), [0, 180] * self.ureg.deg
     )
Пример #3
0
    def test_block_column_stack(self, subtests):
        for func in (np.block, np.column_stack):
            with subtests.test(func=func):

                helpers.assert_quantity_equal(
                    func([self.q[:, 0], self.q[:, 1]]),
                    self.Q_(func([self.q[:, 0].m, self.q[:, 1].m]), self.ureg.m),
                )

                # One or more of the args is a bare array full of zeros or NaNs
                helpers.assert_quantity_equal(
                    func(
                        [
                            self.q_zero_or_nan[:, 0].m,
                            self.q[:, 0],
                            self.q_zero_or_nan[:, 1].m,
                        ]
                    ),
                    self.Q_(
                        func(
                            [
                                self.q_zero_or_nan[:, 0].m,
                                self.q[:, 0].m,
                                self.q_zero_or_nan[:, 1].m,
                            ]
                        ),
                        self.ureg.m,
                    ),
                )
                # One or more of the args is a bare array with at least one non-zero,
                # non-NaN element
                nz = self.q_zero_or_nan
                nz.m[0, 0] = 1
                with pytest.raises(DimensionalityError):
                    func([nz[:, 0].m, self.q[:, 0]])
Пример #4
0
    def _test_quantity_divmod_one(self, a, b):
        if isinstance(a, str):
            a = self.Q_(a)
        if isinstance(b, str):
            b = self.Q_(b)

        q, r = divmod(a, b)
        assert q == a // b
        assert r == a % b
        helpers.assert_quantity_equal(a, (q * b) + r)
        assert q == math.floor(q)
        if b > (0 * b):
            assert (0 * b) <= r < b
        else:
            assert (0 * b) >= r > b
        if isinstance(a, self.Q_):
            assert r.units == a.units
        else:
            assert r.unitless
        assert q.unitless

        copy_a = copy.copy(a)
        a %= b
        assert a == r
        copy_a //= b
        assert copy_a == q
Пример #5
0
    def test_power(self):
        arr = np.array(range(3), dtype=np.float)
        q = self.Q_(arr, "meter")

        for op_ in [op.pow, op.ipow, np.power]:
            q_cp = copy.copy(q)
            with pytest.raises(DimensionalityError):
                op_(2.0, q_cp)
            arr_cp = copy.copy(arr)
            arr_cp = copy.copy(arr)
            q_cp = copy.copy(q)
            with pytest.raises(DimensionalityError):
                op_(q_cp, arr_cp)
            q_cp = copy.copy(q)
            q2_cp = copy.copy(q)
            with pytest.raises(DimensionalityError):
                op_(q_cp, q2_cp)

        helpers.assert_quantity_equal(
            np.power(self.q, self.Q_(2)), self.Q_([[1, 4], [9, 16]], "m**2")
        )
        helpers.assert_quantity_equal(
            self.q ** self.Q_(2), self.Q_([[1, 4], [9, 16]], "m**2")
        )
        self.assertNDArrayEqual(arr ** self.Q_(2), np.array([0, 1, 4]))
Пример #6
0
 def test_round_numpy_func(self):
     helpers.assert_quantity_equal(
         np.around(1.0275 * self.ureg.m, decimals=2), 1.03 * self.ureg.m
     )
     helpers.assert_quantity_equal(
         np.round_(1.0275 * self.ureg.m, decimals=2), 1.03 * self.ureg.m
     )
Пример #7
0
 def test_copyto(self):
     a = self.q.m
     q = copy.copy(self.q)
     np.copyto(q, 2 * q, where=[True, False])
     helpers.assert_quantity_equal(q, self.Q_([[2, 2], [6, 4]], "m"))
     np.copyto(q, 0, where=[[False, False], [True, False]])
     helpers.assert_quantity_equal(q, self.Q_([[2, 2], [0, 4]], "m"))
     np.copyto(a, q)
     self.assertNDArrayEqual(a, np.array([[2, 2], [0, 4]]))
Пример #8
0
 def test_atleast_2d(self):
     actual = np.atleast_2d(self.Q_(0, self.ureg.degC), self.q.flatten())
     expected = (
         self.Q_(np.array([[0]]), self.ureg.degC),
         np.array([[1, 2, 3, 4]]) * self.ureg.m,
     )
     for ind_actual, ind_expected in zip(actual, expected):
         helpers.assert_quantity_equal(ind_actual, ind_expected)
     helpers.assert_quantity_equal(np.atleast_2d(self.q), self.q)
    def test_unwrap_and_wrap_constistent_units(self):
        (a, ), output_wrap_a = unwrap_and_wrap_consistent_units([2, 4, 8] *
                                                                self.ureg.m)
        (b, c), output_wrap_c = unwrap_and_wrap_consistent_units(
            np.arange(4), self.Q_(1, "g/kg"))

        self.assertNDArrayEqual(a, np.array([2, 4, 8]))
        self.assertNDArrayEqual(b, np.array([0, 1000, 2000, 3000]))
        assert c == 1

        helpers.assert_quantity_equal(output_wrap_a(0), 0 * self.ureg.m)
        helpers.assert_quantity_equal(output_wrap_c(0), self.Q_(0, "g/kg"))
Пример #10
0
 def test_reversible_op(self):
     """ """
     x = self.q.magnitude
     u = self.Q_(np.ones(x.shape))
     helpers.assert_quantity_equal(x / self.q, u * x / self.q)
     helpers.assert_quantity_equal(x * self.q, u * x * self.q)
     helpers.assert_quantity_equal(x + u, u + x)
     helpers.assert_quantity_equal(x - u, -(u - x))
Пример #11
0
 def test_concat_stack(self, subtests):
     for func in (np.concatenate, np.stack, np.hstack, np.vstack, np.dstack):
         with subtests.test(func=func):
             helpers.assert_quantity_equal(
                 func([self.q] * 2), self.Q_(func([self.q.m] * 2), self.ureg.m)
             )
             # One or more of the args is a bare array full of zeros or NaNs
             helpers.assert_quantity_equal(
                 func([self.q_zero_or_nan.m, self.q]),
                 self.Q_(func([self.q_zero_or_nan.m, self.q.m]), self.ureg.m),
             )
             # One or more of the args is a bare array with at least one non-zero,
             # non-NaN element
             nz = self.q_zero_or_nan
             nz.m[0, 0] = 1
             with pytest.raises(DimensionalityError):
                 func([nz.m, self.q])
Пример #12
0
 def test_einsum(self):
     a = np.arange(25).reshape(5, 5) * self.ureg.m
     b = np.arange(5) * self.ureg.m
     helpers.assert_quantity_equal(np.einsum("ii", a), 60 * self.ureg.m)
     helpers.assert_quantity_equal(
         np.einsum("ii->i", a), np.array([0, 6, 12, 18, 24]) * self.ureg.m
     )
     helpers.assert_quantity_equal(np.einsum("i,i", b, b), 30 * self.ureg.m ** 2)
     helpers.assert_quantity_equal(
         np.einsum("ij,j", a, b),
         np.array([30, 80, 130, 180, 230]) * self.ureg.m ** 2,
     )
Пример #13
0
    def test_prod_numpy_func(self):
        axis = 0
        where = [[True, False], [True, True]]

        helpers.assert_quantity_equal(np.prod(self.q), 24 * self.ureg.m ** 4)
        helpers.assert_quantity_equal(
            np.prod(self.q, axis=axis), [3, 8] * self.ureg.m ** 2
        )
        helpers.assert_quantity_equal(
            np.prod(self.q, where=where), 12 * self.ureg.m ** 3
        )

        with pytest.raises(DimensionalityError):
            np.prod(self.q, axis=axis, where=where)
        helpers.assert_quantity_equal(
            np.prod(self.q, axis=axis, where=[[True, False], [False, True]]),
            [1, 4] * self.ureg.m,
        )
        helpers.assert_quantity_equal(
            np.prod(self.q, axis=axis, where=[True, False]), [3, 1] * self.ureg.m ** 2
        )
Пример #14
0
    def test_prod(self):
        axis = 0
        where = [[True, False], [True, True]]

        helpers.assert_quantity_equal(self.q.prod(), 24 * self.ureg.m ** 4)
        helpers.assert_quantity_equal(self.q.prod(axis=axis), [3, 8] * self.ureg.m ** 2)
        helpers.assert_quantity_equal(self.q.prod(where=where), 12 * self.ureg.m ** 3)
Пример #15
0
 def test_fix(self):
     helpers.assert_quantity_equal(np.fix(3.14 * self.ureg.m), 3.0 * self.ureg.m)
     helpers.assert_quantity_equal(np.fix(3.0 * self.ureg.m), 3.0 * self.ureg.m)
     helpers.assert_quantity_equal(
         np.fix([2.1, 2.9, -2.1, -2.9] * self.ureg.m),
         [2.0, 2.0, -2.0, -2.0] * self.ureg.m,
     )
Пример #16
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"))
Пример #17
0
    def test_gradient(self):
        grad = np.gradient([[1, 1], [3, 4]] * self.ureg.m, 1 * self.ureg.J)
        helpers.assert_quantity_equal(
            grad[0], [[2.0, 3.0], [2.0, 3.0]] * self.ureg.m / self.ureg.J
        )
        helpers.assert_quantity_equal(
            grad[1], [[0.0, 0.0], [1.0, 1.0]] * self.ureg.m / self.ureg.J
        )

        grad = np.gradient(self.Q_([[1, 1], [3, 4]], self.ureg.degC), 1 * self.ureg.J)
        helpers.assert_quantity_equal(
            grad[0], [[2.0, 3.0], [2.0, 3.0]] * self.ureg.delta_degC / self.ureg.J
        )
        helpers.assert_quantity_equal(
            grad[1], [[0.0, 0.0], [1.0, 1.0]] * self.ureg.delta_degC / self.ureg.J
        )
Пример #18
0
 def test_cumprod_numpy_func(self):
     with pytest.raises(DimensionalityError):
         np.cumprod(self.q)
     with pytest.raises(DimensionalityError):
         np.cumproduct(self.q)
     helpers.assert_quantity_equal(np.cumprod(self.q / self.ureg.m), [1, 2, 6, 24])
     helpers.assert_quantity_equal(
         np.cumproduct(self.q / self.ureg.m), [1, 2, 6, 24]
     )
     helpers.assert_quantity_equal(
         np.cumprod(self.q / self.ureg.m, axis=1), [[1, 2], [3, 12]]
     )
Пример #19
0
 def test_clip(self):
     helpers.assert_quantity_equal(
         self.q.clip(max=2 * self.ureg.m), [[1, 2], [2, 2]] * self.ureg.m
     )
     helpers.assert_quantity_equal(
         self.q.clip(min=3 * self.ureg.m), [[3, 3], [3, 4]] * self.ureg.m
     )
     helpers.assert_quantity_equal(
         self.q.clip(min=2 * self.ureg.m, max=3 * self.ureg.m),
         [[2, 2], [3, 3]] * self.ureg.m,
     )
     with pytest.raises(DimensionalityError):
         self.q.clip(self.ureg.J)
     with pytest.raises(DimensionalityError):
         self.q.clip(1)
Пример #20
0
    def test_put(self):
        q = [1.0, 2.0, 3.0, 4.0] * self.ureg.m
        q.put([0, 2], [10.0, 20.0] * self.ureg.m)
        helpers.assert_quantity_equal(q, [10.0, 2.0, 20.0, 4.0] * self.ureg.m)

        q = [1.0, 2.0, 3.0, 4.0] * self.ureg.m
        q.put([0, 2], [1.0, 2.0] * self.ureg.mm)
        helpers.assert_quantity_equal(q, [0.001, 2.0, 0.002, 4.0] * self.ureg.m)

        q = [1.0, 2.0, 3.0, 4.0] * self.ureg.m / self.ureg.mm
        q.put([0, 2], [1.0, 2.0])
        helpers.assert_quantity_equal(
            q, [0.001, 2.0, 0.002, 4.0] * self.ureg.m / self.ureg.mm
        )

        q = [1.0, 2.0, 3.0, 4.0] * self.ureg.m
        with pytest.raises(DimensionalityError):
            q.put([0, 2], [4.0, 6.0] * self.ureg.J)
        with pytest.raises(DimensionalityError):
            q.put([0, 2], [4.0, 6.0])
Пример #21
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_)

        helpers.assert_quantity_equal(u, u)
        helpers.assert_quantity_equal(u == u, u.magnitude == u.magnitude)
        helpers.assert_quantity_equal(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)
Пример #22
0
 def test_issue252(self):
     ur = UnitRegistry()
     q = ur("3 F")
     t = copy.deepcopy(q)
     u = t.to(ur.mF)
     helpers.assert_quantity_equal(q.to(ur.mF), u)
Пример #23
0
 def test_issue171_T(self):
     a = np.asarray([[1.0, 2.0, 3.0, 4.0], [4.0, 3.0, 2.0, 1.0]])
     q1 = a * self.ureg.meter
     q2 = a.T * self.ureg.meter
     helpers.assert_quantity_equal(q1.T, q2)
Пример #24
0
 def test_issue171_real_imag(self):
     qr = [1.0, 2.0, 3.0, 4.0] * self.ureg.meter
     qi = [4.0, 3.0, 2.0, 1.0] * self.ureg.meter
     q = qr + 1j * qi
     helpers.assert_quantity_equal(q.real, qr)
     helpers.assert_quantity_equal(q.imag, qi)
Пример #25
0
 def test_nan_to_num_numpy_func(self):
     helpers.assert_quantity_equal(
         np.nan_to_num(self.q_nan, nan=-999 * self.ureg.mm),
         [[1, 2], [3, -0.999]] * self.ureg.m,
     )
Пример #26
0
 def test_trim_zeros_numpy_func(self):
     q = [0, 4, 3, 0, 2, 2, 0, 0, 0] * self.ureg.m
     helpers.assert_quantity_equal(np.trim_zeros(q), [4, 3, 0, 2, 2] * self.ureg.m)
Пример #27
0
 def test_flatten(self):
     helpers.assert_quantity_equal(self.q.flatten(), [1, 2, 3, 4] * self.ureg.m)
Пример #28
0
    def test_setitem(self):
        with pytest.raises(TypeError):
            self.q[0, 0] = 1
        with pytest.raises(DimensionalityError):
            self.q[0, 0] = 1 * self.ureg.J
        with pytest.raises(DimensionalityError):
            self.q[0] = 1
        with pytest.raises(DimensionalityError):
            self.q[0] = np.ndarray([1, 2])
        with pytest.raises(DimensionalityError):
            self.q[0] = 1 * self.ureg.J

        q = self.q.copy()
        q[0] = 1 * self.ureg.m
        helpers.assert_quantity_equal(q, [[1, 1], [3, 4]] * self.ureg.m)

        q = self.q.copy()
        q[...] = 1 * self.ureg.m
        helpers.assert_quantity_equal(q, [[1, 1], [1, 1]] * self.ureg.m)

        q = self.q.copy()
        q[:] = 1 * self.ureg.m
        helpers.assert_quantity_equal(q, [[1, 1], [1, 1]] * self.ureg.m)

        # check and see that dimensionless num  bers work correctly
        q = [0, 1, 2, 3] * self.ureg.dimensionless
        q[0] = 1
        helpers.assert_quantity_equal(q, np.asarray([1, 1, 2, 3]))
        q[0] = self.ureg.m / self.ureg.mm
        helpers.assert_quantity_equal(q, np.asarray([1000, 1, 2, 3]))

        q = [0.0, 1.0, 2.0, 3.0] * self.ureg.m / self.ureg.mm
        q[0] = 1.0
        helpers.assert_quantity_equal(q, [0.001, 1, 2, 3] * self.ureg.m / self.ureg.mm)

        # Check that this properly masks the first item without warning
        q = self.ureg.Quantity(
            np.ma.array([0.0, 1.0, 2.0, 3.0], mask=[False, True, False, False]), "m"
        )
        with warnings.catch_warnings(record=True) as w:
            q[0] = np.ma.masked
            # Check for no warnings
            assert not w
            assert q.mask[0]
Пример #29
0
 def test_reshape(self):
     helpers.assert_quantity_equal(
         self.q.reshape([1, 4]), [[1, 2, 3, 4]] * self.ureg.m
     )
Пример #30
0
 def test_copy_numpy_func(self):
     q_copy = np.copy(self.q)
     helpers.assert_quantity_equal(self.q, q_copy)
     assert self.q is not q_copy