forked from CPJKU/madmom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_processors.py
91 lines (77 loc) · 3.34 KB
/
test_processors.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
# encoding: utf-8
# pylint: skip-file
"""
This file contains tests for the madmom.processors module.
"""
from __future__ import absolute_import, division, print_function
import tempfile
import unittest
from madmom.processors import *
from madmom.models import *
from madmom.ml.nn import NeuralNetwork
tmp_file = tempfile.NamedTemporaryFile(delete=False).name
class TestProcessor(unittest.TestCase):
def test_unicode(self):
if sys.version_info[0] == 2:
# load from unicode string
rnn = NeuralNetwork.load(unicode(ONSETS_RNN[0]))
# save to unicode string
rnn.dump(unicode(tmp_file))
class TestBufferProcessor(unittest.TestCase):
def test_1d(self):
buffer = BufferProcessor(5, init=np.zeros(5))
self.assertTrue(np.allclose(buffer.data, 0))
# shift in two new values
result = buffer(np.arange(2))
self.assertTrue(np.allclose(result, [0, 0, 0, 0, 1]))
result = buffer(np.arange(2, 4))
self.assertTrue(np.allclose(result, [0, 0, 1, 2, 3]))
result = buffer(np.arange(4, 6))
self.assertTrue(np.allclose(result, [1, 2, 3, 4, 5]))
# shift in three new values
result = buffer(np.arange(6, 9))
self.assertTrue(np.allclose(result, [4, 5, 6, 7, 8]))
# shift in six new values (bigger than buffer)
result = buffer(np.arange(9, 15))
self.assertTrue(np.allclose(result, [10, 11, 12, 13, 14]))
def test_2d(self):
buffer = BufferProcessor((5, 2), init=np.zeros((5, 2)))
self.assertTrue(buffer.data.shape == (5, 2))
self.assertTrue(np.allclose(buffer.data, 0))
# shift in new values
result = buffer(np.arange(2).reshape((1, -1)))
self.assertTrue(result.shape == (5, 2))
self.assertTrue(np.allclose(result[:4], 0))
self.assertTrue(np.allclose(result[-1], [0, 1]))
result = buffer(np.arange(2, 4).reshape((1, -1)))
self.assertTrue(result.shape == (5, 2))
self.assertTrue(np.allclose(result[:3], 0))
self.assertTrue(np.allclose(result[-2], [0, 1]))
self.assertTrue(np.allclose(result[-1], [2, 3]))
# shift in two new values
result = buffer(np.arange(4, 8).reshape((2, -1)))
self.assertTrue(result.shape == (5, 2))
self.assertTrue(np.allclose(result[0], 0))
self.assertTrue(np.allclose(result[1], [0, 1]))
self.assertTrue(np.allclose(result[2], [2, 3]))
self.assertTrue(np.allclose(result[3], [4, 5]))
self.assertTrue(np.allclose(result[4], [6, 7]))
# shift in three new values
result = buffer(np.arange(8, 14).reshape((3, -1)))
self.assertTrue(result.shape == (5, 2))
self.assertTrue(np.allclose(result.ravel(), np.arange(4, 14)))
# shift in six new values (bigger than buffer)
result = buffer(np.arange(14, 26).reshape((6, -1)))
self.assertTrue(result.shape == (5, 2))
self.assertTrue(np.allclose(result.ravel(), np.arange(16, 26)))
def test_reset(self):
buffer = BufferProcessor(5, init=np.ones(5))
self.assertTrue(np.allclose(buffer.data, 1))
result = buffer(np.arange(2))
self.assertTrue(np.allclose(result, [1, 1, 1, 0, 1]))
buffer.reset()
self.assertTrue(np.allclose(buffer.data, 1))
# clean up
def teardown_module():
import os
os.unlink(tmp_file)