Skip to content

Commit

Permalink
feat: psp gala compat (#266)
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <[email protected]>
  • Loading branch information
nstarman authored Apr 27, 2024
1 parent 2c90375 commit 5e66b3a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/galax/coordinates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
from lazy_loader import attach_stub

from galax.setup_package import RUNTIME_TYPECHECKER
from galax.utils._optional_deps import HAS_GALA

__getattr__, __dir__, __all__ = attach_stub(__name__, __file__)

# runtime type checking
install_import_hook("galax.coordinates", RUNTIME_TYPECHECKER)

if HAS_GALA:
from . import _compat # noqa: F401

# Clean up the namespace
del install_import_hook, attach_stub, RUNTIME_TYPECHECKER
del install_import_hook, attach_stub, RUNTIME_TYPECHECKER, HAS_GALA
25 changes: 25 additions & 0 deletions src/galax/coordinates/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Compatibility.
TODO: make all the `gala` compat be in a linked package.
"""

__all__: list[str] = []

try: # TODO: less hacky way of supporting optional dependencies
import pytest
except ImportError:
pass
else:
_ = pytest.importorskip("gala")

import gala.dynamics as gd
from plum import conversion_method

import galax.coordinates as gcx


@conversion_method(type_from=gd.PhaseSpacePosition, type_to=gcx.PhaseSpacePosition) # type: ignore[misc]
def gala_psp_to_galax_psp(obj: gd.PhaseSpacePosition, /) -> gcx.PhaseSpacePosition:
"""`gala.dynamics.PhaseSpacePosition` -> `galax.coordinates.PhaseSpacePosition`."""
return gcx.PhaseSpacePosition(q=obj.pos, p=obj.vel, t=None)
6 changes: 5 additions & 1 deletion src/galax/dynamics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
from lazy_loader import attach_stub

from galax.setup_package import RUNTIME_TYPECHECKER
from galax.utils._optional_deps import HAS_GALA

with install_import_hook("galax.dynamics", RUNTIME_TYPECHECKER):
__getattr__, __dir__, __all__ = attach_stub(__name__, __file__)

if HAS_GALA:
from . import _compat # noqa: F401


# Cleanup
del install_import_hook, RUNTIME_TYPECHECKER
del install_import_hook, RUNTIME_TYPECHECKER, HAS_GALA
31 changes: 31 additions & 0 deletions src/galax/dynamics/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Compatibility.
TODO: make all the `gala` compat be in a linked package.
"""

__all__: list[str] = []

try: # TODO: less hacky way of supporting optional dependencies
import pytest
except ImportError:
pass
else:
_ = pytest.importorskip("gala")

import gala.dynamics as gd
from plum import conversion_method

import galax.dynamics as gdx


@conversion_method(type_from=gd.Orbit, type_to=gdx.Orbit) # type: ignore[misc]
def gala_orbit_to_galax_orbit(obj: gd.Orbit, /) -> gdx.Orbit:
"""`gala.dynamics.Orbit` -> `galax.dynamics.Orbit`."""
return gdx.Orbit(q=obj.pos, p=obj.vel, t=obj.t)


@conversion_method(type_from=gd.MockStream, type_to=gdx.MockStream) # type: ignore[misc]
def gala_mockstream_to_galax_mockstream(obj: gd.MockStream, /) -> gdx.MockStream:
"""`gala.dynamics.MockStream` -> `galax.dynamics.MockStream`."""
return gdx.MockStream(q=obj.pos, p=obj.vel, release_time=obj.release_time)

0 comments on commit 5e66b3a

Please sign in to comment.