本文整理汇总了Python中astropy.units.UnitsError方法的典型用法代码示例。如果您正苦于以下问题:Python units.UnitsError方法的具体用法?Python units.UnitsError怎么用?Python units.UnitsError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astropy.units
的用法示例。
在下文中一共展示了units.UnitsError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _call_with_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def _call_with_units(self, x, y):
# Gather the current parameters' values with units
values = list(map(attrgetter("as_quantity"), self._get_children()))
try:
results = self.evaluate(x, y, *values)
except u.UnitsError: # pragma: no cover
raise u.UnitsError("Looks like you didn't provide all the units, or you provided the wrong ones, when "
"calling function %s" % self.name)
else:
return results
示例2: hcrs_to_icrs
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def hcrs_to_icrs(hcrs_coo, icrs_frame):
# this is just an origin translation so without a distance it cannot go ahead
if isinstance(hcrs_coo.data, UnitSphericalRepresentation):
raise u.UnitsError(_NEED_ORIGIN_HINT.format(hcrs_coo.__class__.__name__))
if hcrs_coo.data.differentials:
from astropy.coordinates.solar_system import get_body_barycentric_posvel
bary_sun_pos, bary_sun_vel = get_body_barycentric_posvel('sun',
hcrs_coo.obstime)
bary_sun_vel = bary_sun_vel.represent_as(CartesianDifferential)
bary_sun_pos = bary_sun_pos.with_differentials(bary_sun_vel)
else:
from astropy.coordinates.solar_system import get_body_barycentric
bary_sun_pos = get_body_barycentric('sun', hcrs_coo.obstime)
bary_sun_vel = None
return None, bary_sun_pos
示例3: icrs_to_hcrs
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def icrs_to_hcrs(icrs_coo, hcrs_frame):
# this is just an origin translation so without a distance it cannot go ahead
if isinstance(icrs_coo.data, UnitSphericalRepresentation):
raise u.UnitsError(_NEED_ORIGIN_HINT.format(icrs_coo.__class__.__name__))
if icrs_coo.data.differentials:
from astropy.coordinates.solar_system import get_body_barycentric_posvel
bary_sun_pos, bary_sun_vel = get_body_barycentric_posvel('sun',
hcrs_frame.obstime)
bary_sun_pos = -bary_sun_pos
bary_sun_vel = -bary_sun_vel.represent_as(CartesianDifferential)
bary_sun_pos = bary_sun_pos.with_differentials(bary_sun_vel)
else:
from astropy.coordinates.solar_system import get_body_barycentric
bary_sun_pos = -get_body_barycentric('sun', hcrs_frame.obstime)
bary_sun_vel = None
return None, bary_sun_pos
示例4: parse
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def parse(self, angle, unit, debug=False):
try:
found_angle, found_unit = self._parser.parse(
angle, lexer=self._lexer, debug=debug)
except ValueError as e:
if str(e):
raise ValueError("{} in angle {!r}".format(
str(e), angle))
else:
raise ValueError(
f"Syntax error parsing angle {angle!r}")
if unit is None and found_unit is None:
raise u.UnitsError("No unit specified")
return found_angle, found_unit
示例5: test_add_sub_cartesian
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_add_sub_cartesian(self):
c1 = self.cartesian + self.cartesian
assert isinstance(c1, CartesianRepresentation)
assert c1.x.dtype.kind == 'f'
assert np.all(representation_equal(c1, 2. * self.cartesian))
with pytest.raises(TypeError):
self.cartesian + 10.*u.m
with pytest.raises(u.UnitsError):
self.cartesian + (self.cartesian / u.s)
c2 = self.cartesian - self.cartesian
assert isinstance(c2, CartesianRepresentation)
assert np.all(representation_equal(
c2, CartesianRepresentation(0.*u.m, 0.*u.m, 0.*u.m)))
c3 = self.cartesian - self.cartesian / 2.
assert isinstance(c3, CartesianRepresentation)
assert np.all(representation_equal(c3, self.cartesian / 2.))
示例6: test_add_sub
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_add_sub(self, representation):
in_rep = self.cartesian.represent_as(representation)
r1 = in_rep + in_rep
assert isinstance(r1, representation)
expected = 2. * in_rep
for component in in_rep.components:
assert_quantity_allclose(getattr(r1, component),
getattr(expected, component))
with pytest.raises(TypeError):
10.*u.m + in_rep
with pytest.raises(u.UnitsError):
in_rep + (in_rep / u.s)
r2 = in_rep - in_rep
assert isinstance(r2, representation)
assert np.all(representation_equal(
r2.to_cartesian(), CartesianRepresentation(0.*u.m, 0.*u.m, 0.*u.m)))
r3 = in_rep - in_rep / 2.
assert isinstance(r3, representation)
expected = in_rep / 2.
assert_representation_allclose(r3, expected)
示例7: test_add_sub_unit_spherical
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_add_sub_unit_spherical(self):
s1 = self.unit_spherical + self.unit_spherical
assert isinstance(s1, SphericalRepresentation)
expected = 2. * self.unit_spherical
for component in s1.components:
assert_quantity_allclose(getattr(s1, component),
getattr(expected, component))
with pytest.raises(TypeError):
10.*u.m - self.unit_spherical
with pytest.raises(u.UnitsError):
self.unit_spherical + (self.unit_spherical / u.s)
s2 = self.unit_spherical - self.unit_spherical / 2.
assert isinstance(s2, SphericalRepresentation)
expected = self.unit_spherical / 2.
for component in s2.components:
assert_quantity_allclose(getattr(s2, component),
getattr(expected, component))
示例8: test_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_units():
sc = SkyCoord(1, 2, 3, unit='m', representation_type='cartesian') # All get meters
assert sc.x.unit is u.m
assert sc.y.unit is u.m
assert sc.z.unit is u.m
sc = SkyCoord(1, 2*u.km, 3, unit='m', representation_type='cartesian') # All get u.m
assert sc.x.unit is u.m
assert sc.y.unit is u.m
assert sc.z.unit is u.m
sc = SkyCoord(1, 2, 3, unit=u.m, representation_type='cartesian') # All get u.m
assert sc.x.unit is u.m
assert sc.y.unit is u.m
assert sc.z.unit is u.m
sc = SkyCoord(1, 2, 3, unit='m, km, pc', representation_type='cartesian')
assert_quantities_allclose(sc, (1*u.m, 2*u.km, 3*u.pc), ('x', 'y', 'z'))
with pytest.raises(u.UnitsError) as err:
SkyCoord(1, 2, 3, unit=(u.m, u.m), representation_type='cartesian')
assert 'should have matching physical types' in str(err.value)
SkyCoord(1, 2, 3, unit=(u.m, u.km, u.pc), representation_type='cartesian')
assert_quantities_allclose(sc, (1*u.m, 2*u.km, 3*u.pc), ('x', 'y', 'z'))
示例9: test_quantity_output_errors
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_quantity_output_errors(self):
dt = TimeDelta(250., format='sec')
with pytest.raises(u.UnitsError):
dt.to(u.m)
with pytest.raises(u.UnitsError):
dt.to_value(u.m)
with pytest.raises(u.UnitsError):
dt.to_value(unit=u.m)
with pytest.raises(ValueError, match=("not one of the known formats.*"
"failed to parse as a unit")):
dt.to_value('parrot')
with pytest.raises(TypeError):
dt.to_value('sec', unit=u.s)
with pytest.raises(TypeError):
# TODO: would be nice to make this work!
dt.to_value(u.s, subfmt='str')
示例10: test_parameter_lose_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_parameter_lose_units():
"""
Check that parameters that have been set to a quantity that are then set to
a value with no units raise an exception. We do this because setting a
parameter to a value with no units is ambiguous if units were set before:
if a paramter is 1 * u.Jy and the parameter is then set to 4, does this mean
2 without units, or 2 * u.Jy?
"""
g = Gaussian1D(1 * u.Jy, 3, 0.1)
with pytest.raises(UnitsError) as exc:
g.amplitude = 2
assert exc.value.args[0] == ("The 'amplitude' parameter should be given as "
"a Quantity because it was originally "
"initialized as a Quantity")
示例11: test_evaluate_with_quantities_and_equivalencies
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_evaluate_with_quantities_and_equivalencies():
"""
We now make sure that equivalencies are correctly taken into account
"""
g = Gaussian1D(1 * u.Jy, 10 * u.nm, 2 * u.nm)
# We aren't setting the equivalencies, so this won't work
with pytest.raises(UnitsError) as exc:
g(30 * u.PHz)
assert exc.value.args[0] == ("Gaussian1D: Units of input 'x', PHz (frequency), could "
"not be converted to required input units of "
"nm (length)")
# But it should now work if we pass equivalencies when evaluating
assert_quantity_allclose(g(30 * u.PHz, equivalencies={'x': u.spectral()}),
g(9.993081933333332 * u.nm))
示例12: __init__
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def __init__(self, values=None, number=None, spacing=None, format=None,
unit=None, decimal=None, format_unit=None, show_decimal_unit=True):
if unit is None:
unit = u.degree
if format_unit is None:
format_unit = unit
if format_unit not in (u.degree, u.hourangle, u.hour):
if decimal is False:
raise UnitsError("Units should be degrees or hours when using non-decimal (sexagesimal) mode")
self._decimal = decimal
self._sep = None
self.show_decimal_unit = show_decimal_unit
super().__init__(values=values, number=number, spacing=spacing,
format=format, unit=unit, format_unit=format_unit)
示例13: test_quantity_comparison
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_quantity_comparison(self):
assert u.Quantity(1100, unit=u.meter) > u.Quantity(1, unit=u.kilometer)
assert u.Quantity(900, unit=u.meter) < u.Quantity(1, unit=u.kilometer)
with pytest.raises(u.UnitsError):
assert u.Quantity(1100, unit=u.meter) > u.Quantity(1, unit=u.second)
with pytest.raises(u.UnitsError):
assert u.Quantity(1100, unit=u.meter) < u.Quantity(1, unit=u.second)
assert u.Quantity(1100, unit=u.meter) >= u.Quantity(1, unit=u.kilometer)
assert u.Quantity(1000, unit=u.meter) >= u.Quantity(1, unit=u.kilometer)
assert u.Quantity(900, unit=u.meter) <= u.Quantity(1, unit=u.kilometer)
assert u.Quantity(1000, unit=u.meter) <= u.Quantity(1, unit=u.kilometer)
with pytest.raises(u.UnitsError):
assert u.Quantity(
1100, unit=u.meter) >= u.Quantity(1, unit=u.second)
with pytest.raises(u.UnitsError):
assert u.Quantity(1100, unit=u.meter) <= u.Quantity(1, unit=u.second)
assert u.Quantity(1200, unit=u.meter) != u.Quantity(1, unit=u.kilometer)
示例14: test_putmask
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_putmask(self):
q = np.arange(3.) * u.m
mask = [True, False, True]
values = [50, 0, 150] * u.cm
np.putmask(q, mask, values)
assert q.unit == u.m
expected = [50, 100, 150] * u.cm
assert np.all(q == expected)
with pytest.raises(u.UnitsError):
np.putmask(q, mask, values.value)
with pytest.raises(u.UnitsError):
np.putmask(q.value, mask, values)
a = np.arange(3.)
values = [50, 0, 150] * u.percent
np.putmask(a, mask, values)
expected = np.array([0.5, 1., 1.5])
assert np.all(a == expected)
示例15: test_insert
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitsError [as 别名]
def test_insert(self):
# Unit of inserted values is not ignored.
q = np.arange(12.).reshape(6, 2) * u.m
out = np.insert(q, (3, 5), [50., 25.] * u.cm)
assert isinstance(out, u.Quantity)
assert out.unit == q.unit
expected = np.insert(q.value, (3, 5), [0.5, 0.25]) << q.unit
assert np.all(out == expected)
# 0 can have any unit.
out2 = np.insert(q, (3, 5), 0)
expected2 = np.insert(q.value, (3, 5), 0) << q.unit
assert np.all(out2 == expected2)
a = np.arange(3.)
result = np.insert(a, (2,), 50. * u.percent)
assert isinstance(result, u.Quantity)
assert result.unit == u.dimensionless_unscaled
expected = np.insert(a, (2,), 0.5) * u.dimensionless_unscaled
assert np.all(result == expected)
with pytest.raises(TypeError):
np.insert(q, 3 * u.cm, 50. * u.cm)
with pytest.raises(u.UnitsError):
np.insert(q, (3, 5), 0. * u.s)