def test_setitem(self): self.assertRaises(ValueError, self.q.__setitem__, (0,0), 1) self.assertRaises(ValueError, self.q.__setitem__, (0,0), 1*self.ureg.J) self.assertRaises(ValueError, self.q.__setitem__, 0, 1) self.assertRaises(ValueError, self.q.__setitem__, 0, np.ndarray([1, 2])) self.assertRaises(ValueError, self.q.__setitem__, 0, 1*self.ureg.J) q = self.q.copy() q[0] = 1*self.ureg.m self.assertQuantityEqual(q, [[1,1],[3,4]]*self.ureg.m) q = self.q.copy() q.__setitem__(Ellipsis, 1*self.ureg.m) self.assertQuantityEqual(q, [[1,1],[1,1]]*self.ureg.m) q = self.q.copy() q[:] = 1*self.ureg.m self.assertQuantityEqual(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 self.assertQuantityEqual(q, np.asarray([1,1,2,3])) q[0] = self.ureg.m/self.ureg.mm self.assertQuantityEqual(q, np.asarray([1000, 1,2,3])) q = [0.,1.,2.,3.] * self.ureg.m / self.ureg.mm q[0] = 1. self.assertQuantityEqual(q, [0.001,1,2,3]*self.ureg.m / self.ureg.mm)
def test_issue75(self): ureg = UnitRegistry() v1 = np.asarray([1., 2., 3.]) v2 = np.asarray([3., 2., 1.]) q1 = v1 * ureg.ms q2 = v2 * ureg.ms np.testing.assert_array_equal(q1 == q2, v1 == v2) np.testing.assert_array_equal(q1 != q2, v1 != v2) q2s = np.asarray([0.003, 0.002, 0.001]) * ureg.s v2s = q2s.to('ms').magnitude np.testing.assert_array_equal(q1 == q2s, v1 == v2s) np.testing.assert_array_equal(q1 != q2s, v1 != v2s)
def test_issue75(self): ureg = UnitRegistry() v1 = np.asarray([1, 2, 3]) v2 = np.asarray([3, 2, 1]) q1 = v1 * ureg.ms q2 = v2 * ureg.ms self.assertSequenceEqual(q1 == q2, v1 == v2) self.assertSequenceEqual(q1 != q2, v1 != v2) q2s = np.asarray([0.003, 0.002, 0.001]) * ureg.s v2s = q2s.to('ms').magnitude self.assertSequenceEqual(q1 == q2s, v1 == v2s) self.assertSequenceEqual(q1 != q2s, v1 != v2s)
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_issue74(self): ureg = UnitRegistry() v1 = np.asarray([1.0, 2.0, 3.0]) v2 = np.asarray([3.0, 2.0, 1.0]) q1 = v1 * ureg.ms q2 = v2 * ureg.ms np.testing.assert_array_equal(q1 < q2, v1 < v2) np.testing.assert_array_equal(q1 > q2, v1 > v2) np.testing.assert_array_equal(q1 <= q2, v1 <= v2) np.testing.assert_array_equal(q1 >= q2, v1 >= v2) q2s = np.asarray([0.003, 0.002, 0.001]) * ureg.s v2s = q2s.to("ms").magnitude np.testing.assert_array_equal(q1 < q2s, v1 < v2s) np.testing.assert_array_equal(q1 > q2s, v1 > v2s) np.testing.assert_array_equal(q1 <= q2s, v1 <= v2s) np.testing.assert_array_equal(q1 >= q2s, v1 >= v2s)
def qi(self): return np.asarray([1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j]) * self.ureg.m
def qm(self): return np.asarray([1., 2., 3., 4.]) * self.ureg.m
def qless(self): return np.asarray([1, 2, 3, 4], dtype=np.uint8) * self.ureg.dimensionless
def test_ndarray_downcast_with_dtype(self): with self.assertWarns(UnitStrippedWarning): qarr = np.asarray(self.q, dtype=np.float64) self.assertEqual(qarr.dtype, np.float64)
def test_ndarray_downcast(self): with self.assertWarns(UnitStrippedWarning): np.asarray(self.q)
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)
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 self.assertQuantityEqual(q1.T, q2)
def test_ndarray_downcast_with_dtype(self): with pytest.warns(UnitStrippedWarning): qarr = np.asarray(self.q, dtype=np.float64) assert qarr.dtype == np.float64
def test_issue171_T(self): a = np.asarray([[1., 2., 3., 4.],[4., 3., 2., 1.]]) q1 = a * self.ureg.meter q2 = a.T * self.ureg.meter self.assertQuantityEqual(q1.T, q2)
def qm(self): return np.asarray([1, 2, 3, 4], dtype=np.uint8) * self.ureg.m
def qless(self): return np.asarray([1., 2., 3., 4.]) * self.ureg.dimensionless