本文整理匯總了Python中astropy.units.Unit.is_equivalent方法的典型用法代碼示例。如果您正苦於以下問題:Python Unit.is_equivalent方法的具體用法?Python Unit.is_equivalent怎麽用?Python Unit.is_equivalent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類astropy.units.Unit
的用法示例。
在下文中一共展示了Unit.is_equivalent方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from astropy.units import Unit [as 別名]
# 或者: from astropy.units.Unit import is_equivalent [as 別名]
def __init__(self, physical_unit=None, function_unit=None):
if physical_unit is None:
self._physical_unit = dimensionless_unscaled
else:
self._physical_unit = Unit(physical_unit)
if (not isinstance(self._physical_unit, UnitBase) or
self._physical_unit.is_equivalent(
self._default_function_unit)):
raise ValueError("Unit {0} is not a physical unit."
.format(self._physical_unit))
if function_unit is None:
self._function_unit = self._default_function_unit
else:
# any function unit should be equivalent to subclass default
function_unit = Unit(getattr(function_unit, 'function_unit',
function_unit))
if function_unit.is_equivalent(self._default_function_unit):
self._function_unit = function_unit
else:
raise ValueError("Cannot initialize '{0}' instance with "
"function unit '{1}', as it is not "
"equivalent to default function unit '{2}'."
.format(self.__class__.__name__,
function_unit,
self._default_function_unit))
示例2: verify_unit
# 需要導入模塊: from astropy.units import Unit [as 別名]
# 或者: from astropy.units.Unit import is_equivalent [as 別名]
def verify_unit(quantity, unit):
"""Verify unit of passed quantity and return it.
Parameters:
quantity: :py:class:`~astropy.units.Quantity` to be verified. Bare
numbers are valid if the unit is dimensionless.
unit: Equivalent unit, or string parsable by
:py:class:`astropy.units.Unit`
Raises:
ValueError: Units are not equivalent.
Returns:
``quantity`` unchanged. Bare numbers will be converted to a dimensionless
:py:class:`~astropy.units.Quantity`.
Example:
.. code-block:: python
def __init__(self, a):
self.a = verify_unit(a, astropy.units.m)
"""
if not isinstance(unit, UnitBase):
unit = Unit(unit)
q = quantity * u.one
if unit.is_equivalent(q.unit):
return q
else:
raise ValueError("Unit '{}' not equivalent to quantity '{}'.".format(unit, quantity))
示例3: verify_unit
# 需要導入模塊: from astropy.units import Unit [as 別名]
# 或者: from astropy.units.Unit import is_equivalent [as 別名]
def verify_unit(quantity, unit):
"""Verify unit of passed quantity and return it.
Parameters:
quantity: Quantity to be verified.
unit: Equivalent unit, or string parsable by
:py:class:`astropy.units.Unit`
Raises:
ValueError: Units are not equivalent.
Returns:
quantity parameter, unchanged.
Example:
.. code-block:: python
def __init__(self, a):
self.a = verify_unit(a, astropy.units.m)
:type quantity: :py:class:`astropy.units.Quantity`
:type unit: :py:class:`astropy.units.UnitBase`
"""
if not isinstance(unit, Unit):
unit = Unit(unit)
if unit.is_equivalent((quantity * u.one).unit):
return quantity
else:
raise ValueError(
"Unit '{}' not equivalent to quantity '{}'.".format(unit, quantity))
示例4: FunctionUnitBase
# 需要導入模塊: from astropy.units import Unit [as 別名]
# 或者: from astropy.units.Unit import is_equivalent [as 別名]
class FunctionUnitBase(metaclass=ABCMeta):
"""Abstract base class for function units.
Function units are functions containing a physical unit, such as dB(mW).
Most of the arithmetic operations on function units are defined in this
base class.
While instantiation is defined, this class should not be used directly.
Rather, subclasses should be used that override the abstract properties
`_default_function_unit` and `_quantity_class`, and the abstract methods
`from_physical`, and `to_physical`.
Parameters
----------
physical_unit : `~astropy.units.Unit` or `string`
Unit that is encapsulated within the function unit.
If not given, dimensionless.
function_unit : `~astropy.units.Unit` or `string`
By default, the same as the function unit set by the subclass.
"""
# ↓↓↓ the following four need to be set by subclasses
# Make this a property so we can ensure subclasses define it.
@property
@abstractmethod
def _default_function_unit(self):
"""Default function unit corresponding to the function.
This property should be overridden by subclasses, with, e.g.,
`~astropy.unit.MagUnit` returning `~astropy.unit.mag`.
"""
# This has to be a property because the function quantity will not be
# known at unit definition time, as it gets defined after.
@property
@abstractmethod
def _quantity_class(self):
"""Function quantity class corresponding to this function unit.
This property should be overridden by subclasses, with, e.g.,
`~astropy.unit.MagUnit` returning `~astropy.unit.Magnitude`.
"""
@abstractmethod
def from_physical(self, x):
"""Transformation from value in physical to value in function units.
This method should be overridden by subclasses. It is used to
provide automatic transformations using an equivalency.
"""
@abstractmethod
def to_physical(self, x):
"""Transformation from value in function to value in physical units.
This method should be overridden by subclasses. It is used to
provide automatic transformations using an equivalency.
"""
# ↑↑↑ the above four need to be set by subclasses
# have priority over arrays, regular units, and regular quantities
__array_priority__ = 30000
def __init__(self, physical_unit=None, function_unit=None):
if physical_unit is None:
self._physical_unit = dimensionless_unscaled
else:
self._physical_unit = Unit(physical_unit)
if (not isinstance(self._physical_unit, UnitBase) or
self._physical_unit.is_equivalent(
self._default_function_unit)):
raise ValueError("Unit {0} is not a physical unit."
.format(self._physical_unit))
if function_unit is None:
self._function_unit = self._default_function_unit
else:
# any function unit should be equivalent to subclass default
function_unit = Unit(getattr(function_unit, 'function_unit',
function_unit))
if function_unit.is_equivalent(self._default_function_unit):
self._function_unit = function_unit
else:
raise ValueError("Cannot initialize '{0}' instance with "
"function unit '{1}', as it is not "
"equivalent to default function unit '{2}'."
.format(self.__class__.__name__,
function_unit,
self._default_function_unit))
def _copy(self, physical_unit=None):
"""Copy oneself, possibly with a different physical unit."""
if physical_unit is None:
physical_unit = self.physical_unit
return self.__class__(physical_unit, self.function_unit)
@property
def physical_unit(self):
return self._physical_unit
#.........這裏部分代碼省略.........