Maria@4
|
1 # -*- coding: utf-8 -*-
|
Maria@4
|
2 """
|
Maria@4
|
3 Created on Thu Mar 16 01:50:57 2017
|
Maria@4
|
4
|
Maria@4
|
5 @author: mariapanteli
|
Maria@4
|
6 """
|
Maria@4
|
7
|
Maria@4
|
8 import numpy as np
|
Maria@4
|
9 import pandas as pd
|
Maria@4
|
10 import os
|
Maria@4
|
11 from sklearn.decomposition import NMF
|
Maria@4
|
12 import OPMellin as opm
|
Maria@4
|
13 import MFCC as mfc
|
Maria@4
|
14 import PitchBihist as pbi
|
Maria@4
|
15
|
Maria@4
|
16
|
Maria@4
|
17 class FeatureLoader:
|
Maria@4
|
18 def __init__(self, win2sec=8):
|
Maria@4
|
19 self.win2sec = float(win2sec)
|
Maria@4
|
20 self.sr = 44100.
|
Maria@4
|
21 self.win1 = int(round(0.04*self.sr))
|
Maria@4
|
22 self.hop1 = int(round(self.win1/8.))
|
Maria@4
|
23 self.framessr = self.sr/float(self.hop1)
|
Maria@4
|
24 self.win2 = int(round(self.win2sec*self.framessr))
|
Maria@4
|
25 self.hop2 = int(round(0.5*self.framessr))
|
Maria@4
|
26 self.framessr2 = self.framessr/float(self.hop2)
|
Maria@4
|
27
|
Maria@4
|
28
|
m@26
|
29 def get_op_mfcc_for_file(self, melspec_file=None, scale=True, stop_sec=30.0):
|
Maria@4
|
30 op = []
|
Maria@4
|
31 mfc = []
|
Maria@4
|
32 if not os.path.exists(melspec_file):
|
Maria@4
|
33 return op, mfc
|
Maria@4
|
34 print 'extracting onset patterns and mfccs...'
|
Maria@4
|
35 songframes = pd.read_csv(melspec_file, engine="c", header=None)
|
Maria@4
|
36 songframes.iloc[np.where(np.isnan(songframes))] = 0
|
Maria@35
|
37 n_stop = np.int(np.ceil(stop_sec * self.framessr))
|
m@26
|
38 songframes = songframes.iloc[0:min(len(songframes), n_stop), :]
|
Maria@4
|
39 melspec = songframes.get_values().T
|
Maria@4
|
40 op = self.get_op_from_melspec(melspec, K=2)
|
Maria@4
|
41 mfc = self.get_mfcc_from_melspec(melspec)
|
Maria@4
|
42 if scale:
|
Maria@4
|
43 # scale all frames by mean and std of recording
|
Maria@4
|
44 op = (op - np.nanmean(op)) / np.nanstd(op)
|
Maria@4
|
45 mfc = (mfc - np.nanmean(mfc)) / np.nanstd(mfc)
|
Maria@4
|
46 return op, mfc
|
Maria@4
|
47
|
Maria@4
|
48
|
m@26
|
49 def get_chroma_for_file(self, chroma_file=None, scale=True, stop_sec=30.0):
|
Maria@4
|
50 ch = []
|
Maria@4
|
51 if not os.path.exists(chroma_file):
|
Maria@4
|
52 return ch
|
Maria@4
|
53 print 'extracting chroma...'
|
Maria@4
|
54 songframes = pd.read_csv(chroma_file, engine="c", header=None)
|
Maria@4
|
55 songframes.iloc[np.where(np.isnan(songframes))] = 0
|
m@26
|
56 n_stop = np.int(np.ceil(stop_sec * self.framessr))
|
Maria@35
|
57 songframes = songframes.iloc[0:min(len(songframes), n_stop), :]
|
Maria@4
|
58 chroma = songframes.get_values().T
|
Maria@4
|
59 ch = self.get_ave_chroma(chroma)
|
Maria@4
|
60 if scale:
|
Maria@4
|
61 # scale all frames by mean and std of recording
|
Maria@4
|
62 ch = (ch - np.nanmean(ch)) / np.nanstd(ch)
|
Maria@4
|
63 return ch
|
Maria@4
|
64
|
Maria@4
|
65
|
Maria@4
|
66 def get_music_idx_from_bounds(self, bounds, sr=None):
|
Maria@4
|
67 music_idx = []
|
Maria@4
|
68 if len(bounds) == 0:
|
Maria@4
|
69 # bounds is empty list
|
Maria@4
|
70 return music_idx
|
Maria@4
|
71 nbounds = bounds.shape[0]
|
Maria@4
|
72 if len(np.where(bounds[:,2]=='m')[0])==0:
|
Maria@4
|
73 # no music segments
|
Maria@4
|
74 return music_idx
|
Maria@4
|
75 elif len(np.where(bounds[:,2]=='s')[0])==nbounds:
|
Maria@4
|
76 # all segments are speech
|
Maria@4
|
77 return music_idx
|
Maria@4
|
78 else:
|
Maria@34
|
79 win2_frames = np.int(np.round(self.win2sec * self.framessr2))
|
Maria@34
|
80 #half_win_hop = int(round(0.5 * self.win2 / float(self.hop2)))
|
Maria@4
|
81 music_bounds = np.where(bounds[:, 2] == 'm')[0]
|
Maria@4
|
82 bounds_in_frames = np.round(np.array(bounds[:, 0], dtype=float) * sr)
|
Maria@34
|
83 duration_in_frames = np.ceil(np.array(bounds[:, 1], dtype=float) * sr)
|
Maria@4
|
84 for music_bound in music_bounds:
|
Maria@34
|
85 #lower_bound = np.max([0, bounds_in_frames[music_bound] - half_win_hop])
|
Maria@34
|
86 #upper_bound = bounds_in_frames[music_bound] + duration_in_frames[music_bound] - half_win_hop
|
Maria@34
|
87 lower_bound = bounds_in_frames[music_bound]
|
Maria@34
|
88 upper_bound = bounds_in_frames[music_bound] + duration_in_frames[music_bound] - win2_frames
|
Maria@4
|
89 music_idx.append(np.arange(lower_bound, upper_bound, dtype=int))
|
Maria@4
|
90 if len(music_idx)>0:
|
Maria@4
|
91 music_idx = np.sort(np.concatenate(music_idx)) # it should be sorted, but just in case segments overlap -- remove duplicates if segments overlap
|
Maria@4
|
92 return music_idx
|
m@6
|
93
|
Maria@4
|
94
|
Maria@4
|
95 def get_music_idx_for_file(self, segmenter_file=None):
|
Maria@4
|
96 music_idx = []
|
Maria@4
|
97 if os.path.exists(segmenter_file) and os.path.getsize(segmenter_file)>0:
|
Maria@4
|
98 print 'loading speech/music segments...'
|
Maria@4
|
99 bounds = pd.read_csv(segmenter_file, header=None, delimiter='\t').get_values()
|
Maria@4
|
100 if bounds.shape[1] == 1: # depends on the computer platform
|
Maria@4
|
101 bounds = pd.read_csv(segmenter_file, header=None, delimiter=',').get_values()
|
Maria@4
|
102 music_idx = self.get_music_idx_from_bounds(bounds, sr=self.framessr2)
|
Maria@4
|
103 return music_idx
|
Maria@4
|
104
|
Maria@4
|
105
|
m@42
|
106 def get_features(self, df, stop_sec=30.0, class_label='Country', precomp_melody=True):
|
Maria@4
|
107 oplist = []
|
Maria@4
|
108 mflist = []
|
Maria@4
|
109 chlist = []
|
Maria@4
|
110 pblist = []
|
Maria@4
|
111 clabels = []
|
Maria@4
|
112 aulabels = []
|
Maria@4
|
113 n_files = len(df)
|
Maria@4
|
114 for i in range(n_files):
|
Maria@4
|
115 if not (os.path.exists(df['Melspec'].iloc[i]) and os.path.exists(df['Chroma'].iloc[i]) and os.path.exists(df['Melodia'].iloc[i])):
|
Maria@4
|
116 continue
|
Maria@4
|
117 print 'file ' + str(i) + ' of ' + str(n_files)
|
Maria@4
|
118 music_idx = self.get_music_idx_for_file(df['Speech'].iloc[i])
|
m@43
|
119 #min_dur_sec=8.0
|
m@43
|
120 #min_n_frames = np.int(np.floor(min_dur_sec * self.framessr2))
|
m@43
|
121 if len(music_idx)==0: # or len(music_idx)<min_n_frames:
|
m@43
|
122 # no music segments or music segment too short -> skip this file
|
Maria@4
|
123 continue
|
m@46
|
124 try:
|
m@43
|
125 # allow feature extraction of longer segments (2*stop_sec)
|
m@43
|
126 # because some of it might be speech segments that are filtered out
|
m@43
|
127 stop_sec_feat = 2 * stop_sec
|
m@43
|
128 op, mfcc = self.get_op_mfcc_for_file(df['Melspec'].iloc[i], stop_sec=stop_sec_feat)
|
m@43
|
129 ch = self.get_chroma_for_file(df['Chroma'].iloc[i], stop_sec=stop_sec_feat)
|
m@43
|
130 pb = self.get_pb_for_file(df['Melodia'].iloc[i], precomp_melody=precomp_melody, stop_sec=stop_sec_feat)
|
Maria@35
|
131 #if precomp_melody:
|
Maria@35
|
132 # pb = self.load_precomputed_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec)
|
Maria@35
|
133 #else:
|
Maria@35
|
134 # pb = self.get_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec)
|
m@46
|
135 except:
|
Maria@4
|
136 continue
|
m@26
|
137 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
|
Maria@35
|
138 print n_stop, len(op), len(mfcc), len(ch), len(pb)
|
m@43
|
139 max_n_frames = np.min([n_stop, len(op), len(mfcc), len(ch), len(pb)]) # ideally, features should have the same number of frames
|
m@43
|
140 if max_n_frames==0:
|
Maria@4
|
141 # no features extracted -> skip this file
|
Maria@4
|
142 continue
|
m@43
|
143 # music segment duration must be <= 30sec
|
m@43
|
144 music_idx = music_idx[music_idx<max_n_frames]
|
Maria@4
|
145 n_frames = len(music_idx)
|
Maria@4
|
146 oplist.append(op.iloc[music_idx, :])
|
Maria@4
|
147 mflist.append(mfcc.iloc[music_idx, :])
|
Maria@4
|
148 chlist.append(ch.iloc[music_idx, :])
|
Maria@4
|
149 pblist.append(pb.iloc[music_idx, :])
|
Maria@4
|
150 clabels.append(pd.DataFrame(np.repeat(df[class_label].iloc[i], n_frames)))
|
Maria@4
|
151 aulabels.append(pd.DataFrame(np.repeat(df['Audio'].iloc[i], n_frames)))
|
Maria@4
|
152 print len(oplist), len(mflist), len(chlist), len(pblist), len(clabels), len(aulabels)
|
Maria@4
|
153 return pd.concat(oplist), pd.concat(mflist), pd.concat(chlist), pd.concat(pblist), pd.concat(clabels), pd.concat(aulabels)
|
Maria@4
|
154
|
Maria@4
|
155
|
Maria@4
|
156 def get_op_from_melspec(self, melspec, K=None):
|
Maria@4
|
157 op = opm.OPMellin(win2sec=self.win2sec)
|
Maria@4
|
158 opmellin = op.get_opmellin_from_melspec(melspec=melspec, melsr=self.framessr)
|
Maria@35
|
159 opmel = pd.DataFrame(opmellin.T)
|
Maria@4
|
160 if K is not None:
|
Maria@4
|
161 opmel = self.mean_K_bands(opmellin.T, K)
|
Maria@35
|
162 opmel = pd.DataFrame(opmel)
|
Maria@4
|
163 return opmel
|
Maria@4
|
164
|
Maria@4
|
165
|
Maria@4
|
166 def get_mfcc_from_melspec(self, melspec, deltamfcc=True, avelocalframes=True, stdlocalframes=True):
|
Maria@4
|
167 mf = mfc.MFCCs()
|
Maria@4
|
168 mfcc = mf.get_mfccs_from_melspec(melspec=melspec, melsr=self.framessr)
|
Maria@4
|
169 if deltamfcc:
|
Maria@4
|
170 ff = mfcc
|
Maria@4
|
171 ffdiff = np.diff(ff, axis=1)
|
Maria@4
|
172 ffdelta = np.concatenate((ffdiff, ffdiff[:,-1,None]), axis=1)
|
Maria@4
|
173 frames = np.concatenate([ff,ffdelta], axis=0)
|
Maria@4
|
174 mfcc = frames
|
Maria@4
|
175 if avelocalframes:
|
Maria@4
|
176 mfcc = self.average_local_frames(mfcc, getstd=stdlocalframes)
|
Maria@4
|
177 mfcc = pd.DataFrame(mfcc.T)
|
Maria@4
|
178 return mfcc
|
Maria@4
|
179
|
Maria@4
|
180
|
Maria@35
|
181 def get_ave_chroma(self, chroma, alignchroma=True, avelocalframes=True, stdlocalframes=True):
|
Maria@4
|
182 chroma[np.where(np.isnan(chroma))] = 0
|
Maria@4
|
183 if alignchroma:
|
Maria@35
|
184 maxind = np.argmax(np.sum(chroma, axis=1)) # bin with max magnitude across time
|
Maria@4
|
185 chroma = np.roll(chroma, -maxind, axis=0)
|
Maria@4
|
186 if avelocalframes:
|
Maria@4
|
187 chroma = self.average_local_frames(chroma, getstd=stdlocalframes)
|
Maria@4
|
188 chroma = pd.DataFrame(chroma.T)
|
Maria@4
|
189 return chroma
|
Maria@4
|
190
|
Maria@4
|
191
|
Maria@4
|
192 def average_local_frames(self, frames, getstd=False):
|
Maria@4
|
193 nbins, norigframes = frames.shape
|
Maria@4
|
194 if norigframes<self.win2:
|
Maria@4
|
195 nframes = 1
|
Maria@4
|
196 else:
|
Maria@4
|
197 nframes = int(1+np.floor((norigframes-self.win2)/float(self.hop2)))
|
Maria@4
|
198 if getstd:
|
Maria@4
|
199 aveframes = np.empty((nbins+nbins, nframes))
|
Maria@4
|
200 for i in range(nframes): # loop over all 8-sec frames
|
Maria@4
|
201 meanf = np.nanmean(frames[:, (i*self.hop2):min((i*self.hop2+self.win2),norigframes)], axis=1)
|
Maria@4
|
202 stdf = np.nanstd(frames[:, (i*self.hop2):min((i*self.hop2+self.win2),norigframes)], axis=1)
|
Maria@4
|
203 aveframes[:,i] = np.concatenate((meanf,stdf))
|
Maria@4
|
204 else:
|
Maria@4
|
205 aveframes = np.empty((nbins, nframes))
|
Maria@4
|
206 for i in range(nframes): # loop over all 8-sec frames
|
Maria@4
|
207 aveframes[:,i] = np.nanmean(frames[:, (i*self.hop2):min((i*self.hop2+self.win2),norigframes)], axis=1)
|
Maria@4
|
208 return aveframes
|
Maria@4
|
209
|
Maria@4
|
210
|
Maria@4
|
211 def mean_K_bands(self, songframes, K=40, nmels=40):
|
Maria@4
|
212 [F, P] = songframes.shape
|
Maria@4
|
213 Pproc = int((P/nmels)*K)
|
Maria@4
|
214 procframes = np.zeros([F, Pproc])
|
Maria@4
|
215 niters = int(nmels/K)
|
Maria@4
|
216 nbins = P/nmels # must be 200 bins
|
Maria@4
|
217 for k in range(K):
|
Maria@4
|
218 for j in range(k*niters, (k+1)*niters):
|
Maria@4
|
219 procframes[:, (k*nbins):((k+1)*nbins)] += songframes[:, (j*nbins):((j+1)*nbins)]
|
Maria@4
|
220 procframes /= float(niters)
|
Maria@4
|
221 return procframes
|
Maria@4
|
222
|
Maria@4
|
223
|
Maria@4
|
224 def nmfpitchbihist(self, frames):
|
Maria@4
|
225 nbins, nfr = frames.shape
|
Maria@4
|
226 npc = 2
|
Maria@4
|
227 nb = int(np.sqrt(nbins)) # assume structure of pitch bihist is nbins*nbins
|
Maria@4
|
228 newframes = np.empty(((nb+nb)*npc, nfr))
|
Maria@4
|
229 for fr in range(nfr):
|
Maria@4
|
230 pb = np.reshape(frames[:, fr], (nb, nb))
|
Maria@4
|
231 try:
|
Maria@4
|
232 nmfmodel = NMF(n_components=npc).fit(pb)
|
Maria@4
|
233 W = nmfmodel.transform(pb)
|
Maria@4
|
234 H = nmfmodel.components_.T
|
Maria@4
|
235 newframes[:, fr, None] = np.concatenate((W, H)).flatten()[:, None]
|
Maria@4
|
236 except:
|
Maria@4
|
237 newframes[:, fr, None] = np.zeros(((nb+nb)*npc, 1))
|
Maria@4
|
238 return newframes
|
Maria@4
|
239
|
Maria@4
|
240
|
Maria@35
|
241 def get_pb_for_file(self, melodia_file, precomp_melody=False, nmfpb=True, scale=True, stop_sec=30.0):
|
Maria@35
|
242 pbihist = []
|
Maria@35
|
243 if precomp_melody:
|
Maria@35
|
244 pbihist = self.load_precomp_pb_from_melodia(melodia_file=melodia_file, stop_sec=stop_sec)
|
Maria@35
|
245 else:
|
Maria@35
|
246 pbihist = self.extract_pb_from_melodia(melodia_file=melodia_file, stop_sec=stop_sec)
|
Maria@35
|
247 if len(pbihist) == 0:
|
Maria@35
|
248 # no file was found
|
Maria@35
|
249 return pbihist
|
Maria@4
|
250 if nmfpb is True:
|
Maria@4
|
251 pbihist = self.nmfpitchbihist(pbihist)
|
Maria@4
|
252 pbihist = pd.DataFrame(pbihist.T)
|
Maria@4
|
253 if scale:
|
Maria@4
|
254 # scale all frames by mean and std of recording
|
Maria@4
|
255 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
|
Maria@4
|
256 return pbihist
|
Maria@4
|
257
|
Maria@4
|
258
|
Maria@35
|
259 def extract_pb_from_melodia(self, melodia_file=None, stop_sec=30.0):
|
Maria@35
|
260 pbihist = []
|
Maria@35
|
261 if not os.path.exists(melodia_file):
|
Maria@35
|
262 return pbihist
|
Maria@35
|
263 print 'extracting pitch bihist from melodia...'
|
Maria@35
|
264 pb = pbi.PitchBihist(win2sec=self.win2sec)
|
Maria@35
|
265 pbihist = pb.bihist_from_melodia(filename=melodia_file, stop_sec=stop_sec)
|
Maria@35
|
266 return pbihist
|
Maria@35
|
267
|
Maria@35
|
268
|
Maria@35
|
269 def load_precomp_pb_from_melodia(self, melodia_file=None, stop_sec=30.0):
|
Maria@35
|
270 pbihist = []
|
Maria@4
|
271 base = os.path.basename(melodia_file)
|
Maria@4
|
272 root = '/import/c4dm-05/mariap/Melodia-melody-'+str(int(self.win2sec))+'sec/'
|
m@24
|
273 root_BL = '/import/c4dm-04/mariap/FeatureCsvs_BL_old/PB-melodia/'
|
m@24
|
274 root_SM = '/import/c4dm-04/mariap/FeatureCsvs/PB-melodia/'
|
m@24
|
275 if 'SampleAudio' in base:
|
m@24
|
276 root = root_SM
|
m@24
|
277 else:
|
m@24
|
278 root = root_BL
|
m@46
|
279 base = base.split('_')[-1].split('.csv')[0]+'_vamp_mtg-melodia_melodia_melody.csv'
|
m@43
|
280 bihist_file = os.path.join(root, base)
|
m@43
|
281 if not os.path.exists(bihist_file):
|
m@43
|
282 return pbihist
|
Maria@4
|
283 print 'load precomputed pitch bihist', root
|
m@43
|
284 pbihist = np.loadtxt(bihist_file, delimiter=',').T
|
Maria@35
|
285 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
|
Maria@35
|
286 pbihist = pbihist[:, :np.min([pbihist.shape[0], n_stop])]
|
Maria@4
|
287 return pbihist
|
Maria@4
|
288
|