Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG degrees mixed with radians in random draws #160

Merged
merged 4 commits into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions healsparse/healSparseRandoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def make_uniform_randoms(sparse_map, n_random, rng=None):
rng = np.random.RandomState()

# Generate uniform points on a unit sphere
r = 1.0
min_gen = 10000
max_gen = 1000000

Expand All @@ -87,11 +86,18 @@ def make_uniform_randoms(sparse_map, n_random, rng=None):
# Get range of coverage pixels
cov_theta, cov_phi = hpg.pixel_to_angle(sparse_map.nside_coverage, cov_pix, nest=True, lonlat=False)

extra_boundary = 2.0 * hpg.nside_to_resolution(sparse_map.nside_coverage)

ra_range = np.clip([np.min(cov_phi - extra_boundary),
np.max(cov_phi + extra_boundary)],
0.0, 2.0 * np.pi)
extra_boundary = 2.0 * hpg.nside_to_resolution(
sparse_map.nside_coverage,
units="radians",
)
sintheta = np.sin(cov_theta)

ra_range = np.clip(
[np.min(cov_phi - extra_boundary / sintheta),
np.max(cov_phi + extra_boundary / sintheta)],
0.0,
2.0 * np.pi,
)
dec_range = np.clip([np.min((np.pi/2. - cov_theta) - extra_boundary),
np.max((np.pi/2. - cov_theta) + extra_boundary)],
-np.pi/2., np.pi/2.)
Expand All @@ -102,16 +108,16 @@ def make_uniform_randoms(sparse_map, n_random, rng=None):
cov_phi_rot = cov_phi + np.pi
test, = np.where(cov_phi_rot > 2.0 * np.pi)
cov_phi_rot[test] -= 2.0 * np.pi
ra_range_rot = np.clip([np.min(cov_phi_rot - extra_boundary),
np.max(cov_phi_rot + extra_boundary)],
ra_range_rot = np.clip([np.min(cov_phi_rot - extra_boundary / sintheta),
np.max(cov_phi_rot + extra_boundary / sintheta)],
0.0, 2.0 * np.pi)
if ((ra_range_rot[1] - ra_range_rot[0]) < ((ra_range[1] - ra_range[0]) - 0.1)):
# This is a more efficient range in rotated space
ra_range = ra_range_rot
rotated = True

# And the spherical coverage
z_range = r * np.sin(dec_range)
z_range = np.sin(dec_range)
phi_range = ra_range

ra_rand = np.zeros(n_random)
Expand All @@ -128,7 +134,7 @@ def make_uniform_randoms(sparse_map, n_random, rng=None):

z = rng.uniform(low=z_range[0], high=z_range[1], size=n_gen)
phi = rng.uniform(low=phi_range[0], high=phi_range[1], size=n_gen)
theta = np.arcsin(z / r)
theta = np.arcsin(z)

ra_rand_temp = np.degrees(phi)
dec_rand_temp = np.degrees(theta)
Expand Down
Loading