pulse2percept.units
Physical units used by pulse2percept.
Bare numbers retain their documented units. Unitful values are checked for dimensional compatibility and converted at API boundaries.
|
- pulse2percept.units.as_value(value, unit, name=None)[source]
Convert a value to a bare number expressed in
unitThis is p2p’s standard Python-to-numerics boundary. A
Quantityis dimension-checked and rescaled tounit; a bare number is assumed to already be expressed inunitand is passed through untouched (includingNone).- Parameters:
- Returns:
value – The bare numerical value, expressed in
unit.- Return type:
float, np.ndarray, or None
Examples
>>> from pulse2percept.units import as_value, ms, s >>> as_value(20, ms) 20 >>> as_value(0.02 * s, ms) 20.0
- class pulse2percept.units.Dimension(**exponents)[source]
Physical dimensionality of a unit or quantity.
Dimensions are immutable vectors of integer exponents over
BASE_DIMENSIONS.Added in version 0.10.0.
- Parameters:
**exponents (int) – Exponents of the primitive dimensions. Omitted dimensions have exponent zero.
Examples
>>> from pulse2percept.units import Dimension >>> Dimension(current=1) * Dimension(time=1) Dimension('charge')
- property exponents
Tuple of exponents, aligned with
BASE_DIMENSIONS
- property is_dimensionless
Whether all exponents are zero
- property name
Human-readable name, e.g.
'electric current'
- exception pulse2percept.units.DimensionMismatchError[source]
Raised when quantities have incompatible physical dimensions.
Added in version 0.10.0.
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class pulse2percept.units.Quantity(magnitude, unit)[source]
A number (or array of numbers) with a unit
Quantities are what users build by multiplying a number by a unit, and they exist to be checked and converted at p2p’s public API boundaries. They are deliberately not NumPy arrays: p2p strips units before any numerical work, so quantities never reach a Cython kernel and never impose per-element overhead on a simulation.
For the same reason,
np.asarray(5 * uA)does not silently yield5. Removing a unit is something you write down, usingto_value().Equivalent unit choices convert consistently up to floating-point precision, and quantities compare accordingly:
0.0041 * mA == 4.1 * uAis True even though rescaling the former gives4.1000000000000005.Added in version 0.10.0.
- Parameters:
Examples
>>> from pulse2percept.units import uA, mA >>> 500 * uA == 0.5 * mA True >>> (500 * uA).to(mA) 0.5 mA >>> (500 * uA).to_value(mA) 0.5
- property magnitude
The numerical value(s), expressed in
self.unit
- class pulse2percept.units.Unit(dimension, scale, symbol)[source]
A physical unit.
A unit combines a
Dimension, a scale relative to its base unit, and a display symbol. Multiplying a value by a unit produces aQuantity; units may also be multiplied, divided, and raised to integer powers.Units are immutable.
Added in version 0.10.0.
- Parameters:
- property scale
Size of this unit relative to the base unit of its dimension
- property symbol
Short symbol this unit was built with, e.g.
'uA*ms'