How to use the pint.compat.np function in Pint

To help you get started, we’ve selected a few Pint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hgrecco / pint / pint / testsuite / test_umath.py View on Github external
def test_cosh(self):
        self._test1(np.cosh, (np.arange(0, pi/2, pi/4) * self.ureg.dimensionless,
                              np.arange(0, pi/2, pi/4) * self.ureg.radian,
                              np.arange(0, pi/2, pi/4) * self.ureg.mm / self.ureg.m
                             ), (self.ureg.m, ), '', results=(None, None, np.cosh(np.arange(0, pi/2, pi/4)*0.001)))
        self._test1(np.cosh, (np.rad2deg(np.arange(0, pi/2, pi/4)) * self.ureg.degrees,
                             ), results=(np.cosh(np.arange(0, pi/2, pi/4)), ))
github pymedusa / Medusa / ext / pint / testsuite / test_numpy.py View on Github external
def test_nonzero(self):
        q = [1, 0, 5, 6, 0, 9] * self.ureg.m
        np.testing.assert_array_equal(q.nonzero()[0], [0, 2, 3, 5])
github pymedusa / Medusa / ext / pint / testsuite / test_numpy.py View on Github external
def test_gradient(self):
        """shape is a property not a function
        """
        l = np.gradient([[1,1],[3,4]] * self.ureg.J, 1 * self.ureg.m)
        self.assertQuantityEqual(l[0], [[2., 3.], [2., 3.]] * self.ureg.J / self.ureg.m)
        self.assertQuantityEqual(l[1], [[0., 0.], [1., 1.]] * self.ureg.J / self.ureg.m)
github pymedusa / Medusa / ext / pint / testsuite / test_numpy.py View on Github external
def test_ediff1d(self):
        """Units are erased by asanyarray, Quantity does not inherit from NDArray
        """
        self.assertQuantityEqual(np.ediff1d(self.q, 1 * self.ureg.J), [1, 1, 1] * self.ureg.J)
github hgrecco / pint / pint / testsuite / test_numpy_func.py View on Github external
def test_numpy_wrap(self):
        self.assertRaises(ValueError, numpy_wrap, "invalid", np.ones, [], {}, [])
        # TODO (#905 follow-up): test that NotImplemented is returned when upcast types
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
def test_inplace_multiplication(self, input_tuple, expected):
        self.ureg.autoconvert_offset_to_baseunit = False
        (q1v, q1u), (q2v, q2u) = input_tuple
        # update input tuple with new values to have correct values on failure
        input_tuple = (
            (np.array([q1v] * 2, dtype=np.float), q1u),
            (np.array([q2v] * 2, dtype=np.float), q2u),
        )
        Q_ = self.Q_
        qin1, qin2 = input_tuple
        q1, q2 = Q_(*qin1), Q_(*qin2)
        q1_cp = copy.copy(q1)
        if expected == "error":
            self.assertRaises(OffsetUnitCalculusError, op.imul, q1_cp, q2)
        else:
            expected = np.array([expected[0]] * 2, dtype=np.float), expected[1]
            self.assertEqual(op.imul(q1_cp, q2).units, Q_(*expected).units)
            q1_cp = copy.copy(q1)
            self.assertQuantityAlmostEqual(op.imul(q1_cp, q2), Q_(*expected), atol=0.01)
github hgrecco / pint / pint / testsuite / test_numpy.py View on Github external
def test_power(self):
        """This is not supported as different elements might end up with different units

        eg. ([1, 1] * m) ** [2, 3]

        Must force exponent to single value
        """
        self._test2(np.power, self.q1,
                    (self.qless, np.asarray([1., 2, 3, 4])),
                    (self.q2, ),)
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
def test_gt_zero_NP(self):
        ureg = self.ureg
        ureg.autoconvert_offset_to_baseunit = False
        qpos = ureg.Quantity(5, 'J')
        qneg = ureg.Quantity(-5, 'J')
        aeq = np.testing.assert_array_equal
        aeq(qpos > np.zeros(3), np.asarray([True, True, True]))
        aeq(qneg > np.zeros(3), np.asarray([False, False, False]))
        aeq(ureg.Quantity(np.arange(-1, 2), ureg.J) > np.zeros(3),
            np.asarray([False, False, True]))
        aeq(ureg.Quantity(np.arange(-1, 2), ureg.J) > np.zeros(3),
            np.asarray([False, False, True]))
        self.assertRaises(ValueError,
                          ureg.Quantity(np.arange(-1, 2), ureg.J).__gt__,
                          np.zeros(4))
github pymedusa / Medusa / ext / pint / testsuite / __init__.py View on Github external
def assertQuantityAlmostEqual(self, first, second, rtol=1e-07, atol=0, msg=None):
        if msg is None:
            msg = 'Comparing %r and %r. ' % (first, second)

        m1, m2 = self._get_comparable_magnitudes(first, second, msg)

        if isinstance(m1, ndarray) or isinstance(m2, ndarray):
            np.testing.assert_allclose(m1, m2, rtol=rtol, atol=atol, err_msg=msg)
        else:
            self.assertLessEqual(abs(m1 - m2), atol + rtol * abs(m2), msg=msg)
github hgrecco / pint / pint / testsuite / test_issues.py View on Github external
            lambda x: np.cos(x / x.units),  # Issue 399
            np.isfinite,  # Issue 481