-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtest_wrapper_values.py
150 lines (127 loc) · 5.16 KB
/
test_wrapper_values.py
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
import numpy as np
import scipy.signal
from pb_bss.evaluation.wrapper import InputMetrics, OutputMetrics
def scenario():
samples = 10_000
rir_length = 4
channels = 3
speakers = 2
np.random.seed(1)
speech_source_1 = np.random.rand(samples)
speech_source_2 = np.random.rand(samples)
h1 = np.random.rand(channels, rir_length)
h2 = np.random.rand(channels, rir_length)
speech_image_1 = np.array([
scipy.signal.fftconvolve(speech_source_1, h, mode='same')
for h in h1
])
speech_image_2 = np.array([
scipy.signal.fftconvolve(speech_source_2, h, mode='same')
for h in h2
])
assert speech_image_2.shape == (channels, samples)
noise = 0.01 * np.random.rand(channels, samples)
observation = speech_image_1 + speech_image_2 + noise
return {
'speech_source': np.array([speech_source_1, speech_source_2]),
'speech_image': np.array([speech_image_1, speech_image_2]),
'noise_image': noise,
'observation': observation
}
def test_input_metrics():
example = scenario()
metrics = InputMetrics(
observation=example['observation'],
speech_source=example['speech_source'],
speech_image=example['speech_image'],
noise_image=example['noise_image'],
sample_rate=8000,
)
assert metrics.K_source == 2
assert metrics.channels == 3
for k, v in metrics.as_dict().items():
if k == 'invasive_sdr':
np.testing.assert_allclose(
v, [[ 4.634096, 1.821645, 5.012743],
[-4.634303, -1.821825, -5.013139]], rtol=1e-6)
elif k == 'invasive_sir':
np.testing.assert_allclose(
v, [[ 4.63425 , 1.821754, 5.013044],
[-4.63425 , -1.821754, -5.013044]], rtol=1e-6)
elif k == 'invasive_snr':
np.testing.assert_allclose(
v, [[49.137625, 47.859369, 46.598417],
[44.503376, 46.037615, 41.585373]])
elif k == 'mir_eval_sdr':
np.testing.assert_allclose(
v, [[16.286314, 15.048399, 17.420134],
[14.386505, 14.606471, 12.842921]])
elif k == 'mir_eval_sir':
np.testing.assert_allclose(
v, [[18.172265, 17.323722, 18.868235],
[15.523357, 16.609909, 13.310729]])
elif k == 'mir_eval_sar':
np.testing.assert_allclose(
v, [[20.883413, 19.02361 , 22.949934],
[20.883413, 19.02361 , 22.949934]])
elif k == 'pesq':
np.testing.assert_allclose(
v, [[3.494761, 3.034838, 3.755455],
[2.437896, 2.820094, 2.434496]], rtol=1e-6)
elif k == 'stoi':
np.testing.assert_allclose(
v, [[0.691546, 0.626544, 0.717809],
[0.28424 , 0.345368, 0.279996]], rtol=1e-5)
elif k == 'srmr':
np.testing.assert_allclose(
v, [0.51612031, 0.50214891, 0.48237807], rtol=1e-6)
else:
raise KeyError(k, v)
def test_output_metrics():
example = scenario()
# Take speech image + noise as prediction, i.e. perfect croos talber suppression
speech_prediction = example['speech_image'][..., 0, :] + example['noise_image'][..., 0, :]
speech_image_1, speech_image_2 = example['speech_image'][..., 0, :]
speech_contribution = np.array([
[speech_image_1, np.zeros_like(speech_image_2)],
[np.zeros_like(speech_image_1), speech_image_2],
])
noise_contribution = np.array([
example['noise_image'][..., 0, :],
example['noise_image'][..., 0, :],
])
metrics = OutputMetrics(
speech_prediction=speech_prediction,
# observation=example['observation'],
speech_source=example['speech_source'],
# speech_image=example['speech_image'],
# noise_image=example['noise_image'],
speech_contribution=speech_contribution,
noise_contribution=noise_contribution,
sample_rate=8000,
# channel_score_reduce='mean',
)
assert metrics.K_source == 2
for k, v in metrics.as_dict().items():
if k == 'invasive_sdr':
np.testing.assert_allclose(v, [49.137625, 44.503376])
elif k == 'invasive_sir':
np.testing.assert_allclose(v, np.inf)
elif k == 'invasive_snr':
np.testing.assert_allclose(v, [49.137625, 44.503376])
elif k == 'mir_eval_sdr':
np.testing.assert_allclose(v, [17.071665, 24.711722])
elif k == 'mir_eval_sir':
np.testing.assert_allclose(v, [29.423133, 37.060289])
elif k == 'mir_eval_sar':
np.testing.assert_allclose(v, [17.336992, 24.973125])
elif k == 'pesq':
np.testing.assert_allclose(v, [4.37408 , 4.405752])
elif k == 'stoi':
np.testing.assert_allclose(v, [0.968833, 0.976151], rtol=1e-6)
elif k == 'mir_eval_selection':
assert all(v == [0, 1])
elif k == 'srmr':
np.testing.assert_allclose(v, [0.54593548, 0.49966431])
else:
raise KeyError(k, v)