def tracktorDeserialize(path, titles=None): """ get a track from the xml format from tracktor (1?) """ tree = ET.parse(path) root = tree.getroot() tracks = {} for entry in root.find("COLLECTION").iter("ENTRY"): track = Track() track.name = entry.attrib["TITLE"] track.path = entry.find("LOCATION").attrib["FILE"][:-4] #Removing .mp3 cues = [cue for cue in entry.iter("CUE_V2") if cue.attrib["NAME"] != "AutoGrid"] track.features["Cues"] = Signal([cue.attrib["NAME"][:7] for cue in cues], times=[float(cue.attrib["START"]) / 1000 for cue in cues], sparse=True) tracks[track.path] = track if titles: for t in titles: if t in tracks: yield tracks[t] else: print(t, "not in collection") dummytrack = Track() dummytrack.features["Cues"] = Signal(times=[]) yield dummytrack # return [tracks[t] if t in tracks else Track() for t in titles] else: return tracks.values() # bla = TraktorSerializer.tracktorDeserialize( # "/home/mickael/Documents/programming/dj-tracks-switch-points/evaluation/mixed in key/collection.nml") # print(bla)
def testPeakSelection(self): # Basic test myInput = [Signal(1, times=[0, 5, 10]), Signal(0.5, times=[0, 10])] ps = PeakSelection() result = ps.predictOne(myInput, None, None)[0] self.assertEqual(list(result.values), [1.5, 1, 1.5]) self.assertEqual(list(result.times), [0, 5, 10]) # Test with mean ps = PeakSelection(parameterMergeFunction=np.mean) result = ps.predictOne(myInput, None, None)[0] self.assertEqual(list(result.values), [0.75, 1, 0.75]) self.assertEqual(list(result.times), [0, 5, 10])
def testPeakPicking(self): # Basic test myInput = Signal([1.5, 1, 0, 0, 0, 2, 0], sampleRate=1) pp = PeakPicking(parameterMinDistance=2, parameterRelativeThreshold=0.2) result = pp.predictOne(myInput)[0] self.assertEqual(list(result.values), [1.5, 2.]) self.assertEqual(list(result.times), [0, 5])
def testSignal(self): # test sort of the values signal = Signal([5, 4, 3, 2, 1], times=[5, 4, 3, 2, 1]) self.assertEqual(list(signal.values), [1, 2, 3, 4, 5]) # test assertion of duplicate values # with self.assertRaises(AssertionError): signal = Signal([5, 4, 3, 2, 1], times=[5, 4, 3, 3, 1]) # test Qantization: remove doubles signal = Signal(1, times=[0, 1.1, 2.1, 2.5, 2.6, 2.7, 3.1]) grid = Signal(-1, times=list(range(5))) signal.quantizeTo(grid) # In case 1 value is exactly between 2 grid ticks, use the smallest one self.assertEqual(list(signal.values), [1, 1, 2, 3]) # test Quantization: maxThreshold for out of bound signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1]) signal.quantizeTo(grid, maxThreshold=0.2) self.assertEqual(list(signal.times), [0, 1, 2, 3]) self.assertEqual(list(signal.values), [1, 1, 2, 1]) # test quantization: don't remove duplicates signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1]) signal.quantizeTo(grid, maxThreshold=0.2, removeDuplicatedValues=False) self.assertEqual(list(signal.times), [0, 1, 2, 2, 3]) self.assertEqual(list(signal.values), [1, 1, 1, 1, 1]) # test quantization: don't remove out of bounds signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1]) signal.quantizeTo(grid, maxThreshold=0.2, removeDuplicatedValues=False, removeOutOfBound=False) self.assertEqual(list(signal.times), [0, 1, 2, 2, 2.6, 2.7, 3]) self.assertEqual(list(signal.values), [1, 1, 1, 1, 1, 1, 1]) # test getIndex signal = Signal(1, times=[0, 1.1, 2.0, 2.1, 2.6, 2.7, 3.1]) idx = signal.getIndex(2.2, toleranceWindow=0.5) self.assertEqual(idx, 3) # test clusterSignals result = Signal.clusterSignals([SparseSignal(1, [0, 1, 2, 3]), SparseSignal(0, [1, 3, 3.1])], minDistance=0, mergeValue=np.mean) self.assertEqual(list(result.times), [0, 1, 2, 3, 3.1]) self.assertEqual(list(result.values), [1, 0.5, 1, .5, 0])
def testStructureEval(self): # basic tests result = evalCuesMutliple([Signal(1, times=[1, 3, 2]), Signal(1, times=[1, 2, 3])], [Signal(1, times=[1, 2, 3]), Signal(1, times=[1, 2])]) self.assertEqual(result["recall"], 1) self.assertEqual(result["precision"], 5 / 6) self.assertEqual(result["fMeasure"], hmean([1, 5 / 6])) result = evalCuesMutliple([Signal(1, times=[1, 2, 4, 5, 6])], [Signal(1, times=[1, 2, 3, 4])], limitSearchSpace=True) self.assertEqual(result["recall"], 3 / 4) self.assertEqual(result["precision"], 3 / 3) result = evalCuesMutliple([Signal(1, times=[1, 2, 5, 6]), Signal(1, times=[1, 2, 3, 4])], [Signal(1, times=[1, 2, 3, 4]), Signal(1, times=[1, 2, 5, 6])], averagePerDocument=True, returnDetails=True, limitSearchSpace=True) self.assertEqual(result["recall"], [0.5, 0.5]) self.assertEqual(result["precision"], [1, 0.5]) self.assertEqual(result["fMeasure"], list(hmean([[0.5, 0.5], [1, 0.5]], axis=0))) # test duration result = evalCuesMutliple([Signal(1, times=[1, 2, 3])], [SparseSegmentSignal(1, [[1, 2], [1.6, 2.2]])]) self.assertEqual(result["recall"], 1) self.assertEqual(result["precision"], 2 / 3)
def testWindow(self): # RMS value normalize by sample number w = Windowing() input = Signal(2, times=list(range(20))) result, = w.predictOne(input, Signal(0, times=[-1, 2.1, 3.1, 4.1, 20])) self.assertEqual(result[0], result[2])