Skip to content

Commit

Permalink
Update tests and example code to use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
MetinSa committed Apr 27, 2024
1 parent b484d74 commit 771a576
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 279 deletions.
11 changes: 5 additions & 6 deletions docs/examples/get_bandpass_integrated_emission.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from multiprocessing import cpu_count

import astropy.units as u
import healpy as hp
import matplotlib.pyplot as plt
Expand All @@ -15,16 +17,15 @@
plt.plot(freqs, weights)
plt.xlabel("Frequency [GHz]")
plt.ylabel("Weights")
plt.savefig("../img/bandpass.png", dpi=300)

model = Zodipy(model="planck18")
model = Zodipy(model="planck18", n_proc=cpu_count())

emission_central_freq = model.get_binned_emission_pix(
freq=center_freq,
pixels=np.arange(hp.nside2npix(nside)),
nside=nside,
obs_time=Time("2022-03-10"),
obs="SEMB-L2",
obs_pos="SEMB-L2",
)

emission_bandpass_integrated = model.get_binned_emission_pix(
Expand All @@ -33,7 +34,7 @@
pixels=np.arange(hp.nside2npix(nside)),
nside=nside,
obs_time=Time("2022-03-10"),
obs="SEMB-L2",
obs_pos="SEMB-L2",
)

hp.mollview(
Expand All @@ -43,7 +44,6 @@
cmap="afmhot",
norm="log",
)
plt.savefig("../img/center_freq.png", dpi=300)

hp.mollview(
emission_bandpass_integrated,
Expand All @@ -52,5 +52,4 @@
cmap="afmhot",
norm="log",
)
plt.savefig("../img/bandpass_integrated.png", dpi=300)
plt.show()
11 changes: 6 additions & 5 deletions docs/examples/get_binned_emission.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from multiprocessing import cpu_count

import astropy.units as u
import healpy as hp
import matplotlib.pyplot as plt
Expand All @@ -6,15 +8,15 @@

from zodipy import Zodipy

model = Zodipy("planck18")
model = Zodipy("planck18", n_proc=cpu_count())
nside = 256

binned_emission = model.get_binned_emission_pix(
857 * u.GHz,
pixels=np.arange(hp.nside2npix(nside)),
np.arange(hp.nside2npix(nside)),
freq=857 * u.GHz,
nside=nside,
obs_time=Time("2022-06-14"),
obs="earth",
obs_pos="earth",
)

hp.mollview(
Expand All @@ -25,5 +27,4 @@
max=1,
cmap="afmhot",
)
plt.savefig("../img/binned.png", dpi=300)
plt.show()
11 changes: 6 additions & 5 deletions docs/examples/get_binned_emission_solar_cutoff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from multiprocessing import cpu_count

import astropy.units as u
import healpy as hp
import matplotlib.pyplot as plt
Expand All @@ -6,15 +8,15 @@

from zodipy import Zodipy

model = Zodipy("dirbe")
model = Zodipy("dirbe", n_proc=cpu_count())
nside = 256

binned_emission = model.get_binned_emission_pix(
25 * u.micron,
pixels=np.arange(hp.nside2npix(nside)),
np.arange(hp.nside2npix(nside)),
nside=nside,
freq=25 * u.micron,
obs_time=Time("2020-01-01"),
obs="earth",
obs_pos="earth",
solar_cut=60 * u.deg,
)

Expand All @@ -26,5 +28,4 @@
coord="E",
cmap="afmhot",
)
plt.savefig("../img/binned_solar_cutoff.png", dpi=300)
plt.show()
13 changes: 7 additions & 6 deletions docs/examples/get_binned_gal_emission.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from multiprocessing import cpu_count

import astropy.units as u
import healpy as hp
import matplotlib.pyplot as plt
Expand All @@ -6,16 +8,16 @@

from zodipy import Zodipy

model = Zodipy("planck18")
model = Zodipy("planck18", n_proc=cpu_count())
nside = 256

binned_emission = model.get_binned_emission_pix(
857 * u.GHz,
pixels=np.arange(hp.nside2npix(nside)),
np.arange(hp.nside2npix(nside)),
freq=857 * u.GHz,
nside=nside,
obs_time=Time("2022-02-20"),
obs="earth",
coord_in="G", # Coordinates of the input pointing
obs_pos="earth",
frame="galactic", # Coordinates of the input pointing
)

hp.mollview(
Expand All @@ -26,5 +28,4 @@
min=0,
max=1,
)
plt.savefig("../img/binned_gal.png", dpi=300)
plt.show()
11 changes: 6 additions & 5 deletions docs/examples/get_comp_binned_emission.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from multiprocessing import cpu_count

import astropy.units as u
import healpy as hp
import matplotlib.pyplot as plt
Expand All @@ -6,15 +8,15 @@

from zodipy import Zodipy

model = Zodipy("dirbe")
model = Zodipy("dirbe", n_proc=cpu_count())
nside = 256

binned_emission = model.get_binned_emission_pix(
25 * u.micron,
pixels=np.arange(hp.nside2npix(nside)),
np.arange(hp.nside2npix(nside)),
freq=25 * u.micron,
nside=nside,
obs_time=Time("2022-01-01"),
obs="earth",
obs_pos="earth",
return_comps=True,
)
fig = plt.figure(figsize=(8, 6.5), constrained_layout=True)
Expand All @@ -29,5 +31,4 @@
sub=(3, 2, idx + 1),
fig=fig,
)
# plt.savefig("../img/binned_comp.png", dpi=300)
plt.show()
1 change: 0 additions & 1 deletion docs/examples/get_density_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@
plt.title("Cross section of the interplanetary dust density (yz-plane)")
plt.xlabel("x [AU]")
plt.ylabel("z [AU]")
# plt.savefig("../img/density_grid.png", dpi=300)
plt.show()
9 changes: 5 additions & 4 deletions docs/examples/get_emission_ang.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
from multiprocessing import cpu_count

import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
from astropy.time import Time

from zodipy import Zodipy

model = Zodipy("dirbe")
model = Zodipy("dirbe", n_proc=cpu_count())

latitudes = np.linspace(-90, 90, 10000) * u.deg
longitudes = np.zeros_like(latitudes)

emission = model.get_emission_ang(
30 * u.micron,
theta=longitudes,
phi=latitudes,
freq=30 * u.micron,
lonlat=True,
obs_time=Time("2022-06-14"),
obs="earth",
obs_pos="earth",
)


plt.plot(latitudes, emission)
plt.xlabel("Latitude [deg]")
plt.ylabel("Emission [MJy/sr]")
plt.savefig("../img/timestream.png", dpi=300)
plt.show()
7 changes: 4 additions & 3 deletions docs/examples/get_parallel_emission.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from multiprocessing import cpu_count

import astropy.units as u
import healpy as hp
Expand All @@ -10,15 +11,15 @@
nside = 256
pixels = np.arange(hp.nside2npix(nside))
obs_time = Time("2020-01-01")
n_proc = 8
n_proc = cpu_count()

model = Zodipy()
model_parallel = Zodipy(n_proc=n_proc)

start = time.perf_counter()
emission = model.get_binned_emission_pix(
40 * u.micron,
pixels=pixels,
freq=40 * u.micron,
nside=nside,
obs_time=obs_time,
)
Expand All @@ -27,9 +28,9 @@

start = time.perf_counter()
emission_parallel = model_parallel.get_binned_emission_pix(
40 * u.micron,
pixels=pixels,
nside=nside,
freq=40 * u.micron,
obs_time=obs_time,
)
print(
Expand Down
30 changes: 21 additions & 9 deletions tests/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import astropy.coordinates as coords
import astropy.units as u
import healpy as hp
import astropy_healpix as hp
import numpy as np
import numpy.typing as npt
from astropy import time
from hypothesis.extra.numpy import arrays
from hypothesis.strategies import (
DrawFn,
Expand Down Expand Up @@ -65,8 +66,8 @@ def quantities(


@composite
def times(draw: DrawFn) -> times.Time:
return draw(datetimes(min_value=MIN_DATE, max_value=MAX_DATE).map(times.Time))
def times(draw: DrawFn) -> time.Time:
return draw(datetimes(min_value=MIN_DATE, max_value=MAX_DATE).map(time.Time))


@composite
Expand All @@ -76,7 +77,18 @@ def nsides(draw: Callable[[SearchStrategy[int]], int]) -> int:

@composite
def frames(draw: DrawFn) -> type[coords.BaseCoordinateFrame]:
return draw(sampled_from([coords.ICRS, coords.Galactic, coords.HeliocentricMeanEcliptic]))
return draw(
sampled_from(
[
coords.BarycentricTrueEcliptic,
coords.ICRS,
coords.Galactic,
"galactic",
"barycentrictrueecliptic",
"icrs",
]
)
)


@composite
Expand All @@ -100,10 +112,10 @@ def sky_coords(draw: DrawFn) -> coords.SkyCoord:

@composite
def pixels(draw: DrawFn, nside: int) -> int | list[int] | npt.NDArray[np.integer]:
npix = hp.nside2npix(nside)
pixel_strategy = integers(min_value=0, max_value=npix - 1)
healpix = hp.HEALPix(nside=nside)
pixel_strategy = integers(min_value=0, max_value=healpix.npix - 1)

shape = draw(integers(min_value=1, max_value=npix - 1))
shape = draw(integers(min_value=1, max_value=healpix.npix - 1))

list_stategy = lists(pixel_strategy, min_size=1)
array_strategy = arrays(dtype=int, shape=shape, elements=pixel_strategy)
Expand Down Expand Up @@ -189,8 +201,8 @@ def normalize_array(


@composite
def obs_positions(draw: DrawFn, model: zodipy.Zodipy, obs_time: times.Time) -> str:
def get_obs_dist(obs: str, obs_time: times.Time) -> u.Quantity[u.AU]:
def obs_positions(draw: DrawFn, model: zodipy.Zodipy, obs_time: time.Time) -> str:
def get_obs_dist(obs: str, obs_time: time.Time) -> u.Quantity[u.AU]:
if obs == "semb-l2":
obs_pos = (
coords.get_body("earth", obs_time)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_get_emission.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_get_emission_skycoord(
obs_time=time,
obs_pos=observer,
)
assert emission.size == 1
assert emission.size == coordinates.size


@given(zodipy_models(), times(), nsides(), data())
Expand Down
Loading

0 comments on commit 771a576

Please sign in to comment.