本文整理汇总了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
#.........这里部分代码省略.........