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

build: adopt spec-0 #208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13"]
runs-on: [ubuntu-latest, macos-latest, windows-latest]

include:
- python-version: pypy-3.10
runs-on: ubuntu-latest
# TODO: check when pypy3.11 is available
# include:
# - python-version: pypy-3.11
# runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -89,7 +90,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
python-version: ["3.11"]
runs-on: [ubuntu-latest]

steps:
Expand Down Expand Up @@ -124,7 +125,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13"]
runs-on: [ubuntu-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.10
3.11
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ consider citing this work.

[![codecov][codecov-badge]][codecov-link]
[![Actions Status][actions-badge]][actions-link]
[![SPEC 0 — Minimum Supported Dependencies][spec0-badge]][spec0-link]

We welcome contributions!

Expand All @@ -118,6 +119,8 @@ We welcome contributions!
[pypi-version]: https://img.shields.io/pypi/v/unxt
[rtd-badge]: https://readthedocs.org/projects/unxt/badge/?version=latest
[rtd-link]: https://unxt.readthedocs.io/en/latest/?badge=latest
[spec0-badge]: https://img.shields.io/badge/SPEC-0-green?labelColor=%23004811&color=%235CA038
[spec0-link]: https://scientific-python.org/specs/spec-0000/
[zenodo-badge]: https://zenodo.org/badge/734877295.svg
[zenodo-link]: https://zenodo.org/doi/10.5281/zenodo.10850455

Expand Down
9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dynamic = ["version"]
description = "Quantities in JAX"
readme = "README.md"
requires-python = ">=3.10,<3.14"
requires-python = ">=3.11,<3.14"
authors = [
{ name = "GalacticDynamics", email = "[email protected]" },
{ name = "Nathaniel Starkman", email = "[email protected]" },
Expand All @@ -17,7 +17,6 @@
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
Expand All @@ -27,7 +26,7 @@
]
license.file = "LICENSE"
dependencies = [
"astropy>=6.0",
"astropy>=7.0.0",
"dataclassish>=0.3",
"equinox>=0.11.8",
"is-annotated>=1.0",
Expand Down Expand Up @@ -133,7 +132,7 @@
disallow_untyped_defs = false
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
files = ["src", "tests"]
python_version = "3.10"
python_version = "3.11"
strict = true
warn_return_any = false
warn_unreachable = true
Expand Down Expand Up @@ -238,7 +237,7 @@
"wrong-import-order", # handled by ruff
"wrong-import-position",
]
py-version = "3.10"
py-version = "3.11"
reports.output-format = "colorized"
similarities.ignore-imports = "yes"

Expand Down
49 changes: 3 additions & 46 deletions src/unxt/_interop/unxt_interop_astropy/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
import astropy.units as apyu
from astropy.coordinates import Angle as AstropyAngle, Distance as AstropyDistance
from astropy.units import Quantity as AstropyQuantity
from jaxtyping import Array
from packaging.version import Version
from plum import conversion_method, dispatch, type_unparametrized as type_up

import quaxed.numpy as jnp
from dataclassish import field_items, replace

from unxt._interop.optional_deps import OptDeps
from unxt.dims import dimension_of
from unxt.quantity import AbstractQuantity, Quantity, UncheckedQuantity

Expand Down Expand Up @@ -176,46 +173,6 @@ def convert_astropy_quantity_to_unxt_uncheckedquantity(
)


if Version("7.0") <= OptDeps.ASTROPY.version:

def _apy7_unit_to(self: AstropyUnit, other: AstropyUnit, value: Array, /) -> Array:
return self.to(other, value)

else:

def _apy7_unit_to(self: AstropyUnit, other: AstropyUnit, value: Array, /) -> Array:
"""Convert the value to the other unit."""
# return self.get_converter(Unit(other), equivalencies)(value)
# First see if it is just a scaling.
try:
scale = self._to(other)
except apyu.UnitsError:
pass
else:
return scale * value

# if that doesn't work, maybe we can do it with equivalencies?
try:
return self._apply_equivalencies(
self, other, self._normalize_equivalencies([])
)(value)
except apyu.UnitsError as exc:
# Last hope: maybe other knows how to do it?
# We assume the equivalencies have the unit itself as first item.
# TODO: maybe better for other to have a `_back_converter` method?
if hasattr(other, "equivalencies"):
for funit, tunit, _, b in other.equivalencies:
if other is funit:
try:
converter = self.get_converter(tunit, [])
except Exception: # noqa: BLE001, S110 # pylint: disable=W0718
pass
else:
return b(converter(value))

raise exc # noqa: TRY201


@dispatch # type: ignore[misc]
def uconvert(unit: AstropyUnit, x: AbstractQuantity, /) -> AbstractQuantity:
"""Convert the quantity to the specified units.
Expand All @@ -233,21 +190,21 @@ def uconvert(unit: AstropyUnit, x: AbstractQuantity, /) -> AbstractQuantity:
>>> with apyu.add_enabled_equivalencies(apyu.temperature()):
... y = x.uconvert("deg_C")
>>> y
Quantity['temperature'](Array([-272.15, -271.15, -270.15], dtype=float32), unit='deg_C')
Quantity['temperature'](Array([-272.15, -271.15, -270.15], dtype=float32, ...), unit='deg_C')

>>> x = Quantity([1, 2, 3], "radian")
>>> with apyu.add_enabled_equivalencies(apyu.dimensionless_angles()):
... y = x.uconvert("")
>>> y
Quantity['dimensionless'](Array([1., 2., 3.], dtype=float32), unit='')
Quantity['dimensionless'](Array([1., 2., 3.], dtype=float32, ...), unit='')

""" # noqa: E501
# Hot-path: if no unit conversion is necessary
if x.unit == unit:
return x

# Compute the value. Used in all subsequent branches.
value = _apy7_unit_to(x.unit, unit, x.value)
value = x.unit.to(unit, x.value)

# If the dimensions are the same, we can just replace the value and unit.
if dimension_of(x.unit) == dimension_of(unit):
Expand Down
2 changes: 1 addition & 1 deletion src/unxt/_src/quantity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from unxt._src.units.core import AbstractUnits

if TYPE_CHECKING:
from typing_extensions import Self
from typing import Self

Check warning on line 31 in src/unxt/_src/quantity/base.py

View check run for this annotation

Codecov / codecov/patch

src/unxt/_src/quantity/base.py#L31

Added line #L31 was not covered by tests


FMT = TypeVar("FMT")
Expand Down
2 changes: 1 addition & 1 deletion src/unxt/_src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import TYPE_CHECKING, Any, cast

if TYPE_CHECKING:
from typing_extensions import Self
from typing import Self

Check warning on line 11 in src/unxt/_src/utils.py

View check run for this annotation

Codecov / codecov/patch

src/unxt/_src/utils.py#L11

Added line #L11 was not covered by tests


_singleton_insts: dict[type, object] = {}
Expand Down
Loading