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

use classes in tests #28

Merged
merged 2 commits into from
Aug 2, 2024
Merged
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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ universal = true
dev-dependencies = [
"ruff>=0.5.5",
"pytest>=8.3.2",
"coverage>=7.6.0",
]
1 change: 1 addition & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ comm==0.2.2
# via ipywidgets
contourpy==1.2.1
# via matplotlib
coverage==7.6.0
cycler==0.12.1
# via matplotlib
debugpy==1.8.2
Expand Down
1 change: 0 additions & 1 deletion tests/get_data_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import pathlib


Expand Down
6 changes: 0 additions & 6 deletions tests/test__signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@
import pytest
import tempfile

import itertools as it
from acoustic_toolbox.signal import EqualBand
import matplotlib.pyplot as plt

#def test_operator():

#n = 10000
#fs = 5000


def test_wav():
"""Test writing to and reading from wav file."""
Expand Down
30 changes: 16 additions & 14 deletions tests/test_atmosphere.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import numpy as np
from acoustic_toolbox.atmosphere import Atmosphere

import sys
sys.path.append('..')
from get_data_path import data_path
from tests.get_data_path import data_path


class TestAtmosphere:
def test_standard_atmosphere(self):
"""
Test :mod:`acoustic_toolbox.atmosphere`
"""

def test_standard_atmosphere(self):
a = Atmosphere()
"""Default values."""
assert (a.temperature == 293.15)
assert (a.pressure == 101.325)
assert (a.relative_humidity == 0.0)
assert a.temperature == 293.15
assert a.pressure == 101.325
assert a.relative_humidity == 0.0
"""Calculated values belonging to default values."""
assert (abs(a.soundspeed - 343.2) < 1.0e-9)
assert (abs(a.saturation_pressure - 2.33663045) < 1.0e-8)
assert (abs(a.molar_concentration_water_vapour - 0.0) < 1.0e-9)
assert (abs(a.relaxation_frequency_nitrogen - 9.0) < 1.0e-9)
assert (abs(a.relaxation_frequency_oxygen - 24.0) < 1.0e-9)
assert abs(a.soundspeed - 343.2) < 1.0e-9
assert abs(a.saturation_pressure - 2.33663045) < 1.0e-8
assert abs(a.molar_concentration_water_vapour - 0.0) < 1.0e-9
assert abs(a.relaxation_frequency_nitrogen - 9.0) < 1.0e-9
assert abs(a.relaxation_frequency_oxygen - 24.0) < 1.0e-9

def test_attenuation_coefficient(self):
"""Test data is all at an air pressure of one standard atmosphere, 101.325 Pa."""
data = np.loadtxt(data_path() / 'absorption_coefficient.csv', skiprows=1, delimiter=',')

data = np.loadtxt(
data_path() / "absorption_coefficient.csv", skiprows=1, delimiter=","
)
f = np.array([
50.0, 63.0, 80.0, 100.0, 125.0, 160.0, 200.0, 250.0, 315.0, 400.0, 500.0, 630.0, 800.0, 1000.0, 1250.0,
1600.0, 2000.0, 2500.0, 3150.0, 4000.0, 5000.0, 6300.0, 8000.0, 10000.0
Expand Down
185 changes: 87 additions & 98 deletions tests/test_bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,101 +18,90 @@ def third_real():
2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000
])


def test_octave(octave_real):
generated = octave(16, 16000)
real = octave_real
assert_array_equal(generated, real)


def test_octave_high(octave_real):
generated = octave_high(16, 16000)
real = octave_real * np.sqrt(2)
assert_array_almost_equal(generated, real)


def test_octave_low(octave_real):
generated = octave_low(16, 16000)
real = real = octave_real / np.sqrt(2)
assert_array_almost_equal(generated, real)


def test_third(third_real):
generated = third(12.5, 20000)
real = third_real
assert_array_equal(generated, real)


def test_third_high(third_real):
generated = third_high(12.5, 20000)
real = third_real * 2**(1 / 6)
assert_array_almost_equal(generated, real)


def test_third_low(third_real):
generated = third_low(12.5, 20000)
real = third_real / 2**(1 / 6)
assert_array_almost_equal(generated, real)


def test_third2oct():

levels = np.array([10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0])
generated = third2oct(levels)
real = np.array([14.77121255, 14.77121255, 14.77121255])
assert_array_almost_equal(generated, real)


def test_third2oct_2darray_axis0():
levels = np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
[100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]])
generated = third2oct(levels, axis=0)
real = np.array([100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0])
assert_array_almost_equal(generated, real)


def test_third2oct_2darray_axis1():
levels = np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
[100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]])
generated = third2oct(levels, axis=1)
real = np.array([[5.77121255, 5.77121255, 5.77121255], [14.77121255, 14.77121255, 14.77121255],
[104.77121255, 104.77121255, 104.77121255]])
assert_array_almost_equal(generated, real)


def test_third2oct_3darray_axis0():

# Array of ones with shape (3,4,5)
levels = np.array([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]])

generated = third2oct(levels, axis=0)
real = np.array([[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255],
[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255],
[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255],
[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255]])
assert_array_almost_equal(generated, real)


def test_third2oct_2darray():
levels = np.array([[100, 95, 80, 55, 65, 85, 75, 70, 90, 95, 105, 110],
[100, 95, 80, 55, 65, 85, 75, 70, 90, 95, 105, 110]])
generated = third2oct(levels, axis=1)
real = np.array([[101.22618116, 85.04751156, 90.17710468, 111.29641738],
[101.22618116, 85.04751156, 90.17710468, 111.29641738]])
assert_array_almost_equal(generated, real)


@pytest.mark.parametrize("freqs, expected", [
(np.array([125, 250, 500]), 'octave'),
(np.array([12.5, 16, 20]), 'third'),
(np.array([125, 250, 1000, 4000]), 'octave-unsorted'),
(np.array([12.5, 800, 500, 5000]), 'third-unsorted'),
(np.array([100, 200, 300, 400]), None),
])
def test__check_band_type(freqs, expected):
band_type = _check_band_type(freqs)
assert_array_equal(band_type, expected)
class TestBands:
"""
Test :mod:`acoustic_toolbox.bands`
"""

def test_octave(self, octave_real):
generated = octave(16, 16000)
real = octave_real
assert_array_equal(generated, real)

def test_octave_high(self, octave_real):
generated = octave_high(16, 16000)
real = octave_real * np.sqrt(2)
assert_array_almost_equal(generated, real)

def test_octave_low(self, octave_real):
generated = octave_low(16, 16000)
real = real = octave_real / np.sqrt(2)
assert_array_almost_equal(generated, real)

def test_third(self, third_real):
generated = third(12.5, 20000)
real = third_real
assert_array_equal(generated, real)

def test_third_high(self, third_real):
generated = third_high(12.5, 20000)
real = third_real * 2**(1 / 6)
assert_array_almost_equal(generated, real)

def test_third_low(self, third_real):
generated = third_low(12.5, 20000)
real = third_real / 2**(1 / 6)
assert_array_almost_equal(generated, real)

def test_third2oct(self):
levels = np.array([10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0])
generated = third2oct(levels)
real = np.array([14.77121255, 14.77121255, 14.77121255])
assert_array_almost_equal(generated, real)

def test_third2oct_2darray_axis0(self):
levels = np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
[100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]])
generated = third2oct(levels, axis=0)
real = np.array([100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0])
assert_array_almost_equal(generated, real)

def test_third2oct_2darray_axis1(self):
levels = np.array([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
[100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]])
generated = third2oct(levels, axis=1)
real = np.array([[5.77121255, 5.77121255, 5.77121255], [14.77121255, 14.77121255, 14.77121255],
[104.77121255, 104.77121255, 104.77121255]])
assert_array_almost_equal(generated, real)

def test_third2oct_3darray_axis0(self):
levels = np.array([[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]])
generated = third2oct(levels, axis=0)
real = np.array([[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255],
[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255],
[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255],
[5.77121255, 5.77121255, 5.77121255, 5.77121255, 5.77121255]])
assert_array_almost_equal(generated, real)

def test_third2oct_2darray(self):
levels = np.array([[100, 95, 80, 55, 65, 85, 75, 70, 90, 95, 105, 110],
[100, 95, 80, 55, 65, 85, 75, 70, 90, 95, 105, 110]])
generated = third2oct(levels, axis=1)
real = np.array([[101.22618116, 85.04751156, 90.17710468, 111.29641738],
[101.22618116, 85.04751156, 90.17710468, 111.29641738]])
assert_array_almost_equal(generated, real)

@pytest.mark.parametrize("freqs, expected", [
(np.array([125, 250, 500]), 'octave'),
(np.array([12.5, 16, 20]), 'third'),
(np.array([125, 250, 1000, 4000]), 'octave-unsorted'),
(np.array([12.5, 800, 500, 5000]), 'third-unsorted'),
(np.array([100, 200, 300, 400]), None),
])
def test__check_band_type(self, freqs, expected):
band_type = _check_band_type(freqs)
assert_array_equal(band_type, expected)
Loading
Loading