How to use the abjad.tools.sequencetools function in abjad

To help you get started, we’ve selected a few abjad examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Abjad / abjad / abjad / tools / timesignaturetools / establish_metrical_hierarchy.py View on Github external
def get_offsets_at_depth(depth):
        if depth < len(offset_inventory):
            return offset_inventory[depth]
        while len(offset_inventory) <= depth:
            new_offsets = []
            old_offsets = offset_inventory[-1]
            for first, second in \
                sequencetools.iterate_sequence_pairwise_strict(old_offsets):
                new_offsets.append(first)
                new_offsets.append((first + second) / 2)
            new_offsets.append(old_offsets[-1])
            offset_inventory.append(tuple(new_offsets))
        return offset_inventory[depth]
github Abjad / abjad / experimental / tools / musicexpressiontools / IterablePayloadCallbackMixin.py View on Github external
from experimental.tools import musicexpressiontools
        assert isinstance(
            payload_expression, musicexpressiontools.PayloadExpression)
        callback_cache = self.score_specification.interpreter.callback_cache
        evaluation_context = {
            'Duration': durationtools.Duration,
            'NonreducedFraction': mathtools.NonreducedFraction,
            'Offset': durationtools.Offset,
            'Ratio': mathtools.Ratio,
            'RotationExpression': musicexpressiontools.RotationExpression,
            'Timespan': timerelationtools.Timespan,
            'callback_cache': callback_cache,
            'payload_expression': payload_expression,
            'self': self,
            'result': None,
            'sequencetools': sequencetools,
            }
        for callback in self.callbacks:
            assert 'payload_expression' in callback
            evaluation_context['payload_expression'] = payload_expression
            exec(callback, evaluation_context)
            payload_expression = evaluation_context['result']
        return payload_expression
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / compute_depth_of_intervals.py View on Github external
all_bounds = list(timeintervaltools.get_all_unique_bounds_in_intervals(
            tree))
        while all_bounds[0] < bounding_interval.start_offset:
            all_bounds.pop(0)
        while bounding_interval.stop_offset < all_bounds[-1]:
            all_bounds.pop()
        if bounding_interval.start_offset < all_bounds[0]:
            all_bounds.insert(0, bounding_interval.start_offset)
        if all_bounds[-1] < bounding_interval.stop_offset:
            all_bounds.append(bounding_interval.stop_offset)
    else:
        all_bounds = list(timeintervaltools.get_all_unique_bounds_in_intervals(
            tree))
    
    depth_intervals = []
    for start_offset, stop_offset in sequencetools.iterate_sequence_pairwise_strict(
        all_bounds):
        current_interval = timeintervaltools.TimeInterval(start_offset, stop_offset, {})
        found = tree.find_intervals_intersecting_or_tangent_to_interval(
            current_interval)
        depth = 0
        if found:
            depth = len([x for x in found 
                if (not x.start_offset == current_interval.stop_offset 
                and not x.stop_offset == current_interval.start_offset)])
        current_interval['depth'] = depth
        depth_intervals.append(current_interval)

    return timeintervaltools.TimeIntervalTree(depth_intervals)
github Abjad / abjad / trunk / abjad / tools / pitchtools / NamedPitchClassSet / NamedPitchClassSet.py View on Github external
def order_by(self, npc_seg):
        from abjad.tools import pitchtools
        from abjad.tools import sequencetools
        if not len(self) == len(npc_seg):
            raise ValueError('set and segment must be of equal length.')
        for npcs in sequencetools.yield_all_permutations_of_sequence(
            self.named_chromatic_pitch_classes):
            candidate_npc_seg = \
                pitchtools.PitchClassSegment(npcs)
            if candidate_npc_seg.is_equivalent_under_transposition(npc_seg):
                return candidate_npc_seg
        message = 'named pitch-class set %s can not order by '
        message += 'named pitch-class segment %s.'
        raise ValueError(message % (self, npc_seg))
github Abjad / abjad / experimental / tools / specificationtools / StartPositionedPayloadExpression / StartPositionedPayloadExpression.py View on Github external
def partition_by_ratio_of_durations(self, ratio):
        '''Partition start-positioned payload expression by ratio of durations.

        Operate in place and return newly constructed inventory.
        '''
        from experimental.tools import specificationtools
        element_durations = [self._get_duration_of_expr(leaf) for leaf in self.elements]
        integers = durationtools.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_zero(durations)[:-1]
        start_offsets = [self.start_offset + start_offset for start_offset in start_offsets]
        payload_expressions = specificationtools.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
github Abjad / abjad / scoremanager / idetools / Menu.py View on Github external
if strip:
            left_width = max(len(_.strip()) for _ in left_lines)
            right_width = max(len(_.strip()) for _ in right_lines)
        else:
            left_width = max(len(_) for _ in left_lines)
            right_width = max(len(_) for _ in right_lines)
        left_lines = [self._ljust(_, left_width) for _ in left_lines]
        right_lines = [self._ljust(_, right_width) for _ in right_lines]
        if strip:
            left_margin_width, gutter_width = 4, 4 
        else:
            left_margin_width, gutter_width = 0, 4 
        left_margin = left_margin_width * ' '
        gutter = gutter_width * ' '
        conjoined_lines = []
        for _ in sequencetools.zip_sequences(
            [left_lines, right_lines],
            truncate=False,
            ):
            if len(_) == 1:
                left_line = _[0]
                conjoined_line = left_margin + left_line
            elif len(_) == 2:
                left_line, right_line = _
                conjoined_line = left_margin + left_line + gutter + right_line
            conjoined_lines.append(conjoined_line)
        if all_packages_lines:
            blank_line = left_margin
            conjoined_lines.append(blank_line)
        for line in all_packages_lines:
            conjoined_line = left_margin + line
            conjoined_lines.append(conjoined_line)
github Abjad / abjad / experimental / tools / musicexpressiontools / StartPositionedPayloadExpression / StartPositionedPayloadExpression.py View on Github external
def partition_by_ratio(self, ratio):
        r'''Partition start-positioned payload expression by ratio.

        Operates in place and returns newly constructed inventory.
        '''
        from experimental.tools import musicexpressiontools
        parts = sequencetools.partition_sequence_by_ratio_of_lengths(
            self.elements, ratio)
        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,
                )
github Abjad / abjad / abjad / tools / sequencetools / flatten_sequence_at_indices.py View on Github external
if classes is None:
        classes = (list, tuple)

    if not isinstance(sequence, classes):
        raise TypeError()
    ltype = type(sequence)

    len_l = len(sequence)
    indices = [x if 0 <= x else len_l + x for x in indices]

    result = []
    for i, element in enumerate(sequence):
        if i in indices:
            try:
                flattened = sequencetools.flatten_sequence(
                    element, 
                    classes=classes, 
                    depth=depth,
                    )
                result.extend(flattened)
            except:
                result.append(element)
        else:
            result.append(element)

    result = ltype(result)
    return result
github Abjad / abjad / trunk / abjad / tools / sequencetools / partition_sequence_extended_to_counts.py View on Github external
>>> sequencetools.partition_sequence_extended_to_counts(
            ...     (1, 2, 3, 4), 
            ...     (6, 6, 6), 
            ...     overhang=False,
            ...     )
            [(1, 2, 3, 4, 1, 2), (3, 4, 1, 2, 3, 4), (1, 2, 3, 4, 1, 2)]

    Returns sequence of sequence objects.
    '''
    from abjad.tools import sequencetools

    n = int(math.ceil(float(sum(counts)) / len(sequence)))

    sequence = sequencetools.repeat_sequence_n_times(sequence, n)

    return sequencetools.partition_sequence_by_counts(
        sequence, 
        counts, 
        cyclic=False, 
        overhang=overhang,
        )
github Abjad / abjad / trunk / abjad / tools / mathtools / Ratio.py View on Github external
def __new__(cls, *args):
        from abjad.tools import sequencetools
        if len(args) == 1 and isinstance(args[0], (list, tuple)):
            args = args[0]
        assert args, repr(args)
        assert all(x != 0 for x in args), repr(args)
        args = \
            sequencetools.divide_sequence_elements_by_greatest_common_divisor(
            args)
        self = NonreducedRatio.__new__(cls, args)
        return self