Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: add Quantity API #16

Merged
merged 7 commits into from
Dec 5, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -141,6 +141,7 @@ ignore = [
"PLR09", # Too many <...>
"PLR2004", # Magic value used in comparison
"RET505", # Unnecessary `else`/`elif` after `return` statement
"RUF022", # `__all__` is not sorted
]

[tool.ruff.lint.per-file-ignores]
8 changes: 7 additions & 1 deletion src/quantity/__init__.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,13 @@
Copyright (c) 2024 Astropy Developers. All rights reserved.
"""

from . import api
from ._src import Quantity
from .version import version as __version__ # noqa: F401

__all__ = ["Quantity"]
__all__ = [
# modules
"api",
# functions and classes
"Quantity",
]
96 changes: 96 additions & 0 deletions src/quantity/_src/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""The Quantity API. Private module."""

__all__ = ["Quantity", "QuantityArray", "Unit"]

from typing import Protocol, runtime_checkable

from astropy.units import UnitBase as Unit

from .array_api import Array


@runtime_checkable
class Quantity(Protocol):
"""Minimal definition of the Quantity API.

At minimum a Quantity must have the following attributes:

- `value`: the numerical value of the quantity (adhering to the Array API)
- `unit`: the unit of the quantity

In practice, Quantities themselves must adhere to the Array API, not just
their values. This stricter requirement is described by the `QuantityArray`
protocol.

See Also
--------
QuantityArray : A Quantity that adheres to the Array API

Examples
--------
>>> import numpy as np
>>> import astropy.units as u
>>> from quantity import Quantity
>>> from quantity import api

>>> issubclass(Quantity, api.Quantity)
True

>>> q = Quantity(value=np.array([1, 2, 3]), unit=u.m)
>>> isinstance(q, api.Quantity)
True

"""

#: The numerical value of the quantity, adhering to the Array API.
value: Array

#: The unit of the quantity.
unit: Unit

@classmethod
def __subclasshook__(cls: type, c: type) -> bool:
"""Enable the subclass check for non-dataclass descriptors."""
return (
hasattr(c, "value") or "value" in getattr(c, "__annotations__", ())
) and (hasattr(c, "unit") or "unit" in getattr(c, "__annotations__", ()))


@runtime_checkable
class QuantityArray(Quantity, Array, Protocol):
"""An array-valued Quantity.

A QuantityArray is a Quantity that itself adheres to the Array API. This
means that the QuantityArray has properties like `shape`, `dtype`, and the
`__array_namespace__` method, among many other properties and methods. To
understand the full requirements of the Array API, see the `Array` protocol.
The `Quantity` protocol describes the minimal requirements for a Quantity,
separate from the Array API. QuantityArray is the combination of these two
protocols and is the most complete description of a Quantity.

See Also
--------
Quantity : The minimal Quantity API, separate from the Array API

Examples
--------
>>> import numpy as np
>>> import astropy.units as u
>>> from quantity import Quantity
>>> from quantity import api

>>> issubclass(Quantity, api.QuantityArray)
True

>>> q = Quantity(value=np.array([1, 2, 3]), unit=u.m)
>>> isinstance(q, api.QuantityArray)
True

"""

...

@classmethod
def __subclasshook__(cls: type, c: type) -> bool:
"""Enable the subclass check for non-dataclass descriptors."""
return Quantity.__subclasscheck__(c) and issubclass(c, Array)
25 changes: 25 additions & 0 deletions src/quantity/_src/array_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Minimal definition of the Array API.

NOTE: this module will be deprecated when
https://github.com/data-apis/array-api-typing is released.

"""

from __future__ import annotations

__all__ = ["HasArrayNameSpace", "Array"]

from typing import Any, Protocol, runtime_checkable


class HasArrayNameSpace(Protocol):
"""Minimal definition of the Array API."""

def __array_namespace__(self) -> Any: ...


@runtime_checkable
class Array(HasArrayNameSpace, Protocol):
"""Minimal definition of the Array API."""

def __pow__(self, other: Any) -> Array: ...
18 changes: 10 additions & 8 deletions src/quantity/_src/core.py
Original file line number Diff line number Diff line change
@@ -10,25 +10,27 @@
import numpy as np
from astropy.units.quantity_helper import UFUNC_HELPERS

from .api import QuantityArray
from .utils import has_array_namespace

if TYPE_CHECKING:
from typing import Any

from .api import Unit
from .array_api import Array


DIMENSIONLESS = u.dimensionless_unscaled

PYTHON_NUMBER = float | int | complex


def get_value_and_unit(arg, default_unit=None):
# HACK: interoperability with astropy Quantity. Have protocol?
try:
unit = arg.unit
except AttributeError:
return arg, default_unit
else:
return arg.value, unit
def get_value_and_unit(
arg: QuantityArray | Array, default_unit: Unit | None = None
) -> tuple[Array, Unit]:
return (
(arg.value, arg.unit) if isinstance(arg, QuantityArray) else (arg, default_unit)
)


def value_in_unit(value, unit):
16 changes: 16 additions & 0 deletions src/quantity/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Quantity-2.0: the Quantity API.

This module provides runtime-checkable Protocol objects that define the Quantity
API. In particular there are:

- `Quantity`: the minimal definition of a Quantity, separate from the Array API.
- `QuantityArray`: a Quantity that adheres to the Array API. This is the most
complete definition of a Quantity, inheriting from the Quantity API and
adding the requirements for the Array API.

"""
# Copyright (c) 2024 Astropy Developers. All rights reserved.

from ._src.api import Quantity, QuantityArray

__all__ = ["Quantity", "QuantityArray"]
20 changes: 20 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Test the Quantity class Array API compatibility."""

import astropy.units as u
import numpy as np

from quantity import Quantity, api


def test_issubclass_api():
"""Test that Quantity is a subclass of api.Quantity and api.QuantityArray."""
assert issubclass(Quantity, api.Quantity)
assert issubclass(Quantity, api.QuantityArray)


def test_isintsance_api():
"""Test that Quantity is an instance of api.Quantity and api.QuantityArray."""
q = Quantity(value=np.array([1, 2, 3]), unit=u.m)
assert isinstance(q, api.Quantity)
assert isinstance(q, api.QuantityArray)