-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_data_info.py
More file actions
199 lines (176 loc) · 7.27 KB
/
plot_data_info.py
File metadata and controls
199 lines (176 loc) · 7.27 KB
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
import numpy as np
import matplotlib.pyplot as plt
import json
import os
G_FLAG = True#False
C_FLAG = True
B_FLAG = True
K_FLAG = True#False
FILES_VER = False#True
dataf = input('Enter data file name:')
outfd = input('Enter output folder:')
if not os.path.exists(outfd): os.makedirs(outfd)
out_g = os.path.join(outfd, 'dataset_GENRE.txt')
out_c = os.path.join(outfd, 'dataset_CATEGORY.txt')
out_b = os.path.join(outfd, 'dataset_BPM.txt')
out_k = os.path.join(outfd, 'dataset_KEY.txt')
genres = ['8Bit Chiptune', 'Acid', 'Acoustic', 'Ambient', 'Big Room', 'Blues', 'Boom Bap', 'Breakbeat', 'Chill Out', 'Cinematic', 'Classical', 'Comedy', 'Country', 'Crunk', 'Dance', 'Dancehall', 'Deep House', 'Dirty', 'Disco', 'Drum And Bass', 'Dub', 'Dubstep', 'EDM', 'Electro', 'Electronic', 'Ethnic', 'Folk', 'Funk', 'Fusion', 'Garage', 'Glitch', 'Grime', 'Grunge', 'Hardcore', 'Hardstyle', 'Heavy Metal', 'Hip Hop', 'House', 'Indie', 'Industrial', 'Jazz', 'Jungle', 'Lo-Fi', 'Moombahton', 'Orchestral', 'Pop', 'Psychedelic', 'Punk', 'Rap', 'Rave', 'Reggae', 'Reggaeton', 'Religious', 'RnB', 'Rock', 'Samba', 'Ska', 'Soul', 'Spoken Word', 'Techno', 'Trance', 'Trap', 'Trip Hop', 'Weird']
categories = ['Accordion', 'Arpeggio', 'Bagpipe', 'Banjo', 'Bass', 'Bass Guitar', 'Bass Synth', 'Bass Wobble', 'Beatbox', 'Bells', 'Brass', 'Choir', 'Clarinet', 'Didgeridoo',
'Drum', 'Flute', 'Fx', 'Groove', 'Guitar Acoustic', 'Guitar Electric', 'Harmonica', 'Harp', 'Harpsichord', 'Mandolin', 'Orchestral', 'Organ', 'Pad', 'Percussion', 'Piano', 'Rhodes Piano', 'Scratch', 'Sitar', 'Soundscapes', 'Strings', 'Synth', 'Tabla', 'Ukulele', 'Violin', 'Vocal', 'Woodwind']
if FILES_VER:
folder = input('enter the folder that contains files:')
files = os.listdir(folder)
cats, gens, bpm, key = dict(), dict(), dict(), dict()
with open(dataf, 'r') as i:
for line in i.readlines():
if line=="\n": continue
data = json.loads(line)
for t in data['tags']:
if t.strip('Loops').strip(' ') in genres:
if FILES_VER:
filename = data['index']+'.wav'
if filename not in files: continue
tt = t.strip('Loops').strip(' ')
if tt in gens: gens[tt] += 1
else: gens[tt] = 1
if t.strip('Loops').strip(' ') in categories:
if FILES_VER:
filename = data['index']+'.wav'
if filename not in files: continue
tt = t.strip('Loops').strip(' ')
if tt in cats: cats[tt] += 1
else: cats[tt] = 1
if t.endswith('bpm'):
if FILES_VER:
filename = data['index']+'.wav'
if filename not in files: continue
tt = t.split(' ')[0]
tt = (str(int(tt)//10*10))
if tt in bpm: bpm[tt] += 1
else: bpm[tt] = 1
if t.startswith('Key'):
if FILES_VER:
filename = data['index']+'.wav'
if filename not in files: continue
tt = t.split(' ')[-1]
if tt in key: key[tt] += 1
else: key[tt] = 1
if(G_FLAG):
print('--------------------GENRE--------------------')
# by Genres
ts = [t for t in genres and gens]
s = 5
y_pos = np.arange(0, len(ts)*s, s)
vs = [gens[t] for t in genres and gens]
print(len(gens))
print(gens)
# save text to file
with open(out_g, 'w') as o:
for j in range(len(genres)):
if genres[j] in gens:
o.write(genres[j]+','+str(gens[genres[j]])+'\n')
else:
o.write(genres[j]+',0\n')
# Plot - by Genres
plt.figure(figsize=(23, 17), num='Looperman Dataset')
plt.barh(y_pos, vs, align='center', alpha=0.5)
for a, b in zip(y_pos, vs):
plt.text(b+0.5, a-1.5, str(b))
plt.yticks(y_pos, ts)
plt.xlabel('Data #')
plt.ylabel('Genres')
plt.xlim([0,max(vs)+1])
plt.ylim([-2,y_pos[-1]+2.3])
plt.tick_params(axis='y', which='major', labelsize=7)
plt.title('Looperman Dataset')
plt.savefig(os.path.join(outfd, 'looperman_dataset_genres.png'), bbox_inches='tight', pad_inches=0.0)
#plt.show()
plt.close()
if(C_FLAG):
print('--------------------Categories--------------------')
# by categories
ts = [t for t in categories and cats]
s = 5
y_pos = np.arange(0, len(ts)*s, s)
vs = [cats[t] for t in categories and cats]
print(len(cats))
print(cats)
# save text to file
with open(out_c, 'w') as o:
for j in range(len(categories)):
if categories[j] in cats:
o.write(categories[j]+','+str(cats[categories[j]])+'\n')
else:
o.write(categories[j]+',0\n')
# Plot - by categories
plt.figure(figsize=(23, 17), num='Looperman Dataset')
plt.barh(y_pos, vs, align='center', alpha=0.5, color='plum')
for a, b in zip(y_pos, vs):
plt.text(b+0.5, a-1.5, str(b))
plt.yticks(y_pos, ts)
plt.xlabel('Data #')
plt.ylabel('Categories')
plt.xlim([0,max(vs)+1])
plt.ylim([-2,y_pos[-1]+2.3])
plt.tick_params(axis='y', which='major', labelsize=7)
plt.title('Looperman Dataset')
plt.savefig(os.path.join(outfd, 'looperman_dataset_categories.png'), bbox_inches='tight', pad_inches=0.0)
#plt.show()
plt.close()
if(K_FLAG):
print('--------------------KEY--------------------')
# by Keys
k = sorted(key.items(), key=lambda a:a[0], reverse=True)
ts = [t[0] for t in k]
vs = [t[1] for t in k]
#ts = [t for t in key]
s = 5
y_pos = np.arange(0, len(ts)*s, s)
#vs = [key[t] for t in key]
print(len(key))
print(key)
# save text to file
with open(out_k, 'w') as o:
for j in key: o.write(j+','+str(key[j])+'\n')
# Plot
plt.figure(figsize=(17, 10), num='Looperman Dataset')
plt.barh(y_pos, vs, align='center', alpha=0.5, color='green')
for a, b in zip(y_pos, vs):
plt.text(b+0.5, a-1.5, str(b))
plt.yticks(y_pos, ts)
plt.xlabel('Data #')
plt.ylabel('Key')
plt.xlim([0,max(vs)+1])
plt.ylim([-2,y_pos[-1]+2.3])
plt.tick_params(axis='y', which='major', labelsize=7)
plt.title('Looperman Dataset')
plt.savefig(os.path.join(outfd, 'looperman_dataset_keys.png'), bbox_inches='tight', pad_inches=0.0)
#plt.show()
plt.close()
if(B_FLAG):
print('--------------------BPM--------------------')
# by BPM
b = sorted(bpm.items(), key=lambda k:int(k[0]))
ts = [t[0] for t in b]
vs = [t[1] for t in b]
s = 1
y_pos = np.arange(0, len(ts)*s, s)
print(len(bpm))
print(bpm)
# save text to file
with open(out_b, 'w') as o:
for j in bpm: o.write(j+','+str(bpm[j])+'\n')
# Plot - by BPM
plt.figure(figsize=(23, 17), num='Looperman Dataset')
plt.bar(ts, vs, align='center', alpha=0.5, color='red')
for a, b in zip(y_pos, vs):plt.text(a, b+2, str(b), ha='center')
#plt.yticks(y_pos, ts)
plt.ylabel('Data #')
plt.xlabel('BPM')
#plt.ylim([0,max(vs)+1])
plt.xlim([-2,y_pos[-1]+2.3])
plt.tick_params(axis='x', which='major', labelsize=7)
plt.title('Looperman Dataset')
plt.savefig(os.path.join(outfd, 'looperman_dataset_bpm.png'), bbox_inches='tight', pad_inches=0.0)
#plt.show()
plt.close()