comparison scripts/load_features.py @ 26:29b5ee381305 branch-tests

notebooks rerunning, and changes in load features for melodia
author mpanteli <m.x.panteli@gmail.com>
date Wed, 13 Sep 2017 17:33:48 +0100
parents 8dea2b8349c5
children e4736064d282
comparison
equal deleted inserted replaced
25:8dea2b8349c5 26:29b5ee381305
24 self.win2 = int(round(self.win2sec*self.framessr)) 24 self.win2 = int(round(self.win2sec*self.framessr))
25 self.hop2 = int(round(0.5*self.framessr)) 25 self.hop2 = int(round(0.5*self.framessr))
26 self.framessr2 = self.framessr/float(self.hop2) 26 self.framessr2 = self.framessr/float(self.hop2)
27 27
28 28
29 def get_op_mfcc_for_file(self, melspec_file=None, scale=True): 29 def get_op_mfcc_for_file(self, melspec_file=None, scale=True, stop_sec=30.0):
30 op = [] 30 op = []
31 mfc = [] 31 mfc = []
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 songframes = songframes.iloc[0:min(len(songframes), 18000), :] # only first 1.5 minutes 37 n_stop = np.int(np.ceil(stop_sec * self.framessr))
38 songframes = songframes.iloc[0:min(len(songframes), n_stop), :]
38 melspec = songframes.get_values().T 39 melspec = songframes.get_values().T
39 op = self.get_op_from_melspec(melspec, K=2) 40 op = self.get_op_from_melspec(melspec, K=2)
40 mfc = self.get_mfcc_from_melspec(melspec) 41 mfc = self.get_mfcc_from_melspec(melspec)
41 if scale: 42 if scale:
42 # scale all frames by mean and std of recording 43 # scale all frames by mean and std of recording
43 op = (op - np.nanmean(op)) / np.nanstd(op) 44 op = (op - np.nanmean(op)) / np.nanstd(op)
44 mfc = (mfc - np.nanmean(mfc)) / np.nanstd(mfc) 45 mfc = (mfc - np.nanmean(mfc)) / np.nanstd(mfc)
45 return op, mfc 46 return op, mfc
46 47
47 48
48 def get_chroma_for_file(self, chroma_file=None, scale=True): 49 def get_chroma_for_file(self, chroma_file=None, scale=True, stop_sec=30.0):
49 ch = [] 50 ch = []
50 if not os.path.exists(chroma_file): 51 if not os.path.exists(chroma_file):
51 return ch 52 return ch
52 print 'extracting chroma...' 53 print 'extracting chroma...'
53 songframes = pd.read_csv(chroma_file, engine="c", header=None) 54 songframes = pd.read_csv(chroma_file, engine="c", header=None)
54 songframes.iloc[np.where(np.isnan(songframes))] = 0 55 songframes.iloc[np.where(np.isnan(songframes))] = 0
55 songframes = songframes.iloc[0:min(len(songframes), 18000), :] # only first 1.5 minutes 56 n_stop = np.int(np.ceil(stop_sec * self.framessr))
57 songframes = songframes.iloc[0:min(len(songframes), n_stop), :]
56 chroma = songframes.get_values().T 58 chroma = songframes.get_values().T
57 ch = self.get_ave_chroma(chroma) 59 ch = self.get_ave_chroma(chroma)
58 if scale: 60 if scale:
59 # scale all frames by mean and std of recording 61 # scale all frames by mean and std of recording
60 ch = (ch - np.nanmean(ch)) / np.nanstd(ch) 62 ch = (ch - np.nanmean(ch)) / np.nanstd(ch)
96 bounds = pd.read_csv(segmenter_file, header=None, delimiter=',').get_values() 98 bounds = pd.read_csv(segmenter_file, header=None, delimiter=',').get_values()
97 music_idx = self.get_music_idx_from_bounds(bounds, sr=self.framessr2) 99 music_idx = self.get_music_idx_from_bounds(bounds, sr=self.framessr2)
98 return music_idx 100 return music_idx
99 101
100 102
101 def get_features(self, df, class_label='Country'): 103 def get_features(self, df, stop_sec=30.0, class_label='Country'):
102 oplist = [] 104 oplist = []
103 mflist = [] 105 mflist = []
104 chlist = [] 106 chlist = []
105 pblist = [] 107 pblist = []
106 clabels = [] 108 clabels = []
112 print 'file ' + str(i) + ' of ' + str(n_files) 114 print 'file ' + str(i) + ' of ' + str(n_files)
113 music_idx = self.get_music_idx_for_file(df['Speech'].iloc[i]) 115 music_idx = self.get_music_idx_for_file(df['Speech'].iloc[i])
114 if len(music_idx)==0: 116 if len(music_idx)==0:
115 # no music segments -> skip this file 117 # no music segments -> skip this file
116 continue 118 continue
117 try: 119 if 1:
118 op, mfcc = self.get_op_mfcc_for_file(df['Melspec'].iloc[i]) 120 op, mfcc = self.get_op_mfcc_for_file(df['Melspec'].iloc[i], stop_sec=stop_sec)
119 ch = self.get_chroma_for_file(df['Chroma'].iloc[i]) 121 ch = self.get_chroma_for_file(df['Chroma'].iloc[i], stop_sec=stop_sec)
120 pb = self.get_pb_from_melodia(df['Melodia'].iloc[i]) 122 #pb = self.get_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec)
121 #pb = self.load_precomputed_pb_from_melodia(df['Melodia'].iloc[i]) 123 pb = self.load_precomputed_pb_from_melodia(df['Melodia'].iloc[i], stop_sec=stop_sec)
122 #pb = self.get_contour_feat_from_melodia(df['Melodia'].iloc[i]) 124 #pb = self.get_contour_feat_from_melodia(df['Melodia'].iloc[i])
123 except: 125 else:
124 continue 126 continue
125 min_n_frames = np.min([len(op), len(mfcc), len(ch), len(pb)]) # ideally, features should have the same number of frames 127 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
128 print n_stop, len(op), len(mfcc), len(ch), len(pb)
129 min_n_frames = np.min([n_stop, len(op), len(mfcc), len(ch), len(pb)]) # ideally, features should have the same number of frames
126 if min_n_frames==0: 130 if min_n_frames==0:
127 # no features extracted -> skip this file 131 # no features extracted -> skip this file
128 continue 132 continue
129 music_idx = music_idx[music_idx<min_n_frames] 133 music_idx = music_idx[music_idx<min_n_frames]
130 n_frames = len(music_idx) 134 n_frames = len(music_idx)
220 except: 224 except:
221 newframes[:, fr, None] = np.zeros(((nb+nb)*npc, 1)) 225 newframes[:, fr, None] = np.zeros(((nb+nb)*npc, 1))
222 return newframes 226 return newframes
223 227
224 228
225 def get_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True): 229 def get_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True, stop_sec=30.0):
226 pb = [] 230 pb = []
227 if not os.path.exists(melodia_file): 231 if not os.path.exists(melodia_file):
228 return pb 232 return pb
229 print 'extracting pitch bihist from melodia...' 233 print 'extracting pitch bihist from melodia...'
230 pb = pbi.PitchBihist(win2sec=self.win2sec) 234 pb = pbi.PitchBihist(win2sec=self.win2sec)
231 pbihist = pb.bihist_from_melodia(filename=melodia_file, stop_sec=90.0) 235 pbihist = pb.bihist_from_melodia(filename=melodia_file, stop_sec=stop_sec)
232 if nmfpb is True: 236 if nmfpb is True:
233 pbihist = self.nmfpitchbihist(pbihist) 237 pbihist = self.nmfpitchbihist(pbihist)
234 pbihist = pd.DataFrame(pbihist.T) 238 pbihist = pd.DataFrame(pbihist.T)
235 if scale: 239 if scale:
236 # scale all frames by mean and std of recording 240 # scale all frames by mean and std of recording
237 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist) 241 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
238 return pbihist 242 return pbihist
239 243
240 244
241 def load_precomputed_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True): 245 def load_precomputed_pb_from_melodia(self, melodia_file=None, nmfpb=True, scale=True, stop_sec=30.0):
242 base = os.path.basename(melodia_file) 246 base = os.path.basename(melodia_file)
243 root = '/import/c4dm-05/mariap/Melodia-melody-'+str(int(self.win2sec))+'sec/' 247 root = '/import/c4dm-05/mariap/Melodia-melody-'+str(int(self.win2sec))+'sec/'
244 root_BL = '/import/c4dm-04/mariap/FeatureCsvs_BL_old/PB-melodia/' 248 root_BL = '/import/c4dm-04/mariap/FeatureCsvs_BL_old/PB-melodia/'
245 root_SM = '/import/c4dm-04/mariap/FeatureCsvs/PB-melodia/' 249 root_SM = '/import/c4dm-04/mariap/FeatureCsvs/PB-melodia/'
246 if 'SampleAudio' in base: 250 if 'SampleAudio' in base:
247 root = root_SM 251 root = root_SM
248 else: 252 else:
249 root = root_BL 253 root = root_BL
254 base = base.split('_')[-1].split('.csv')[0]+'_vamp_mtg-melodia_melodia_melody.csv'
250 print 'load precomputed pitch bihist', root 255 print 'load precomputed pitch bihist', root
251 if self.win2sec == 8: 256 #if self.win2sec == 8:
252 pbihist = pd.read_csv(os.path.join(root, base)).iloc[1:,1:] 257 # pbihist = pd.read_csv(os.path.join(root, base))
253 else: 258 #else:
259 if 1:
254 pbihist = np.loadtxt(os.path.join(root, base), delimiter=',').T 260 pbihist = np.loadtxt(os.path.join(root, base), delimiter=',').T
255 if nmfpb is True: 261 if nmfpb is True:
256 pbihist = self.nmfpitchbihist(pbihist) 262 pbihist = self.nmfpitchbihist(pbihist)
257 pbihist = pd.DataFrame(pbihist.T) 263 pbihist = pd.DataFrame(pbihist.T)
264 n_stop = np.int(np.ceil(stop_sec * self.framessr2))
265 pbihist = pbihist.iloc[:np.min([pbihist.shape[0], n_stop]), :]
258 print pbihist.shape 266 print pbihist.shape
259 if scale: 267 if scale:
260 # scale all frames by mean and std of recording 268 # scale all frames by mean and std of recording
261 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist) 269 pbihist = (pbihist - np.nanmean(pbihist)) / np.nanstd(pbihist)
262 return pbihist 270 return pbihist