|
| 1 | +"""The Quantity API. Private module.""" |
| 2 | + |
| 3 | +__all__ = ["Quantity", "QuantityArray", "Unit"] |
| 4 | + |
| 5 | +from typing import Protocol, runtime_checkable |
| 6 | + |
| 7 | +from astropy.units import UnitBase as Unit |
| 8 | + |
| 9 | +from .array_api import Array |
| 10 | + |
| 11 | + |
| 12 | +@runtime_checkable |
| 13 | +class Quantity(Protocol): |
| 14 | + """Minimal definition of the Quantity API. |
| 15 | +
|
| 16 | + At minimum a Quantity must have the following attributes: |
| 17 | +
|
| 18 | + - `value`: the numerical value of the quantity (adhering to the Array API) |
| 19 | + - `unit`: the unit of the quantity |
| 20 | +
|
| 21 | + In practice, Quantities themselves must adhere to the Array API, not just |
| 22 | + their values. This stricter requirement is described by the `QuantityArray` |
| 23 | + protocol. |
| 24 | +
|
| 25 | + See Also |
| 26 | + -------- |
| 27 | + QuantityArray : A Quantity that adheres to the Array API |
| 28 | +
|
| 29 | + Examples |
| 30 | + -------- |
| 31 | + >>> import numpy as np |
| 32 | + >>> import astropy.units as u |
| 33 | + >>> from quantity import Quantity |
| 34 | + >>> from quantity import api |
| 35 | +
|
| 36 | + >>> issubclass(Quantity, api.Quantity) |
| 37 | + True |
| 38 | +
|
| 39 | + >>> q = Quantity(value=np.array([1, 2, 3]), unit=u.m) |
| 40 | + >>> isinstance(q, api.Quantity) |
| 41 | + True |
| 42 | +
|
| 43 | + """ |
| 44 | + |
| 45 | + #: The numerical value of the quantity, adhering to the Array API. |
| 46 | + value: Array |
| 47 | + |
| 48 | + #: The unit of the quantity. |
| 49 | + unit: Unit |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def __subclasshook__(cls: type, c: type) -> bool: |
| 53 | + """Enable the subclass check for data descriptors.""" |
| 54 | + return ( |
| 55 | + hasattr(c, "value") or "value" in getattr(c, "__annotations__", ()) |
| 56 | + ) and (hasattr(c, "unit") or "unit" in getattr(c, "__annotations__", ())) |
| 57 | + |
| 58 | + |
| 59 | +@runtime_checkable |
| 60 | +class QuantityArray(Quantity, Array, Protocol): |
| 61 | + """An array-valued Quantity. |
| 62 | +
|
| 63 | + A QuantityArray is a Quantity that itself adheres to the Array API. This |
| 64 | + means that the QuantityArray has properties like `shape`, `dtype`, and the |
| 65 | + `__array_namespace__` method, among many other properties and methods. To |
| 66 | + understand the full requirements of the Array API, see the `Array` protocol. |
| 67 | + The `Quantity` protocol describes the minimal requirements for a Quantity, |
| 68 | + separate from the Array API. QuantityArray is the combination of these two |
| 69 | + protocols and is the most complete description of a Quantity. |
| 70 | +
|
| 71 | + See Also |
| 72 | + -------- |
| 73 | + Quantity : The minimal Quantity API, separate from the Array API |
| 74 | +
|
| 75 | + Examples |
| 76 | + -------- |
| 77 | + >>> import numpy as np |
| 78 | + >>> import astropy.units as u |
| 79 | + >>> from quantity import Quantity |
| 80 | + >>> from quantity import api |
| 81 | +
|
| 82 | + >>> issubclass(Quantity, api.QuantityArray) |
| 83 | + True |
| 84 | +
|
| 85 | + >>> q = Quantity(value=np.array([1, 2, 3]), unit=u.m) |
| 86 | + >>> isinstance(q, api.QuantityArray) |
| 87 | + True |
| 88 | +
|
| 89 | + """ |
| 90 | + |
| 91 | + ... |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def __subclasshook__(cls: type, c: type) -> bool: |
| 95 | + """Enable the subclass check for data descriptors.""" |
| 96 | + return Quantity.__subclasscheck__(c) and issubclass(c, Array) |
0 commit comments