Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielaBreitman committed May 20, 2024
1 parent c924acd commit 60b590d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
18 changes: 3 additions & 15 deletions src/powerbox/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,6 @@ def _spherical2cartesian(r, phi_n):
np.sin(phi_n[0]) * np.sin(phi_n[1]),
]
)
elif phi_n.shape[0] == 3:
return r * np.array(
[
np.cos(phi_n[0]),
np.sin(phi_n[0]) * np.cos(phi_n[1]),
np.sin(phi_n[0]) * np.sin(phi_n[1]) * np.cos(phi_n[2]),
np.sin(phi_n[0]) * np.sin(phi_n[1]) * np.sin(phi_n[2]),
]
)
else:
phi_n = np.concatenate(
[2 * np.pi * np.ones(phi_n.shape[1])[np.newaxis, ...], phi_n], axis=0
Expand All @@ -266,12 +257,9 @@ def _asvoid(arr):
viewed as one value. This allows comparisons to be performed on the entire row.
"""
arr = np.ascontiguousarray(arr)
if np.issubdtype(arr.dtype, np.floating):
"""Care needs to be taken here since
np.array([-0.]).view(np.void) != np.array([0.]).view(np.void)
Adding 0. converts -0. to 0.
"""
arr += 0.0
# Since np.array([-0.]).view(np.void) != np.array([0.]).view(np.void)
# Adding 0. converts -0. to 0.
arr += 0.0
return arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))


Expand Down
29 changes: 26 additions & 3 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ def test_interp_w_weights():
assert np.all(p_k_lin == 1.0)


def test_interp_method():
x = np.linspace(-3, 3, 40)
P = np.ones((40, 40, 40))
freq = [x, x, x]
with pytest.raises(ValueError):
ave, coord, var = angular_average_nd(
P, freq, bins=20, get_variance=True, interpolation_method="abc"
)

with pytest.raises(ValueError):
ave, coord, var = angular_average(
P, freq, bins=20, get_variance=True, interpolation_method="abc"
)


def test_angular_avg_nd():
x = np.linspace(-3, 3, 40)
X, Y, Z = np.meshgrid(x, x, x)
Expand All @@ -64,8 +79,14 @@ def test_angular_avg_nd():
# Test 4D avg works
P = np.repeat(P, 10).reshape(40, 40, 40, 10)
freq = [x, x, x, np.linspace(-2, 2, 10)]
p_k_lin, k_av_bins_lin = angular_average(
P, freq, bins=10, interpolation_method="linear", weights=np.ones_like(P)
p_k_lin, k_av_bins_lin, sumweights = angular_average(
P,
freq,
bins=10,
log_bins=True,
return_sumweights=True,
interpolation_method="linear",
weights=np.ones_like(P),
)

p_k_lin, k_av_bins_lin = angular_average_nd(
Expand Down Expand Up @@ -188,7 +209,9 @@ def test_angular_avg_nd_2_1_varnull():
P = np.ones((200, 10))

coords = [x, np.linspace(-2, 2, 10)]
p_k, k_av_bins, var = angular_average_nd(P, coords, bins=20, n=1, get_variance=True)
p_k, k_av_bins, var, sw = angular_average_nd(
P, coords, bins=20, n=1, get_variance=True, return_sumweights=True
)

assert np.all(var == 0)

Expand Down

0 comments on commit 60b590d

Please sign in to comment.