def test_multiannotator_events(): event_data1 = annotations.Events( np.array([[0.2, 0.3], [0.3, 0.4]]), "seconds", ["event A", "event B"], "open", np.array([1.0, 1.0]), ) event_data2 = annotations.Events( np.array([[0.2, 0.3], [0.3, 0.4]]), "seconds", ["", "a great label"], "open", np.array([0.0, 1.0]), ) event_data3 = annotations.Events( np.array([[0.2, 0.3], [0.3, 20.0]]), # invalid because > duration "seconds", ["", "a great label"], "open", np.array([0.0, 1.0]), ) multiannotator_data = annotations.MultiAnnotator( ["01", "02", "03"], [event_data1, event_data2, event_data3] ) jam = jams_utils.jams_converter( events=multiannotator_data, metadata={"duration": 10.0} ) assert jam.validate()
def load_annotation(fhandle: TextIO) -> annotations.MultiAnnotator: """ Load an annotation file. Args: fhandle (str or file-like): path or file-like object pointing to an annotation file Returns: * annotations.MultiAnnotator - sound events with start time, end time, label and confidence """ df = pd.read_csv(fhandle) annotators = [] annotations_ = [] for id, dfa in df.groupby("annotator"): intervals = dfa[["onset", "offset"]].values label = dfa["event_label"].tolist() events = annotations.Events( intervals=intervals, intervals_unit="seconds", labels=label, labels_unit="open", confidence=np.ones((len(label),)), ) annotators.append(f"{id:02d}") annotations_.append(events) return annotations.MultiAnnotator(annotators=annotators, annotations=annotations_)
def load_events(fhandle: TextIO) -> annotations.Events: """Load an TUT Sound events 2017 annotation file Args: fhandle (str or file-like): File-like object or path to the sound events annotation file Returns: Events: sound events annotation data """ times = [] labels = [] confidence = [] reader = csv.reader(fhandle, delimiter="\t") for line in reader: offset = ( 0 if len(line) == 3 else 2 ) # ann files in dev and eval have different format times.append([float(line[offset]), float(line[offset + 1])]) labels.append(line[offset + 2]) confidence.append(1.0) events_data = annotations.Events( np.array(times), "seconds", labels, "open", np.array(confidence) ) return events_data
def test_events(): event_data1 = annotations.Events( np.array([[0.2, 0.3], [0.3, 0.4]]), "seconds", ["event A", "event B"], "open", np.array([1.0, 1.0]), ) event_data2 = annotations.Events( np.array([[0.2, 0.3], [0.3, 0.4]]), "seconds", ["", "a great label"], "open", np.array([0.0, 1.0]), ) event_data3 = annotations.Events( np.array([[0.2, 0.3], [0.3, 20.0]]), # invalid because > duration "seconds", ["", "a great label"], "open", np.array([0.0, 1.0]), ) event_data4 = ("jazz", "wrong format") event_data5 = ["wrong format too"] event_data6 = [("wrong", "description")] jam1 = jams_utils.jams_converter(events=event_data1, metadata={"duration": 10.0}) assert jam1.validate() jam2 = jams_utils.jams_converter(events=event_data2, metadata={"duration": 10.0}) assert jam2.validate() jam3 = jams_utils.jams_converter(events=event_data3, metadata={"duration": 10.0}) assert jam3.validate() with pytest.raises(TypeError): jams_utils.jams_converter(events=event_data4) with pytest.raises(TypeError): jams_utils.jams_converter(events=event_data5) with pytest.raises(TypeError): jams_utils.jams_converter(events=event_data6)
def test_repr(): class TestAnnotation(annotations.Annotation): def __init__(self): self.a = ["a", "b", "c"] self.b = np.array([[1, 2], [1, 4]]) self._c = "hidden" test_track = TestAnnotation() assert test_track.__repr__() == """TestAnnotation(a, b)""" event_data = annotations.Events(np.array([[1.0, 2.0], [3.0, 4.0]]), "seconds", ["Siren", "Dog"], "open") assert (event_data.__repr__( ) == "Events(confidence, intervals, intervals_unit, labels, labels_unit)")
def load_events(fhandle: TextIO) -> annotations.Events: """Load an URBAN-SED sound events annotation file Args: fhandle (str or file-like): File-like object or path to the sound events annotation file Raises: IOError: if txt_path doesn't exist Returns: Events: sound events annotation data """ times = [] labels = [] confidence = [] reader = csv.reader(fhandle, delimiter="\t") for line in reader: times.append([float(line[0]), float(line[1])]) labels.append(line[2]) confidence.append(1.0) events_data = annotations.Events(np.array(times), "seconds", labels, "open", np.array(confidence)) return events_data
def test_events(): # test good data intervals = np.array([[1.0, 2.0], [1.5, 3.0], [2.0, 3.0]]) labels = ["Siren", "Laughter", "Engine"] confidence = np.array([1.0, 0.0, 1.0]) events = annotations.Events(intervals, "seconds", labels, "open", confidence) assert np.allclose(events.intervals, intervals) assert events.labels == labels assert np.allclose(events.confidence, confidence) # test bad data bad_intervals = np.array([[1.0, 0.0], [1.5, 3.0], [2.0, 3.0]]) pytest.raises( ValueError, annotations.Events, bad_intervals, "seconds", labels, "open", confidence, ) bad_labels = ["Siren", "Laughter", 5] pytest.raises( TypeError, annotations.Events, intervals, "seconds", bad_labels, "open", confidence, ) bad_confidence = np.array([1, 0.5, -0.2]) pytest.raises( ValueError, annotations.Events, intervals, "seconds", labels, "open", bad_confidence, ) # test units with pytest.raises(ValueError): annotations.Events(intervals, "seconds", labels, "bad_unit", confidence) with pytest.raises(ValueError): annotations.Events(intervals, "bad_unit", labels, "open", confidence) with pytest.raises(TypeError): annotations.Events(intervals, labels, confidence) with pytest.raises(TypeError): annotations.Events(intervals, labels, "open", confidence) with pytest.raises(TypeError): annotations.Events(intervals, "seconds", labels, confidence)