def list_unordered_named_pitch_pairs_in_expr(expr): '''Lists unordered named pitch pairs in `expr`. :: >>> chord = Chord("<c' cs' d' ef'>4") :: >>> for pair in pitchtools.list_unordered_named_pitch_pairs_in_expr(chord): ... pair ... (NamedPitch("c'"), NamedPitch("cs'")) (NamedPitch("c'"), NamedPitch("d'")) (NamedPitch("c'"), NamedPitch("ef'")) (NamedPitch("cs'"), NamedPitch("d'")) (NamedPitch("cs'"), NamedPitch("ef'")) (NamedPitch("d'"), NamedPitch("ef'")) Returns generator. ''' from abjad.tools import pitchtools for pair in sequencetools.yield_all_unordered_pairs_of_sequence( sorted(pitchtools.list_named_pitches_in_expr(expr))): yield pair
def list_unordered_named_pitch_pairs_in_expr(expr): '''List unordered named pitch pairs in `expr`: :: >>> chord = Chord("<c' cs' d' ef'>4") :: >>> for pair in pitchtools.list_unordered_named_pitch_pairs_in_expr(chord): ... pair ... (NamedPitch("c'"), NamedPitch("cs'")) (NamedPitch("c'"), NamedPitch("d'")) (NamedPitch("c'"), NamedPitch("ef'")) (NamedPitch("cs'"), NamedPitch("d'")) (NamedPitch("cs'"), NamedPitch("ef'")) (NamedPitch("d'"), NamedPitch("ef'")) Returns generator. ''' from abjad.tools import pitchtools for pair in sequencetools.yield_all_unordered_pairs_of_sequence( sorted(pitchtools.list_named_pitches_in_expr(expr))): yield pair
def numbered_inversion_equivalent_interval_class_dictionary(pitches): r'''Change named `pitches` to numbered inversion-equivalent interval-class number dictionary: :: >>> chord = Chord("<c' d' b''>4") >>> vector = pitchtools.numbered_inversion_equivalent_interval_class_dictionary( ... chord.written_pitches) >>> for i in range(7): ... print('\t%s\t%s' % (i, vector[i])) ... 0 0 1 1 2 1 3 1 4 0 5 0 6 0 Returns dictionary. ''' from abjad.tools import pitchtools numbers = [] for pitch in pitches: if not isinstance(pitch, pitchtools.NamedPitch): raise ValueError numbers.append(pitch.pitch_number) pairs = sequencetools.yield_all_unordered_pairs_of_sequence(numbers) intervals = [] for pair in pairs: interval = max(pair) - min(pair) interval %= 12 if 6 < interval: interval = 12 - interval intervals.append(interval) vector = {} # 12-ET pitches only if all(isinstance(interval, int) for interval in intervals): for i in range(7): vector[i] = intervals.count(i) # 24-ET pitches included else: for i in range(13): if i % 2 == 0: key = i / 2 else: key = i / 2.0 vector[key] = intervals.count(key) return vector
def __init__(self, tokens=None, item_class=None, custom_identifier=None): from abjad.tools import pitchtools prototype = ( pitchtools.PitchClassSegment, pitchtools.PitchClassSet, pitchtools.PitchSegment, pitchtools.PitchSet, ) if isinstance(tokens, prototype): tokens = list(tokens) pairs = sequencetools.yield_all_unordered_pairs_of_sequence(tokens) tokens = [second - first for first, second in pairs] Set.__init__(self, tokens=tokens, item_class=item_class, custom_identifier=custom_identifier)
def __init__(self, items=None, item_class=None): from abjad.tools import pitchtools prototype = ( pitchtools.PitchClassSegment, pitchtools.PitchClassSet, pitchtools.PitchSegment, pitchtools.PitchSet, ) if isinstance(items, prototype): items = list(items) pairs = sequencetools.yield_all_unordered_pairs_of_sequence(items) items = [second - first for first, second in pairs] Set.__init__( self, items=items, item_class=item_class, )
def __init__(self, items=None, item_class=None): from abjad.tools import pitchtools prototype = ( pitchtools.PitchClassSegment, pitchtools.PitchClassSet, pitchtools.PitchSegment, pitchtools.PitchSet, ) if isinstance(items, prototype): items = list(items) pairs = sequencetools.yield_all_unordered_pairs_of_sequence( items) items = [second - first for first, second in pairs] Set.__init__( self, items=items, item_class=item_class, )
def __init__(self, items=None, item_class=None): from abjad.tools import pitchtools if isinstance(items, ( pitchtools.PitchSegment, pitchtools.PitchSet, pitchtools.PitchClassSegment, pitchtools.PitchClassSet, )): intervals = [] for first, second in \ sequencetools.yield_all_unordered_pairs_of_sequence( tuple(items)): intervals.append(second - first) items = intervals Vector.__init__( self, items=items, item_class=item_class, )
def from_selection(cls, selection, item_class=None, custom_identifier=None): r'''Initialize interval set from component selection: :: >>> staff_1 = Staff("c'4 <d' fs' a'>4 b2") >>> staff_2 = Staff("c4. r8 g2") >>> selection = select((staff_1, staff_2)) >>> intervals = pitchtools.IntervalSet.from_selection( ... selection) >>> for interval in intervals: ... interval ... NamedInterval('+m3') NamedInterval('+M3') NamedInterval('+P4') NamedInterval('+M7') NamedInterval('-M6') NamedInterval('+m2') NamedInterval('+aug11') NamedInterval('-P5') NamedInterval('+M13') NamedInterval('+P8') NamedInterval('-M3') NamedInterval('-aug4') NamedInterval('+M9') NamedInterval('+m7') NamedInterval('-M2') NamedInterval('-m3') NamedInterval('+P5') Returns interval set. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) pairs = sequencetools.yield_all_unordered_pairs_of_sequence( pitch_segment) intervals = (second - first for first, second in pairs) return cls( tokens=intervals, item_class=item_class, custom_identifier=custom_identifier, )
def from_selection(cls, selection, item_class=None): r'''Initialize interval set from component selection: :: >>> staff_1 = Staff("c'4 <d' fs' a'>4 b2") >>> staff_2 = Staff("c4. r8 g2") >>> selection = select((staff_1, staff_2)) >>> intervals = pitchtools.IntervalSet.from_selection( ... selection) >>> for interval in sorted(intervals): ... interval ... NamedInterval('-M6') NamedInterval('-P5') NamedInterval('-aug4') NamedInterval('-M3') NamedInterval('-m3') NamedInterval('-M2') NamedInterval('+m2') NamedInterval('+m3') NamedInterval('+M3') NamedInterval('+P4') NamedInterval('+P5') NamedInterval('+m7') NamedInterval('+M7') NamedInterval('+P8') NamedInterval('+M9') NamedInterval('+aug11') NamedInterval('+M13') Returns interval set. ''' from abjad.tools import pitchtools pitch_segment = pitchtools.PitchSegment.from_selection(selection) pairs = sequencetools.yield_all_unordered_pairs_of_sequence( pitch_segment) intervals = (second - first for first, second in pairs) return cls( items=intervals, item_class=item_class, )
timespan_collection = timetools.TimespanCollection() start_offset = 0 for _ in range(5): duration = random.randint(1, 5) stop_offset = start_offset + duration timespan = timespantools.Timespan( start_offset=start_offset, stop_offset=stop_offset, ) timespan_collection.insert(timespan) start_offset += stop_offset start_offset = int(timespan_collection.start_offset - 1) stop_offset = int(timespan_collection.stop_offset + 1) indices = tuple(range(start_offset, stop_offset + 1)) for pair in \ sequencetools.yield_all_unordered_pairs_of_sequence(indices): start_offset, stop_offset = sorted(pair) target_timespan = timespantools.Timespan( start_offset=start_offset, stop_offset=stop_offset, ) pairs.append((timespan_collection, target_timespan)) random.shuffle(pairs) pairs = pairs[:20] @pytest.mark.parametrize('pair', pairs) def test_TimeCollection_find_timespans_intersecting_timespan_01(pair): """ Non-overlapping.""" found_by_search = set(
def iterate_leaf_pairs_in_expr(expr): r'''Iterate leaf pairs forward in `expr`: :: >>> score = Score([]) >>> notes = [Note("c'8"), Note("d'8"), Note("e'8"), Note("f'8"), Note("g'4")] >>> score.append(Staff(notes)) >>> notes = [Note(x, (1, 4)) for x in [-12, -15, -17]] >>> score.append(Staff(notes)) >>> clef = contexttools.ClefMark('bass') >>> attach(clef, score[1]) ClefMark('bass')(Staff{3}) >>> show(score) # doctest: +SKIP .. doctest:: >>> f(score) \new Score << \new Staff { c'8 d'8 e'8 f'8 g'4 } \new Staff { \clef "bass" c4 a,4 g,4 } >> :: >>> for pair in iterationtools.iterate_leaf_pairs_in_expr(score): ... pair (Note("c'8"), Note('c4')) (Note("c'8"), Note("d'8")) (Note('c4'), Note("d'8")) (Note("d'8"), Note("e'8")) (Note("d'8"), Note('a,4')) (Note('c4'), Note("e'8")) (Note('c4'), Note('a,4')) (Note("e'8"), Note('a,4')) (Note("e'8"), Note("f'8")) (Note('a,4'), Note("f'8")) (Note("f'8"), Note("g'4")) (Note("f'8"), Note('g,4')) (Note('a,4'), Note("g'4")) (Note('a,4'), Note('g,4')) (Note("g'4"), Note('g,4')) Iterate leaf pairs left-to-right and top-to-bottom. Returns generator. ''' from abjad.tools import iterationtools vertical_moments = iterationtools.iterate_vertical_moments_in_expr(expr) for moment_1, moment_2 in \ sequencetools.iterate_sequence_pairwise_strict(vertical_moments): for pair in sequencetools.yield_all_unordered_pairs_of_sequence( moment_1.start_leaves): yield pair pairs = sequencetools.yield_all_pairs_between_sequences( moment_1.leaves, moment_2.start_leaves) for pair in pairs: yield pair else: for pair in sequencetools.yield_all_unordered_pairs_of_sequence( moment_2.start_leaves): yield pair
def by_leaf_pair(self): r'''Iterate leaf pairs forward in `expr`: :: >>> score = Score([]) >>> notes = [Note("c'8"), Note("d'8"), Note("e'8"), ... Note("f'8"), Note("g'4")] >>> score.append(Staff(notes)) >>> notes = [Note(x, (1, 4)) for x in [-12, -15, -17]] >>> score.append(Staff(notes)) >>> clef = Clef('bass') >>> attach(clef, score[1]) >>> show(score) # doctest: +SKIP .. doctest:: >>> print(format(score)) \new Score << \new Staff { c'8 d'8 e'8 f'8 g'4 } \new Staff { \clef "bass" c4 a,4 g,4 } >> :: >>> for pair in iterate(score).by_leaf_pair(): ... pair (Note("c'8"), Note('c4')) (Note("c'8"), Note("d'8")) (Note('c4'), Note("d'8")) (Note("d'8"), Note("e'8")) (Note("d'8"), Note('a,4')) (Note('c4'), Note("e'8")) (Note('c4'), Note('a,4')) (Note("e'8"), Note('a,4')) (Note("e'8"), Note("f'8")) (Note('a,4'), Note("f'8")) (Note("f'8"), Note("g'4")) (Note("f'8"), Note('g,4')) (Note('a,4'), Note("g'4")) (Note('a,4'), Note('g,4')) (Note("g'4"), Note('g,4')) Iterate leaf pairs left-to-right and top-to-bottom. Returns generator. ''' vertical_moments = self.by_vertical_moment() for moment_1, moment_2 in \ sequencetools.iterate_sequence_nwise(vertical_moments): for pair in sequencetools.yield_all_unordered_pairs_of_sequence( moment_1.start_leaves): yield pair pairs = sequencetools.yield_all_pairs_between_sequences( moment_1.leaves, moment_2.start_leaves) for pair in pairs: yield pair else: for pair in sequencetools.yield_all_unordered_pairs_of_sequence( moment_2.start_leaves): yield pair
timespan_collection = timetools.TimespanCollection() start_offset = 0 for _ in range(5): duration = random.randint(1, 5) stop_offset = start_offset + duration timespan = timespantools.Timespan( start_offset=start_offset, stop_offset=stop_offset, ) timespan_collection.insert(timespan) start_offset += stop_offset start_offset = int(timespan_collection.start_offset - 1) stop_offset = int(timespan_collection.stop_offset + 1) indices = tuple(range(start_offset, stop_offset + 1)) for pair in \ sequencetools.yield_all_unordered_pairs_of_sequence(indices): start_offset, stop_offset = sorted(pair) target_timespan = timespantools.Timespan( start_offset=start_offset, stop_offset=stop_offset, ) pairs.append((timespan_collection, target_timespan)) @pytest.mark.parametrize('pair', pairs) def test_TimeCollection_find_timespans_intersecting_timespan_01(pair): r'''Non-overlapping.''' found_by_search = set( timespan_collection.find_timespans_intersecting_timespan( target_timespan)) found_by_brute_force = set()