def test_section_data(): intervals = np.array([[1.0, 2.0], [1.5, 3.0], [2.0, 3.0]]) labels = ["a", "b", "c"] section_data = annotations.SectionData(intervals) assert np.allclose(section_data.intervals, intervals) assert section_data.labels is None section_data2 = annotations.SectionData(intervals, labels) assert np.allclose(section_data2.intervals, intervals) assert section_data2.labels == labels with pytest.raises(ValueError): annotations.SectionData(None, None) with pytest.raises(TypeError): annotations.SectionData([1.0, 2.0]) with pytest.raises(TypeError): annotations.SectionData(intervals, np.array(labels)) with pytest.raises(TypeError): annotations.SectionData(intervals.astype(int)) with pytest.raises(ValueError): annotations.SectionData(intervals, ["a"]) with pytest.raises(ValueError): annotations.SectionData(np.array([1.0, 2.0, 3.0])) with pytest.raises(ValueError): annotations.SectionData(np.array([[1.0, 2.0], [2.0, 1.0]]))
def load_sections(sections_path): """Load salami sections data from a file Args: sections_path (str): path to sectin annotation file Returns: SectionData: section data """ if sections_path is None: return None if not os.path.exists(sections_path): raise IOError("sections_path {} does not exist".format(sections_path)) times = [] secs = [] with open(sections_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter="\t") for line in reader: times.append(float(line[0])) secs.append(line[1]) times = np.array(times) secs = np.array(secs) # remove sections with length == 0 times_revised = np.delete(times, np.where(np.diff(times) == 0)) secs_revised = np.delete(secs, np.where(np.diff(times) == 0)) return annotations.SectionData( np.array([times_revised[:-1], times_revised[1:]]).T, list(secs_revised[:-1]))
def load_sections(sections_path): """Load Beatles format section data from a file Args: sections_path (str): path to section annotation file Returns: SectionData: loaded section data """ if sections_path is None: return None if not os.path.exists(sections_path): raise IOError("sections_path {} does not exist".format(sections_path)) start_times, end_times, sections = [], [], [] with open(sections_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter="\t") for line in reader: start_times.append(float(line[0])) end_times.append(float(line[1])) sections.append(line[3]) section_data = annotations.SectionData( np.array([start_times, end_times]).T, sections) return section_data
def load_sections(sections_path): """Load rwc section data from a file Args: sections_path (str): path to sections annotation file Returns: SectionData: section data """ if not os.path.exists(sections_path): raise IOError("sections_path {} does not exist".format(sections_path)) begs = [] # timestamps of section beginnings ends = [] # timestamps of section endings secs = [] # section labels with open(sections_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter="\t") for line in reader: begs.append(float(line[0]) / 100.0) ends.append(float(line[1]) / 100.0) secs.append(line[2]) if not begs: # some files are empty return None return annotations.SectionData(np.array([begs, ends]).T, secs)
def load_sections(sections_path): """Load tracks sections Args: sections_path (str): Local path where the section annotation is stored. Returns: SectionData: section annotations for track """ if sections_path is None: return None if not os.path.exists(sections_path): raise IOError("sections_path {} does not exist".format(sections_path)) intervals = [] section_labels = [] with open(sections_path, "r") as fhandle: reader = csv.reader(fhandle, delimiter=",") for line in reader: if line: intervals.append([ float(line[0]), float(line[0]) + float(line[2]), ]) section_labels.append(str(line[3]) + "-" + str(line[1])) # Return None if sections file is empty if not intervals: return None return annotations.SectionData(np.array(intervals), section_labels)
def load_sections(fhandle: TextIO) -> annotations.SectionData: """Load salami sections data from a file Args: fhandle (str or file-like): File-like object or path to sectin annotation file Returns: SectionData: section data """ times = [] secs = [] reader = csv.reader(fhandle, delimiter="\t") for line in reader: times.append(float(line[0])) secs.append(line[1]) times = np.array(times) secs = np.array(secs) # remove sections with length == 0 times_revised = np.delete(times, np.where(np.diff(times) == 0)) secs_revised = np.delete(secs, np.where(np.diff(times) == 0)) return annotations.SectionData( np.array([times_revised[:-1], times_revised[1:]]).T, list(secs_revised[:-1]))
def load_sections(fhandle: TextIO) -> annotations.SectionData: """Load Beatles format section data from a file Args: fhandle (str or file-like): path or file-like object pointing to a section annotation file Returns: SectionData: loaded section data """ start_times, end_times, sections = [], [], [] reader = csv.reader(fhandle, delimiter="\t") for line in reader: start_times.append(float(line[0])) end_times.append(float(line[1])) sections.append(line[3]) return annotations.SectionData( np.array([start_times, end_times]).T, sections)
def load_sections(fhandle: TextIO) -> Optional[annotations.SectionData]: """Load rwc section data from a file Args: fhandle (str or file-like): File-like object or path to sections annotation file Returns: SectionData: section data """ begs = [] # timestamps of section beginnings ends = [] # timestamps of section endings secs = [] # section labels reader = csv.reader(fhandle, delimiter="\t") for line in reader: begs.append(float(line[0]) / 100.0) ends.append(float(line[1]) / 100.0) secs.append(line[2]) if not begs: # some files are empty return None return annotations.SectionData(np.array([begs, ends]).T, secs)
def _load_sections(fpath: str, section_type: str): timed_sections = _parse_timed_sections(fpath) assert timed_sections is not None # Clean sections timed_sections_clean = [ ts for ts in timed_sections if ts["section"] is not None ] start_times = [] end_times = [] sections = [] if section_type == "letter": section_label_idx = 0 elif section_type == "name": section_label_idx = 1 else: raise ValueError("This section type is not available.") for idx, ts in enumerate(timed_sections_clean): if idx < len(timed_sections_clean) - 1: start_times.append(timed_sections_clean[idx]["time"]) end_times.append(timed_sections_clean[idx + 1]["time"]) sections.append( timed_sections_clean[idx]["section"][section_label_idx]) else: start_times.append(timed_sections_clean[idx]["time"]) end_times.append(timed_sections[-1]["time"]) # end of song sections.append( timed_sections_clean[idx]["section"][section_label_idx]) section_data = annotations.SectionData( np.array([start_times, end_times]).T, sections) return section_data
def test_multi_sections(): multi_section_data_1 = [ ( [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), None, ), ], None, ) ] multi_section_data_2 = [ ( [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), 0, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), 1, ), ], "annotator_1", ) ] multi_section_data_3 = [ ( [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), 0, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), 1, ), ], "annotator_1", ), ( [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), 0, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), 1, ), ], "annotator_2", ), ] multi_section_data_4 = ( [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), None, ), ], None, ) multi_section_data_5 = [ [ [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), None, ), ], None, ] ] multi_section_data_6 = [ ( ( ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 25.0]]).T, ["verse a", "verse b", "verse a"], ), None, ), ), None, ) ] multi_section_data_7 = [([(None, None), (None, None)], None)] multi_section_data_8 = [ ( [ ( annotations.EventData( np.array([[0.2, 0.3], [0.3, 0.4]]).T, ["event A", "event B"], ), None, ), ( annotations.EventData( np.array([[0.2, 0.3], [0.3, 0.4]]).T, ["event A", "event B"], ), None, ), ], None, ) ] jam_1 = jams_utils.jams_converter(multi_section_data=multi_section_data_1) jam_2 = jams_utils.jams_converter(multi_section_data=multi_section_data_2) jam_3 = jams_utils.jams_converter(multi_section_data=multi_section_data_3) jam_7 = jams_utils.jams_converter(multi_section_data=multi_section_data_7) time, duration, value, confidence = get_jam_data(jam_1, "multi_segment", 0) assert time == [0.0, 0.0, 10.0, 15.0, 20.0, 20.0] assert duration == [10.0, 15.0, 10.0, 5.0, 5.0, 5.0] assert value == [ {"label": "verse A", "level": None}, {"label": "verse a", "level": None}, {"label": "verse B", "level": None}, {"label": "verse b", "level": None}, {"label": "verse A", "level": None}, {"label": "verse a", "level": None}, ] assert confidence == [None, None, None, None, None, None] assert ( jam_2.annotations[0]["annotation_metadata"]["annotator"]["name"] == "annotator_1" ) time, duration, value, confidence = get_jam_data(jam_3, "multi_segment", 0) assert time == [0.0, 0.0, 10.0, 15.0, 20.0, 20.0] assert duration == [10.0, 15.0, 10.0, 5.0, 5.0, 5.0] assert value == [ {"label": "verse A", "level": 0}, {"label": "verse a", "level": 1}, {"label": "verse B", "level": 0}, {"label": "verse b", "level": 1}, {"label": "verse A", "level": 0}, {"label": "verse a", "level": 1}, ] assert confidence == [None, None, None, None, None, None] time, duration, value, confidence = get_jam_data(jam_3, "multi_segment", 1) assert time == [0.0, 0.0, 10.0, 15.0, 20.0, 20.0] assert duration == [10.0, 15.0, 10.0, 5.0, 5.0, 5.0] assert value == [ {"label": "verse A", "level": 0}, {"label": "verse a", "level": 1}, {"label": "verse B", "level": 0}, {"label": "verse b", "level": 1}, {"label": "verse A", "level": 0}, {"label": "verse a", "level": 1}, ] assert confidence == [None, None, None, None, None, None] time, duration, value, confidence = get_jam_data(jam_7, "multi_segment", 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(multi_section_data=multi_section_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(multi_section_data=multi_section_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(multi_section_data=multi_section_data_6) with pytest.raises(TypeError): jams_utils.jams_converter(multi_section_data=multi_section_data_8)
def test_sections(): section_data_1 = [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ) ] section_data_2 = [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), "sections_2", ) ] section_data_3 = [ ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), "sections_1", ), ( annotations.SectionData( np.array([[0.0, 15.0, 20.0], [15.0, 20.0, 30.0]]).T, ["verse A", "verse B", "verse C"], ), "sections_2", ), ] section_data_4 = ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ) section_data_5 = [ [ annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), None, ], ( annotations.SectionData( np.array([[0.0, 10.0, 20.0], [10.0, 20.0, 25.0]]).T, ["verse A", "verse B", "verse A"], ), "sections_2", ), ] section_data_6 = [(None, None)] section_data_7 = [ ( annotations.EventData( np.array([[0.2, 0.3], [0.3, 0.4]]).T, ["event A", "event B"], ), None, ) ] jam_1 = jams_utils.jams_converter(section_data=section_data_1) jam_2 = jams_utils.jams_converter(section_data=section_data_2) jam_3 = jams_utils.jams_converter(section_data=section_data_3) jam_6 = jams_utils.jams_converter(section_data=section_data_6) time, duration, value, confidence = get_jam_data(jam_1, "segment", 0) assert time == [0.0, 10.0, 20.0] assert duration == [10.0, 10.0, 5.0] assert value == ["verse A", "verse B", "verse A"] assert confidence == [None, None, None] assert jam_2.annotations[0]["sandbox"]["name"] == "sections_2" time, duration, value, confidence = get_jam_data(jam_3, "segment", 0) assert time == [0.0, 10.0, 20.0] assert duration == [10.0, 10.0, 5.0] assert value == ["verse A", "verse B", "verse A"] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_3, "segment", 1) assert time == [0.0, 15.0, 20.0] assert duration == [15.0, 5.0, 10.0] assert value == ["verse A", "verse B", "verse C"] assert confidence == [None, None, None] time, duration, value, confidence = get_jam_data(jam_6, "segment", 0) assert time == [] assert duration == [] assert value == [] assert confidence == [] assert type(jam_1) == jams.JAMS with pytest.raises(TypeError): jams_utils.jams_converter(section_data=section_data_4) with pytest.raises(TypeError): jams_utils.jams_converter(section_data=section_data_5) with pytest.raises(TypeError): jams_utils.jams_converter(section_data=section_data_7)