def test_is_quantity_sequence(self): self.assertTrue( _is_quantity_sequence((self.Q_(0, "m"), self.Q_(32.0, "degF")))) self.assertTrue(_is_quantity_sequence(np.arange(4) * self.ureg.m)) self.assertFalse(_is_quantity_sequence((self.Q_(0), True))) self.assertFalse(_is_quantity_sequence([1, 3, 5])) self.assertFalse(_is_quantity_sequence(9 * self.ureg.m)) self.assertFalse(_is_quantity_sequence(np.arange(4))) self.assertFalse(_is_quantity_sequence("0123"))
def test_rad2deg(self): self._test1( np.rad2deg, (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, ), 'degree', results=(None, None, np.rad2deg(np.arange(0, pi / 2, pi / 4) * 0.001)))
def test_arccosh(self): self._test1( np.arccosh, ( np.arange(1.0, 1.9, 0.1) * self.ureg.dimensionless, np.arange(1.0, 1.9, 0.1) * self.ureg.m / self.ureg.m, ), (1 * self.ureg.m, ), "radian", )
def test_arctanh(self): self._test1( np.arctanh, ( np.arange(0, 0.9, 0.1) * self.ureg.dimensionless, np.arange(0, 0.9, 0.1) * self.ureg.m / self.ureg.m, ), (0.1 * self.ureg.m, ), "radian", )
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_rad2deg(self): self._test1(np.rad2deg, (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, ), 'degree', results=(None, None, np.rad2deg(np.arange(0, pi/2, pi/4)*0.001) * self.ureg.degree, ))
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_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_deg2rad(self): self._test1( np.deg2rad, (np.arange(0, pi / 2, pi / 4) * self.ureg.degrees, ), (1 * self.ureg.m, ), "radians", )
def test_convert_to_consistent_units_with_dimensionality_error(self): with pytest.raises(DimensionalityError): convert_to_consistent_units( self.Q_(32.0, "degF"), pre_calc_units=self.ureg.meter, ) with pytest.raises(DimensionalityError): convert_to_consistent_units( np.arange(4), pre_calc_units=self.ureg.meter, )
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_tanh(self): self._test1(np.tanh, (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 ), (1*self.ureg.m, ), '', results=(None, None, np.tanh(np.arange(0, pi/2, pi/4)*0.001))) self._test1(np.tanh, (np.rad2deg(np.arange(0, pi/2, pi/4)) * self.ureg.degrees, ), results=(np.tanh(np.arange(0, pi/2, pi/4)), ))
def test_tanh(self): self._test1(np.tanh, (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.tanh(np.arange(0, pi/2, pi/4)*0.001))) self._test1(np.tanh, (np.rad2deg(np.arange(0, pi/2, pi/4)) * self.ureg.degrees, ), results=(np.tanh(np.arange(0, pi/2, pi/4)), ))
def test_convert_to_consistent_units_with_dimensionality_error(self): self.assertRaises( DimensionalityError, convert_to_consistent_units, self.Q_(32.0, "degF"), pre_calc_units=self.ureg.meter, ) self.assertRaises( DimensionalityError, convert_to_consistent_units, np.arange(4), pre_calc_units=self.ureg.meter, )
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_arctan2(self): m = self.ureg.m j = self.ureg.J km = self.ureg.km self._test2(np.arctan2, np.arange(0, .9, .1) * m, (np.arange(0, .9, .1) * m, np.arange(.9, 0., -.1) * m, np.arange(0, .9, .1) * km, np.arange(.9, 0., -.1) * km, ), raise_with=np.arange(0, .9, .1) * j, output_units='radian')
def test_arctan2(self): m = self.ureg.m j = self.ureg.J km = self.ureg.km self._test2( np.arctan2, np.arange(0, 0.9, 0.1) * m, ( np.arange(0, 0.9, 0.1) * m, np.arange(0.9, 0.0, -0.1) * m, np.arange(0, 0.9, 0.1) * km, np.arange(0.9, 0.0, -0.1) * km, ), raise_with=np.arange(0, 0.9, 0.1) * j, output_units="radian", )
def test_cos(self): self._test1( np.cos, ( 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, ), (1 * self.ureg.m, ), "", results=(None, None, np.cos(np.arange(0, pi / 2, pi / 4) * 0.001)), ) self._test1( np.cos, (np.rad2deg(np.arange(0, pi / 2, pi / 4)) * self.ureg.degrees, ), results=(np.cos(np.arange(0, pi / 2, pi / 4)), ), )
def test_arccos(self): x = np.arange(0, .9, .1) * self.ureg.m self._test1(np.arccos, (np.arange(0, .9, .1) * self.ureg.dimensionless, np.arange(0, .9, .1) * self.ureg.m / self.ureg.m ), (self.ureg.m, ), 'radian')
def test_shape(self): u = self.Q_(np.arange(12)) u.shape = 4, 3 self.assertEqual(u.magnitude.shape, (4, 3))
def test_pad(self): # Tests reproduced with modification from NumPy documentation a = [1, 2, 3, 4, 5] * self.ureg.m b = self.Q_([4.0, 6.0, 8.0, 9.0, -3.0], "degC") self.assertQuantityEqual(np.pad(a, (2, 3), "constant"), [0, 0, 1, 2, 3, 4, 5, 0, 0, 0] * self.ureg.m) self.assertQuantityEqual( np.pad(a, (2, 3), "constant", constant_values=(0, 600 * self.ureg.cm)), [0, 0, 1, 2, 3, 4, 5, 6, 6, 6] * self.ureg.m, ) self.assertQuantityEqual( np.pad(b, (2, 1), "constant", constant_values=(np.nan, self.Q_(10, "degC"))), self.Q_([np.nan, np.nan, 4, 6, 8, 9, -3, 10], "degC"), ) self.assertRaises(DimensionalityError, np.pad, a, (2, 3), "constant", constant_values=4) self.assertQuantityEqual(np.pad(a, (2, 3), "edge"), [1, 1, 1, 2, 3, 4, 5, 5, 5, 5] * self.ureg.m) self.assertQuantityEqual( np.pad(a, (2, 3), "linear_ramp"), [0, 0, 1, 2, 3, 4, 5, 3, 1, 0] * self.ureg.m, ) self.assertQuantityEqual( np.pad(a, (2, 3), "linear_ramp", end_values=(5, -4) * self.ureg.m), [5, 3, 1, 2, 3, 4, 5, 2, -1, -4] * self.ureg.m, ) self.assertQuantityEqual(np.pad(a, (2, ), "maximum"), [5, 5, 1, 2, 3, 4, 5, 5, 5] * self.ureg.m) self.assertQuantityEqual(np.pad(a, (2, ), "mean"), [3, 3, 1, 2, 3, 4, 5, 3, 3] * self.ureg.m) self.assertQuantityEqual(np.pad(a, (2, ), "median"), [3, 3, 1, 2, 3, 4, 5, 3, 3] * self.ureg.m) self.assertQuantityEqual( np.pad(self.q, ((3, 2), (2, 3)), "minimum"), [ [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], [3, 3, 3, 4, 3, 3, 3], [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], ] * self.ureg.m, ) self.assertQuantityEqual(np.pad(a, (2, 3), "reflect"), [3, 2, 1, 2, 3, 4, 5, 4, 3, 2] * self.ureg.m) self.assertQuantityEqual( np.pad(a, (2, 3), "reflect", reflect_type="odd"), [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8] * self.ureg.m, ) self.assertQuantityEqual(np.pad(a, (2, 3), "symmetric"), [2, 1, 1, 2, 3, 4, 5, 5, 4, 3] * self.ureg.m) self.assertQuantityEqual( np.pad(a, (2, 3), "symmetric", reflect_type="odd"), [0, 1, 1, 2, 3, 4, 5, 5, 6, 7] * self.ureg.m, ) self.assertQuantityEqual(np.pad(a, (2, 3), "wrap"), [4, 5, 1, 2, 3, 4, 5, 1, 2, 3] * self.ureg.m) def pad_with(vector, pad_width, iaxis, kwargs): pad_value = kwargs.get("padder", 10) vector[:pad_width[0]] = pad_value vector[-pad_width[1]:] = pad_value b = self.Q_(np.arange(6).reshape((2, 3)), "degC") self.assertQuantityEqual( np.pad(b, 2, pad_with), self.Q_( [ [10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 0, 1, 2, 10, 10], [10, 10, 3, 4, 5, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10], ], "degC", ), ) self.assertQuantityEqual( np.pad(b, 2, pad_with, padder=100), self.Q_( [ [100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100], [100, 100, 0, 1, 2, 100, 100], [100, 100, 3, 4, 5, 100, 100], [100, 100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100, 100], ], "degC", ), ) # Note: Does not support Quantity pad_with vectorized callable use
def test_convert_to_consistent_units_with_dimensionless(self): args, kwargs = convert_to_consistent_units(np.arange(2), pre_calc_units=self.ureg.g / self.ureg.kg) self.assertNDArrayEqual(args[0], np.array([0, 1000])) assert kwargs == {}
def test_arccosh(self): self._test1(np.arccosh, (np.arange(1., 1.9, .1) * self.ureg.dimensionless, np.arange(1., 1.9, .1) * self.ureg.m / self.ureg.m ), (self.ureg.m, ), 'radian')
def test_arctanh(self): self._test1(np.arctanh, (np.arange(0, .9, .1) * self.ureg.dimensionless, np.arange(0, .9, .1) * self.ureg.m / self.ureg.m ), (.1 * self.ureg.m, ), 'radian')
def test_arccosh(self): self._test1(np.arccosh, (np.arange(1., 1.9, .1) * self.ureg.dimensionless, np.arange(1., 1.9, .1) * self.ureg.m / self.ureg.m), (self.ureg.m, ), 'radian')
def test_is_quantity(self): assert _is_quantity(self.Q_(0)) assert _is_quantity(np.arange(4) * self.ureg.m) assert not _is_quantity(1.0) assert not _is_quantity(np.array([1, 1, 2, 3, 5, 8])) assert not _is_quantity("not-a-quantity")
def test_arctanh(self): self._test1(np.arctanh, (np.arange(0, .9, .1) * self.ureg.dimensionless, np.arange(0, .9, .1) * self.ureg.m / self.ureg.m), (.1 * self.ureg.m, ), 'radian')
def test_shape(self): u = self.Q_(np.arange(12)) u.shape = 4, 3 assert u.magnitude.shape == (4, 3)
def test_arccos(self): x = np.arange(0, .9, .1) * self.ureg.m self._test1(np.arccos, (np.arange(0, .9, .1) * self.ureg.dimensionless, np.arange(0, .9, .1) * self.ureg.m / self.ureg.m), (self.ureg.m, ), 'radian')
def test_deg2rad(self): self._test1(np.deg2rad, (np.arange(0, pi/2, pi/4) * self.ureg.degrees, ), (self.ureg.m, ), 'radians')
def test_is_quantity(self): self.assertTrue(_is_quantity(self.Q_(0))) self.assertTrue(_is_quantity(np.arange(4) * self.ureg.m)) self.assertFalse(_is_quantity(1.0)) self.assertFalse(_is_quantity(np.array([1, 1, 2, 3, 5, 8]))) self.assertFalse(_is_quantity("not-a-quantity"))