How to use the pint.testsuite.helpers.requires_numpy 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_issues.py View on Github external
    @helpers.requires_numpy()
    def test_issue127(self):
        q = [1.0, 2.0, 3.0, 4.0] * self.ureg.meter
        q[0] = np.nan
        self.assertNotEqual(q[0], 1.0)
        self.assertTrue(math.isnan(q[0].magnitude))
        q[1] = float("NaN")
        self.assertNotEqual(q[1], 2.0)
        self.assertTrue(math.isnan(q[1].magnitude))
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
    @helpers.requires_numpy()
    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 / test_quantity.py View on Github external
    @helpers.requires_numpy()
    @ParameterizedTestCase.parameterize(("input", "expected_output"),
                                        divisions)
    def test_inplace_truedivision(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.itruediv, q1_cp, q2)
        else:
            expected = np.array([expected[0]]*2, dtype=np.float), expected[1]
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
    @helpers.requires_numpy()
    def test_imul_and_div_reduction(self):
        ureg = UnitRegistry(auto_reduce_dimensions=True, force_ndarray=True)
        mass = self._icalc_mass(ureg)
        self.assertEqual(mass.units, ureg.g)
        ureg = UnitRegistry(auto_reduce_dimensions=False, force_ndarray=True)
        mass = self._icalc_mass(ureg)
        self.assertEqual(mass.units, ureg.g / ureg.L * ureg.milliliter)
github hgrecco / pint / pint / testsuite / test_unit.py View on Github external
    @helpers.requires_numpy()
    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)
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
    @helpers.requires_numpy()
    def test_from_sequence(self):
        u_array_ref = self.Q_([200, 1000], 'g')
        u_array_ref_reversed = self.Q_([1000, 200], 'g')
        u_seq = [self.Q_('200g'), self.Q_('1kg')]
        u_seq_reversed = u_seq[::-1]

        u_array = self.Q_.from_sequence(u_seq)
        self.assertTrue(all(u_array == u_array_ref))

        u_array_2 = self.Q_.from_sequence(u_seq_reversed)
        self.assertTrue(all(u_array_2 == u_array_ref_reversed))
        self.assertFalse(u_array_2.u == u_array_ref_reversed.u)

        u_array_3 = self.Q_.from_sequence(u_seq_reversed, units='g')
        self.assertTrue(all(u_array_3 == u_array_ref_reversed))
        self.assertTrue(u_array_3.u == u_array_ref_reversed.u)
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
    @helpers.requires_numpy()
    @ParameterizedTestCase.parameterize(("input", "expected_output"),
                                        multiplications)
    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]
github hgrecco / pint / pint / testsuite / test_issues.py View on Github external
    @helpers.requires_numpy()
    def test_issue483(self):
        ureg = self.ureg
        a = np.asarray([1, 2, 3])
        q = [1, 2, 3] * ureg.dimensionless
        p = (q ** q).m
        np.testing.assert_array_equal(p, a ** a)
github hgrecco / pint / pint / testsuite / test_quantity.py View on Github external
    @helpers.requires_numpy()
    def test_quantity_array_format(self):
        x = self.Q_(
            np.array([1e-16, 1.0000001, 10000000.0, 1e12, np.nan, np.inf]),
            "kg * m ** 2",
        )
        for spec, result in (
            ("{}", str(x)),
            ("{.magnitude}", str(x.magnitude)),
            (
                "{:e}",
                "[1.000000e-16 1.000000e+00 1.000000e+07 1.000000e+12 nan inf] kilogram * meter ** 2",
            ),
            (
                "{:E}",
                "[1.000000E-16 1.000000E+00 1.000000E+07 1.000000E+12 NAN INF] kilogram * meter ** 2",
            ),
github pymedusa / Medusa / ext / pint / testsuite / test_quantity.py View on Github external
    @helpers.requires_numpy()
    @ParameterizedTestCase.parameterize(("input", "expected_output"),
                                        subtractions)
    def test_inplace_subtraction(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.isub, q1_cp, q2)
        else:
            expected = np.array([expected[0]]*2, dtype=np.float), expected[1]