Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)), ))
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])
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)
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)
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
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)
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, ),)
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))
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)
lambda x: np.cos(x / x.units), # Issue 399
np.isfinite, # Issue 481