本文整理汇总了Python中astropy.units.UnitConversionError方法的典型用法代码示例。如果您正苦于以下问题:Python units.UnitConversionError方法的具体用法?Python units.UnitConversionError怎么用?Python units.UnitConversionError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astropy.units
的用法示例。
在下文中一共展示了units.UnitConversionError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_all_to_value
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_all_to_value():
"""test all_to_value"""
x_m = np.arange(5) * u.m
y_mm = np.arange(5) * 1000 * u.mm
z_km = np.arange(5) * 1e-3 * u.km
nono_deg = np.arange(5) * 1000 * u.deg
# one argument
x = all_to_value(x_m, unit=u.m)
assert (x == np.arange(5)).all()
# two arguments
x, y = all_to_value(x_m, y_mm, unit=u.m)
assert (x == np.arange(5)).all()
assert (y == np.arange(5)).all()
# three
x, y, z = all_to_value(x_m, y_mm, z_km, unit=u.m)
assert (x == np.arange(5)).all()
assert (y == np.arange(5)).all()
assert (z == np.arange(5)).all()
# cannot be converted
with pytest.raises(u.UnitConversionError):
all_to_value(x_m, nono_deg, unit=x_m.unit)
示例2: __init__
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def __init__(self, frequency, power, nyquist=None, label=None,
targetid=None, default_view='frequency', meta={}):
# Input validation
if not isinstance(frequency, u.quantity.Quantity):
raise ValueError('frequency must be an `astropy.units.Quantity` object.')
if not isinstance(power, u.quantity.Quantity):
raise ValueError('power must be an `astropy.units.Quantity` object.')
# Frequency must have frequency units
try:
frequency.to(u.Hz)
except u.UnitConversionError:
raise ValueError('Frequency must be in units of 1/time.')
# Frequency and power must have sensible shapes
if frequency.shape[0] <= 1:
raise ValueError('frequency and power must have a length greater than 1.')
if frequency.shape != power.shape:
raise ValueError('frequency and power must have the same length.')
self.frequency = frequency
self.power = power
self.nyquist = nyquist
self.label = label
self.targetid = targetid
self.default_view = self._validate_view(default_view)
self.meta = meta
示例3: test_set_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_set_units():
p = Parameter('test_parameter',1.0, unit=u.keV)
p.value = 3.0 * u.MeV
assert p.value == 3000.0
with pytest.raises(u.UnitConversionError):
p.value = 3.0 * u.cm
with pytest.raises(CannotConvertValueToNewUnits):
p.unit = u.cm
with pytest.raises(CannotConvertValueToNewUnits):
p.unit = u.dimensionless_unscaled
p.unit = u.MeV
assert p.unit == u.MeV
p.display()
示例4: _determine_quantity
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def _determine_quantity(self):
# scroll thru conversions until one works
for k, v in self._flux_lookup.items():
try:
self._flux_unit.to(v)
self._flux_type = k
except (u.UnitConversionError):
continue
if self._flux_type is None:
raise InvalidUnitError(
"The flux_unit provided is not a valid flux quantity"
)
示例5: unit
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def unit(self, value):
"""
The unit should be set to a value consistent with the parent NDData
unit and the uncertainty type.
"""
if value is not None:
# Check the hidden attribute below, not the property. The property
# raises an exception if there is no parent_nddata.
if self._parent_nddata is not None:
parent_unit = self.parent_nddata.unit
try:
# Check for consistency with the unit of the parent_nddata
self._data_unit_to_uncertainty_unit(parent_unit).to(value)
except UnitConversionError:
raise UnitConversionError("Unit {} is incompatible "
"with unit {} of parent "
"nddata".format(value,
parent_unit))
self._unit = Unit(value)
else:
self._unit = value
示例6: test_incompatible_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_incompatible_units():
# NOTE: minversion check does not work properly for matplotlib dev.
try:
# https://github.com/matplotlib/matplotlib/pull/13005
from matplotlib.units import ConversionError
except ImportError:
err_type = u.UnitConversionError
else:
err_type = ConversionError
plt.figure()
with quantity_support():
plt.plot([1, 2, 3] * u.m)
with pytest.raises(err_type):
plt.plot([105, 210, 315] * u.kg)
示例7: test_input_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_input_units(data):
t, y, dy, params = data
t_unit = u.day
y_unit = u.mag
with pytest.raises(u.UnitConversionError):
BoxLeastSquares(t * t_unit, y * y_unit, dy * u.one)
with pytest.raises(u.UnitConversionError):
BoxLeastSquares(t * t_unit, y * u.one, dy * y_unit)
with pytest.raises(u.UnitConversionError):
BoxLeastSquares(t * t_unit, y, dy * y_unit)
model = BoxLeastSquares(t*t_unit, y * u.one, dy)
assert model.dy.unit == model.y.unit
model = BoxLeastSquares(t*t_unit, y * y_unit, dy)
assert model.dy.unit == model.y.unit
model = BoxLeastSquares(t*t_unit, y*y_unit)
assert model.dy is None
示例8: _validate_inputs
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def _validate_inputs(self, t, y, dy):
# Validate shapes of inputs
if dy is None:
t, y = np.broadcast_arrays(t, y, subok=True)
else:
t, y, dy = np.broadcast_arrays(t, y, dy, subok=True)
if t.ndim != 1:
raise ValueError("Inputs (t, y, dy) must be 1-dimensional")
# validate units of inputs if any is a Quantity
if any(has_units(arr) for arr in (t, y, dy)):
t, y = map(units.Quantity, (t, y))
if dy is not None:
dy = units.Quantity(dy)
try:
dy = units.Quantity(dy, unit=y.unit)
except units.UnitConversionError:
raise ValueError("Units of dy not equivalent "
"to units of y")
return t, y, dy
示例9: _set_unit
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def _set_unit(self, input_unit):
# This will fail if the input is not valid
new_unit = self._safe_assign_unit(input_unit)
# Now transform the current _value in the new unit, unless the current unit is dimensionless, in which
# case there is no transformation to make
# (self._unit is the OLD unit here)
if self._unit != u.dimensionless_unscaled:
# This will fail if the new unit is not compatible with the old one
try:
self.value = self.as_quantity.to(new_unit).value
except u.UnitConversionError:
if new_unit == u.dimensionless_unscaled:
new_unit_name = '(dimensionless)'
else:
new_unit_name = new_unit
raise CannotConvertValueToNewUnits("Cannot convert the value %s from %s to the "
"new units %s" % (self.value, self._unit, new_unit_name))
else:
# This is possibly the first time the unit is set
pass
# Finally store the new unit
self._unit = new_unit
示例10: parent_nddata
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def parent_nddata(self, value):
if value is not None and not isinstance(value, weakref.ref):
# Save a weak reference on the uncertainty that points to this
# instance of NDData. Direct references should NOT be used:
# https://github.com/astropy/astropy/pull/4799#discussion_r61236832
value = weakref.ref(value)
# Set _parent_nddata here and access below with the property because value
# is a weakref
self._parent_nddata = value
# set uncertainty unit to that of the parent if it was not already set, unless initializing
# with empty parent (Value=None)
if value is not None:
parent_unit = self.parent_nddata.unit
if self.unit is None:
if parent_unit is None:
self.unit = None
else:
# Set the uncertainty's unit to the appropriate value
self.unit = self._data_unit_to_uncertainty_unit(parent_unit)
else:
# Check that units of uncertainty are compatible with those of
# the parent. If they are, no need to change units of the
# uncertainty or the data. If they are not, let the user know.
unit_from_data = self._data_unit_to_uncertainty_unit(parent_unit)
try:
unit_from_data.to(self.unit)
except UnitConversionError:
raise UnitConversionError("Unit {} of uncertainty "
"incompatible with unit {} of "
"data".format(self.unit,
parent_unit))
示例11: test_changing_unit_to_value_inconsistent_with_parent_fails
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_changing_unit_to_value_inconsistent_with_parent_fails(NDClass,
UncertClass):
ndd1 = NDClass(1, unit='adu')
v = UncertClass(1)
# Sets the uncertainty unit to whatever makes sense with this data.
ndd1.uncertainty = v
with pytest.raises(u.UnitConversionError):
# Nothing special about 15 except no one would ever use that unit
v.unit = ndd1.unit ** 15
示例12: test_assigning_uncertainty_with_bad_unit_to_parent_fails
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_assigning_uncertainty_with_bad_unit_to_parent_fails(NDClass,
UncertClass):
# Does assigning an uncertainty with a non-matching unit to an NDData
# with a unit work?
ndd = NDClass([1, 1], unit=u.adu)
# Set the unit to something inconsistent with ndd's unit
v = UncertClass([1, 1], unit=u.second)
with pytest.raises(u.UnitConversionError):
ndd.uncertainty = v
示例13: test_bad_human_file_size
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_bad_human_file_size(size):
assert pytest.raises(u.UnitConversionError, console.human_file_size, size)
示例14: test_period_units
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def test_period_units(data):
t, y, dy, params = data
t_unit = u.day
y_unit = u.mag
model = BoxLeastSquares(t * t_unit, y * y_unit, dy)
p = model.autoperiod(params["duration"])
assert p.unit == t_unit
p = model.autoperiod(params["duration"] * 24 * u.hour)
assert p.unit == t_unit
with pytest.raises(u.UnitConversionError):
model.autoperiod(params["duration"] * u.mag)
p = model.autoperiod(params["duration"], minimum_period=0.5)
assert p.unit == t_unit
with pytest.raises(u.UnitConversionError):
p = model.autoperiod(params["duration"], minimum_period=0.5*u.mag)
p = model.autoperiod(params["duration"], maximum_period=0.5)
assert p.unit == t_unit
with pytest.raises(u.UnitConversionError):
p = model.autoperiod(params["duration"], maximum_period=0.5*u.mag)
p = model.autoperiod(params["duration"], minimum_period=0.5,
maximum_period=1.5)
p2 = model.autoperiod(params["duration"], maximum_period=0.5,
minimum_period=1.5)
assert_quantity_allclose(p, p2)
示例15: _validate_t
# 需要导入模块: from astropy import units [as 别名]
# 或者: from astropy.units import UnitConversionError [as 别名]
def _validate_t(self, t):
t = np.asanyarray(t)
if has_units(self._trel):
t = units.Quantity(t)
try:
t = units.Quantity(t, unit=self._trel.unit)
except units.UnitConversionError:
raise ValueError("Units of t not equivalent to "
"units of input self.t")
return t