def partition_by_ratio_of_durations(self, ratio):
        r'''Partition start-positioned payload expression
        by ratio of durations.

        Operates in place and returns newly constructed inventory.
        '''
        from experimental.tools import musicexpressiontools
        element_durations = [
            self._get_duration_of_expr(leaf) for leaf in self.elements]
        integers = self._durations_to_integers(element_durations)
        parts = sequencetools.partition_sequence_by_ratio_of_weights(
            integers, ratio)
        part_lengths = [len(part) for part in parts]
        parts = sequencetools.partition_sequence_by_counts(
            self.elements, part_lengths)
        durations = [self._get_duration_of_list(part) for part in parts]
        payload_parts = self._split_payload_at_offsets(durations)
        start_offsets = mathtools.cumulative_sums(durations)[:-1]
        start_offsets = [
            self.start_offset + start_offset
            for start_offset in start_offsets]
        payload_expressions = \
            musicexpressiontools.TimespanScopedSingleContextSetExpressionInventory()
        for payload_part, start_offset in zip(payload_parts, start_offsets):
            timespan = timespantools.Timespan(start_offset)
            payload_expression = type(self)(
                [],
                start_offset=timespan.start_offset,
                voice_name=self.voice_name,
                )
            payload_expression._payload = payload_part
            payload_expressions.append(payload_expression)
        return payload_expressions
    def partition_by_ratio_of_durations(self, ratio):
        r'''Partition payload expression by ratio of durations.

        ::

            >>> result = \
            ...     payload_expression.partition_by_ratio_of_durations((1, 1))

        ::

            >>> for element in result:
            ...     print element.storage_format
            musicexpressiontools.IterablePayloadExpression(
                payload=((4, 16),)
                )
            musicexpressiontools.IterablePayloadExpression(
                payload=((2, 16),)
                )

        Returns newly constructed payload expression.
        '''
        element_durations = [self._duration_helper(x) for x in self.payload]
        element_tokens = self._durations_to_integers(element_durations)
        token_parts = sequencetools.partition_sequence_by_ratio_of_weights(
                element_tokens, ratio)
        part_lengths = [len(x) for x in token_parts]
        duration_parts = sequencetools.partition_sequence_by_counts(
            element_durations, part_lengths)
        element_parts = sequencetools.partition_sequence_by_counts(
            self.payload, part_lengths)
        result = []
        for part in element_parts:
            part = self.new(payload=part)
            result.append(part)
        return result
Esempio n. 3
0
    def partition_by_ratio_of_durations(self, ratio):
        r'''Partition start-positioned payload expression
        by ratio of durations.

        Operates in place and returns newly constructed inventory.
        '''
        from experimental.tools import musicexpressiontools
        element_durations = [
            self._get_duration_of_expr(leaf) for leaf in self.elements
        ]
        integers = self._durations_to_integers(element_durations)
        parts = sequencetools.partition_sequence_by_ratio_of_weights(
            integers, ratio)
        part_lengths = [len(part) for part in parts]
        parts = sequencetools.partition_sequence_by_counts(
            self.elements, part_lengths)
        durations = [self._get_duration_of_list(part) for part in parts]
        payload_parts = self._split_payload_at_offsets(durations)
        start_offsets = mathtools.cumulative_sums(durations)[:-1]
        start_offsets = [
            self.start_offset + start_offset for start_offset in start_offsets
        ]
        payload_expressions = \
            musicexpressiontools.TimespanScopedSingleContextSetExpressionInventory()
        for payload_part, start_offset in zip(payload_parts, start_offsets):
            timespan = timespantools.Timespan(start_offset)
            payload_expression = type(self)(
                [],
                start_offset=timespan.start_offset,
                voice_name=self.voice_name,
            )
            payload_expression._payload = payload_part
            payload_expressions.append(payload_expression)
        return payload_expressions
def test_sequencetools_partition_sequence_by_ratio_of_weights_01():
    r'''Common cases.
    '''

    l = [1] * 10

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [1, 1, 1])
    assert result == [[1, 1, 1], [1, 1, 1, 1], [1, 1, 1]]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [1, 1, 1, 1])
    assert result == [[1, 1, 1], [1, 1], [1, 1, 1], [1, 1]]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [2, 2, 3])
    assert result == [[1, 1, 1], [1, 1, 1], [1, 1, 1, 1]]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [3, 2, 2])
    assert result == [[1, 1, 1, 1], [1, 1, 1], [1, 1, 1]]
def test_sequencetools_partition_sequence_by_ratio_of_weights_03():
    r'''More unusual cases.
    '''

    l = [7, 3]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [1, 1, 1])
    assert result == [[7], [], [3]]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [1, 1, 1, 1])
    assert result == [[7], [], [3], []]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [2, 2, 3])
    assert result == [[7], [], [3]]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [3, 2, 2])
    assert result == [[7], [], [3]]
def test_sequencetools_partition_sequence_by_ratio_of_weights_02():
    r'''Unusual cases.
    '''

    l = [5, 5]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [1, 1, 1])
    assert result == [[5], [5], []]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [1, 1, 1, 1])
    assert result == [[5], [], [5], []]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [2, 2, 3])
    assert result == [[5], [5], []]

    result = sequencetools.partition_sequence_by_ratio_of_weights(l, [3, 2, 2])
    assert result == [[5], [5], []]
def partition(meter_list, weights_ratio, tempi):
    new_meter_list = [int(2 * x) for x in meter_list]
    assert sum(new_meter_list) == 2 * sum(meter_list)
    parts = sequencetools.partition_sequence_by_ratio_of_weights(
        new_meter_list, weights_ratio)
    counts = [len(x) for x in parts]
    #parts = sequencetools.partition_sequence_once_by_counts_without_overhang(
    #    meter_list, counts)
    parts = sequencetools.partition_sequence_by_counts(
        meter_list,
        counts,
        cyclic=False,
        overhang=False,
        )
    parts = sequencetools.zip_sequences([parts, tempi], cyclic=True)
    return parts
Esempio n. 8
0
    def test_function_name_to_title_lines(test_function_name):
        r'''Changes `test_function_name` to title lines.

        Returns list.
        '''
        from abjad.tools import sequencetools
        title_lines = []
        test_function_name = test_function_name[5:]
        if '__' in test_function_name:
            left_half, right_half = test_function_name.split('__')
            left_half = left_half.replace('_', ' ')
            title_lines.append(left_half)
            parts = right_half.split('_')
        else:
            parts = test_function_name.split('_')
        test_number = int(parts[-1])
        parts.pop(-1)
        if parts[0][0].isupper() and 1 < len(parts[0]):
            title_lines.append(parts.pop(0))
        lengths = [len(part) for part in parts]
        if 35 < sum(lengths):
            halves = sequencetools.partition_sequence_by_ratio_of_weights(
                lengths,
                [1, 1],
                )
            left_count = len(halves[0])
            right_count = len(halves[-1])
            assert left_count + right_count == len(lengths)
            left_parts = parts[:left_count]
            title_lines.append(' '.join(left_parts))
            right_parts = parts[-right_count:]
            right_parts.append(str(test_number))
            title_lines.append(' '.join(right_parts))
        else:
            title_words = ' '.join(parts)
            if 'schematic example' in title_words:
                space = ''
            else:
                space = ' '
            title = '{}{}{}'.format(title_words, space, test_number)
            title_lines.append(title)
        return title_lines
Esempio n. 9
0
    def test_function_name_to_title_lines(test_function_name):
        r'''Changes `test_function_name` to title lines.

        Returns list.
        '''
        from abjad.tools import sequencetools
        title_lines = []
        test_function_name = test_function_name[5:]
        if '__' in test_function_name:
            left_half, right_half = test_function_name.split('__')
            left_half = left_half.replace('_', ' ')
            title_lines.append(left_half)
            parts = right_half.split('_')
        else:
            parts = test_function_name.split('_')
        test_number = int(parts[-1])
        parts.pop(-1)
        if parts[0][0].isupper() and 1 < len(parts[0]):
            title_lines.append(parts.pop(0))
        lengths = [len(part) for part in parts]
        if 35 < sum(lengths):
            halves = sequencetools.partition_sequence_by_ratio_of_weights(
                lengths,
                [1, 1],
                )
            left_count = len(halves[0])
            right_count = len(halves[-1])
            assert left_count + right_count == len(lengths)
            left_parts = parts[:left_count]
            title_lines.append(' '.join(left_parts))
            right_parts = parts[-right_count:]
            right_parts.append(str(test_number))
            title_lines.append(' '.join(right_parts))
        else:
            title_words = ' '.join(parts)
            if 'schematic example' in title_words:
                space = ''
            else:
                space = ' '
            title = '{}{}{}'.format(title_words, space, test_number)
            title_lines.append(title)
        return title_lines
Esempio n. 10
0
    def partition_by_ratio_of_durations(self, ratio):
        r'''Partition payload expression by ratio of durations.

        ::

            >>> result = \
            ...     payload_expression.partition_by_ratio_of_durations((1, 1))

        ::

            >>> for element in result:
            ...     print(format(element))
            musicexpressiontools.IterablePayloadExpression(
                payload=(
                    (4, 16),
                    ),
                )
            musicexpressiontools.IterablePayloadExpression(
                payload=(
                    (2, 16),
                    ),
                )

        Returns newly constructed payload expression.
        '''
        element_durations = [self._duration_helper(x) for x in self.payload]
        element_tokens = self._durations_to_integers(element_durations)
        token_parts = sequencetools.partition_sequence_by_ratio_of_weights(
            element_tokens, ratio)
        part_lengths = [len(x) for x in token_parts]
        duration_parts = sequencetools.partition_sequence_by_counts(
            element_durations, part_lengths)
        element_parts = sequencetools.partition_sequence_by_counts(
            self.payload, part_lengths)
        result = []
        for part in element_parts:
            part = new(self, payload=part)
            result.append(part)
        return result
Esempio n. 11
0
def get_pitches(pitch_class_type, index_of_transposition):
    '''Gets fully registered pitches of either type 1 or type 2.
    '''
    from archipel.etc.implementation.data.archipel_aggregates \
        import archipel_aggregates as aggregates
    from archipel.etc.implementation.data.archipel_pitch_classes_type_1 \
        import archipel_pitch_classes_type_1 as pc1
    from archipel.etc.implementation.data.archipel_pitch_classes_type_2 \
        import archipel_pitch_classes_type_2 as pc2
    from archipel.etc.implementation.data.sargasso_pitches import \
        sargasso_pitches
    from archipel.etc.implementation.data.serration_pitches import \
        serration_pitches

    if pitch_class_type == 'sargasso':
        pitches = datastructuretools.CyclicTuple(sargasso_pitches)
        return pitches
    elif pitch_class_type == 'serration':
        pitches = serration_pitches
        pitches = datastructuretools.CyclicTuple(serration_pitches)
        return pitches
    elif pitch_class_type == 'C8':
        return datastructuretools.CyclicTuple([48])
    elif pitch_class_type == 'Bb7':
        return datastructuretools.CyclicTuple([46])
    elif pitch_class_type == 'E4':
        return datastructuretools.CyclicTuple([4])
    elif pitch_class_type == 'E2':
        return datastructuretools.CyclicTuple([-20])
    elif pitch_class_type == 'F#2':
        return datastructuretools.CyclicTuple([-18])
    elif pitch_class_type == 'C3':
        return datastructuretools.CyclicTuple([-12])
    elif pitch_class_type == 'C#2':
        return datastructuretools.CyclicTuple([-23])
    elif pitch_class_type == 'marimba descent':
        return [-17, -17, -19, -21, -21, -22, -22]
    elif pitch_class_type == 1:
        pcs = pc1
    elif pitch_class_type == 2:
        pcs = pc2
    else:
        raise ValueError(pitch_class_type)

    pcs = [(pc + index_of_transposition) % 12 for pc in pcs]
    parts = sequencetools.partition_sequence_by_ratio_of_weights(
        pcs,
        len(aggregates) * [1],
        )
    registered_parts = []
    for part, aggregate in zip(parts, aggregates):
        #registered_part = \
        #    pitchtools.register_pitch_class_numbers_by_pitch_number_aggregate(
        #    part, aggregate)
        pitch_set = pitchtools.PitchSet(
            items=aggregate,
            item_class=pitchtools.NamedPitch,
            )
        registered_part = pitch_set.register(part)
        registered_parts.append(registered_part)

    pitches = sequencetools.flatten_sequence(registered_parts)
    pitches = datastructuretools.CyclicTuple(pitches)

    return pitches
# -*- coding: utf-8 -*-
from abjad.tools import sequencetools
from archipel.etc.implementation.data import archipel_numerators_larger
from archipel.etc.implementation.utilities import keep_data


# divide 19 ice measures into six sections
meter_tokens = archipel_numerators_larger[1]
meter_tokens = sequencetools.partition_sequence_by_ratio_of_weights(meter_tokens, 6 * [1])

# swap first two sections
meter_tokens[:2] = reversed(meter_tokens[:2])

# make tokens
measures_per_section = [len(x) for x in meter_tokens]
meter_tokens = sequencetools.flatten_sequence(meter_tokens)
meter_tokens = [(x, 8) for x in meter_tokens]
meter_tokens = sequencetools.partition_sequence_once_by_counts_without_overhang(
    meter_tokens, measures_per_section)

# keep tokens
#keep_data(meter_tokens, 'ice_meter_tokens_by_section')