def printWSD2_scores( wavF, true_annF, template_annF, WSD2_clf, WSD2_feExFun, lt, scoreClassLabels, outF, strini="", strend="", m="auto", readSectionsWSD2="default", # for WSD2 labelsHierarchy="default", ): # for instantiating truth labels if labelsHierarchy == "default": labelsHierarchy = ["c"] # load waveform waveForm, fs = sT.wav2waveform(wavF) tf = len(waveForm) / fs if m == "auto": m = int(len(waveForm) / 512) # ground thruth y_true_names = auf.annotationsFi2instances(true_annF, m, tf, labelsHierarchy=labelsHierarchy) # predictions y_pred_names = pT.WSD2predict(wavF, template_annF, WSD2_feExFun, lt, WSD2_clf, m, tf, readSections=readSectionsWSD2) # print P, R, f1, support print_precision_recall_fscore_support(y_true_names, y_pred_names, outF, strini=strini, strend=strend, labels=scoreClassLabels) return outF
def plotAnnotatedSpectro(wavFi, annFi, outDir, callAsTitle=True, figsize=None, dpi=None, labelsHeight=10, cmapName='seismic', lw = 1, **kwargs): ''' plots the spectrogram with it's annotations Parameters ---------- wavAnnCollection : collection of paths to wavs and annotations files outDir : dir where the plots will be saved labelHeight : dictionary with the names of the labels and the height dpi : size of the image lw : line width for the annotated section ''' ## wav file try: waveForm, fs = sT.wav2waveform(wavFi) except: return False M, ff, tf, _ = sT.spectralRep(waveForm, fs)#plt.specgram(waveForm, Fs=fs) plt.ioff() fig, ax = plt.subplots(figsize=figsize) ax.imshow(M.T, aspect='auto', origin='bottom', #interpolation='nearest', extent=[0, tf, 0, fs/2/1000.], **kwargs) ax.set_xlabel('time (s)') ax.set_ylabel('frequency (KHz)') ### read annotations annD = annT.parseAupFile(annFi) ## custom ann clrs idx2labs = list(set([item['label'] for item in annD])) labs2idx = { idx2labs[i] : i for i in range(len(idx2labs)) } cmap = plt.cm.get_cmap( cmapName, len(idx2labs)) annLabel='' ### annotate for item in annD: # plot call segments - black line ax.hlines(labelsHeight, float(item['startTime']), float(item['endTime']), colors=cmap(labs2idx[item['label']]) , lw=lw) annLabel += '{} '.format(item['label']) #read labels if callAsTitle: ax.set_title('{}'.format(annLabel)) #if callAsTitle: ax.set_title('{}'.format(fp.parseHeikesNameConv(wavFi)['call'])) outPl = os.path.join( outDir, os.path.basename(wavFi).replace('wav', 'png')) plt.savefig(outPl, dpi=dpi) del fig, ax return outPl
def coll_clf_scores(clf, wavAnnCollection, featExtFun, labelTransformer, labelSet=None): """estimates the score of a classifiers, for each wav file in the collection Parameters: < clf : classifier < wavAnnCollection : collection of wavs and annotations list of tuples (<wav>, <txt>) < featExtFun : function(wavs, annotLi) --> A0, a_names, _, _ import functools; featExtFun = functools.partial(sT.waveform2featMatrix, textWS=textWS, **featConstD) < labelEncoder : label encoder of the features < labelSet : list of labels to consider, if None => all labels are kept """ n = len(wavAnnCollection) acc = np.zeros(n) pre = np.zeros(n) rec = np.zeros(n) f1 = np.zeros(n) sizes = np.zeros(n) if labelSet is None: labelSet = labelTransformer.num2nom(clf.classes_) i = 0 for wavF, annF in wavAnnCollection[:]: waveForm, fs = sT.wav2waveform(wavF) # read wavs annotLi_t = sT.aupTxt2annTu(annF) # read annotations A0, a_names, _, _ = featExtFun(waveForm, fs, annotations=annotLi_t) mask = np.in1d(a_names, labelSet) # filer unwanted labels a = labelTransformer.nom2num( a_names[mask]) # convert labels to numeric A = A0[mask] y_pred = clf.predict(A) ## scores acc[i] = mt.accuracy_score(a, y_pred) pre[i] = mt.precision_score(a, y_pred) rec[i] = mt.recall_score(a, y_pred) f1[i] = mt.f1_score(a, y_pred) sizes[i] = len(a) i += 1 return acc, pre, rec, f1, sizes