forked from librosa/librosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_decompose.py
214 lines (132 loc) · 5.32 KB
/
test_decompose.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
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
#!/usr/bin/env python
# CREATED: 2013-10-06 22:31:29 by Dawen Liang <dl2771@columbia.edu>
# unit tests for librosa.decompose
import warnings
# Disable cache
import os
try:
os.environ.pop('LIBROSA_CACHE_DIR')
except:
pass
import numpy as np
import scipy.sparse
import librosa
import sklearn.decomposition
from nose.tools import raises
from test_core import srand
warnings.resetwarnings()
warnings.simplefilter('always')
def test_default_decompose():
X = np.array([[1, 2, 3, 4, 5, 6], [1, 1, 1.2, 1, 0.8, 1]])
(W, H) = librosa.decompose.decompose(X, random_state=0)
assert np.allclose(X, W.dot(H), rtol=1e-2, atol=1e-2)
def test_given_decompose():
D = sklearn.decomposition.NMF(random_state=0)
X = np.array([[1, 2, 3, 4, 5, 6], [1, 1, 1.2, 1, 0.8, 1]])
(W, H) = librosa.decompose.decompose(X, transformer=D)
assert np.allclose(X, W.dot(H), rtol=1e-2, atol=1e-2)
def test_decompose_fit():
srand()
D = sklearn.decomposition.NMF(random_state=0)
X = np.array([[1, 2, 3, 4, 5, 6], [1, 1, 1.2, 1, 0.8, 1]])
# Do a first fit
(W, H) = librosa.decompose.decompose(X, transformer=D, fit=True)
# Make random data and decompose with the same basis
X = np.random.randn(*X.shape)**2
(W2, H2) = librosa.decompose.decompose(X, transformer=D, fit=False)
# Make sure the basis hasn't changed
assert np.allclose(W, W2)
@raises(librosa.ParameterError)
def test_decompose_fit_false():
X = np.array([[1, 2, 3, 4, 5, 6], [1, 1, 1.2, 1, 0.8, 1]])
(W, H) = librosa.decompose.decompose(X, fit=False)
def test_sorted_decompose():
X = np.array([[1, 2, 3, 4, 5, 6], [1, 1, 1.2, 1, 0.8, 1]])
(W, H) = librosa.decompose.decompose(X, sort=True, random_state=0)
assert np.allclose(X, W.dot(H), rtol=1e-2, atol=1e-2)
def test_real_hpss():
# Load an audio signal
y, sr = librosa.load('data/test1_22050.wav')
D = np.abs(librosa.stft(y))
def __hpss_test(window, power, mask, margin):
H, P = librosa.decompose.hpss(D, kernel_size=window, power=power,
mask=mask, margin=margin)
if margin == 1.0 or margin == (1.0, 1.0):
if mask:
assert np.allclose(H + P, np.ones_like(D))
else:
assert np.allclose(H + P, D)
else:
if mask:
assert np.all(H + P <= np.ones_like(D))
else:
assert np.all(H + P <= D)
for window in [31, (5, 5)]:
for power in [1, 2, 10]:
for mask in [False, True]:
for margin in [1.0, 3.0, (1.0, 1.0), (9.0, 10.0)]:
yield __hpss_test, window, power, mask, margin
@raises(librosa.ParameterError)
def test_hpss_margin_error():
y, sr = librosa.load('data/test1_22050.wav')
D = np.abs(librosa.stft(y))
H, P = librosa.decompose.hpss(D, margin=0.9)
def test_complex_hpss():
# Load an audio signal
y, sr = librosa.load('data/test1_22050.wav')
D = librosa.stft(y)
H, P = librosa.decompose.hpss(D)
assert np.allclose(H + P, D)
def test_nn_filter_mean():
srand()
X = np.random.randn(10, 100)
# Build a recurrence matrix, just for testing purposes
rec = librosa.segment.recurrence_matrix(X)
X_filtered = librosa.decompose.nn_filter(X)
# Normalize the recurrence matrix so dotting computes an average
rec = librosa.util.normalize(rec, axis=1, norm=1)
assert np.allclose(X_filtered, X.dot(rec.T))
def test_nn_filter_mean_rec():
srand()
X = np.random.randn(10, 100)
# Build a recurrence matrix, just for testing purposes
rec = librosa.segment.recurrence_matrix(X)
# Knock out the first three rows of links
rec[:3] = 0
X_filtered = librosa.decompose.nn_filter(X, rec=rec)
for i in range(3):
assert np.allclose(X_filtered[:, i], X[:, i])
# Normalize the recurrence matrix
rec = librosa.util.normalize(rec, axis=1, norm=1)
assert np.allclose(X_filtered[:, 3:], (X.dot(rec.T))[:, 3:])
def test_nn_filter_mean_rec_sparse():
srand()
X = np.random.randn(10, 100)
# Build a recurrence matrix, just for testing purposes
rec = librosa.segment.recurrence_matrix(X, sparse=True)
X_filtered = librosa.decompose.nn_filter(X, rec=rec)
# Normalize the recurrence matrix
rec = librosa.util.normalize(rec.toarray(), axis=1, norm=1)
assert np.allclose(X_filtered, (X.dot(rec.T)))
def test_nn_filter_avg():
srand()
X = np.random.randn(10, 100)
# Build a recurrence matrix, just for testing purposes
rec = librosa.segment.recurrence_matrix(X, mode='affinity')
X_filtered = librosa.decompose.nn_filter(X, rec=rec, aggregate=np.average)
# Normalize the recurrence matrix so dotting computes an average
rec = librosa.util.normalize(rec, axis=1, norm=1)
assert np.allclose(X_filtered, X.dot(rec.T))
def test_nn_filter_badselfsim():
@raises(librosa.ParameterError)
def __test(x, y, sparse):
srand()
X = np.empty((10, 100))
# Build a recurrence matrix, just for testing purposes
rec = np.random.randn(x, y)
if sparse:
rec = scipy.sparse.csr_matrix(rec)
librosa.decompose.nn_filter(X, rec=rec)
for (x, y) in [(10, 10), (100, 20), (20, 100), (100, 101), (101, 101)]:
for sparse in [False, True]:
yield __test, x, y, sparse