forked from CPJKU/madmom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_evaluation_notes.py
377 lines (337 loc) · 16.2 KB
/
test_evaluation_notes.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.evaluation.notes module.
"""
from __future__ import absolute_import, division, print_function
import math
import unittest
from madmom.evaluation.notes import *
from . import ANNOTATIONS_PATH, DETECTIONS_PATH
DETECTIONS = np.asarray([[0.147, 72], # TP
[0.147, 80], # FP
[0.147, 60], # FP, octave error
# [1.567, 41], FN
[2.540, 77], # 14ms too late
[2.520, 60], # 29ms too early
# [2.563, 65], FN
# [2.577, 57], FN + FP, 1 note off
[3.368, 75], # 1ms too early
[3.449, 43]])
ANNOTATIONS = np.asarray([[0.147, 72, 3.323, 63],
[1.567, 41, 0.223, 29],
[2.526, 77, 0.930, 72],
[2.549, 60, 0.211, 28],
[2.563, 65, 0.202, 34],
[2.577, 56, 0.234, 31],
[3.369, 75, 0.780, 64],
[3.449, 43, 0.272, 35]])
# test functions
class TestRemoveDuplicateNotesFunction(unittest.TestCase):
def test_types(self):
notes = remove_duplicate_notes(ANNOTATIONS)
self.assertIsInstance(notes, np.ndarray)
def test_results(self):
ann = np.vstack((ANNOTATIONS, ANNOTATIONS[2]))
notes = remove_duplicate_notes(ann)
self.assertTrue(np.allclose(notes, ANNOTATIONS))
class TestNoteConstantsClass(unittest.TestCase):
def test_types(self):
self.assertIsInstance(WINDOW, float)
def test_values(self):
self.assertEqual(WINDOW, 0.025)
class TestNoteOnsetEvaluationFunction(unittest.TestCase):
def test_types(self):
tp, fp, tn, fn, errors = note_onset_evaluation(DETECTIONS, ANNOTATIONS,
0.025)
self.assertIsInstance(tp, np.ndarray)
self.assertIsInstance(fp, np.ndarray)
self.assertIsInstance(tn, np.ndarray)
self.assertIsInstance(fn, np.ndarray)
self.assertIsInstance(errors, np.ndarray)
tp, fp, tn, fn, errors = note_onset_evaluation([[]], [[]], 0.025)
self.assertIsInstance(tp, np.ndarray)
self.assertIsInstance(fp, np.ndarray)
self.assertIsInstance(tn, np.ndarray)
self.assertIsInstance(fn, np.ndarray)
self.assertIsInstance(errors, np.ndarray)
with self.assertRaises(ValueError):
note_onset_evaluation([], [], 0.025)
def test_results(self):
# empty detections and annotations
tp, fp, tn, fn, errors = note_onset_evaluation([[]], [[]], 0.02)
self.assertTrue(np.allclose(tp, np.zeros((0, 2))))
self.assertTrue(np.allclose(fp, np.zeros((0, 2))))
self.assertTrue(np.allclose(tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(fn, np.zeros((0, 2))))
self.assertTrue(np.allclose(errors, np.zeros((0, 2))))
# empty annotations
tp, fp, tn, fn, errors = note_onset_evaluation(DETECTIONS, [[]], 0.02)
self.assertTrue(np.allclose(tp, np.zeros((0, 2))))
self.assertTrue(np.allclose(fp, DETECTIONS))
self.assertTrue(np.allclose(tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(fn, np.zeros((0, 2))))
self.assertTrue(np.allclose(errors, np.zeros((0, 2))))
# empty detections
tp, fp, tn, fn, errors = note_onset_evaluation([[]], ANNOTATIONS, 0.02)
self.assertTrue(np.allclose(tp, np.zeros((0, 2))))
self.assertTrue(np.allclose(fp, np.zeros((0, 2))))
self.assertTrue(np.allclose(tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(fn, ANNOTATIONS))
self.assertTrue(np.allclose(errors, np.zeros((0, 2))))
# window = 0.01
tp, fp, tn, fn, errors = note_onset_evaluation(DETECTIONS, ANNOTATIONS,
0.01)
self.assertTrue(np.allclose(tp, [[0.147, 72], [3.368, 75],
[3.449, 43]]))
self.assertTrue(np.allclose(fp, [[0.147, 60], [0.147, 80],
[2.520, 60], [2.540, 77]]))
self.assertTrue(np.allclose(tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(fn, [[1.567, 41], [2.526, 77], [2.549, 60],
[2.563, 65], [2.577, 56]]))
self.assertTrue(np.allclose(errors, [[0, 72], [-0.001, 75], [0, 43]]))
# default window (= 0.025)
tp, fp, tn, fn, errors = note_onset_evaluation(DETECTIONS, ANNOTATIONS)
self.assertTrue(np.allclose(tp, [[0.147, 72], [2.540, 77],
[3.368, 75], [3.449, 43]]))
self.assertTrue(np.allclose(fp, [[0.147, 60], [0.147, 80],
[2.520, 60]]))
self.assertTrue(np.allclose(tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(fn, [[1.567, 41], [2.549, 60],
[2.563, 65], [2.577, 56]]))
self.assertTrue(np.allclose(errors, [[0, 72], [0.014, 77],
[-0.001, 75], [0, 43]]))
# window = 0.03
tp, fp, tn, fn, errors = note_onset_evaluation(DETECTIONS, ANNOTATIONS,
0.03)
self.assertTrue(np.allclose(tp, [[0.147, 72], [2.520, 60], [2.540, 77],
[3.368, 75], [3.449, 43]]))
self.assertTrue(np.allclose(fp, [[0.147, 60], [0.147, 80]]))
self.assertTrue(np.allclose(tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(fn, [[1.567, 41], [2.563, 65],
[2.577, 56]]))
self.assertTrue(np.allclose(errors, [[0, 72], [-0.029, 60],
[0.014, 77], [-0.001, 75],
[0, 43]]))
# test evaluation class
class TestNoteEvaluationClass(unittest.TestCase):
def test_types(self):
e = NoteEvaluation(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 of lists should work
e = NoteEvaluation([[0, 0]], [[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)
def test_results(self):
# empty detections / annotations
e = NoteEvaluation([[]], [[]])
self.assertTrue(np.allclose(e.tp, np.zeros((0, 2))))
self.assertTrue(np.allclose(e.fp, np.zeros((0, 2))))
self.assertTrue(np.allclose(e.tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(e.fn, np.zeros((0, 2))))
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, np.zeros((0, 2))))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# real detections / annotations
e = NoteEvaluation(DETECTIONS, ANNOTATIONS)
self.assertTrue(np.allclose(e.tp, [[0.147, 72], [2.540, 77],
[3.368, 75], [3.449, 43]]))
self.assertTrue(np.allclose(e.fp, [[0.147, 60], [0.147, 80],
[2.520, 60]]))
self.assertTrue(np.allclose(e.tn, np.zeros((0, 2))))
self.assertTrue(np.allclose(e.fn, [[1.567, 41], [2.549, 60],
[2.563, 65], [2.577, 56]]))
self.assertEqual(e.num_tp, 4)
self.assertEqual(e.num_fp, 3)
self.assertEqual(e.num_tn, 0)
self.assertEqual(e.num_fn, 4)
self.assertEqual(e.precision, 4. / 7.)
self.assertEqual(e.recall, 4. / 8.)
f = 2 * (4. / 7.) * (4. / 8.) / ((4. / 7.) + (4. / 8.))
self.assertEqual(e.fmeasure, f)
self.assertEqual(e.accuracy, (4. + 0) / (4 + 3 + 0 + 4))
# errors
# tp = [[0.147, 72], [2.540, 77], [3.368, 75], [3.449, 43]]
# ann = [[0.147, 72], [2.526, 77], [3.369, 75], [3.449, 43]]
# err = [[0. , 72], [0.014, 77], [-0.001, 75], [0. , 43]]
errors = np.asarray([[0., 72], [0.014, 77], [-0.001, 75], [0., 43]])
self.assertTrue(np.allclose(e.errors, errors))
self.assertTrue(np.allclose(e.mean_error,
np.mean([0, 0.014, -0.001, 0])))
self.assertTrue(np.allclose(e.std_error,
np.std([0, 0.014, -0.001, 0])))
def test_tostring(self):
print(NoteEvaluation([], []))
class TestNoteSumEvaluationClass(unittest.TestCase):
def test_types(self):
e = NoteSumEvaluation([])
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 = NoteSumEvaluation([])
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, np.zeros((0, 2))))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# sum evaluation of empty note evaluation
e1 = NoteEvaluation([], [])
e = NoteSumEvaluation([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, np.zeros((0, 2))))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# sum evaluation of empty and real onset evaluation
e2 = NoteEvaluation(DETECTIONS, ANNOTATIONS)
e = NoteSumEvaluation([e1, e2])
# everything must be the same as e2, since e1 was empty and thus did
# not ad anything to the sum evaluation
self.assertEqual(e.num_tp, e2.num_tp)
self.assertEqual(e.num_fp, e2.num_fp)
self.assertEqual(e.num_tn, e2.num_tn)
self.assertEqual(e.num_fn, e2.num_fn)
self.assertEqual(e.precision, e2.precision)
self.assertEqual(e.recall, e2.recall)
self.assertEqual(e.fmeasure, e2.fmeasure)
self.assertEqual(e.accuracy, e2.accuracy)
self.assertTrue(np.allclose(e.errors, e2.errors))
self.assertEqual(e.mean_error, e2.mean_error)
self.assertEqual(e.std_error, e2.std_error)
def test_tostring(self):
print(NoteSumEvaluation([]))
class TestNoteMeanEvaluationClass(unittest.TestCase):
def test_types(self):
e = NoteMeanEvaluation([])
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 = NoteMeanEvaluation([])
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, np.zeros((0, 2))))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# mean evaluation of empty note evaluation
e1 = NoteEvaluation([], [])
e = NoteMeanEvaluation([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, np.zeros((0, 2))))
self.assertTrue(math.isnan(e.mean_error))
self.assertTrue(math.isnan(e.std_error))
# mean evaluation of empty and real note evaluation
e2 = NoteEvaluation(DETECTIONS, ANNOTATIONS)
e = NoteMeanEvaluation([e1, e2])
self.assertTrue(np.allclose(
e.num_tp, np.mean([e_.num_tp for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.num_fp, np.mean([e_.num_fp for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.num_tn, np.mean([e_.num_tn for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.num_fn, np.mean([e_.num_fn for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.precision, np.mean([e_.precision for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.recall, np.mean([e_.recall for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.fmeasure, np.mean([e_.fmeasure for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.accuracy, np.mean([e_.accuracy for e_ in [e1, e2]])))
self.assertTrue(np.allclose(
e.errors, np.concatenate([e_.errors for e_ in [e1, e2]])))
# mean and std errors are those of e2, since those of e1 are NaN
self.assertEqual(e.mean_error, e2.mean_error)
self.assertEqual(e.std_error, e2.std_error)
def test_tostring(self):
print(NoteMeanEvaluation([]))
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(['notes', ANNOTATIONS_PATH,
DETECTIONS_PATH])
self.assertTrue(args.ann_dir is None)
self.assertTrue(args.ann_suffix == '.notes')
self.assertTrue(args.det_dir is None)
self.assertTrue(args.det_suffix == '.notes.txt')
self.assertTrue(args.eval == NoteEvaluation)
self.assertTrue(args.files == [ANNOTATIONS_PATH, DETECTIONS_PATH])
self.assertTrue(args.ignore_non_existing is False)
self.assertTrue(args.mean_eval == NoteMeanEvaluation)
# 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 == NoteSumEvaluation)
self.assertTrue(args.verbose == 0)
self.assertTrue(args.window == 0.025)