Skip to content

Commit

Permalink
Gala unit system (#87)
Browse files Browse the repository at this point in the history
* optional_deps
* register gala UnitSystem -> galax Unitsystem converter
* gala optional dependencies
* rearrange
* test dimensionless

Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed Jan 24, 2024
1 parent 26ca62b commit 9382775
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ dependencies = [
]

[project.optional-dependencies]
compat-gala = [
"gala",
]
compat-all = [
"galax[compat-gala]",
]
test = [
"hypothesis[numpy]",
"pytest >=6",
Expand All @@ -57,6 +63,7 @@ docs = [
"furo>=2023.08.17",
]
all = [
"galax[compat-all]",
"galax[test]",
"galax[docs]",
"galax[dev]",
Expand Down
34 changes: 33 additions & 1 deletion src/galax/potential/_potential/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from astropy.units import Quantity
from jax import Array

from galax.units import UnitSystem, dimensionless, galactic, solarsystem
from galax.units import (
DimensionlessUnitSystem,
UnitSystem,
dimensionless,
galactic,
solarsystem,
)


@singledispatch
Expand Down Expand Up @@ -141,3 +147,29 @@ def _convert_from_representation(
)
)
return _convert_from_baserep(value, units=units)


##############################################################################
# Gala compatibility
# TODO: move this to an interoperability module

# isort: split
from galax.utils._optional_deps import HAS_GALA # noqa: E402

if HAS_GALA:
from gala.units import (
DimensionlessUnitSystem as GalaDimensionlessUnitSystem,
UnitSystem as GalaUnitSystem,
)

@converter_to_usys.register
def _from_gala(value: GalaUnitSystem, /) -> UnitSystem:
usys = UnitSystem(*value._core_units) # noqa: SLF001
usys._registry = value._registry # noqa: SLF001
return usys

@converter_to_usys.register
def _from_gala_dimensionless(
value: GalaDimensionlessUnitSystem, /
) -> DimensionlessUnitSystem:
return dimensionless
7 changes: 7 additions & 0 deletions src/galax/utils/_optional_deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Optional dependencies."""

__all__ = ["HAS_GALA"]

from importlib.util import find_spec

HAS_GALA = find_spec("gala") is not None
18 changes: 18 additions & 0 deletions tests/unit/potential/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
galactic,
solarsystem,
)
from galax.utils._optional_deps import HAS_GALA


class TestConverterToUtils:
Expand Down Expand Up @@ -45,6 +46,23 @@ def test_from_name(self):
with pytest.raises(NotImplementedError):
converter_to_usys("invalid_value")

@pytest.mark.skipif(not HAS_GALA, reason="requires gala")
def test_from_gala(self):
"""Test conversion from gala."""
# -------------------------------
# UnitSystem
from gala.units import UnitSystem as GalaUnitSystem

value = GalaUnitSystem(u.km, u.s, u.Msun, u.radian)
assert converter_to_usys(value) == UnitSystem(*value._core_units)

# -------------------------------
# DimensionlessUnitSystem
from gala.units import DimensionlessUnitSystem as GalaDimensionlessUnitSystem

value = GalaDimensionlessUnitSystem()
assert converter_to_usys(value) == dimensionless


# ============================================================================

Expand Down

0 comments on commit 9382775

Please sign in to comment.