Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
dist/
.eggs/
sfs.egg-info/

2 changes: 1 addition & 1 deletion sfs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def db(x, power=False):

"""
with np.errstate(divide='ignore'):
return 10 if power else 20 * np.log10(np.abs(x))
return (10 if power else 20) * np.log10(np.abs(x))


class XyzComponents(np.ndarray):
Expand Down
92 changes: 84 additions & 8 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import numpy as np
from numpy.testing import assert_allclose
from numpy.testing import assert_array_equal
import pytest
import sfs

cart_sph_data = [
((1, 1, 1), (np.pi/4, np.arccos(1/np.sqrt(3)), np.sqrt(3))),
((-1, 1, 1), (np.arctan2(1, -1), np.arccos(1/np.sqrt(3)), np.sqrt(3))),
((1, -1, 1), (-np.pi/4, np.arccos(1/np.sqrt(3)), np.sqrt(3))),
((-1, -1, 1), (np.arctan2(-1, -1), np.arccos(1/np.sqrt(3)), np.sqrt(3))),
((1, 1, -1), (np.pi/4, np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
((-1, 1, -1), (np.arctan2(1, -1), np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
((1, -1, -1), (-np.pi/4, np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
((-1, -1, -1), (np.arctan2(-1, -1), np.arccos(-1/np.sqrt(3)), np.sqrt(3))),
((1, 1, 1), (np.pi / 4, np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
((-1, 1, 1), (np.arctan2(1, -1), np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
((1, -1, 1), (-np.pi / 4, np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
((-1, -1, 1), (np.arctan2(-1, -1), np.arccos(1 / np.sqrt(3)), np.sqrt(3))),
((1, 1, -1), (np.pi / 4, np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
((-1, 1, -1), (np.arctan2(1, -1), np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
((1, -1, -1), (-np.pi / 4, np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
((-1, -1, -1), (np.arctan2(-1, -1),
np.arccos(-1 / np.sqrt(3)), np.sqrt(3))),
]


Expand All @@ -27,3 +29,77 @@ def test_sph2cart(coord, polar):
alpha, beta, r = polar
b = sfs.util.sph2cart(alpha, beta, r)
assert_allclose(b, coord)


direction_vector_data = [
((np.pi / 4, np.pi / 4), (0.5, 0.5, np.sqrt(2) / 2)),
((3 * np.pi / 4, 3 * np.pi / 4), (-1 / 2, 1 / 2, -np.sqrt(2) / 2)),
((3 * np.pi / 4, -3 * np.pi / 4), (1 / 2, -1 / 2, -np.sqrt(2) / 2)),
((np.pi / 4, -np.pi / 4), (-1 / 2, -1 / 2, np.sqrt(2) / 2)),
((-np.pi / 4, np.pi / 4), (1 / 2, -1 / 2, np.sqrt(2) / 2)),
((-3 * np.pi / 4, 3 * np.pi / 4), (-1 / 2, -1 / 2, -np.sqrt(2) / 2)),
((-3 * np.pi / 4, -3 * np.pi / 4), (1 / 2, 1 / 2, -np.sqrt(2) / 2)),
((-np.pi / 4, -np.pi / 4), (-1 / 2, 1 / 2, np.sqrt(2) / 2)),
]


@pytest.mark.parametrize('input, vector', direction_vector_data)
def test_direction_vector(input, vector):
alpha, beta = input
c = sfs.util.direction_vector(alpha, beta)
assert_allclose(c, vector)


db_data = [
(0, -np.inf),
(1, 0),
(10, 10),
(10 * 2, (10 + 3.010299956639813)),
(10 * 10, (10 + 10)),
(10 * 3, (10 + 4.771212547196624)),
(10 * 4, (10 + 6.02059991327962)),
(10 * 0.5, (10 - 3.01029995663981198)),
(10 * 0.1, (10 - 10)),
(10 * 0.25, (10 - 6.02059991327962396))
]


@pytest.mark.parametrize('linear, decibel', db_data)
def test_db_amplitude(linear, decibel):
d = sfs.util.db(linear, True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the function name.

Also, you should not use a bare True or False as a function argument (unless probably if it's the only argument).
You should use it as a keyword argument, i.e. you should use power=True (except you should actually use that in test_db_power()).

assert_allclose(d, decibel)


@pytest.mark.parametrize('linear, decibel', db_data)
def test_db_power(linear, decibel):
d = sfs.util.db(linear)
assert_allclose(d, 2 * decibel)


image_sources_for_box_data = [(
(([1, 1, 1], [5, 4, 3], 1)),
((((1, 1, -1),
(1, -1, 1),
(-1, 1, 1),
(1, 1, 1),
(9, 1, 1),
(1, 7, 1),
(1, 1, 5)), ((0, 0, 0, 0, 1, 0),
(0, 0, 1, 0, 0, 0),
(1, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0),
(0, 1, 0, 0, 0, 0),
(0, 0, 0, 1, 0, 0),
(0, 0, 0, 0, 0, 1)))),
)]


@pytest.mark.parametrize('in_image_source, out_image_source',
image_sources_for_box_data)
def test_image_sources_for_box(in_image_source, out_image_source):
X, L, N = in_image_source
Xs, wall = out_image_source
e = sfs.util.image_sources_for_box(X, L, N)
img, wall_count = e
assert_allclose(img, Xs)
assert_array_equal(wall_count, wall)