forked from CPJKU/madmom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_evaluation_onsets.py
353 lines (317 loc) · 14.6 KB
/
test_evaluation_onsets.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.evaluation.onsets module.
"""
from __future__ import absolute_import, division, print_function
import math
import unittest
from madmom.evaluation.onsets import *
from . import ANNOTATIONS_PATH, DETECTIONS_PATH
# dummy detections/annotations
DETECTIONS = [0.99999999, 1.02999999, 1.45, 2.01, 2.02, 2.5, 3.025000001]
ANNOTATIONS = [1, 1.02, 1.5, 2.0, 2.03, 2.05, 2.5, 3]
# real detections/annotations
SAMPLE_DETECTIONS = [0.01, 0.085, 0.275, 0.445, 0.61, 0.795, 0.98, 1.115,
1.365, 1.475, 1.62, 1.795, 2.14, 2.33, 2.485, 2.665]
SAMPLE_ANNOTATIONS = [0.0943, 0.2844, 0.4528, 0.6160, 0.7630, 0.8025, 0.9847,
1.1233, 1.4820, 1.6276, 1.8032, 2.1486, 2.3351, 2.4918,
2.6710]
# loading function
class TestOnsetConstantsClass(unittest.TestCase):
def test_types(self):
self.assertIsInstance(WINDOW, float)
def test_values(self):
self.assertEqual(WINDOW, 0.025)
# test evaluation function
class TestOnsetEvaluationFunction(unittest.TestCase):
def test_errors(self):
# detections / annotations must not be None
with self.assertRaises(TypeError):
onset_evaluation(None, ANNOTATIONS)
with self.assertRaises(TypeError):
onset_evaluation(DETECTIONS, None)
# tolerance must be > 0
with self.assertRaises(ValueError):
onset_evaluation(DETECTIONS, ANNOTATIONS, 0)
# tolerance must be correct type
with self.assertRaises(TypeError):
onset_evaluation(DETECTIONS, ANNOTATIONS, None)
with self.assertRaises(TypeError):
onset_evaluation(DETECTIONS, ANNOTATIONS, [])
with self.assertRaises(TypeError):
onset_evaluation(DETECTIONS, ANNOTATIONS, {})
def test_results(self):
# default window
tp, fp, tn, fn, errors = onset_evaluation(DETECTIONS, ANNOTATIONS)
self.assertTrue(np.allclose(tp, [0.999999, 1.029999, 2.01, 2.02, 2.5]))
self.assertTrue(np.allclose(fp, [1.45, 3.025000001]))
self.assertTrue(np.allclose(tn, []))
self.assertTrue(np.allclose(fn, [1.5, 2.05, 3.0]))
self.assertTrue(np.allclose(errors, [-0.00000001, 0.00999999, 0.01,
-0.01, 0]))
# window = 0.01
tp, fp, tn, fn, errors = onset_evaluation(DETECTIONS, ANNOTATIONS,
window=0.01)
self.assertTrue(np.allclose(tp, [0.999999, 1.029999, 2.01, 2.02, 2.5]))
self.assertTrue(np.allclose(fp, [1.45, 3.025000001]))
self.assertTrue(np.allclose(tn, []))
self.assertTrue(np.allclose(fn, [1.5, 2.05, 3.0]))
self.assertTrue(np.allclose(errors, [-0.00000001, 0.00999999, 0.01,
-0.01, 0]))
# window = 0.04
tp, fp, tn, fn, errors = onset_evaluation(DETECTIONS, ANNOTATIONS,
window=0.04)
self.assertTrue(np.allclose(tp, [0.999999, 1.029999, 2.01, 2.02, 2.5,
3.025000001]))
self.assertTrue(np.allclose(fp, [1.45]))
self.assertTrue(np.allclose(tn, []))
self.assertTrue(np.allclose(fn, [1.5, 2.05]))
self.assertTrue(np.allclose(errors, [-0.00000001, 0.00999999, 0.01,
-0.01, 0, 0.025]))
# test evaluation class
class TestOnsetEvaluationClass(unittest.TestCase):
def test_types(self):
e = OnsetEvaluation(DETECTIONS, ANNOTATIONS)
self.assertIsInstance(e.num_tp, int)
self.assertIsInstance(e.num_fp, int)
self.assertIsInstance(e.num_tn, int)
self.assertIsInstance(e.num_fn, int)
self.assertIsInstance(e.precision, float)
self.assertIsInstance(e.recall, float)
self.assertIsInstance(e.fmeasure, float)
self.assertIsInstance(e.accuracy, float)
self.assertIsInstance(e.errors, np.ndarray)
self.assertIsInstance(e.mean_error, float)
self.assertIsInstance(e.std_error, float)
def test_conversion(self):
# conversion from list should work
e = OnsetEvaluation([0], [0])
self.assertIsInstance(e.tp, np.ndarray)
self.assertIsInstance(e.fp, np.ndarray)
self.assertIsInstance(e.tn, np.ndarray)
self.assertIsInstance(e.fn, np.ndarray)
self.assertIsInstance(e.errors, np.ndarray)
# conversion from single values should work
e = OnsetEvaluation(0, 0)
self.assertIsInstance(e.tp, np.ndarray)
self.assertIsInstance(e.fp, np.ndarray)
self.assertIsInstance(e.tn, np.ndarray)
self.assertIsInstance(e.fn, np.ndarray)
self.assertIsInstance(e.errors, np.ndarray)
def test_results(self):
# empty detections / annotations
e = OnsetEvaluation([], [])
self.assertTrue(np.allclose(e.tp, []))
self.assertTrue(np.allclose(e.fp, []))
self.assertTrue(np.allclose(e.tn, []))
self.assertTrue(np.allclose(e.fn, []))
self.assertTrue(np.allclose(e.errors, []))
self.assertEqual(e.num_tp, 0)
self.assertEqual(e.num_fp, 0)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 0)
self.assertEqual(e.precision, 1)
self.assertEqual(e.recall, 1)
self.assertEqual(e.fmeasure, 1)
self.assertEqual(e.accuracy, 1)
self.assertTrue(np.allclose(e.errors, []))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# real detections / annotations
e = OnsetEvaluation(DETECTIONS, ANNOTATIONS)
self.assertTrue(np.allclose(e.tp, [0.99999, 1.02999, 2.01, 2.02, 2.5]))
self.assertTrue(np.allclose(e.fp, [1.45, 3.025000001]))
self.assertTrue(np.allclose(e.tn, []))
self.assertTrue(np.allclose(e.fn, [1.5, 2.05, 3.0]))
self.assertEqual(e.num_tp, 5)
self.assertEqual(e.num_fp, 2)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 3)
# p = correct / retrieved
self.assertEqual(e.precision, 5. / 7.)
# r = correct / relevant
self.assertEqual(e.recall, 5. / 8.)
# f = 2 * P * R / (P + R)
f = 2 * (5. / 7.) * (5. / 8.) / ((5. / 7.) + (5. / 8.))
self.assertEqual(e.fmeasure, f)
# acc = (TP + TN) / (TP + FP + TN + FN)
self.assertEqual(e.accuracy, (5. + 0) / (5 + 2 + 0 + 3))
# errors
# det 0.99999999, 1.02999999, 1.45, 2.01, 2.02, 2.5, 3.030000001
# tar 1, 1.02, 1.5, 2.0, 2.03, 2.05, 2.5, 3
errors = [0.99999999 - 1, 1.02999999 - 1.02, # 1.45 - 1.5,
2.01 - 2, 2.02 - 2.03, 2.5 - 2.5] # , 3.030000001 - 3
self.assertTrue(np.allclose(e.errors, errors))
mean = np.mean([0.99999999 - 1, 1.02999999 - 1.02, 2.01 - 2,
2.02 - 2.03, 2.5 - 2.5])
self.assertEqual(e.mean_error, mean)
std = np.std([0.99999999 - 1, 1.02999999 - 1.02, 2.01 - 2, 2.02 - 2.03,
2.5 - 2.5])
self.assertEqual(e.std_error, std)
def test_tostring(self):
print(OnsetEvaluation([], []))
class TestOnsetSumEvaluationClass(unittest.TestCase):
def test_types(self):
e = OnsetSumEvaluation([])
self.assertIsInstance(e.num_tp, int)
self.assertIsInstance(e.num_fp, int)
self.assertIsInstance(e.num_tn, int)
self.assertIsInstance(e.num_fn, int)
self.assertIsInstance(e.precision, float)
self.assertIsInstance(e.recall, float)
self.assertIsInstance(e.fmeasure, float)
self.assertIsInstance(e.accuracy, float)
self.assertIsInstance(e.errors, np.ndarray)
self.assertIsInstance(e.mean_error, float)
self.assertIsInstance(e.std_error, float)
def test_results(self):
# empty sum evaluation
e = OnsetSumEvaluation([])
self.assertEqual(e.num_tp, 0)
self.assertEqual(e.num_fp, 0)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 0)
self.assertEqual(e.precision, 1)
self.assertEqual(e.recall, 1)
self.assertEqual(e.fmeasure, 1)
self.assertEqual(e.accuracy, 1)
self.assertTrue(np.allclose(e.errors, []))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# sum evaluation of empty onset evaluation
e1 = OnsetEvaluation([], [])
e = OnsetSumEvaluation([e1])
self.assertEqual(e.num_tp, 0)
self.assertEqual(e.num_fp, 0)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 0)
self.assertEqual(e.precision, 1)
self.assertEqual(e.recall, 1)
self.assertEqual(e.fmeasure, 1)
self.assertEqual(e.accuracy, 1)
self.assertTrue(np.allclose(e.errors, []))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# sum evaluation of empty and real onset evaluation
e2 = OnsetEvaluation(DETECTIONS, ANNOTATIONS)
e = OnsetSumEvaluation([e1, e2])
self.assertEqual(e.num_tp, 5)
self.assertEqual(e.num_fp, 2)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 3)
# p = correct / retrieved
self.assertEqual(e.precision, 5. / 7.)
# r = correct / relevant
self.assertEqual(e.recall, 5. / 8.)
# f = 2 * P * R / (P + R)
f = 2 * (5. / 7.) * (5. / 8.) / ((5. / 7.) + (5. / 8.))
self.assertEqual(e.fmeasure, f)
# acc = (TP + TN) / (TP + FP + TN + FN)
self.assertEqual(e.accuracy, (5. + 0) / (5 + 2 + 0 + 3))
# errors is just a concatenation of all errors, i.e. those of e2
self.assertTrue(np.allclose(e.errors, e2.errors))
# thus mean and std of errors is those of e2
self.assertEqual(e.mean_error, e2.mean_error)
self.assertEqual(e.std_error, e2.std_error)
def test_tostring(self):
print(OnsetSumEvaluation([]))
class TestOnsetMeanEvaluationClass(unittest.TestCase):
def test_types(self):
e = OnsetMeanEvaluation([])
self.assertIsInstance(e.num_tp, float)
self.assertIsInstance(e.num_fp, float)
self.assertIsInstance(e.num_tn, float)
self.assertIsInstance(e.num_fn, float)
self.assertIsInstance(e.precision, float)
self.assertIsInstance(e.recall, float)
self.assertIsInstance(e.fmeasure, float)
self.assertIsInstance(e.accuracy, float)
self.assertIsInstance(e.errors, np.ndarray)
self.assertIsInstance(e.mean_error, float)
self.assertIsInstance(e.std_error, float)
def test_results(self):
# empty mean evaluation
e = OnsetMeanEvaluation([])
self.assertEqual(e.num_tp, 0)
self.assertEqual(e.num_fp, 0)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 0)
self.assertTrue(math.isnan(e.precision))
self.assertTrue(math.isnan(e.recall))
self.assertTrue(math.isnan(e.fmeasure))
self.assertTrue(math.isnan(e.accuracy))
self.assertTrue(np.allclose(e.errors, []))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# mean evaluation of empty onset evaluation
e1 = OnsetEvaluation([], [])
e = OnsetMeanEvaluation([e1])
self.assertEqual(e.num_tp, 0)
self.assertEqual(e.num_fp, 0)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 0)
self.assertEqual(e.precision, 1)
self.assertEqual(e.recall, 1)
self.assertEqual(e.fmeasure, 1)
self.assertEqual(e.accuracy, 1)
self.assertTrue(np.allclose(e.errors, []))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# mean evaluation of empty and real onset evaluation
e2 = OnsetEvaluation(DETECTIONS, ANNOTATIONS)
e3 = OnsetEvaluation(ANNOTATIONS, DETECTIONS)
e = OnsetMeanEvaluation([e1, e2, e3])
self.assertTrue(np.allclose(
e.num_tp, np.mean([e_.num_tp for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.num_fp, np.mean([e_.num_fp for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.num_tn, np.mean([e_.num_tn for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.num_fn, np.mean([e_.num_fn for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.precision, np.mean([e_.precision for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.recall, np.mean([e_.recall for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.fmeasure, np.mean([e_.fmeasure for e_ in [e1, e2, e3]])))
self.assertTrue(np.allclose(
e.accuracy, np.mean([e_.accuracy for e_ in [e1, e2, e3]])))
# errors is just a concatenation of all errors
# (inherited from SumOnsetEvaluation)
self.assertTrue(np.allclose(
e.errors, np.concatenate([e_.errors for e_ in [e2, e3]])))
# mean and std errors are those of e2 and e3, since those of e1 are NaN
self.assertEqual(e.mean_error,
np.mean([e_.mean_error for e_ in [e2, e3]]))
self.assertEqual(e.std_error,
np.mean([e_.std_error for e_ in [e2, e3]]))
def test_tostring(self):
print(OnsetMeanEvaluation([]))
class TestAddParserFunction(unittest.TestCase):
def setUp(self):
import argparse
self.parser = argparse.ArgumentParser()
sub_parser = self.parser.add_subparsers()
self.sub_parser, self.group = add_parser(sub_parser)
def test_args(self):
args = self.parser.parse_args(['onsets', ANNOTATIONS_PATH,
DETECTIONS_PATH])
self.assertTrue(args.ann_dir is None)
self.assertTrue(args.ann_suffix == '.onsets')
self.assertTrue(args.combine == 0.03)
self.assertTrue(args.delay == 0.0)
self.assertTrue(args.det_dir is None)
self.assertTrue(args.det_suffix == '.onsets.txt')
self.assertTrue(args.eval == OnsetEvaluation)
self.assertTrue(args.files == [ANNOTATIONS_PATH, DETECTIONS_PATH])
self.assertTrue(args.ignore_non_existing is False)
self.assertTrue(args.mean_eval == OnsetMeanEvaluation)
# self.assertTrue(args.outfile == StringIO.StringIO)
from madmom.evaluation import tostring
self.assertTrue(args.output_formatter == tostring)
self.assertTrue(args.quiet is False)
self.assertTrue(args.sum_eval == OnsetSumEvaluation)
self.assertTrue(args.verbose == 0)
self.assertTrue(args.window == 0.025)