def test_nancumsum(self): # Testing a 1D array mag_1d = np.array([5., np.nan]) speeds_1d = Quantity.from_units(mag=mag_1d, units='m/s') units_1d = speeds_1d.units expected_nancumsum_1d = Quantity._from_qty(mag=np.nancumsum(mag_1d), units=units_1d) np.testing.assert_array_equal(expected_nancumsum_1d, np.nancumsum(speeds_1d)) # Testing a 2D array mag_2d = np.array([[5., 6.], [7., np.nan]]) speeds_2d = Quantity.from_units(mag=mag_2d, units='m/s') units_2d = speeds_2d.units # Axis not specified expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d), units=units_2d) np.testing.assert_array_equal(expected_nancumsum_2d, np.nancumsum(speeds_2d)) # Axis = 0 axis = 0 expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d, axis=axis), units=units_2d) np.testing.assert_array_equal(expected_nancumsum_2d, np.nancumsum(speeds_2d, axis=axis)) # Axis = 1 axis = 1 expected_nancumsum_2d = Quantity._from_qty(mag=np.nancumsum(mag_2d, axis=axis), units=units_2d) np.testing.assert_array_equal(expected_nancumsum_2d, np.nancumsum(speeds_2d, axis=axis))
def test_nanprod(self): # Testing a 1D array mag_1d = np.array([5., 6., np.nan]) speeds_1d = Quantity.from_units(mag=mag_1d, units='m/s') units_1d = speeds_1d.units expected_nanprod_1d = Quantity._from_qty(mag=np.nanprod(mag_1d), units=units_1d*len(mag_1d)) self.assertEqual(expected_nanprod_1d, np.nanprod(speeds_1d)) # Testing a 2D array mag_2d = np.array([[5., 6.], [7., np.nan]]) speeds_2d = Quantity.from_units(mag=mag_2d, units='m/s') units_2d = speeds_2d.units # Axis not specified expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d), units=units_2d*mag_2d.size) self.assertEqual(expected_nanprod_2d, np.nanprod(speeds_2d)) # Axis = 0 axis = 0 expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d, axis=axis), units=units_2d*mag_2d.shape[1]) np.testing.assert_array_equal(expected_nanprod_2d, np.nanprod(speeds_2d, axis=axis)) # Axis = 1 axis = 1 expected_nanprod_2d = Quantity._from_qty(mag=np.nanprod(mag_2d, axis=axis), units=units_2d*mag_2d.shape[0]) np.testing.assert_array_equal(expected_nanprod_2d, np.nanprod(speeds_2d, axis=axis))
def fmin(x1, x2, **kwargs): x2_units = x1._get_other_units(x2) if not x1.equals(x2): err_msg = ('Incompatible units between x1 ({}) and x2 ({}) for ' 'numpy.fmin.'.format(x1, x2)) raise TypeError(err_msg) return Qty._from_qty(units=x.units, mag=np.fmin(x1.mag, x2.mag, **kwargs))
def trapz(y, x=None, dx=1.): try: x_units = x.units except AttributeError: if x is None: # If x is not set, infer units from dx try: x_units = dx.units except AttributeError: warn_msg = ('Inputted dx variable to numpy.trapz was not a ' 'Qty object ({}) so units cannot be inferred. ' 'Therefore the result will have the same units as ' 'y.'.format(dx)) warn(warn_msg) x_units = Qty().units else: warn_msg = ('Inputted x variable to numpy.trapz was not a ' 'Qty object ({}) so units cannot be inferred. ' 'Therefore the result will have the same units as ' 'y.'.format(x)) warn(warn_msg) x_units = Qty().units units_out = y_units + x_units return Qty._from_qty(mag=np.trapz(y.mag, **kwargs), units=units_out)
def nancumsum(a, **kwargs): return Qty._from_qty(units=a.units, mag=np.nancumsum(a.mag, **kwargs))
def nanprod(a, **kwargs): mag_out = np.nanprod(a.mag, **kwargs) units_out = _get_units_prod(a.mag, mag_out) * a.units return Qty._from_qty(units=units_out, mag=mag_out)
def mean(a): return Qty._from_qty(units=a.units, mag=np.mean(a.mag, **kwargs))
def nan_to_num(x, **kwargs): return Qty._from_qty(units=x.units, mag=np.nan_to_num(x.mag, **kwargs))
def minimum(x, **kwargs): return Qty._from_qty(units=x.units, mag=np.minimum(x.mag, **kwargs))
def absolute(x, **kwargs): return Qty._from_qty(units=x.units, mag=np.absolute(x.mag, **kwargs))
def square(x, **kwargs): units_out = x.units * 2. return Qty._from_qty(units=units_out, mag=np.square(x.mag, **kwargs))
def cbrt(x, **kwargs): units_out = a.units / 3. return Qty._from_qty(units=units_out, mag=np.cbrt(x.mag, **kwargs))
def clip(a, **kwargs): return Qty._from_qty(units=a.units, mag=np.clip(a.mag, **kwargs))
def diff(a, **kwargs): return Qty._from_qty(units=a.units, mag=np.diff(a.mag, **kwargs))
def fabs(x, **kwargs): return Qty._from_qty(units=x.units, mag=np.fabs(x.mag, **kwargs))
def ediff1d(a, **kwargs): return Qty._from_qty(units=a.units, mag=np.ediff1d(a.mag, **kwargs))
def test_from_qty(self): self.assertEqual(Quantity._from_qty(mag=self.mag1, units=self.vel1.units), self.vel1)