-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_output_representations.py
171 lines (127 loc) · 4.29 KB
/
test_output_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
import io
import unittest
import numpy as np
import pandas as pd
from AugmentedNet.feature_representation import CHORD_QUALITIES
from AugmentedNet.joint_parser import J_LISTTYPE_COLUMNS
from AugmentedNet.output_representations import (
Bass35,
Inversion4,
RomanNumeral31,
LocalKey38,
PrimaryDegree22,
SecondaryDegree22,
ChordQuality11,
ChordRoot35,
TonicizedKey38,
HarmonicRhythm7,
PitchClassSet121,
)
from test import AuxiliaryFiles
aux = AuxiliaryFiles("output_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=" ")
def _one_hot_encode(encoding, classList):
timesteps = len(encoding)
features = len(classList)
array = np.zeros((timesteps, features), dtype="i8")
for t, enc in enumerate(encoding):
array[t, enc] = 1
return array
class TestBass35(unittest.TestCase):
clas = Bass35
encodingGT = aux.haydnBass35GT
dfFeature = "a_bass"
transpositions = ["m2", "M6", "P5", "d7"]
def setUp(self):
self.maxDiff = None
self.df = _load_dfgt(aux.haydn)
self.timesteps = len(self.df.index)
def test_encoding(self):
encoding = self.clas(self.df).array.reshape(-1)
encodingGT = np.loadtxt(self.encodingGT, dtype="i1")
encodingGT = np.argmax(encodingGT, axis=1)
for timestep in range(self.timesteps):
with self.subTest(timestep=timestep):
bass = encoding[timestep]
bassGT = encodingGT[timestep]
self.assertEqual(bass, bassGT)
def test_decoding(self):
encoding = self.clas(self.df).array
decoded = self.clas.decode(encoding)
for timestep, (gt, x) in enumerate(
zip(self.df[self.dfFeature], decoded)
):
with self.subTest(timestep=timestep):
self.assertEqual(gt, x)
# def test_data_augmentation(self):
# rep = self.clas(self.df)
# daArray = np.copy(rep.array)
# daGT = np.zeros(rep.shape)
# for idx, da in enumerate(
# rep.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 TestHarmonicRhythm7(TestBass35):
clas = HarmonicRhythm7
encodingGT = aux.haydnHarmonicRhythm7
dfFeature = "a_harmonicRhythm"
class TestTonicizedKey38(TestBass35):
clas = TonicizedKey38
encodingGT = aux.haydnTonicizedKey38
dfFeature = "a_tonicizedKey"
class TestPcSet94(TestBass35):
clas = PitchClassSet121
encodingGT = aux.haydnPitchClassSet121
dfFeature = "a_pcset"
# def test_encoding(self):
# super().test_encoding()
# def test_decoding(self):
# super().test_decoding()
# def test_data_augmentation(self):
# super().test_data_augmentation()
class TestInversion4(TestBass35):
clas = Inversion4
encodingGT = aux.haydnInversion4
dfFeature = "a_inversion"
class TestRomanNumeral31(TestBass35):
clas = RomanNumeral31
encodingGT = aux.haydnRomanNumeral31
dfFeature = "a_romanNumeral"
class TestLocalKey38(TestBass35):
clas = LocalKey38
encodingGT = aux.haydnLocalKey38
dfFeature = "a_localKey"
class TestPrimaryDegree22(TestBass35):
clas = PrimaryDegree22
encodingGT = aux.haydnPrimaryDegree22
dfFeature = "a_degree1"
class TestSecondaryDegree22(TestBass35):
clas = SecondaryDegree22
encodingGT = aux.haydnSecondaryDegree22
dfFeature = "a_degree2"
class TestChordQuality11(TestBass35):
clas = ChordQuality11
encodingGT = aux.haydnChordQuality11
dfFeature = "a_quality"
def test_encoding(self):
super().test_encoding()
def test_decoding(self):
super().test_decoding()
class TestChordRoot35(TestBass35):
clas = ChordRoot35
encodingGT = aux.haydnChordRoot35
dfFeature = "a_root"