-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_input_representations.py
213 lines (178 loc) · 7.88 KB
/
test_input_representations.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
import io
import re
import unittest
from music21.interval import Interval
import numpy as np
import pandas as pd
from AugmentedNet.input_representations import (
BassChromagram38,
BassChromagram70,
BassIntervals58,
MeasureNoteOnset14,
Intervals19,
)
from AugmentedNet.joint_parser import J_LISTTYPE_COLUMNS
from test import AuxiliaryFiles
aux = AuxiliaryFiles("input_representations")
def _load_dfgt(csvGT):
dfGT = pd.read_csv(csvGT)
dfGT.set_index("j_offset", inplace=True)
for col in J_LISTTYPE_COLUMNS:
dfGT[col] = dfGT[col].apply(eval)
return dfGT
def _plot_array(arr):
import matplotlib.pyplot as plt
plt.pcolor(arr.T, edgecolors="k", linewidth=1, cmap="tab20")
plt.show()
def _save(arr):
np.savetxt("tmp.txt", arr, fmt="%i", delimiter=" ")
class TestMeasureNoteOnset14(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
self.transpositions = ["m2", "M6", "P5", "d7"]
def test_encoding(self):
encoding = MeasureNoteOnset14(self.df).array
encodingGT = np.loadtxt(aux.haydnMeasureNoteOnset14GT, dtype="i1")
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
ar = np.nonzero(encoding[timestep])
arGT = np.nonzero(encodingGT[timestep])
self.assertEqual(tuple(ar[0]), tuple(arGT[0]))
def test_decoding(self):
encoding = MeasureNoteOnset14(self.df).array
decoded = MeasureNoteOnset14.decode(encoding)
decodedGT = aux.haydnMeasureNoteOnset14DecodedGT
decodedGT = [(tuple(t[0]), tuple(t[1])) for t in decodedGT]
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
self.assertEqual(decoded[timestep], decodedGT[timestep])
class TestBassChromagram38(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
self.transpositions = ["m2", "M6", "P5", "d7"]
def test_encoding(self):
encoding = BassChromagram38(self.df).array
encodingGT = np.loadtxt(aux.haydnBassChromagram38GT, dtype="i1")
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
ar = np.nonzero(encoding[timestep])
arGT = np.nonzero(encodingGT[timestep])
self.assertEqual(tuple(ar[0]), tuple(arGT[0]))
def test_decoding(self):
encoding = BassChromagram38(self.df).array
decoded = BassChromagram38.decode(encoding)
decodedGT = aux.haydnBassChromagram38DecodedGT
for timestep in range(self.timesteps):
decodedGT[timestep][2] = tuple(decodedGT[timestep][2])
decodedGT[timestep][3] = tuple(decodedGT[timestep][3])
with self.subTest(timestep=timestep):
self.assertEqual(
tuple(decoded[timestep]), tuple(decodedGT[timestep])
)
def test_data_augmentation(self):
bc38 = BassChromagram38(self.df)
daArray = bc38.array
daGT = np.loadtxt(aux.haydnBassChromagram38DA, dtype="i8")
for idx, da in enumerate(
bc38.dataAugmentation(intervals=self.transpositions)
):
daArray += (idx + 2) * da
for timestep, (gt, x) in enumerate(zip(daArray, daGT)):
with self.subTest(timestep=timestep):
self.assertEqual(gt.tolist(), x.tolist())
class TestBassIntervals58(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
self.transpositions = ["m2", "M6", "P5", "d7"]
def test_encoding(self):
encoding = BassIntervals58(self.df).array
encodingGT = np.loadtxt(aux.haydnBassIntervals58GT, dtype="i1")
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
ar = np.nonzero(encoding[timestep])
arGT = np.nonzero(encodingGT[timestep])
self.assertEqual(tuple(ar[0]), tuple(arGT[0]))
def test_decoding(self):
encoding = BassIntervals58(self.df).array
decoded = BassIntervals58.decode(encoding)
for timestep, (gt, x) in enumerate(zip(self.df.s_intervals, decoded)):
with self.subTest(timestep=timestep):
self.assertEqual(set(gt), set(x[2]))
def test_data_augmentation(self):
bi63 = BassIntervals58(self.df)
daArray = bi63.array
daGT = np.loadtxt(aux.haydnBassIntervals58DA, dtype="i8")
for idx, da in enumerate(
bi63.dataAugmentation(intervals=self.transpositions)
):
daArray += (idx + 2) * da
for timestep, (gt, x) in enumerate(zip(daArray, daGT)):
with self.subTest(timestep=timestep):
self.assertEqual(gt.tolist(), x.tolist())
class TestIntervals19(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
self.transpositions = ["m2", "M6", "P5", "d7"]
def test_encoding(self):
encoding = Intervals19(self.df).array
encodingGT = np.loadtxt(aux.haydnIntervals19GT, dtype="i1")
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
ar = np.nonzero(encoding[timestep])
arGT = np.nonzero(encodingGT[timestep])
self.assertEqual(tuple(ar[0]), tuple(arGT[0]))
def test_decoding(self):
encoding = Intervals19(self.df).array
decoded = Intervals19.decode(encoding)
for timestep, (gt, x) in enumerate(zip(self.df.s_intervals, decoded)):
intervals = [Interval(i) for i in gt]
generics = [i.generic.simpleUndirected for i in intervals]
generics = tuple(sorted(set(generics)))
chromatics = [i.semitones for i in intervals]
chromatics = tuple(sorted(set(chromatics)))
with self.subTest(timestep=timestep):
self.assertEqual((generics, chromatics), x)
# def test_data_augmentation(self):
# TODO: but it should be transposition invariant
class TestBassChromagram70(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
self.transpositions = ["m2", "M6", "P5", "d7"]
def test_encoding(self):
encoding = BassChromagram70(self.df).array
encodingGT = np.loadtxt(aux.haydnBassChromagram70GT, dtype="i1")
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
ar = np.nonzero(encoding[timestep])
arGT = np.nonzero(encodingGT[timestep])
self.assertEqual(tuple(ar[0]), tuple(arGT[0]))
def test_decoding(self):
encoding = BassChromagram70(self.df).array
decoded = BassChromagram70.decode(encoding)
for timestep, (gt, x) in enumerate(zip(self.df.s_notes, decoded)):
notes = [re.sub(r"\d", "", n) for n in gt]
bassGT, chromagramGT = notes[0], set(sorted(notes))
bass, chromagram = x[0], set(x[1])
with self.subTest(timestep=timestep):
self.assertEqual((bassGT, chromagramGT), (bass, chromagram))
def test_data_augmentation(self):
bc70 = BassChromagram70(self.df)
daArray = bc70.array
daGT = np.loadtxt(aux.haydnBassChromagram70DA, dtype="i8")
for idx, da in enumerate(
bc70.dataAugmentation(intervals=self.transpositions)
):
daArray += (idx + 2) * da
for timestep, (gt, x) in enumerate(zip(daArray, daGT)):
with self.subTest(timestep=timestep):
self.assertEqual(gt.tolist(), x.tolist())