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

💥 boom(quantity): rename UncheckedQuantity to BareQuantity #386

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Parametric Class

Non-parametric Class
A non-parametric class is a class that is not defined by a set of parameters.
The `unxt.UncheckedQuantity` class is an example of a non-parametric class; it does not use any parameter.
The `unxt.quantity.BareQuantity` class is an example of a non-parametric class; it does not use any parameter.

```
13 changes: 6 additions & 7 deletions docs/guides/type-checking.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,15 @@ separated
This parametric design is how `unxt` supports runtime type checking.

In `unxt` not all Quantity classes are parametric. The base class,
`AbstractQuantity` is not parametric, nor is the concrete class
`UncheckedQuantity`. Parametric classes incur a small performance overhead
(generally eliminated in [`jit`ted code][JAX-jit-link]), which
ultra-performance-optimized code might want to avoid, at the cost of inference
and checking of the dimensions.
`AbstractQuantity` is not parametric, nor is the concrete class `BareQuantity`.
Parametric classes incur a small performance overhead (generally eliminated in
[`jit`ted code][JAX-jit-link]), which ultra-performance-optimized code might
want to avoid, at the cost of inference and checking of the dimensions.

:::{note}

`UncheckedQuantity[<dimension>]` **does nothing** and is for informational
purposes only.
`BareQuantity[<dimension>]` **does nothing** and is for informational purposes
only.

:::

Expand Down
18 changes: 9 additions & 9 deletions src/unxt/_interop/unxt_interop_astropy/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from .custom_types import APYUnits
from unxt.dims import dimension_of
from unxt.quantity import AbstractQuantity, Quantity, UncheckedQuantity, ustrip
from unxt.quantity import AbstractQuantity, BareQuantity, Quantity, ustrip
from unxt.units import unit, unit_of

# ============================================================================
Expand Down Expand Up @@ -179,27 +179,27 @@ def convert_astropy_quantity_to_unxt_quantity(q: AstropyQuantity, /) -> Quantity


# ============================================================================
# UncheckedQuantity
# BareQuantity


@conversion_method(type_from=AstropyQuantity, type_to=UncheckedQuantity) # type: ignore[arg-type]
@conversion_method(type_from=AstropyQuantity, type_to=BareQuantity) # type: ignore[arg-type]
def convert_astropy_quantity_to_unxt_uncheckedquantity(
q: AstropyQuantity, /
) -> UncheckedQuantity:
"""Convert a `astropy.units.Quantity` to a `unxt.UncheckedQuantity`.
) -> BareQuantity:
"""Convert a `astropy.units.Quantity` to a `unxt.BareQuantity`.

Examples
--------
>>> from astropy.units import Quantity as AstropyQuantity
>>> from plum import convert
>>> from unxt.quantity import UncheckedQuantity
>>> from unxt.quantity import BareQuantity

>>> convert(AstropyQuantity(1.0, "cm"), UncheckedQuantity)
UncheckedQuantity(Array(1., dtype=float32), unit='cm')
>>> convert(AstropyQuantity(1.0, "cm"), BareQuantity)
BareQuantity(Array(1., dtype=float32), unit='cm')

"""
u = unit_of(q)
return UncheckedQuantity(ustrip(u, q), u)
return BareQuantity(ustrip(u, q), u)


###############################################################################
Expand Down
4 changes: 2 additions & 2 deletions src/unxt/_interop/unxt_interop_mpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from zeroth import zeroth

from unxt.quantity import AbstractQuantity, UncheckedQuantity, ustrip
from unxt.quantity import AbstractQuantity, BareQuantity, ustrip


@dataclass
Expand Down Expand Up @@ -52,7 +52,7 @@
if isinstance(obj, AbstractQuantity):
return ustrip(unit, obj)

return ustrip(unit, UncheckedQuantity.from_(obj, axis.get_units()))
return BareQuantity.from_(obj, axis.get_units()).unstrip(unit)

Check warning on line 55 in src/unxt/_interop/unxt_interop_mpl/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/unxt/_interop/unxt_interop_mpl/__init__.py#L55

Added line #L55 was not covered by tests

def axisinfo(self, unit: Any, _: Axes) -> matplotlib.units.AxisInfo:
"""Return axis information for this particular unit."""
Expand Down
12 changes: 6 additions & 6 deletions src/unxt/_src/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from jaxtyping import ArrayLike
from plum.parametric import type_unparametrized

from .quantity import AbstractQuantity, UncheckedQuantity as FastQ, ustrip
from .quantity import AbstractQuantity, BareQuantity, ustrip
from .units import AstropyUnits, unit, unit_of

Args = TypeVarTuple("Args")
Expand Down Expand Up @@ -81,7 +81,7 @@ def grad(
@partial(jax.grad, argnums=argnums)
def gradfun_mag(*args: Any) -> ArrayLike:
args_ = (
(a if unit is None else FastQ(a, unit))
(a if unit is None else BareQuantity(a, unit))
for a, unit in zip(args, theunits, strict=True)
)
return ustrip(fun(*args_)) # type: ignore[arg-type]
Expand Down Expand Up @@ -134,7 +134,7 @@ def jacfwd(

>>> jacfwd_cubbe_volume = u.experimental.jacfwd(cubbe_volume, units=("m",))
>>> jacfwd_cubbe_volume(u.Quantity(2.0, "m"))
UncheckedQuantity(Array(12., dtype=float32, weak_type=True), unit='m2')
BareQuantity(Array(12., dtype=float32, weak_type=True), unit='m2')

"""
argnums = eqx.error_if(
Expand All @@ -148,7 +148,7 @@ def jacfwd(
@partial(jax.jacfwd, argnums=argnums)
def jacfun_mag(*args: Any) -> R:
args_ = tuple(
(a if unit is None else FastQ(a, unit))
(a if unit is None else BareQuantity(a, unit))
for a, unit in zip(args, theunits, strict=True)
)
return fun(*args_) # type: ignore[arg-type]
Expand Down Expand Up @@ -201,15 +201,15 @@ def hessian(

>>> hessian_cubbe_volume = u.experimental.hessian(cubbe_volume, units=("m",))
>>> hessian_cubbe_volume(u.Quantity(2.0, "m"))
UncheckedQuantity(Array(12., dtype=float32, weak_type=True), unit='m')
BareQuantity(Array(12., dtype=float32, weak_type=True), unit='m')

"""
theunits: tuple[AstropyUnits, ...] = tuple(map(unit_or_none, units))

@partial(jax.hessian)
def hessfun_mag(*args: Any) -> R:
args_ = tuple(
(a if unit is None else FastQ(a, unit))
(a if unit is None else BareQuantity(a, unit))
for a, unit in zip(args, theunits, strict=True)
)
return fun(*args_) # type: ignore[arg-type]
Expand Down
3 changes: 2 additions & 1 deletion src/unxt/_src/quantity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"AbstractParametricQuantity",
"AbstractQuantity",
"Quantity",
"BareQuantity",
"UncheckedQuantity",
"is_unit_convertible",
"uconvert",
Expand All @@ -16,5 +17,5 @@
from .base import AbstractQuantity, is_any_quantity
from .base_parametric import AbstractParametricQuantity
from .quantity import Quantity
from .unchecked import UncheckedQuantity
from .unchecked import BareQuantity, UncheckedQuantity
from .value import convert_to_quantity_value
26 changes: 13 additions & 13 deletions src/unxt/_src/quantity/register_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@
from .api import ustrip
from .base import AbstractQuantity
from .quantity import Quantity
from .unchecked import UncheckedQuantity
from .unchecked import BareQuantity
from unxt.units import unit_of


@conversion_method(type_from=AbstractQuantity, type_to=UncheckedQuantity) # type: ignore[arg-type]
def _quantity_to_unchecked(q: AbstractQuantity, /) -> UncheckedQuantity:
@conversion_method(type_from=AbstractQuantity, type_to=BareQuantity) # type: ignore[arg-type]
def _quantity_to_unchecked(q: AbstractQuantity, /) -> BareQuantity:
"""Convert any quantity to an unchecked quantity.

Examples
--------
>>> from plum import convert
>>> from unxt.quantity import Quantity, UncheckedQuantity
>>> from unxt.quantity import Quantity, BareQuantity

>>> q = Quantity(1, "m")
>>> convert(q, UncheckedQuantity)
UncheckedQuantity(Array(1, dtype=int32, weak_type=True), unit='m')
>>> convert(q, BareQuantity)
BareQuantity(Array(1, dtype=int32, weak_type=True), unit='m')

The self-conversion doesn't copy the object:

>>> q = UncheckedQuantity(1, "m")
>>> convert(q, UncheckedQuantity) is q
>>> q = BareQuantity(1, "m")
>>> convert(q, BareQuantity) is q
True

"""
if isinstance(q, UncheckedQuantity):
if isinstance(q, BareQuantity):
return q
unit = unit_of(q)
return UncheckedQuantity(ustrip(unit, q), unit)
return BareQuantity(ustrip(unit, q), unit)


@conversion_method(type_from=AbstractQuantity, type_to=Quantity) # type: ignore[arg-type]
Expand All @@ -44,11 +44,11 @@ def _quantity_to_checked(q: AbstractQuantity, /) -> Quantity:
Examples
--------
>>> from plum import convert
>>> from unxt.quantity import Quantity, UncheckedQuantity
>>> from unxt.quantity import Quantity, BareQuantity

>>> q = UncheckedQuantity(1, "m")
>>> q = BareQuantity(1, "m")
>>> q
UncheckedQuantity(Array(1, dtype=int32, ...), unit='m')
BareQuantity(Array(1, dtype=int32, ...), unit='m')

>>> convert(q, Quantity)
Quantity['length'](Array(1, dtype=int32, ...), unit='m')
Expand Down
Loading