-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests+fix: starry comparison
- Loading branch information
Showing
6 changed files
with
208 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# mypy: ignore-errors | ||
|
||
import nox | ||
|
||
ALL_PYTHON_VS = ["3.9", "3.11"] | ||
|
||
|
||
@nox.session(python=ALL_PYTHON_VS) | ||
def test(session): | ||
session.install(".[test]") | ||
session.run("pytest", *session.posargs) | ||
|
||
|
||
@nox.session(python=["3.9"]) | ||
def comparison(session): | ||
session.install(".[test,comparison]", "numpy<1.22") | ||
session.run("python", "-c", "import starry") | ||
session.run("python", "-c", "import theano") | ||
if session.posargs: | ||
args = session.posargs | ||
else: | ||
args = ("tests/starry_comparison",) | ||
session.run( | ||
"pytest", | ||
"-n", | ||
"auto", | ||
*args, | ||
env={"JAX_ENABLE_X64": "True"}, | ||
) |
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 |
---|---|---|
@@ -1,36 +1,30 @@ | ||
[tool.poetry] | ||
[project] | ||
name = "spotter" | ||
version = "0.0.5-beta" | ||
version = "0.0.6-beta" | ||
description = "Stellar contamination estimates from rotational light curves" | ||
authors = ["Lionel Garcia", "Benjamin Rackham"] | ||
authors = [{name="Lionel Garcia"}, {name="Benjamin Rackham"}] | ||
license = "MIT" | ||
readme = "readme.md" | ||
requires-python = ">=3.9" | ||
packages = [{ include = "spotter" },] | ||
dependencies = ["numpy", "healpy", "matplotlib", "jax", "jaxlib"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.9" | ||
numpy = "^1.25.0" | ||
healpy = "*" | ||
matplotlib = "*" | ||
jax = "*" | ||
jaxlib = "*" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "*" | ||
black = "*" | ||
|
||
[tool.poetry.group.docs.dependencies] | ||
sphinx = "*" | ||
docutils = "*" | ||
jupyterlab = "*" | ||
myst-parser = "*" | ||
sphinx-book-theme = "^1.0.0" | ||
myst-nb = "*" | ||
sphinx-copybutton = "*" | ||
toml = "*" | ||
ipywidgets = "*" | ||
tqdm = "*" | ||
[project.optional-dependencies] | ||
dev = ["black", "pytest", "nox"] | ||
test = ["pytest", "pytest-xdist"] | ||
comparison = ["starry", "tqdm", "xarray<2023.10.0", "numpy<1.22"] | ||
docs = [ | ||
"sphinx", | ||
"docutils", | ||
"jupyterlab", | ||
"myst-parser", | ||
"sphinx-book-theme", | ||
"myst-nb", | ||
"sphinx-copybutton", | ||
"toml", | ||
"ipywidgets" | ||
] | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
requires = ["hatchling", "hatch-vcs"] | ||
build-backend = "hatchling.build" |
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,79 @@ | ||
from collections import defaultdict | ||
|
||
import healpy as hp | ||
import numpy as np | ||
import pytest | ||
|
||
from spotter import Star | ||
|
||
|
||
@pytest.mark.parametrize("deg", (3, 10)) | ||
@pytest.mark.parametrize("u", ([], [0.1, 0.4])) | ||
def test_starry(deg, u): | ||
starry = pytest.importorskip("starry") | ||
starry.config.lazy = False | ||
|
||
# starry map with random coefficients | ||
np.random.seed(deg + len(u)) | ||
y = np.random.randn((deg + 1) ** 2) | ||
y[0] = 1.0 | ||
y[1:] *= 1e-2 | ||
inc = np.pi / 2 | ||
ms = starry.Map(ydeg=deg, udeg=len(u), inc=np.rad2deg(inc)) | ||
if len(u) > 0: | ||
ms[1:] = u | ||
ms[:, :] = y | ||
|
||
# rotation to map healpix | ||
_m = starry._core.core.OpsYlm(deg, 0, 0, 1) | ||
ry = _m.dotR([y], 0.0, 1.0, 0.0, np.pi / 2)[0] | ||
ry = _m.dotR([ry], 1.0, 0.0, 0.0, -np.pi / 2)[0] | ||
ry = _m.dotR([ry], 0.0, 0.0, 1.0, np.pi)[0] | ||
|
||
def starry2healpy(y): | ||
# Edmonds to Condon-Shortley phase convention | ||
lmax = int(np.floor(np.sqrt(len(y)))) | ||
|
||
_hy = defaultdict(lambda: 0 + 0j) | ||
|
||
i = 0 | ||
|
||
for l in range(0, lmax): | ||
for m in range(-l, l + 1): | ||
j = hp.sphtfunc.Alm.getidx(lmax, l, np.abs(m)) | ||
if m < 0: | ||
_hy[j] += 1j * y[i] / (np.sqrt(2) * (-1) ** m) | ||
elif m == 0: | ||
_hy[j] += y[i] | ||
else: | ||
_hy[j] += y[i] / (np.sqrt(2) * (-1) ** m) | ||
i += 1 | ||
|
||
hn = hp.sphtfunc.Alm.getsize(lmax) | ||
hy = np.zeros(hn, dtype=np.complex128) | ||
for i in range(hn): | ||
hy[i] = _hy[i] | ||
|
||
return hy | ||
|
||
# starry to healpix to spotter | ||
N = 2**7 | ||
y2 = starry2healpy(ry) | ||
mh = hp.alm2map(y2, nside=N) | ||
|
||
# mh with same ptp as ims | ||
ims = ms.render(projection="moll") | ||
mh = mh - np.nanmin(mh) | ||
mh = mh / np.nanmax(mh) | ||
mh = mh * (np.nanmax(ims) - np.nanmin(ims)) | ||
mh = mh + np.nanmin(ims) | ||
|
||
star = Star(N=N, u=u) | ||
star.map_spot = 1 - mh | ||
|
||
# comparison | ||
phases = np.linspace(0, 2 * np.pi, 100) | ||
expected = np.array(ms.flux(theta=np.rad2deg(phases))) | ||
calc = star.flux(phases) | ||
|
||
np.testing.assert_allclose(calc, expected, atol=1e-4) |