-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_parameters.py
More file actions
257 lines (211 loc) · 9.54 KB
/
test_parameters.py
File metadata and controls
257 lines (211 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import numpy as np
import pytest
import pyfar as pf
import pyrato as ra
import numpy.testing as npt
import re
from pyrato.parameters import clarity
from pyrato.parameters import _energy_ratio
# parameter clarity tests
def test_clarity_accepts_timedata_returns_correct_type(make_edc):
energy = np.concatenate(([1, 1, 1, 1], np.zeros(124)))
edc = make_edc(energy=energy)
result = clarity(edc, early_time_limit=4) # 4 ms
assert isinstance(result, (float, np.ndarray))
assert result.shape == edc.cshape
def test_clarity_rejects_non_timedata_input():
invalid_input = np.array([1, 2, 3])
expected_error_message = "Input must be a pyfar.TimeData object."
with pytest.raises(TypeError, match=re.escape(expected_error_message)):
clarity(invalid_input)
def test_clarity_rejects_non_numeric_early_time_limit(make_edc):
edc = make_edc()
invalid_time_limit = "not_a_number"
expected_error_message = "early_time_limit must be a number."
with pytest.raises(TypeError, match=re.escape(expected_error_message)):
clarity(edc, invalid_time_limit)
def test_clarity_rejects_complex_timedata():
complex_data = pf.TimeData(np.array([1+1j, 2+2j, 3+3j]),
np.arange(3) / 1000, is_complex=True)
expected_error_message = "Complex-valued input detected. Clarity is"
"only defined for real TimeData."
with pytest.raises(ValueError, match=re.escape(expected_error_message)):
clarity(complex_data, early_time_limit=2)
def test_clarity_rejects_invalid_time_range(make_edc):
energy = np.zeros(128)
edc = make_edc(energy=energy)
actual_signal_length_ms = edc.signal_length * 1000
# Test negative time limit
expected_error_message = "early_time_limit must be in the range of 0"
f"and {actual_signal_length_ms}."
with pytest.raises(ValueError, match=re.escape(expected_error_message)):
clarity(edc, early_time_limit=-1)
# Test time limit beyond signal length
with pytest.raises(ValueError, match=re.escape(expected_error_message)):
clarity(edc, early_time_limit=200000)
def test_clarity_preserves_multichannel_shape(make_edc):
energy = np.ones((2,2,10)) / (1+np.arange(10))
edc = make_edc(energy=energy, sampling_rate=10)
output = clarity(edc, early_time_limit=80)
assert edc.cshape == output.shape
def test_clarity_returns_nan_for_zero_signal():
edc = pf.TimeData(np.zeros((1, 128)), np.arange(128) / 1000)
result = clarity(edc)
assert np.isnan(result)
def test_clarity_calculates_known_reference_value(make_edc):
# Linear decay → early_time_limit at 1/2 energy -> ratio = 1 -> 0 dB
edc_vals = np.array([1.0, 0.75, 0.5, 0.0]) # monotonic decay
edc = make_edc(energy=edc_vals, sampling_rate=1000)
result = clarity(edc, early_time_limit=2)
np.testing.assert_allclose(result, 0.0, atol=1e-6)
def test_clarity_values_for_given_ratio(make_edc):
energy_early = 1
energy_late = .5
energy = np.zeros((3, 1000))
edc = make_edc(energy=energy,
sampling_rate=1000,
dynamic_range = 120.0)
edc.time[..., 10] = energy_early
edc.time[..., 100] = energy_late
edc = ra.edc.schroeder_integration(edc, is_energy=True)
edc = pf.dsp.normalize(edc, reference_method='max')
result = clarity(edc, early_time_limit=80)
clarity_value_db = 10 * np.log10(energy_early/energy_late)
npt.assert_allclose(result, clarity_value_db, atol=1e-6)
def test_clarity_for_exponential_decay(make_edc):
rt60 = 2.0 # seconds
sampling_rate = 1000
total_samples = 2000
early_cutoff = 80 # ms
# Generate EDC
edc = make_edc(rt=rt60,
sampling_rate=sampling_rate,
total_samples=total_samples)
result = clarity(edc, early_time_limit=early_cutoff)
# Analytical expected value
te = early_cutoff / 1000 # convert ms to seconds
a = 13.8155 / rt60
expected_ratio = np.exp(a * te) - 1
expected_dB = 10 * np.log10(expected_ratio)
np.testing.assert_allclose(result, expected_dB, atol=1e-6)
# _energy_ratio tests
def test_energy_ratio_accepts_timedata_and_returns_correct_shape(make_edc):
energy = np.linspace(1, 0, 10)
edc = make_edc(energy=energy, sampling_rate=1000)
limits = np.array([0.0, 0.001, 0.0, 0.005])
result = _energy_ratio(limits, edc, edc)
assert isinstance(result, np.ndarray)
assert result.shape == edc.cshape
def test_energy_ratio_rejects_non_timedata_input():
invalid_input = np.arange(10)
limits = np.array([0.0, 0.001, 0.0, 0.005])
expected_message = "pyfar.TimeData"
with pytest.raises(TypeError, match=expected_message):
_energy_ratio(limits, invalid_input, invalid_input)
def test_energy_ratio_rejects_if_second_edc_is_not_timedata(make_edc):
edc = make_edc(energy=np.linspace(1, 0, 10), sampling_rate=1000)
limits = np.array([0.0, 0.001, 0.0, 0.005])
with pytest.raises(TypeError, match="pyfar.TimeData"):
_energy_ratio(limits, edc, "invalid_type")
def test_energy_ratio_rejects_non_numpy_array_limits(make_edc):
edc = make_edc(energy=np.linspace(1, 0, 10), sampling_rate=1000)
result = _energy_ratio([0.0, 0.001, 0.0, 0.005], edc, edc)
assert isinstance(result, np.ndarray)
def test_energy_ratio_rejects_wrong_shape_limits(make_edc):
edc = make_edc(energy=np.linspace(1, 0, 10), sampling_rate=1000)
wrong_shape_limits = np.array([0.0, 0.001, 0.005]) # Only 3 elements
with pytest.raises(ValueError, match="limits must have shape"):
_energy_ratio(wrong_shape_limits, edc, edc)
def test_energy_ratio_rejects_wrong_type_limits(make_edc):
edc = make_edc(energy=np.linspace(1, 0, 10), sampling_rate=1000)
wrong_type_limits = "3, 2, 0.5, 1" # string
with pytest.raises(TypeError,
match="limits must be a numpy ndarray"):
_energy_ratio(wrong_type_limits, edc, edc)
def test_energy_ratio_computes_known_ratio_correctly(make_edc):
"""
If EDC is linear, energy ratio should be 1.
numerator = e(lim3)-e(lim4) = (1.0 - 0.75) = 0.25
denominator = e(lim1)-e(lim2) = (0.75 - 0.5) = 0.25
ratio = 1
"""
edc_vals = np.array([1.0, 0.75, 0.5, 0.25])
edc = make_edc(energy=edc_vals, sampling_rate=1000)
# For linear EDC:
limits = np.array([0.0, 0.001, 0.001, 0.002])
result = _energy_ratio(limits, edc, edc)
npt.assert_allclose(result, 1.0, atol=1e-12)
def test_energy_ratio_handles_multichannel_data_correctly(make_edc):
energy = np.linspace(1, 0, 10)
multi = np.stack([energy, energy * 0.5])
edc = make_edc(energy=multi, sampling_rate=1000)
limits = np.array([0.0, 0.001, 0.0, 0.005])
result = _energy_ratio(limits, edc, edc)
assert result.shape == edc.cshape
def test_energy_ratio_returns_nan_for_zero_denominator(make_edc):
"""If denominator e(lim1)-e(lim2)=0, expect NaN (invalid ratio)."""
energy = np.ones(10)
edc = make_edc(energy=energy, sampling_rate=1000)
limits = np.array([0.0, 0.001, 0.002, 0.003])
result = _energy_ratio(limits, edc, edc)
assert np.isnan(result)
def test_energy_ratio_matches_reference_case(make_edc):
"""
Analytical reference:
EDC = exp(-a*t). For exponential decay, ratio known analytically.
"""
sampling_rate = 1000
a = 13.8155 # decay constant
times = np.arange(1000) / sampling_rate
edc_vals = np.exp(-a * times)
edc = make_edc(energy=edc_vals, sampling_rate=sampling_rate)
limits = np.array([0.0, 0.02, 0.0, 0.05])
lim1, lim2, lim3, lim4 = limits
analytical_ratio = (
(np.exp(-a*lim3) - np.exp(-a*lim4)) /
(np.exp(-a*lim1) - np.exp(-a*lim2))
)
result = _energy_ratio(limits, edc, edc)
npt.assert_allclose(result, analytical_ratio, atol=1e-8)
def test_energy_ratio_works_with_two_different_edcs(make_edc):
"""
Energy ratio between two different EDCs.
should compute distinct ratio.
"""
edc1 = make_edc(energy=np.linspace(1, 0, 10), sampling_rate=1000)
edc2 = make_edc(energy=np.linspace(1, 0, 10) ** 2, sampling_rate=1000)
limits = np.array([0.0, 0.002, 0.0, 0.004])
# Expect a ratio != 1 because edc2 decays faster
ratio = _energy_ratio(limits, edc1, edc2)
assert np.all(np.isfinite(ratio))
assert not np.allclose(ratio, 1.0)
def test_energy_ratio_rejects_limits_outside_time_range(make_edc):
"""Test that limits outside valid time range are rejected."""
edc1 = make_edc(energy=np.linspace(1, 0, 100), sampling_rate=1000)
edc2 = make_edc(energy=np.linspace(1, 0, 100), sampling_rate=1000)
max_time = edc1.times[-1]
# Test negative limit
limits_negative = np.array([-0.01, 0.02, 0.02, 0.05])
with pytest.raises(
ValueError,
match=r"limits\[0:2\] must be between 0 and",
):
_energy_ratio(limits_negative, edc1, edc2)
# Test limit beyond signal length
limits_too_large = np.array([0.0, 0.02, 0.02, max_time + 0.01])
with pytest.raises(
ValueError,
match=r"limits\[2:4\] must be between 0 and",
):
_energy_ratio(limits_too_large, edc1, edc2)
def test_energy_ratio_handles_different_edc_lengths(make_edc):
"""Test that validation uses the shorter EDC's time range."""
edc1 = make_edc(energy=np.linspace(1, 0, 100), sampling_rate=1000)
edc2 = make_edc(energy=np.linspace(1, 0, 50), sampling_rate=1000)
# Limit valid for edc1 but not edc2
limits = np.array([0.0, 0.02, 0.02, 0.06]) # 0.06s > edc2.times[-1]
with pytest.raises(
ValueError,
match=r"limits\[2:4\] must be between 0 and",
):
_energy_ratio(limits, edc1, edc2)