Skip to content

Commit

Permalink
Add test for ProcessWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenw committed Mar 21, 2024
1 parent 3deb7e5 commit 0df1117
Showing 1 changed file with 97 additions and 6 deletions.
103 changes: 97 additions & 6 deletions tests/test_process_func_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def addition(signal, sampling_rate, value=1):
return signal + value * signal


def identity(signal, sampling_rate):
return signal


def mean(signal, sampling_rate, offset=0):
return np.mean(signal + offset)


def mean_with_context(signal, sampling_rate, starts, ends, offset=0):
return [np.mean(signal + offset)]


def segment(signal, sampling_rate, offset=0):
return audinterface.utils.signal_index(
starts=0 + offset,
Expand Down Expand Up @@ -183,7 +183,7 @@ def parse_output(output):
),
],
)
def test_process(
def test_interfaces(
tmpdir,
signal,
sampling_rate,
Expand All @@ -194,7 +194,18 @@ def test_process(
process_func_args_during_call,
expected_output,
):
r"""Test process_func_args for different interfaces.
This tests the correct behavior
for the ``process_func_args``
local (when calling process methods)
and global settings (when instantiating the interface),
for the following interfaces:
:class:`audinterface.Process`,
:class:`audinterface.Feature`,
:class:`audinterface.Segment`.
"""
# create test file
folder = audeer.mkdir(tmpdir, 'wav')
file = os.path.join(folder, 'file.wav')
Expand All @@ -213,7 +224,6 @@ def test_process(
sampling_rate,
process_func_args=process_func_args_during_call,
)
print(y)
output = parse_output(y)
np.testing.assert_equal(output, expected_output)

Expand Down Expand Up @@ -248,3 +258,84 @@ def test_process(
)
output = parse_output(y)
np.testing.assert_equal(output, expected_output)


@pytest.mark.parametrize('signal', [np.ones((1, 8000))])
@pytest.mark.parametrize('sampling_rate', [8000])
@pytest.mark.parametrize('interface_object', [audinterface.ProcessWithContext])
@pytest.mark.parametrize(
'process_func, process_func_args, '
'process_func_args_during_call, expected_output',
[
(
mean_with_context,
None,
None,
1,
),
(
mean_with_context,
{'offset': 1},
None,
2,
),
(
mean_with_context,
None,
{'offset': 1},
2,
),
(
mean_with_context,
{'offset': 0},
{'offset': 2},
3,
),
(
mean_with_context,
{'offset': 2},
{'offset': 0},
1,
),
],
)
def test_process_with_context(
tmpdir,
signal,
sampling_rate,
interface_object,
process_func,
process_func_args,
process_func_args_during_call,
expected_output,
):
r"""Test process_func_args for the ProcessWithContext interface.
This tests the correct behavior
for the ``process_func_args``
local (when calling process methods)
and global settings (when instantiating the interface).
As :class:`audinterface.ProcessWithContext`
has only a subset of methods,
its easier to have a separate test for it.
"""
# create test file
folder = audeer.mkdir(tmpdir, 'wav')
file = os.path.join(folder, 'file.wav')
audiofile.write(file, signal, sampling_rate, bit_depth=32)

interface = interface_object(
process_func=process_func,
process_func_args=process_func_args,
verbose=False,
)

# index
y = interface.process_index(
audformat.segmented_index(file, 0, 1),
process_func_args=process_func_args_during_call,
)
output = parse_output(y)
np.testing.assert_equal(output, expected_output)

0 comments on commit 0df1117

Please sign in to comment.