forked from CPJKU/madmom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bin_evaluate.py
107 lines (88 loc) · 3.73 KB
/
test_bin_evaluate.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
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the /bin/evaluate script.
"""
from __future__ import absolute_import, division, print_function
import imp
import os
import sys
import unittest
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
import numpy as np
from . import ANNOTATIONS_PATH, DETECTIONS_PATH
from madmom.evaluation.beats import BeatIntervalError
eval_script = os.path.dirname(os.path.realpath(__file__)) + '/../bin/evaluate'
# prevent writing compiled Python files to disk
sys.dont_write_bytecode = True
def run_script(task, det_suffix=None, args=None):
# import module, capture stdout
test = imp.load_source('test', eval_script)
sys.argv = [eval_script, task, '--csv', DETECTIONS_PATH, ANNOTATIONS_PATH]
if det_suffix:
sys.argv.extend(['-d', det_suffix])
if args:
sys.argv.extend(args)
backup = sys.stdout
sys.stdout = StringIO()
# run evaluation script
test.main()
# get data from stdout, restore environment
data = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = backup
return data.splitlines()
class TestEvaluateScript(unittest.TestCase):
def test_onsets(self):
res = run_script('onsets', det_suffix='.super_flux.txt')
# second line contains the summed results
sum_res = np.fromiter(res[1].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(
sum_res, [14, 2, 0, 1, 15, 0.875, 0.933, 0.903, 0.824]))
# third line contains the mean results
mean_res = np.fromiter(res[2].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(mean_res, sum_res))
def test_beats(self):
res = run_script('beats', det_suffix='.beat_detector.txt')
# second line contains the results
res = np.fromiter(res[1].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(res, [0.667, 0.5, 0.639, 1, 0, 0,
0.875, 0.875, 3.322, 3.322]))
def test_downbeats(self):
with self.assertRaises(BeatIntervalError):
run_script('beats', det_suffix='.dbn_downbeat_tracker.txt',
args=['--down'])
def test_chords(self):
res = run_script('chords')
# second line contains the weighted mean results
weighted = np.fromiter(res[1].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(weighted, [0.897, 0.641, 0.462, 0.462,
0.282, 0.590, 0.590, 0.923]))
# third line contains the piecewise mean results
piecewise = np.fromiter(res[2].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(weighted, piecewise))
def test_key(self):
res = run_script('key')
# second line contains the results
res = np.fromiter(res[1].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(res, [0, 0, 1, 0, 0, 0.3]))
def test_notes(self):
res = run_script('notes', det_suffix='.piano_transcriptor.txt',
args=['-w', '0.05'])
# second line contains the summed results
sum_res = np.fromiter(res[1].split(',')[1:], dtype=float)
self.assertTrue(
np.allclose(sum_res, [8, 0, 0, 0, 8, 1, 1, 1, 1]))
# third line contains the mean results
mean_res = np.fromiter(res[2].split(',')[1:], dtype=float)
self.assertTrue(np.allclose(mean_res, sum_res))
def test_tempo(self):
res = run_script('tempo', det_suffix='.tempo_detector.txt',
args=['-a', '.tempo'])
# second line contains the results
res = np.fromiter(res[1].split(',')[1:], dtype=float)
self.assertTrue(
np.allclose(res, [0.3, 1, 0, 0, 1]))