From b676d2f119ce83a8e1c6ea34a5e11b78278027e5 Mon Sep 17 00:00:00 2001 From: Jeremy Aweda Date: Mon, 8 Oct 2018 15:39:28 +0200 Subject: [PATCH 1/2] Test for util.db, util.direction_vector This is a part of #61 --- sfs/util.py | 2 +- tests/test_util.py | 62 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/sfs/util.py b/sfs/util.py index 006bf47f..f91c4a6a 100644 --- a/sfs/util.py +++ b/sfs/util.py @@ -361,7 +361,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): diff --git a/tests/test_util.py b/tests/test_util.py index 89ff89d1..31b617e7 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -4,14 +4,15 @@ 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))), ] @@ -27,3 +28,48 @@ 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) + 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) From 059410f2aab46e83f8b2bf5898a9b17c346e6ed4 Mon Sep 17 00:00:00 2001 From: Matthias Geier Date: Tue, 5 Feb 2019 20:33:36 +0100 Subject: [PATCH 2/2] TST: A little update to the dB tests --- tests/test_util.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/tests/test_util.py b/tests/test_util.py index 31b617e7..d46fd76c 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -51,25 +51,20 @@ def test_direction_vector(input, vector): db_data = [ (0, -np.inf), + (0.5, -3.01029995663981), (1, 0), + (2, 3.01029995663981), (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) - assert_allclose(d, decibel) +@pytest.mark.parametrize('linear, power_db', db_data) +def test_db_amplitude(linear, power_db): + d = sfs.util.db(linear) + assert_allclose(d, power_db * 2) -@pytest.mark.parametrize('linear, decibel', db_data) -def test_db_power(linear, decibel): - d = sfs.util.db(linear) - assert_allclose(d, 2 * decibel) +@pytest.mark.parametrize('linear, power_db', db_data) +def test_db_power(linear, power_db): + d = sfs.util.db(linear, power=True) + assert_allclose(d, power_db)