Python astropy.units.kg() Examples
The following are 30
code examples of astropy.units.kg().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
astropy.units
, or try the search function
.
Example #1
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_nested(): with quantity_support(): with quantity_support(): fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(Angle([1, 2, 3], u.deg), [3, 4, 5] * u.kg) assert ax.xaxis.get_units() == u.deg assert ax.yaxis.get_units() == u.kg fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(Angle([1, 2, 3], u.arcsec), [3, 4, 5] * u.pc) assert ax.xaxis.get_units() == u.arcsec assert ax.yaxis.get_units() == u.pc
Example #2
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_quantity_subclass(): """Check that subclasses are recognized. This sadly is not done by matplotlib.units itself, though there is a PR to change it: https://github.com/matplotlib/matplotlib/pull/13536 """ plt.figure() with quantity_support(): plt.scatter(Angle([1, 2, 3], u.deg), [3, 4, 5] * u.kg) plt.scatter([105, 210, 315] * u.arcsec, [3050, 3025, 3010] * u.g) plt.plot(Angle([105, 210, 315], u.arcsec), [3050, 3025, 3010] * u.g) assert plt.gca().xaxis.get_units() == u.deg assert plt.gca().yaxis.get_units() == u.kg
Example #3
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_compose_fractional_powers(): # Warning: with a complicated unit, this test becomes very slow; # e.g., x = (u.kg / u.s ** 3 * u.au ** 2.5 / u.yr ** 0.5 / u.sr ** 2) # takes 3 s x = u.m ** 0.5 / u.yr ** 1.5 factored = x.compose() for unit in factored: assert x.decompose() == unit.decompose() factored = x.compose(units=u.cgs) for unit in factored: assert x.decompose() == unit.decompose() factored = x.compose(units=u.si) for unit in factored: assert x.decompose() == unit.decompose()
Example #4
Source File: PlanetPhysicalModel.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def calc_radius_from_mass(self, Mp): """Helper function for calculating radius given the mass. Prototype provides only a dummy function that assumes a density of water. Args: Mp (astropy Quantity array): Planet mass in units of Earth mass Returns: Rp (astropy Quantity array): Planet radius in units of Earth radius """ rho = 1000*u.kg/u.m**3. Rp = ((3.*Mp/rho/np.pi/4.)**(1./3.)).to('earthRad') return Rp
Example #5
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_units(): plt.figure() with quantity_support(): buff = io.BytesIO() plt.plot([1, 2, 3] * u.m, [3, 4, 5] * u.kg, label='label') plt.plot([105, 210, 315] * u.cm, [3050, 3025, 3010] * u.g) plt.legend() # Also test fill_between, which requires actual conversion to ndarray # with numpy >=1.10 (#4654). plt.fill_between([1, 3] * u.m, [3, 5] * u.kg, [3050, 3010] * u.g) plt.savefig(buff, format='svg') assert plt.gca().xaxis.get_units() == u.m assert plt.gca().yaxis.get_units() == u.kg
Example #6
Source File: test_equivalencies.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_is_equivalent(): assert u.m.is_equivalent(u.pc) assert u.cycle.is_equivalent(u.mas) assert not u.cycle.is_equivalent(u.dimensionless_unscaled) assert u.cycle.is_equivalent(u.dimensionless_unscaled, u.dimensionless_angles()) assert not (u.Hz.is_equivalent(u.J)) assert u.Hz.is_equivalent(u.J, u.spectral()) assert u.J.is_equivalent(u.Hz, u.spectral()) assert u.pc.is_equivalent(u.arcsecond, u.parallax()) assert u.arcminute.is_equivalent(u.au, u.parallax()) # Pass a tuple for multiple possibilities assert u.cm.is_equivalent((u.m, u.s, u.kg)) assert u.ms.is_equivalent((u.m, u.s, u.kg)) assert u.g.is_equivalent((u.m, u.s, u.kg)) assert not u.L.is_equivalent((u.m, u.s, u.kg)) assert not (u.km / u.s).is_equivalent((u.m, u.s, u.kg))
Example #7
Source File: test_KnownRVPlanets.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_mass(self): r"""Test gen_mass method. Approach: Ensures the output is set, of the correct type, length, and units. Check the range of the returned values. Check that, for this power law, there are more small than large masses (for n large). """ plan_pop = self.fixture n = 10000 # call the routine masses = plan_pop.gen_mass(n) # check the type self.assertEqual(type(masses), type(1.0 * u.kg)) # crude check on the shape (more small than large for this power law) midpoint = np.mean(plan_pop.Mprange) self.assertGreater(np.count_nonzero(masses < midpoint), np.count_nonzero(masses > midpoint)) # test some illegal "n" values n_list_bad = [-1, '100', 22.5] for n in n_list_bad: with self.assertRaises(AssertionError): masses = plan_pop.gen_mass(n)
Example #8
Source File: test_quantity_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_radian(self, function): q1 = function(180. * u.degree, 0. * u.arcmin, 0. * u.arcsec) assert_allclose(q1.value, np.pi) assert q1.unit == u.radian q2 = function(0. * u.degree, 30. * u.arcmin, 0. * u.arcsec) assert_allclose(q2.value, (30. * u.arcmin).to(u.radian).value) assert q2.unit == u.radian q3 = function(0. * u.degree, 0. * u.arcmin, 30. * u.arcsec) assert_allclose(q3.value, (30. * u.arcsec).to(u.radian).value) # the following doesn't make much sense in terms of the name of the # routine, but we check it gives the correct result. q4 = function(3. * u.radian, 0. * u.arcmin, 0. * u.arcsec) assert_allclose(q4.value, 3.) assert q4.unit == u.radian with pytest.raises(TypeError): function(3. * u.m, 2. * u.s, 1. * u.kg)
Example #9
Source File: test_quantity_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_comparison_valid_units(self, ufunc): q_i1 = np.array([-3.3, 2.1, 10.2]) * u.kg / u.s q_i2 = np.array([10., -5., 1.e6]) * u.g / u.Ms q_o = ufunc(q_i1, q_i2) assert not isinstance(q_o, u.Quantity) assert q_o.dtype == bool assert np.all(q_o == ufunc(q_i1.value, q_i2.to_value(q_i1.unit))) q_o2 = ufunc(q_i1 / q_i2, 2.) assert not isinstance(q_o2, u.Quantity) assert q_o2.dtype == bool assert np.all(q_o2 == ufunc((q_i1 / q_i2) .to_value(u.dimensionless_unscaled), 2.)) # comparison with 0., inf, nan is OK even for dimensional quantities # (though ignore numpy runtime warnings for comparisons with nan). with catch_warnings(RuntimeWarning): for arbitrary_unit_value in (0., np.inf, np.nan): ufunc(q_i1, arbitrary_unit_value) ufunc(q_i1, arbitrary_unit_value*np.ones(len(q_i1))) # and just for completeness ufunc(q_i1, np.array([0., np.inf, np.nan]))
Example #10
Source File: test_representation.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_unit_mismatch(self): q_len = u.Quantity([1], u.km) q_nonlen = u.Quantity([1], u.kg) with pytest.raises(u.UnitsError) as exc: s1 = CartesianRepresentation(x=q_nonlen, y=q_len, z=q_len) assert exc.value.args[0] == "x, y, and z should have matching physical types" with pytest.raises(u.UnitsError) as exc: s1 = CartesianRepresentation(x=q_len, y=q_nonlen, z=q_len) assert exc.value.args[0] == "x, y, and z should have matching physical types" with pytest.raises(u.UnitsError) as exc: s1 = CartesianRepresentation(x=q_len, y=q_len, z=q_nonlen) assert exc.value.args[0] == "x, y, and z should have matching physical types"
Example #11
Source File: test_quantity.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_complicated_operation(self): """ Perform a more complicated test """ from astropy.units import imperial # Multiple units distance = u.Quantity(15., u.meter) time = u.Quantity(11., u.second) velocity = (distance / time).to(imperial.mile / u.hour) assert_array_almost_equal( velocity.value, 3.05037, decimal=5) G = u.Quantity(6.673E-11, u.m ** 3 / u.kg / u.s ** 2) new_q = ((1. / (4. * np.pi * G)).to(u.pc ** -3 / u.s ** -2 * u.kg)) # Area side1 = u.Quantity(11., u.centimeter) side2 = u.Quantity(7., u.centimeter) area = side1 * side2 assert_array_almost_equal(area.value, 77., decimal=15) assert area.unit == u.cm * u.cm
Example #12
Source File: test_quantity_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_invariant_twoarg_array(self, ufunc): q_i1 = np.array([-3.3, 2.1, 10.2]) * u.kg / u.s q_i2 = np.array([10., -5., 1.e6]) * u.g / u.us q_o = ufunc(q_i1, q_i2) assert isinstance(q_o, u.Quantity) assert q_o.unit == q_i1.unit assert_allclose(q_o.value, ufunc(q_i1.value, q_i2.to_value(q_i1.unit)))
Example #13
Source File: test_quantity_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_comparison_ufuncs_inplace(self, ufunc): q_i1 = np.array([-3.3, 2.1, 10.2]) * u.kg / u.s q_i2 = np.array([10., -5., 1.e6]) * u.g / u.Ms check = np.empty(q_i1.shape, bool) ufunc(q_i1.value, q_i2.to_value(q_i1.unit), out=check) result = np.empty(q_i1.shape, bool) q_o = ufunc(q_i1, q_i2, out=result) assert q_o is result assert type(q_o) is np.ndarray assert q_o.dtype == bool assert np.all(q_o == check)
Example #14
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unit_division_by_string(): """Check that multiplication with strings produces the correct unit.""" u1 = u.cm us = 'kg' assert us / u1 == u.Unit(us) / u1 assert u1 / us == u1 / u.Unit(us)
Example #15
Source File: test_quantity_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_invariant_array(self, ufunc): q_i = np.array([-3.3, 2.1, 10.2]) * u.kg / u.s q_o = ufunc(q_i) assert isinstance(q_o, u.Quantity) assert q_o.unit == q_i.unit assert np.all(q_o.value == ufunc(q_i.value))
Example #16
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_enable_unit_groupings(): from astropy.units import cds with cds.enable(): assert cds.geoMass in u.kg.find_equivalent_units() from astropy.units import imperial with imperial.enable(): assert imperial.inch in u.m.find_equivalent_units()
Example #17
Source File: test_units.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_self_compose(): unit = u.kg * u.s assert len(unit.compose(units=[u.g, u.s])) == 1
Example #18
Source File: test_quantity_ufuncs.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_jv_invalid_units(self, function): # Can't use jv() with non-dimensionless quantities with pytest.raises(TypeError) as exc: function(1. * u.kg, 3. * u.m / u.s) assert exc.value.args[0] == ("Can only apply '{}' function to " "dimensionless quantities" .format(function.__name__))
Example #19
Source File: test_logarithmic.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unit_decomposition(self): lu = u.mag(u.Jy) assert lu.decompose() == u.mag(u.Jy.decompose()) assert lu.decompose().physical_unit.bases == [u.kg, u.s] assert lu.si == u.mag(u.Jy.si) assert lu.si.physical_unit.bases == [u.kg, u.s] assert lu.cgs == u.mag(u.Jy.cgs) assert lu.cgs.physical_unit.bases == [u.g, u.s]
Example #20
Source File: test_quantity_decorator.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_str_unit_typo(): @u.quantity_input def myfunc_args(x: "kilograam"): return x with pytest.raises(ValueError): result = myfunc_args(u.kg)
Example #21
Source File: test_quantity_array_methods.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_round(self): q1 = np.array([1.253, 2.253, 3.253]) * u.kg assert np.all(np.round(q1) == np.array([1, 2, 3]) * u.kg) assert np.all(np.round(q1, decimals=2) == np.round(q1.value, decimals=2) * u.kg) assert np.all(q1.round(decimals=2) == q1.value.round(decimals=2) * u.kg)
Example #22
Source File: test_quantity_array_methods.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_round_inplace(self): q1 = np.array([1.253, 2.253, 3.253]) * u.kg qi = np.zeros(3) * u.s a = q1.round(decimals=2, out=qi) assert a is qi assert np.all(q1.round(decimals=2) == qi)
Example #23
Source File: test_equivalencies.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_equivalent_units(): from astropy.units import imperial with u.add_enabled_units(imperial): units = u.g.find_equivalent_units() units_set = set(units) match = set( [u.M_e, u.M_p, u.g, u.kg, u.solMass, u.t, u.u, u.M_earth, u.M_jup, imperial.oz, imperial.lb, imperial.st, imperial.ton, imperial.slug]) assert units_set == match r = repr(units) assert r.count('\n') == len(units) + 2
Example #24
Source File: test_equivalencies.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_invalid_equivalency(): with pytest.raises(ValueError): u.m.to(u.kg, equivalencies=[(u.m,)]) with pytest.raises(ValueError): u.m.to(u.kg, equivalencies=[(u.m, 5.0)])
Example #25
Source File: test_equivalencies.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_irrelevant_equivalency(): with pytest.raises(u.UnitsError): u.m.to(u.kg, equivalencies=[(u.m, u.l)])
Example #26
Source File: test_quantity.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_value_array(tmpdir): testval = [3.14159] testunit = units.kg yaml = """ quantity: !unit/quantity-1.1.0 value: !core/ndarray-1.0.0 {} unit: {} """.format(testval, testunit) quantity = units.Quantity(testval, unit=testunit) roundtrip_quantity(yaml, quantity)
Example #27
Source File: test_representation.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_unit_non_length(self): s1 = CartesianRepresentation(x=1 * u.kg, y=2 * u.kg, z=3 * u.kg) s2 = CartesianRepresentation(x=1 * u.km / u.s, y=2 * u.km / u.s, z=3 * u.km / u.s) banana = u.def_unit('banana') s3 = CartesianRepresentation(x=1 * banana, y=2 * banana, z=3 * banana)
Example #28
Source File: Observatory.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def mass_dec(self, dF_lateral, t_int): """Returns mass_used and deltaV The values returned by this method are used to decrement spacecraft mass for station-keeping. Args: dF_lateral (astropy Quantity): Lateral disturbance force in units of N t_int (astropy Quantity): Integration time in units of day Returns: tuple: intMdot (astropy Quantity): Mass flow rate in units of kg/s mass_used (astropy Quantity): Mass used in station-keeping units of kg deltaV (astropy Quantity): Change in velocity required for station-keeping in units of km/s """ intMdot = (1./np.cos(np.radians(45))*np.cos(np.radians(5))* dF_lateral/const.g0/self.skIsp).to('kg/s') mass_used = (intMdot*t_int).to('kg') deltaV = (dF_lateral/self.scMass*t_int).to('km/s') return intMdot, mass_used, deltaV
Example #29
Source File: Observatory.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def mass_dec_sk(self, TL, sInd, currentTime, t_int): """Returns mass_used, deltaV and disturbance forces This method calculates all values needed to decrement spacecraft mass for station-keeping. Args: TL (TargetList module): TargetList class object sInd (integer): Integer index of the star of interest currentTime (astropy Time): Current absolute mission time in MJD t_int (astropy Quantity): Integration time in units of day Returns: tuple: dF_lateral (astropy Quantity): Lateral disturbance force in units of N dF_axial (astropy Quantity): Axial disturbance force in units of N intMdot (astropy Quantity): Mass flow rate in units of kg/s mass_used (astropy Quantity): Mass used in station-keeping units of kg deltaV (astropy Quantity): Change in velocity required for station-keeping in units of km/s """ dF_lateral, dF_axial = self.distForces(TL, sInd, currentTime) intMdot, mass_used, deltaV = self.mass_dec(dF_lateral, t_int) return dF_lateral, dF_axial, intMdot, mass_used, deltaV
Example #30
Source File: Observatory.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 5 votes |
def log_occulterResults(self,DRM,slewTimes,sInd,sd,dV): """Updates the given DRM to include occulter values and results Args: DRM (dict): Design Reference Mission, contains the results of one complete observation (detection and characterization) slewTimes (astropy Quantity): Time to transfer to new star line of sight in units of days sInd (integer): Integer index of the star of interest sd (astropy Quantity): Angular separation between stars in rad dV (astropy Quantity): Delta-V used to transfer to new star line of sight in units of m/s Returns: dict: Design Reference Mission dictionary, contains the results of one complete observation (detection and characterization) """ DRM['slew_time'] = slewTimes.to('day') DRM['slew_angle'] = sd.to('deg') slew_mass_used = slewTimes*self.defburnPortion*self.flowRate DRM['slew_dV'] = (slewTimes*self.ao*self.defburnPortion).to('m/s') DRM['slew_mass_used'] = slew_mass_used.to('kg') self.scMass = self.scMass - slew_mass_used DRM['scMass'] = self.scMass.to('kg') return DRM