Exemplo n.º 1
0
    def test_get_window(self):
        filename_data, filename_labels = get_labeled_txt()
        df = AudioDataFile().load(filename_data, formatter=AIFFormatter())
        df.load_labels(filename_labels,
                       labels_formatter=TXTLabelsFormatter(),
                       label="whale")
        st1 = df.data.index[0]
        en1 = df.data.index[5]
        st2 = df.metadata["labels"][0][0]
        en2 = st2 + 4

        df.add_window(st1, en1)  # Length 6
        df.add_window(st2, en2)  # Length 4
        wdf = df.get_windows_data_frame()
        assert len(wdf) == 2
        w1, l1 = df.get_window(0)
        w2, l2 = df.get_window(1)
        assert len(w1) == 6
        assert len(w2) == 4

        # Out of range index
        assert df.get_window(100) is None

        # Other label treatments
        df.parameters["labels_treatment"] = "mode"
        df.get_window(0)
        df.parameters["labels_treatment"] = "mean"
        _, l1mn = df.get_window(0)
        assert type(l1mn) is float
        with pytest.raises(ValueError):
            df.parameters["labels_treatment"] = "fourier"
            # There is actually no labels treatment called fourier
            df.get_window(0)  # Raises value error
Exemplo n.º 2
0
    def test_add_windows(self):
        filename_data, filename_labels = get_labeled_txt()
        df = AudioDataFile().load(filename_data, formatter=AIFFormatter())
        df.load_labels(filename_labels,
                       labels_formatter=TXTLabelsFormatter(),
                       label="whale")
        st1 = df.data.index[0]
        en1 = df.data.index[5]
        st2 = df.metadata["labels"][0][0]
        en2 = st2 + 4

        # No windows added yet
        assert df.parameters["number_of_windows"] == 0
        d = df.get_window(0)
        assert all(d == df.data)

        # Add windows
        # First an incorrect one
        with pytest.raises(AttributeError):
            df.add_window(st1 + 100, en1 - 1)

        df.add_window(st1, en1)  # Length 6
        df.add_window(st2, en2)  # Length 4
        assert df.parameters["number_of_windows"] == 2
        assert str(df) == "AudioDataFile (2 windows)"
Exemplo n.º 3
0
    def test_segments_repr(self):
        filename_data, filename_labels = get_labeled_txt()
        df = AudioDataFile().load(filename_data, formatter=AIFFormatter())
        df.load_labels(filename_labels,
                       labels_formatter=TXTLabelsFormatter(),
                       label="whale")
        # Make 5 windows
        st = df.start_time
        step = df.duration / 5
        out = AudioSegments()
        assert str(out) == "AudioSegments"

        for i in range(5):
            df.add_window(st + i * step, st + (i + 1) * step)
            out.add_segment(*df.get_window(i))

        assert str(out) == "AudioSegments (5 segments)"
Exemplo n.º 4
0
    def load_feature_datafile(self):
        filename_data, filename_labels = get_labeled_txt()
        df = AudioDataFile().load(filename_data, formatter=AIFFormatter())
        df.load_labels(filename_labels,
                       labels_formatter=TXTLabelsFormatter(),
                       label="whale")
        # Make 5 windows
        st = df.start_time
        step = df.duration / 5
        out = AudioSegments()
        for i in range(5):
            df.add_window(st + i * step, st + (i + 1) * step)
            out.add_segment(*df.get_window(i))
        df = out

        # Get features
        f1 = Energy()
        f1.parameters["data"] = df
        f2 = Kurtosis()
        f2.parameters["data"] = df
        f1 = f1.transform()
        f2 = f2.transform()

        return FeatureDataFile().concatenate([f1, f2])