-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_clean.py
345 lines (276 loc) · 12.7 KB
/
data_clean.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
import pickle
import re
import ujson
import collections
import antlr4
from chord_labels import parse_chord, chord_labels
from progressbar import ProgressBar, Bar, Percentage, AdaptiveETA, Counter
from numpy import sum
print("Opening files")
with open('dataset_values.pickle', 'rb') as values:
#print('opening values')
formatted_values = pickle.load(values)
with open('dataset_chroma.pickle', 'rb') as chroma:
#print('opening chroma')
formatted_chroma = pickle.load(chroma)
print("Files Opened\n")
cleaned_keys = []
cleaned_chroma = []
key_binary_pairs = {}
blank = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
blank12 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# How many frames to look at per chord:
frame_size = 20
# Step size per time sample in chroma files
timestep_abs = 0.046439909
# id = 3
#
# lastTime = int(list(reversed(formatted_values[id].keys()))[1])
# print("LAST: " + str(lastTime))
#
# for time in reversed(formatted_values[id]):
# intTime = int(time)
# print(str(intTime))
#
# #for chromaTime in reversed(formatted_chroma[id]):
# [fileID, [time, [x24 vals]]]
# [fileid, [time, chord]]
# [intTime, [x24 avg vals]]
# [[x24 vals], chord]
# returns averaged list from formatted_chroma
def average_chroma_by_time(id, start_time, end_time):
averaged_vals = blank
added = blank
for time in list(formatted_chroma[id]):
if time > end_time:
# print("ADDED: " + str(added))
for num, val in enumerate(added):
if val != 0:
# print("num added: " + str(added[num]))
averaged_vals[num] = averaged_vals[num] / added[num]
# print(str(averaged_vals))
break
if end_time >= time >= start_time:
averaged_vals = list(sum([averaged_vals, formatted_chroma[id][time]], axis=0))
for num, val in enumerate(formatted_chroma[id][time]):
if val != 0:
added[num] += 1
# print("INDEX: " + str(num))
# print("EQUALS: " + str(averaged_vals))
# print("EQUALS SIZE: " + str(cleaned_vals[intTime]))
return averaged_vals
def get_frames_by_time(id, chord_time):
blank = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
timestep = round(timestep_abs, 9)
values = []
chord_timestamps = list(formatted_values[id])
start_time = chord_time - (timestep * 15)
end_time = chord_time + (timestep * 15)
end_time_bounded = end_time
if start_time < 0.0:
start_num_diff = int(abs(start_time - timestep) / timestep)
print("NUM BEFORE: %s" % start_num_diff)
for i in range(start_num_diff):
values.append(blank)
start_time = timestep
if end_time > list(formatted_chroma[id])[-1]:
end_time_bounded = list(formatted_chroma[id])[-1]
current_time = start_time
while current_time < end_time_bounded:
values.append(formatted_chroma[id][current_time])
print("Added time: %s" % current_time)
current_time += timestep
current_time = round(current_time, 9)
if end_time > list(formatted_chroma[id])[-1]:
end_num_diff = int(abs(end_time - list(formatted_chroma[id])[-1]) / timestep)
print("NUM END: %s" % end_num_diff)
for i in range(end_num_diff):
values.append(blank)
return values
def get_frames_by_time(id, chord_time, num_before, num_after):
vals = []
timestamps = list(formatted_chroma[id])
start_num_to_add = num_before
end_num_to_add = num_after
start_to_add_blank = 0
end_to_add_blank = 0
for time in range(len(timestamps)):
# print("Time: %s" % timestamps[time])
# if time == formatted_values[id][999]:
# # print("End: %s" % timestamps[time])
# return vals
if time == chord_time or timestamps[time] < chord_time < timestamps[time+1] or timestamps[time+1] > chord_time:
# print("In chord time range")
if len(timestamps[:time]) < start_num_to_add:
start_to_add_blank = start_num_to_add - len(timestamps[:time])
start_num_to_add -= start_to_add_blank
if len(timestamps[time:]) < end_num_to_add:
end_num_to_add = len(timestamps[time:])-1
end_to_add_blank = num_after - end_num_to_add
# print("Start num to add: %s" % start_num_to_add)
# print("End num to add: %s" % end_num_to_add)
# print("Start to add: %s" % start_to_add_blank)
# print("End to add: %s" % end_to_add_blank)
# Chord time to middle
vals.append(formatted_chroma[id][timestamps[time]])
#add to front as decreasing
i = 1
while start_num_to_add > 0:
vals.insert(0, formatted_chroma[id][timestamps[time - i]])
i += 1
start_num_to_add -= 1
#add to back as increasing
i = 1
while end_num_to_add > 0:
try:
vals.append(formatted_chroma[id][timestamps[time + i]])
except IndexError:
pass
i += 1
end_num_to_add -= 1
if len(timestamps[:time]) < num_before:
for i in range(start_to_add_blank):
vals.insert(0, blank)
if len(timestamps[time:]) < num_after:
for i in range(end_to_add_blank):
vals.append(blank)
return vals
return vals
# cleaned_data[0] = average_chroma_by_time(3, 148, 149)
# print(str(cleaned_data))
# start_test = get_frames_by_time(3, 7.3469387e-2)
# mid_test = get_frames_by_time(3, 5.015510204)
# end_test = get_frames_by_time(9999, 0.743038548)
# print(start_test)
# print(len(start_test))
# print(mid_test)
# print(len(mid_test))
# print(end_test)
# print(len(end_test))
num_excepted = 0
with open('file_ids.txt', 'r') as idList:
print("--CLEANING FILES: 890 TODO--\n")
progress_bar = ProgressBar(widgets=['PROCESSED: ', Counter(), '/890 ', Bar('>'), Percentage(), ' --- ', AdaptiveETA()], maxval=891)
progress_bar.start()
for i, id in enumerate(idList):
progress_bar.update(value=i)
#id = int(id.strip('\n'))
#print("CURRENT ID: %s" % id)
id = 3
for index, time in enumerate(formatted_values[id]):
if time == 999:
break
num_chords = range(len(formatted_values[id][time]))
try:
next_time = list(formatted_values[id])[index + 1]
#print("NEXT TIME: " + str(next_time))
if next_time == 999:
break
else:
#print("NEXT TIME = 999 (END)")
steps_between_chords = len(formatted_chroma[id][time:next_time])
time_increment = (next_time-time)/steps_between_chords
print("steps between chords: {}".format(steps_between_chords))
# print("End time: " + str(end_time))
# print("i: " + str(i))
if steps_between_chords > 62:
try:
num_steps = int(steps_between_chords / 31)
print("num steps: {}".format(num_steps))
blank_timestamps = []
for i in range(num_steps):
curr_time = (num_steps / steps_between_chords) * (i + 1)
print("curr_time: {}".format(curr_time))
blank_timestamps.append(get_frames_by_time(id, curr_time, 10, 20))
except BaseException:
num_excepted += 1
for i in num_chords:
key = str(formatted_values[id][time][i])
if ' ' in key:
key = key.split(' ')[0]
key = re.sub(r" ?\([^)]+\)", "", key)
key = re.sub(r"/[^)]+", "", key)
if '*' not in key and key != "" and key != "&pause" and key != '5':
try:
key_binary = parse_chord(key).tones_binary
hold = []
for i in range(15):
hold.append(blank12)
hold.append(key_binary)
for i in range(15):
hold.append(blank12)
cleaned_keys.append(hold)
cleaned_chroma.append(get_frames_by_time(id, time, 10, 20))
key_binary_pairs[tuple(key_binary)] = key
except BaseException:
num_excepted += 1
# print("CLEANED DATA [" + str(formatted_values[id][time][i]) + "]: " + str(cleaned_data[formatted_values[id][time][i]]))
break
# else:
# #print("num " + str(len(formatted_values[id][time])))
# time_increment = (next_time - time) / (len(formatted_values[id][time]))
# for i in num_chords:
# end_time = (time + (time_increment * (i + 1)))
# key = str(formatted_values[id][time][i])
# steps_between_chords = (end_time - time) / time_increment
# print("steps between chords: {}".format(steps_between_chords))
# # print("End time: " + str(end_time))
# # print("i: " + str(i))
# if steps_between_chords > 32:
# num_steps = int(steps_between_chords / 31)
# print("num steps: {}".format(num_steps))
# blank_timestamps = []
# for i in range(num_steps):
# curr_time = (num_steps/steps_between_chords)*(i+1)
# print("curr_time: {}".format(curr_time))
# blank_timestamps.append(get_frames_by_time(id, curr_time, 10, 20))
#
# if ' ' in key:
# key = key.split(' ')[0]
# key = re.sub(r" ?\([^)]+\)", "", key)
# key = re.sub(r"/[^)]+", "", key)
# if '*' not in key and key != "" and key != "&pause" and key != '5':
# try:
# key_binary = parse_chord(key).tones_binary
# hold = []
# for i in range(15):
# hold.append(blank12)
# hold.append(key_binary)
# for i in range(15):
# hold.append(blank12)
# cleaned_keys.append(hold)
# cleaned_chroma.append(get_frames_by_time(id, time, 10, 20))
# key_binary_pairs[tuple(key_binary)] = key
# except BaseException:
# num_excepted += 1
# # print("CLEANED DATA [" + str(formatted_values[id][time][i]) + "]: " + str(cleaned_data[formatted_values[id][time][i]]))
except IndexError:
None
print("NUM EXCEPTED DUE TO PARSER: %s" % num_excepted)
def nested_list_count(l):
return sum(1+nested_list_count(i) for i in l if isinstance(i,list))
for id, list in enumerate(cleaned_chroma):
if len(cleaned_chroma[id]) != 31:
print("not 31 : %s" % id)
raise UserWarning
print('\n')
print("<------------------------------------------------------->")
print("<------------------COUNTING KEYS------------------------>")
print("<------------------------------------------------------->")
# num_keys = collections.Counter(cleaned_keys)
# for key in sorted(num_keys, key=num_keys.get):
# if num_keys[key] < 1000:
# while key in cleaned_keys:
# index = cleaned_keys.index(key)
# cleaned_keys.remove(key)
# cleaned_chroma.__delitem__(index)
#print(str(cleaned_keys))
#print(str(cleaned_chroma))
print("NUM OBJECTS: " + str(len(cleaned_keys)))
with open("cleaned_keys.json", 'w') as file:
ujson.dump(cleaned_keys, file)
with open("cleaned_chroma.json", 'w') as file:
ujson.dump(cleaned_chroma, file)
with open("key_binary_pairs.json", 'w') as file:
ujson.dump(key_binary_pairs, file)
print("DONE SAVING")