comparison scripts/load_features.py @ 35:c4428589b82b branch-tests

more tests in load_features
author Maria Panteli
date Thu, 14 Sep 2017 13:07:19 +0100
parents 115774aff442
children 3b67cd634b9a
comparison
equal deleted inserted replaced
34:115774aff442 35:c4428589b82b
32 if not os.path.exists(melspec_file): 32 if not os.path.exists(melspec_file):
33 return op, mfc 33 return op, mfc
34 print 'extracting onset patterns and mfccs...' 34 print 'extracting onset patterns and mfccs...'
35 songframes = pd.read_csv(melspec_file, engine="c", header=None) 35 songframes = pd.read_csv(melspec_file, engine="c", header=None)
36 songframes.iloc[np.where(np.isnan(songframes))] = 0 36 songframes.iloc[np.where(np.isnan(songframes))] = 0
37 n_stop = np.int(np.ceil(stop_sec * self.framessr)) 37 n_stop = np.int(np.ceil(stop_sec * self.framessr))
38 songframes = songframes.iloc[0:min(len(songframes), n_stop), :] 38 songframes = songframes.iloc[0:min(len(songframes), n_stop), :]
39 melspec = songframes.get_values().T 39 melspec = songframes.get_values().T
40 op = self.get_op_from_melspec(melspec, K=2) 40 op = self.get_op_from_melspec(melspec, K=2)
41 mfc = self.get_mfcc_from_melspec(melspec) 41 mfc = self.get_mfcc_from_melspec(melspec)
42 if scale: 42 if scale:
52 return ch 52 return ch
53 print 'extracting chroma...' 53 print 'extracting chroma...'
54 songframes = pd.read_csv(chroma_file, engine="c", header=None) 54 songframes = pd.read_csv(chroma_file, engine="c", header=None)
55 songframes.iloc[np.where(np.isnan(songframes))] = 0 55 songframes.iloc[np.where(np.isnan(songframes))] = 0
56 n_stop = np.int(np.ceil(stop_sec * self.framessr)) 56 n_stop = np.int(np.ceil(stop_sec * self.framessr))
57 songframes = songframes.iloc[0:min(len(songframes), n_stop), :] 57 songframes = songframes.iloc[0:min(len(songframes), n_stop), :]
58 chroma = songframes.get_values().T 58 chroma = songframes.get_values().T
59 ch = self.get_ave_chroma(chroma) 59 ch = self.get_ave_chroma(chroma)
60 if scale: 60 if scale:
61 # scale all frames by mean and std of recording 61 # scale all frames by mean and std of recording
62 ch = (ch - np.nanmean(ch)) / np.nanstd(ch) 62 ch = (ch - np.nanmean(ch)) / np.nanstd(ch)
101 bounds = pd.read_csv(segmenter_file, header=None, delimiter=',').get_values() 101 bounds = pd.read_csv(segmenter_file, header=None, delimiter=',').get_values()
102 music_idx = self.get_music_idx_from_bounds(bounds, sr=self.framessr2) 102 music_idx = self.get_music_idx_from_bounds(bounds, sr=self.framessr2)
103 return music_idx 103 return music_idx
104 104
105 105
106 def get_features(self, df, stop_sec=30.0, class_label='Country'): 106 def get_features(self, df, stop_sec=30.0, class_label='Country', precomp_melody=False):
107 oplist = [] 107 oplist = []
108 mflist = [] 108 mflist = []
109 chlist = [] 109 chlist = []
110 pblist = [] 110 pblist = []
111 clabels = [] 111 clabels = []
120 # no music segments -> skip this file 120 # no music segments -> skip this file
121 continue 121 continue
122 try: 122 try:
123 op, mfcc = self.get_op_mfcc_for_file(df['Melspec'].iloc[i], stop_sec=stop_sec) 123 op, mfcc = self.get_op_mfcc_for_file(df['Melspec'].iloc[i], stop_sec=stop_sec)
124 ch = self.get_chroma_for_file(df['Chroma'].iloc[i], stop_sec=stop_sec) 124 ch = self.get_chroma_for_file(df['Chroma'].iloc[i], stop_sec=stop_sec)
125 #pb = self.get_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec) 125 pb = self.get_pb_for_file(df['Melodia'].iloc[i], precomp_melody=precomp_melody, stop_sec=stop_sec)
126 pb = self.load_precomputed_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec) 126 #if precomp_melody:
127 #pb = self.get_contour_feat_from_melodia(df['Melodia'].iloc[i]) 127 # pb = self.load_precomputed_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec)
128 #else:
129 # pb = self.get_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec)
128 except: 130 except:
129 continue 131 continue
130 n_stop = np.int(np.ceil(stop_sec * self.framessr2)) 132 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
131 print n_stop, len(op), len(mfcc), len(ch), len(pb) 133 print n_stop, len(op), len(mfcc), len(ch), len(pb)
132 min_n_frames = np.min([n_stop, len(op), len(mfcc), len(ch), len(pb)]) # ideally, features should have the same number of frames 134 min_n_frames = np.min([n_stop, len(op), len(mfcc), len(ch), len(pb)]) # ideally, features should have the same number of frames
133 if min_n_frames==0: 135 if min_n_frames==0:
134 # no features extracted -> skip this file 136 # no features extracted -> skip this file
135 continue 137 continue
136 music_idx = music_idx[music_idx<min_n_frames] 138 music_idx = music_idx[music_idx<min_n_frames]
137 n_frames = len(music_idx) 139 n_frames = len(music_idx)
146 148
147 149
148 def get_op_from_melspec(self, melspec, K=None): 150 def get_op_from_melspec(self, melspec, K=None):
149 op = opm.OPMellin(win2sec=self.win2sec) 151 op = opm.OPMellin(win2sec=self.win2sec)
150 opmellin = op.get_opmellin_from_melspec(melspec=melspec, melsr=self.framessr) 152 opmellin = op.get_opmellin_from_melspec(melspec=melspec, melsr=self.framessr)
153 opmel = pd.DataFrame(opmellin.T)
151 if K is not None: 154 if K is not None:
152 opmel = self.mean_K_bands(opmellin.T, K) 155 opmel = self.mean_K_bands(opmellin.T, K)
153 opmel = pd.DataFrame(opmel) 156 opmel = pd.DataFrame(opmel)
154 return opmel 157 return opmel
155 158
156 159
157 def get_mfcc_from_melspec(self, melspec, deltamfcc=True, avelocalframes=True, stdlocalframes=True): 160 def get_mfcc_from_melspec(self, melspec, deltamfcc=True, avelocalframes=True, stdlocalframes=True):
158 mf = mfc.MFCCs() 161 mf = mfc.MFCCs()
167 mfcc = self.average_local_frames(mfcc, getstd=stdlocalframes) 170 mfcc = self.average_local_frames(mfcc, getstd=stdlocalframes)
168 mfcc = pd.DataFrame(mfcc.T) 171 mfcc = pd.DataFrame(mfcc.T)
169 return mfcc 172 return mfcc
170 173
171 174
172 def get_ave_chroma(self, chroma, avelocalframes=True, stdlocalframes=True, alignchroma=True): 175 def get_ave_chroma(self, chroma, alignchroma=True, avelocalframes=True, stdlocalframes=True):
173 chroma[np.where(np.isnan(chroma))] = 0 176 chroma[np.where(np.isnan(chroma))] = 0
174 if alignchroma: 177 if alignchroma:
175 maxind = np.argmax(np.sum(chroma, axis=1)) 178 maxind = np.argmax(np.sum(chroma, axis=1)) # bin with max magnitude across time
176 chroma = np.roll(chroma, -maxind, axis=0) 179 chroma = np.roll(chroma, -maxind, axis=0)
177 if avelocalframes: 180 if avelocalframes:
178 chroma = self.average_local_frames(chroma, getstd=stdlocalframes) 181 chroma = self.average_local_frames(chroma, getstd=stdlocalframes)
179 chroma = pd.DataFrame(chroma.T) 182 chroma = pd.DataFrame(chroma.T)
180 return chroma 183 return chroma
227 except: 230 except:
228 newframes[:, fr, None] = np.zeros(((nb+nb)*npc, 1)) 231 newframes[:, fr, None] = np.zeros(((nb+nb)*npc, 1))
229 return newframes 232 return newframes
230 233
231 234
232 def get_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True, stop_sec=30.0): 235 def get_pb_for_file(self, melodia_file, precomp_melody=False, nmfpb=True, scale=True, stop_sec=30.0):
233 pb = [] 236 pbihist = []
234 if not os.path.exists(melodia_file): 237 if precomp_melody:
235 return pb 238 pbihist = self.load_precomp_pb_from_melodia(melodia_file=melodia_file, stop_sec=stop_sec)
236 print 'extracting pitch bihist from melodia...' 239 else:
237 pb = pbi.PitchBihist(win2sec=self.win2sec) 240 pbihist = self.extract_pb_from_melodia(melodia_file=melodia_file, stop_sec=stop_sec)
238 pbihist = pb.bihist_from_melodia(filename=melodia_file, stop_sec=stop_sec) 241 if len(pbihist) == 0:
242 # no file was found
243 return pbihist
239 if nmfpb is True: 244 if nmfpb is True:
240 pbihist = self.nmfpitchbihist(pbihist) 245 pbihist = self.nmfpitchbihist(pbihist)
241 pbihist = pd.DataFrame(pbihist.T) 246 pbihist = pd.DataFrame(pbihist.T)
242 if scale: 247 if scale:
243 # scale all frames by mean and std of recording 248 # scale all frames by mean and std of recording
244 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist) 249 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
245 return pbihist 250 return pbihist
246 251
247 252
248 def load_precomputed_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True, stop_sec=30.0): 253 def extract_pb_from_melodia(self, melodia_file=None, stop_sec=30.0):
254 pbihist = []
255 if not os.path.exists(melodia_file):
256 return pbihist
257 print 'extracting pitch bihist from melodia...'
258 pb = pbi.PitchBihist(win2sec=self.win2sec)
259 pbihist = pb.bihist_from_melodia(filename=melodia_file, stop_sec=stop_sec)
260 return pbihist
261
262
263 def load_precomp_pb_from_melodia(self, melodia_file=None, stop_sec=30.0):
264 pbihist = []
249 base = os.path.basename(melodia_file) 265 base = os.path.basename(melodia_file)
250 root = '/import/c4dm-05/mariap/Melodia-melody-'+str(int(self.win2sec))+'sec/' 266 root = '/import/c4dm-05/mariap/Melodia-melody-'+str(int(self.win2sec))+'sec/'
251 root_BL = '/import/c4dm-04/mariap/FeatureCsvs_BL_old/PB-melodia/' 267 root_BL = '/import/c4dm-04/mariap/FeatureCsvs_BL_old/PB-melodia/'
252 root_SM = '/import/c4dm-04/mariap/FeatureCsvs/PB-melodia/' 268 root_SM = '/import/c4dm-04/mariap/FeatureCsvs/PB-melodia/'
253 if 'SampleAudio' in base: 269 if 'SampleAudio' in base:
254 root = root_SM 270 root = root_SM
255 else: 271 else:
256 root = root_BL 272 root = root_BL
257 base = base.split('_')[-1].split('.csv')[0]+'_vamp_mtg-melodia_melodia_melody.csv' 273 base = base.split('_')[-1].split('.csv')[0]+'_vamp_mtg-melodia_melodia_melody.csv'
258 print 'load precomputed pitch bihist', root 274 print 'load precomputed pitch bihist', root
259 #if self.win2sec == 8: 275 pbihist = np.loadtxt(os.path.join(root, base), delimiter=',').T
260 # pbihist = pd.read_csv(os.path.join(root, base)) 276 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
261 #else: 277 pbihist = pbihist[:, :np.min([pbihist.shape[0], n_stop])]
262 if 1:
263 pbihist = np.loadtxt(os.path.join(root, base), delimiter=',').T
264 if nmfpb is True:
265 pbihist = self.nmfpitchbihist(pbihist)
266 pbihist = pd.DataFrame(pbihist.T)
267 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
268 pbihist = pbihist.iloc[:np.min([pbihist.shape[0], n_stop]), :]
269 print pbihist.shape
270 if scale:
271 # scale all frames by mean and std of recording
272 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
273 return pbihist 278 return pbihist
279
280
281 # def get_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True, stop_sec=30.0):
282 # if not os.path.exists(melodia_file):
283 # return []
284 # print 'extracting pitch bihist from melodia...'
285 # pb = pbi.PitchBihist(win2sec=self.win2sec)
286 # pbihist = pb.bihist_from_melodia(filename=melodia_file, stop_sec=stop_sec)
287 # if nmfpb is True:
288 # pbihist = self.nmfpitchbihist(pbihist)
289 # pbihist = pd.DataFrame(pbihist.T)
290 # if scale:
291 # # scale all frames by mean and std of recording
292 # pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
293 # return pbihist
294
295
296 # def load_precomputed_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True, stop_sec=30.0):
297 # base = os.path.basename(melodia_file)
298 # root = '/import/c4dm-05/mariap/Melodia-melody-'+str(int(self.win2sec))+'sec/'
299 # root_BL = '/import/c4dm-04/mariap/FeatureCsvs_BL_old/PB-melodia/'
300 # root_SM = '/import/c4dm-04/mariap/FeatureCsvs/PB-melodia/'
301 # if 'SampleAudio' in base:
302 # root = root_SM
303 # else:
304 # root = root_BL
305 # base = base.split('_')[-1].split('.csv')[0]+'_vamp_mtg-melodia_melodia_melody.csv'
306 # print 'load precomputed pitch bihist', root
307 # #if self.win2sec == 8:
308 # # pbihist = pd.read_csv(os.path.join(root, base))
309 # #else:
310 # if 1:
311 # pbihist = np.loadtxt(os.path.join(root, base), delimiter=',').T
312 # if nmfpb is True:
313 # pbihist = self.nmfpitchbihist(pbihist)
314 # pbihist = pd.DataFrame(pbihist.T)
315 # n_stop = np.int(np.ceil(stop_sec * self.framessr2))
316 # pbihist = pbihist.iloc[:np.min([pbihist.shape[0], n_stop]), :]
317 # print pbihist.shape
318 # if scale:
319 # # scale all frames by mean and std of recording
320 # pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
321 # return pbihist
274 322