-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: nstarman <nstarman@users.noreply.github.com> optional dependency Signed-off-by: nstarman <nstarman@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
449 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Input/output/conversion of potential objects. | ||
This module contains the machinery for I/O and conversion of potential objects. | ||
Conversion is useful for e.g. converting a | ||
:class:`galax.potential.AbstractPotential` object to a | ||
:class:`gala.potential.PotentialBase` object. | ||
""" | ||
|
||
__all__: list[str] = ["gala_to_galax"] | ||
|
||
|
||
from galax.utils._optional_deps import HAS_GALA | ||
|
||
if HAS_GALA: | ||
from .gala import gala_to_galax | ||
else: | ||
from .gala_noop import gala_to_galax # type: ignore[assignment] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
"""Interoperability.""" | ||
|
||
__all__ = ["gala_to_galax"] | ||
|
||
from functools import singledispatch | ||
|
||
import numpy as np | ||
from gala.potential import ( | ||
CompositePotential as GalaCompositePotential, | ||
HernquistPotential as GalaHernquistPotential, | ||
IsochronePotential as GalaIsochronePotential, | ||
KeplerPotential as GalaKeplerPotential, | ||
MiyamotoNagaiPotential as GalaMiyamotoNagaiPotential, | ||
NFWPotential as GalaNFWPotential, | ||
NullPotential as GalaNullPotential, | ||
PotentialBase as GalaPotentialBase, | ||
) | ||
|
||
from galax.potential._potential.base import AbstractPotentialBase | ||
from galax.potential._potential.builtin import ( | ||
HernquistPotential, | ||
IsochronePotential, | ||
KeplerPotential, | ||
MiyamotoNagaiPotential, | ||
NFWPotential, | ||
NullPotential, | ||
) | ||
from galax.potential._potential.composite import CompositePotential | ||
|
||
############################################################################## | ||
# GALA -> GALAX | ||
|
||
|
||
def _static_at_origin(pot: GalaPotentialBase, /) -> bool: | ||
return pot.R is None and np.array_equal(pot.origin, (0, 0, 0)) | ||
|
||
|
||
@singledispatch | ||
def gala_to_galax(pot: GalaPotentialBase, /) -> AbstractPotentialBase: | ||
"""Convert a :mod:`gala` potential to a :mod:`galax` potential. | ||
Parameters | ||
---------- | ||
pot : :class:`~gala.potential.PotentialBase` | ||
:mod:`gala` potential. | ||
Returns | ||
------- | ||
gala_pot : :class:`~galax.potential.AbstractPotentialBase` | ||
:mod:`galax` potential. | ||
""" | ||
msg = ( | ||
"`gala_to_galax` does not have a registered function to convert " | ||
f"{pot.__class__.__name__!r} to a `galax.AbstractPotentialBase` instance." | ||
) | ||
raise NotImplementedError(msg) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_composite(pot: GalaCompositePotential, /) -> CompositePotential: | ||
"""Convert a Gala CompositePotential to a Galax potential.""" | ||
return CompositePotential(**{k: gala_to_galax(p) for k, p in pot.items()}) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_hernquist(pot: GalaHernquistPotential, /) -> HernquistPotential: | ||
"""Convert a Gala HernquistPotential to a Galax potential.""" | ||
if not _static_at_origin(pot): | ||
msg = "Galax does not support rotating or offset potentials." | ||
raise TypeError(msg) | ||
params = pot.parameters | ||
return HernquistPotential(m=params["m"], c=params["c"], units=pot.units) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_isochrone(pot: GalaIsochronePotential, /) -> IsochronePotential: | ||
"""Convert a Gala IsochronePotential to a Galax potential.""" | ||
if not _static_at_origin(pot): | ||
msg = "Galax does not support rotating or offset potentials." | ||
raise TypeError(msg) | ||
params = pot.parameters | ||
return IsochronePotential(m=params["m"], b=params["b"], units=pot.units) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_kepler(pot: GalaKeplerPotential, /) -> KeplerPotential: | ||
"""Convert a Gala KeplerPotential to a Galax potential.""" | ||
if not _static_at_origin(pot): | ||
msg = "Galax does not support rotating or offset potentials." | ||
raise TypeError(msg) | ||
params = pot.parameters | ||
return KeplerPotential(m=params["m"], units=pot.units) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_miyamotonagi( | ||
pot: GalaMiyamotoNagaiPotential, / | ||
) -> MiyamotoNagaiPotential: | ||
"""Convert a Gala MiyamotoNagaiPotential to a Galax potential.""" | ||
if not _static_at_origin(pot): | ||
msg = "Galax does not support rotating or offset potentials." | ||
raise TypeError(msg) | ||
params = pot.parameters | ||
return MiyamotoNagaiPotential( | ||
m=params["m"], a=params["a"], b=params["b"], units=pot.units | ||
) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_nfw(pot: GalaNFWPotential, /) -> NFWPotential: | ||
"""Convert a Gala NFWPotential to a Galax potential.""" | ||
if not _static_at_origin(pot): | ||
msg = "Galax does not support rotating or offset potentials." | ||
raise TypeError(msg) | ||
params = pot.parameters | ||
return NFWPotential( | ||
m=params["m"], r_s=params["r_s"], softening_length=0, units=pot.units | ||
) | ||
|
||
|
||
@gala_to_galax.register | ||
def _gala_to_galax_nullpotential(pot: GalaNullPotential, /) -> NullPotential: | ||
"""Convert a Gala NullPotential to a Galax potential.""" | ||
if not _static_at_origin(pot): | ||
msg = "Galax does not support rotating or offset potentials." | ||
raise TypeError(msg) | ||
return NullPotential(units=pot.units) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""Interoperability.""" | ||
|
||
__all__ = ["gala_to_galax"] | ||
|
||
|
||
from typing import TYPE_CHECKING | ||
|
||
from galax.potential._potential.base import AbstractPotentialBase | ||
|
||
if TYPE_CHECKING: | ||
from gala.potential import PotentialBase as GalaPotentialBase | ||
|
||
|
||
def gala_to_galax(pot: "GalaPotentialBase", /) -> AbstractPotentialBase: | ||
"""Convert a :mod:`gala` potential to a :mod:`galax` potential. | ||
Parameters | ||
---------- | ||
pot : :class:`~gala.potential.PotentialBase` | ||
:mod:`gala` potential. | ||
Returns | ||
------- | ||
gala_pot : :class:`~galax.potential.AbstractPotentialBase` | ||
:mod:`galax` potential. | ||
""" | ||
msg = "The `gala` package must be installed to use this function. " | ||
raise ImportError(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.