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]))
def test_inplace_exponentiation(self, input_tuple, expected): self.ureg.default_as_delta = False in1, in2 = input_tuple if type(in1) is tuple and type(in2) is tuple: (q1v, q1u), (q2v, q2u) = in1, in2 in1 = self.Q_(*(np.array([q1v]*2, dtype=np.float), q1u)) in2 = self.Q_(q2v, q2u) elif not type(in1) is tuple and type(in2) is tuple: in2 = self.Q_(*in2) else: in1 = self.Q_(*in1) input_tuple = in1, in2 expected_copy = expected[:] for i, mode in enumerate([False, True]): self.ureg.autoconvert_offset_to_baseunit = mode in1_cp = copy.copy(in1) if expected_copy[i] == 'error': self.assertRaises((OffsetUnitCalculusError, DimensionalityError), op.ipow, in1_cp, in2) else: if type(expected_copy[i]) is tuple: expected = self.Q_(np.array([expected_copy[i][0]]*2, dtype=np.float), expected_copy[i][1]) self.assertEqual(op.ipow(in1_cp, in2).units, expected.units) else: expected = np.array([expected_copy[i]]*2, dtype=np.float) in1_cp = copy.copy(in1) self.assertQuantityAlmostEqual(op.ipow(in1_cp, in2), expected)
def test_where(self): helpers.assert_quantity_equal( np.where(self.q >= 2 * self.ureg.m, self.q, 20 * self.ureg.m), [[20, 2], [3, 4]] * self.ureg.m, ) helpers.assert_quantity_equal( np.where(self.q >= 2 * self.ureg.m, self.q, 0), [[0, 2], [3, 4]] * self.ureg.m, ) helpers.assert_quantity_equal( np.where(self.q >= 2 * self.ureg.m, self.q, np.nan), [[np.nan, 2], [3, 4]] * self.ureg.m, ) helpers.assert_quantity_equal( np.where(self.q >= 3 * self.ureg.m, 0, self.q), [[1, 2], [0, 0]] * self.ureg.m, ) helpers.assert_quantity_equal( np.where(self.q >= 3 * self.ureg.m, np.nan, self.q), [[1, 2], [np.nan, np.nan]] * self.ureg.m, ) helpers.assert_quantity_equal( np.where(self.q >= 2 * self.ureg.m, self.q, np.array(np.nan)), [[np.nan, 2], [3, 4]] * self.ureg.m, ) helpers.assert_quantity_equal( np.where(self.q >= 3 * self.ureg.m, np.array(np.nan), self.q), [[1, 2], [np.nan, np.nan]] * self.ureg.m, ) with pytest.raises(DimensionalityError): np.where( self.q < 2 * self.ureg.m, self.q, 0 * self.ureg.J, )
def test_comparisons(self): self.assertNDArrayEqual( self.q > 2 * self.ureg.m, np.array([[False, False], [True, True]]) ) self.assertNDArrayEqual( self.q < 2 * self.ureg.m, np.array([[True, False], [False, False]]) )
def test_issue973(self): """Verify that an empty array Quantity can be created through multiplication.""" q0 = np.array([]) * ureg.m # by Unit q1 = np.array([]) * ureg("m") # by Quantity assert isinstance(q0, ureg.Quantity) assert isinstance(q1, ureg.Quantity) assert len(q0) == len(q1) == 0
def test_isclose_numpy_func(self): q2 = [[1000.05, 2000], [3000.00007, 4001]] * self.ureg.mm self.assertNDArrayEqual(np.isclose(self.q, q2), np.array([[False, True], [True, False]])) self.assertNDArrayEqual( np.isclose(self.q, q2, atol=1e-5, rtol=1e-7), np.array([[False, True], [True, False]]), )
def test_issue94(self): ureg = UnitRegistry() v1 = np.array([5, 5]) * ureg.meter v2 = 0.1 * ureg.meter v3 = np.array([5, 5]) * ureg.meter v3 += v2 self.assertSequenceEqual((v1 + v2).magnitude, np.array([5.1, 5.1])) self.assertSequenceEqual(v3.magnitude, np.array([5, 5]))
def test_issue94(self): v1 = np.array([5, 5]) * ureg.meter v2 = 0.1 * ureg.meter v3 = np.array([5, 5]) * ureg.meter v3 += v2 np.testing.assert_array_equal((v1 + v2).magnitude, np.array([5.1, 5.1])) np.testing.assert_array_equal(v3.magnitude, np.array([5, 5]))
def test_issue94(self): ureg = UnitRegistry() v1 = np.array([5, 5]) * ureg.meter v2 = 0.1 * ureg.meter v3 = np.array([5, 5]) * ureg.meter v3 += v2 np.testing.assert_array_equal((v1 + v2).magnitude, np.array([5.1, 5.1])) np.testing.assert_array_equal(v3.magnitude, np.array([5, 5]))
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): self.assertQuantityEqual(ind_actual, ind_expected) self.assertQuantityEqual(np.atleast_2d(self.q), self.q)
def test_einsum(self): a = np.arange(25).reshape(5, 5) * self.ureg.m b = np.arange(5) * self.ureg.m self.assertQuantityEqual(np.einsum("ii", a), 60 * self.ureg.m) self.assertQuantityEqual(np.einsum("ii->i", a), np.array([0, 6, 12, 18, 24]) * self.ureg.m) self.assertQuantityEqual(np.einsum("i,i", b, b), 30 * self.ureg.m**2) self.assertQuantityEqual( np.einsum("ij,j", a, b), np.array([30, 80, 130, 180, 230]) * self.ureg.m**2, )
def test_atleast_3d(self): actual = np.atleast_3d(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_3d(self.q), np.array([[[1], [2]], [[3], [4]]]) * self.ureg.m )
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"))
def test_isnan_nat(): a = np.array(["2000-01-01", "NaT"], dtype="M8") b = np.array(["2000-01-01", "2000-01-02"], dtype="M8") assert isnan(a, True) assert not isnan(b, True) np.testing.assert_equal(isnan(a, False), np.array([False, True])) np.testing.assert_equal(isnan(b, False), np.array([False, False])) # Scalar numpy.datetime64 assert not isnan(a[0], True) assert not isnan(a[0], False) assert isnan(a[1], True) assert isnan(a[1], False)
def test_convert_to_consistent_units_with_pre_calc_units(self): args, kwargs = convert_to_consistent_units( self.Q_(50, "cm"), np.arange(4).reshape(2, 2) * self.ureg.m, [0.042] * self.ureg.km, (self.Q_(0, "m"), self.Q_(1, "dam")), a=6378 * self.ureg.km, pre_calc_units=self.ureg.meter, ) assert args[0] == 0.5 self.assertNDArrayEqual(args[1], np.array([[0, 1], [2, 3]])) self.assertNDArrayEqual(args[2], np.array([42])) assert args[3][0] == 0 assert args[3][1] == 10 assert kwargs["a"] == 6.378e6
def test_addition_with_incompatible_scalar(self): a = np.array([0, 1, 2]) b = 1.0 * self.ureg.m with pytest.raises(DimensionalityError): op.add(a, b) with pytest.raises(DimensionalityError): op.add(b, a)
def test_addition_with_scalar(self): a = np.array([0, 1, 2]) b = 10.0 * self.ureg("gram/kilogram") self.assertQuantityAlmostEqual( a + b, self.Q_([0.01, 1.01, 2.01], self.ureg.dimensionless)) self.assertQuantityAlmostEqual( b + a, self.Q_([0.01, 1.01, 2.01], self.ureg.dimensionless))
def test_full_like(self): self.assertQuantityEqual( np.full_like(self.q, self.Q_(0, self.ureg.degC)), self.Q_([[0, 0], [0, 0]], self.ureg.degC), ) self.assertNDArrayEqual(np.full_like(self.q, 2), np.array([[2, 2], [2, 2]]))
def test_inplace_multiplication_with_autoconvert(self, input_tuple, expected): self.ureg.autoconvert_offset_to_baseunit = True (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_copyto(self): a = self.q.m q = copy.copy(self.q) np.copyto(q, 2 * q, where=[True, False]) self.assertQuantityEqual(q, self.Q_([[2, 2], [6, 4]], "m")) np.copyto(q, 0, where=[[False, False], [True, False]]) self.assertQuantityEqual(q, self.Q_([[2, 2], [0, 4]], "m")) np.copyto(a, q) self.assertNDArrayEqual(a, np.array([[2, 2], [0, 4]]))
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] self.assertEqual(op.itruediv(q1_cp, q2).units, Q_(*expected).units) q1_cp = copy.copy(q1) self.assertQuantityAlmostEqual(op.itruediv(q1_cp, q2), Q_(*expected), atol=0.01)
def test_convert_to_consistent_units_without_pre_calc_units(self): args, kwargs = convert_to_consistent_units( (self.Q_(0), self.Q_(10, "degC")), [1, 2, 3, 5, 7] * self.ureg.m, pre_calc_units=None, ) assert args[0][0] == 0 assert args[0][1] == 10 self.assertNDArrayEqual(args[1], np.array([1, 2, 3, 5, 7])) assert kwargs == {}
def test_isin(self): self.assertNDArrayEqual( np.isin(self.q, self.Q_([0, 2, 4], "m")), np.array([[False, True], [False, True]]), ) self.assertNDArrayEqual( np.isin(self.q, self.Q_([0, 2, 4], "J")), np.array([[False, False], [False, False]]), ) self.assertNDArrayEqual( np.isin(self.q, [self.Q_(2, "m"), self.Q_(4, "J")]), np.array([[False, True], [False, False]]), ) self.assertNDArrayEqual(np.isin(self.q, self.q.m), np.array([[False, False], [False, False]])) self.assertNDArrayEqual( np.isin(self.q / self.ureg.cm, [1, 3]), np.array([[True, False], [True, False]]), ) self.assertRaises(ValueError, np.isin, self.q.m, self.q)
def test_is_sequence_with_quantity_elements(self): assert _is_sequence_with_quantity_elements( (self.Q_(0, "m"), self.Q_(32.0, "degF"))) assert _is_sequence_with_quantity_elements(np.arange(4) * self.ureg.m) assert _is_sequence_with_quantity_elements((self.Q_(0), 0)) assert _is_sequence_with_quantity_elements((0, self.Q_(0))) assert not _is_sequence_with_quantity_elements([1, 3, 5]) assert not _is_sequence_with_quantity_elements(9 * self.ureg.m) assert not _is_sequence_with_quantity_elements(np.arange(4)) assert not _is_sequence_with_quantity_elements("0123") assert not _is_sequence_with_quantity_elements([]) assert not _is_sequence_with_quantity_elements(np.array([]))
def test_isnan_numpy(): assert isnan(np.nan, True) assert isnan(np.nan, False) assert not isnan(np.array([0, 0]), True) assert isnan(np.array([0, np.nan]), True) assert not isnan(np.array(["A", "B"]), True) np.testing.assert_equal( isnan(np.array([1, np.nan]), False), np.array([False, True]) ) np.testing.assert_equal( isnan(np.array(["A", "B"]), False), np.array([False, False]) )
def test_is_sequence_with_quantity_elements(self): self.assertTrue( _is_sequence_with_quantity_elements( (self.Q_(0, "m"), self.Q_(32.0, "degF")))) self.assertTrue( _is_sequence_with_quantity_elements(np.arange(4) * self.ureg.m)) self.assertFalse( _is_sequence_with_quantity_elements((self.Q_(0), True))) self.assertFalse(_is_sequence_with_quantity_elements([1, 3, 5])) self.assertFalse(_is_sequence_with_quantity_elements(9 * self.ureg.m)) self.assertFalse(_is_sequence_with_quantity_elements(np.arange(4))) self.assertFalse(_is_sequence_with_quantity_elements("0123")) self.assertFalse(_is_sequence_with_quantity_elements([])) self.assertFalse(_is_sequence_with_quantity_elements(np.array([])))
def test_exponentiation_array_exp_2(self): arr = np.array(range(3), dtype=np.float) #q = self.Q_(copy.copy(arr), None) q = self.Q_(copy.copy(arr), 'meter') arr_cp = copy.copy(arr) q_cp = copy.copy(q) # this fails as expected since numpy 1.8.0 but... self.assertRaises(DimensionalityError, op.pow, arr_cp, q_cp) # ..not for op.ipow ! # q_cp is treated as if it is an array. The units are ignored. # _Quantity.__ipow__ is never called arr_cp = copy.copy(arr) q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op.ipow, arr_cp, q_cp)
def test_exponentiation_array_exp(self): arr = np.array(range(3), dtype=np.float) q = self.Q_(arr, None) for op_ in [op.pow, op.ipow]: q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, 2., q_cp) arr_cp = copy.copy(arr) arr_cp = copy.copy(arr) q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, q_cp, arr_cp) q_cp = copy.copy(q) q2_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, q_cp, q2_cp)
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) self.assertRaises(DimensionalityError, op_, 2.0, q_cp) arr_cp = copy.copy(arr) arr_cp = copy.copy(arr) q_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, q_cp, arr_cp) q_cp = copy.copy(q) q2_cp = copy.copy(q) self.assertRaises(DimensionalityError, op_, q_cp, q2_cp)
def test_where(self): self.assertQuantityEqual( np.where(self.q >= 2 * self.ureg.m, self.q, 20 * self.ureg.m), [[20, 2], [3, 4]] * self.ureg.m, ) self.assertQuantityEqual( np.where(self.q >= 2 * self.ureg.m, self.q, 0), [[0, 2], [3, 4]] * self.ureg.m, ) self.assertQuantityEqual( np.where(self.q >= 2 * self.ureg.m, self.q, np.nan), [[np.nan, 2], [3, 4]] * self.ureg.m, ) self.assertQuantityEqual( np.where(self.q >= 3 * self.ureg.m, 0, self.q), [[1, 2], [0, 0]] * self.ureg.m, ) self.assertQuantityEqual( np.where(self.q >= 3 * self.ureg.m, np.nan, self.q), [[1, 2], [np.nan, np.nan]] * self.ureg.m, ) self.assertQuantityEqual( np.where(self.q >= 2 * self.ureg.m, self.q, np.array(np.nan)), [[np.nan, 2], [3, 4]] * self.ureg.m, ) self.assertQuantityEqual( np.where(self.q >= 3 * self.ureg.m, np.array(np.nan), self.q), [[1, 2], [np.nan, np.nan]] * self.ureg.m, ) self.assertRaises( DimensionalityError, np.where, self.q < 2 * self.ureg.m, self.q, 0 * self.ureg.J, )
def test_zero_or_nan_numpy(): assert zero_or_nan(np.nan, True) assert zero_or_nan(np.nan, False) assert zero_or_nan(np.array([0, np.nan]), True) assert not zero_or_nan(np.array([1, np.nan]), True) assert not zero_or_nan(np.array([0, 1]), True) assert not zero_or_nan(np.array(["A", "B"]), True) np.testing.assert_equal( zero_or_nan(np.array([0, 1, np.nan]), False), np.array([True, False, True]) )
def test_interp_numpy_func(self): x = [1, 4] * self.ureg.m xp = np.linspace(0, 3, 5) * self.ureg.m fp = self.Q_([0, 5, 10, 15, 20], self.ureg.degC) self.assertQuantityAlmostEqual( np.interp(x, xp, fp), self.Q_([6.66667, 20.0], self.ureg.degC), rtol=1e-5 ) x_ = np.array([1, 4]) xp_ = np.linspace(0, 3, 5) fp_ = [0, 5, 10, 15, 20] self.assertQuantityAlmostEqual( np.interp(x_, xp_, fp), self.Q_([6.6667, 20.0], self.ureg.degC), rtol=1e-5 ) self.assertQuantityAlmostEqual(np.interp(x, xp, fp_), [6.6667, 20.0], rtol=1e-5)
def test_nanargmin_numpy_func(self): self.assertNDArrayEqual(np.nanargmin(self.q_nan, axis=0), np.array([0, 0]))
def test_tile(self): self.assertQuantityEqual( np.tile(self.q, 2), np.array([[1, 2, 1, 2], [3, 4, 3, 4]]) * self.ureg.m)