def annotationsFi2instances(annFi, m, tf, labelsHierarchy=None, gaps='b', sep=None): """reads annotations from file into instances Parameters ---------- annFi: str path to file m: int number of instances labelsHierarchy: list like object hierarchy of the annotations in case of overlaps. Only one label per instance, eg = ['c', 'w'] gaps: string, label for the gaps Returns ------- labels_arr: ndarray (m, ) array with the instance labels""" if labelsHierarchy is None: labelsHierarchy = ['c', 'w'] T, L = annT.anns2TLndarrays(annFi, sep=sep) if tf == 'auto': tf = T[-1, -1] + 0.1 return annotations2instanceArray(T, L, m, tf, labelsHierarchy=labelsHierarchy)
def annotationsFi2instances(annFi, m, tf, labelsHierarchy=None, gaps="b"): """reads annotations from file into instances""" if labelsHierarchy is None: labelsHierarchy = ["c", "w"] T, L = annT.anns2TLndarrays(annFi) if tf == "auto": tf = T[-1, -1] + 0.1 return annotations2instanceArray(T, L, m, tf, labelsHierarchy=labelsHierarchy)
def test_predictions(): # predict annotations wF = os.path.join(pDir, 'data/WAV_0111_001-48kHz_30-60sec.wav') anF = os.path.join(pDir, 'data/WAV_0111_001-48kHz_30-60sec.txt') predsF1 = os.path.join(pDir, 'data/WAV_0111_001-48kHz_30-60sec-preds0.txt') pre.predictAnnotationSections(wF, anF, clf, feExFun, lt, outFile=predsF1 ) predsF2 = os.path.join(pDir, 'data/WAV_0111_001-48kHz_30-60sec-preds0.txt') pre.predictAnnotationSections0(wF, anF, clf, feExFun, lt, outFile=predsF2 ) ## load annotations into T, L format Tfi1, Lfi1 = annT.anns2TLndarrays( predsF1, Tcols=(0,1), Lcols=(-1,)) Tfi2, Lfi2 = annT.anns2TLndarrays( predsF2, Tcols=(0,1), Lcols=(-1,)) # test both prediction types np.testing.assert_array_equal(Lfi1, Lfi2) np.testing.assert_array_equal(Tfi1, Tfi2) # test one more prediction mode Tp, Lp = pre.TLpredictAnnotationSections( wF, anF, clf, feExFun, lt) np.testing.assert_array_equal(Lfi1, Lp) np.testing.assert_array_equal(Tfi1, Tp)
def TLpredictAnnotationSections(wavF, annF, clf, featExtFun, lt, printProbs=False, readSections=None, printreadSectionsC=True): """generates annotations predicting audio section classes Parameters ---------- wavF : str annF : str path to the file with the annotation section to predict clf : estimator featExtFun : callable lt : labelTransformer printProbs : bool readSections : list of str regions in the annF for which we predict printreadSectionsC : bool """ ## load annotations waveform, fs = sT.wav2waveform(wavF) T, L0 = annT.anns2TLndarrays(annF) ## set of annotation-sections to predict if readSections is None: readSections = np.array(list(set(L0))) ## filter for sections of interest IO_sections = np.isin(L0, readSections) Tp = T[IO_sections] L = L0[IO_sections] Lp = np.zeros_like(L) ## for each annotation section for i, label in enumerate(L): # for each section waveformSec = auf.getWavSec(waveform, fs, *Tp[i]) # load waveform section M0 = featExtFun(waveformSec) # extract features M = np.expand_dims(M0.flatten(), axis=0) Lp[i] = lt.num2nom(clf.predict(M))[0] # predict return Tp, Lp
def __init__(self, annFi): self.file_path = annFi self.T, self.L = annT.anns2TLndarrays(self.file_path) self.tf = self.T[-1, -1]
def predictAnnotationSections(wavF, annF, clf, featExtFun, lt, outFile=None, sep='\t', printProbs=False, header='', readSections=None, printreadSectionsC=True): """predicts annotations for call types sections Parameters ---------- wavF: str annF: str clf: estimator featExtFun: callable lt: labelTransformer outFil: str sep: str printProbs: bool header: str readSections: list of str regions in the annF for which we predict printreadSectionsC: bool See also -------- TLpredictAnnotationSections TODO: recode to use TLpredictAnnotationSections """ if outFile is None: outFile = os.path.splitext(annF)[0] + '-sectionPredictions.txt' try: # remove file if exists os.remove(outFile) except OSError: pass ## load files waveform, fs = sT.wav2waveform(wavF) T, L = annT.anns2TLndarrays(annF) if readSections == None: readSections = list(set(L)) ## for each annotation section for i, label in enumerate(L): if label in readSections: waveformSec = auf.getWavSec(waveform, fs, *T[i]) ## predict try: M0 = featExtFun(waveformSec) # estract features M = np.expand_dims(M0.flatten(), axis=0) y_pred = lt.num2nom(clf.predict(M)) # predict label except AssertionError: y_pred = [label] ## write with open(outFile, 'a') as f: f.write("{}\t{}\t{}\t{}\n".format(T[i, 0], T[i, 1], label, *y_pred)) elif printreadSectionsC: with open(outFile, 'a') as f: f.write("{}\t{}\t{}\t{}\n".format(T[i, 0], T[i, 1], label, label)) return outFile