Skip to content

Commit

Permalink
register gala UnitSystem -> galax Unitsystem converter
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed Jan 21, 2024
1 parent 2516bbb commit b025e83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/galax/potential/_potential/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""galax: Galactic Dynamix in Jax."""


from functools import singledispatch
from typing import Any

Expand Down Expand Up @@ -41,3 +40,19 @@ def _from_named(value: str, /) -> UnitSystem:

msg = f"cannot convert {value} to a UnitSystem"
raise NotImplementedError(msg)


##############################################################################
# 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 UnitSystem as GalaUnitSystem

@converter_to_usys.register
def _from_gala(value: GalaUnitSystem, /) -> UnitSystem:
# TODO: transfer all informatino, not just core units
return UnitSystem(*value._core_units) # noqa: SLF001
9 changes: 9 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,14 @@ 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."""
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)


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

Expand Down

0 comments on commit b025e83

Please sign in to comment.