Beispiel #1
0
    def test_intervals_to_samples(self):
        """Check that an interval set is sampled properly, with boundaries
        conditions and out-of-range values.
        """
        labels = list('abc')
        intervals = np.array([(n, n + 1.0) for n in range(len(labels))])

        expected_times = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
        expected_labels = ['a', 'a', 'b', 'b', 'c', 'c']
        result = util.intervals_to_samples(intervals,
                                           labels,
                                           offset=0,
                                           sample_size=0.5,
                                           fill_value='N')
        self.assertEqual(result[0], expected_times)
        self.assertEqual(result[1], expected_labels)

        expected_times = [0.25, 0.75, 1.25, 1.75, 2.25, 2.75]
        expected_labels = ['a', 'a', 'b', 'b', 'c', 'c']
        result = util.intervals_to_samples(intervals,
                                           labels,
                                           offset=0.25,
                                           sample_size=0.5,
                                           fill_value='N')
        self.assertEqual(result[0], expected_times)
        self.assertEqual(result[1], expected_labels)
Beispiel #2
0
    def transform_annotation(self, ann, duration):
        '''Apply the structure agreement transformation.

        Parameters
        ----------
        ann : jams.Annotation
            The segment annotation

        duration : number > 0
            The target duration

        Returns
        -------
        data : dict
            data['agree'] : np.ndarray, shape=(n, n), dtype=bool
        '''

        intervals, values = ann.to_interval_values()

        intervals, values = adjust_intervals(intervals,
                                             values,
                                             t_min=0,
                                             t_max=duration)
        # Re-index the labels
        ids, _ = index_labels(values)

        rate = float(self.hop_length) / self.sr
        # Sample segment labels on our frame grid
        _, labels = intervals_to_samples(intervals, ids, sample_size=rate)

        # Make the agreement matrix
        return {'agree': np.equal.outer(labels, labels)}
Beispiel #3
0
def midi_to_trajectory(des_timebase, onsets, offsets, pitches):

    hop = des_timebase[2] - des_timebase[1]

    intervals = np.concatenate([np.array(onsets)[:, None], np.array(offsets)[:, None]], axis=1)

    timebase, midipitches = intervals_to_samples(intervals, list(pitches),
                                                 offset=des_timebase[0], sample_size=hop, fill_value=0)

    return np.array(timebase), np.array(midipitches)
Beispiel #4
0
def test_intervals_to_samples():
    """Check that an interval set is sampled properly, with boundaries
    conditions and out-of-range values.
    """
    labels = list('abc')
    intervals = np.array([(n, n + 1.0) for n in range(len(labels))])

    expected_times = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
    expected_labels = ['a', 'a', 'b', 'b', 'c', 'c']
    result = util.intervals_to_samples(
        intervals, labels, offset=0, sample_size=0.5, fill_value='N')
    assert result[0] == expected_times
    assert result[1] == expected_labels

    expected_times = [0.25, 0.75, 1.25, 1.75, 2.25, 2.75]
    expected_labels = ['a', 'a', 'b', 'b', 'c', 'c']
    result = util.intervals_to_samples(
        intervals, labels, offset=0.25, sample_size=0.5, fill_value='N')
    assert result[0] == expected_times
    assert result[1] == expected_labels