(
    quantized_sequence, start_step=0, min_steps_discard=None,
    max_steps_discard=None, max_steps_truncate=None)
return pianoroll_seqs, list(stats.values())
Extracts a polyphonic track from the given quantized NoteSequence.

  Currently, this extracts only one pianoroll from a given track.

  Args:
    quantized_sequence: A quantized NoteSequence.
    start_step: Start extracting a sequence at this time step. Assumed
        to be the beginning of a bar.
    min_steps_discard: Minimum length of tracks in steps. Shorter tracks are
        discarded.
    max_steps_discard: Maximum length of tracks in steps. Longer tracks are
        discarded. Mutually exclusive with `max_steps_truncate`.
    max_steps_truncate: Maximum length of tracks in steps. Longer tracks are
        truncated. Mutually exclusive with `max_steps_discard`.

  Returns:
    pianoroll_seqs: A python list of PianorollSequence instances.
    stats: A dictionary mapping string names to `statistics.Statistic` objects.

  Raises:
    ValueError: If both `max_steps_discard` and `max_steps_truncate` are
        specified.
Extracts a polyphonic track from the given quantized NoteSequence.
Extracts a polyphonic track from the given quantized NoteSequence .
def extract_pianoroll_sequences(
    quantized_sequence, start_step=0, min_steps_discard=None,
    max_steps_discard=None, max_steps_truncate=None):
  """Extracts a polyphonic track from the given quantized NoteSequence.

  Currently, this extracts only one pianoroll from a given track.

  Args:
    quantized_sequence: A quantized NoteSequence.
    start_step: Start extracting a sequence at this time step. Assumed
        to be the beginning of a bar.
    min_steps_discard: Minimum length of tracks in steps. Shorter tracks are
        discarded.
    max_steps_discard: Maximum length of tracks in steps. Longer tracks are
        discarded. Mutually exclusive with `max_steps_truncate`.
    max_steps_truncate: Maximum length of tracks in steps. Longer tracks are
        truncated. Mutually exclusive with `max_steps_discard`.

  Returns:
    pianoroll_seqs: A python list of PianorollSequence instances.
    stats: A dictionary mapping string names to `statistics.Statistic` objects.

  Raises:
    ValueError: If both `max_steps_discard` and `max_steps_truncate` are
        specified.
  """

  if (max_steps_discard, max_steps_truncate).count(None) == 0:
    raise ValueError(
        'Only one of `max_steps_discard` and `max_steps_truncate` can be '
        'specified.')
  sequences_lib.assert_is_relative_quantized_sequence(quantized_sequence)

  # pylint: disable=g-complex-comprehension
  stats = dict((stat_name, statistics.Counter(stat_name)) for stat_name in
               ['pianoroll_tracks_truncated_too_long',
                'pianoroll_tracks_discarded_too_short',
                'pianoroll_tracks_discarded_too_long',
                'pianoroll_tracks_discarded_more_than_1_program'])
  # pylint: enable=g-complex-comprehension

  steps_per_bar = sequences_lib.steps_per_bar_in_quantized_sequence(
      quantized_sequence)

  # Create a histogram measuring lengths (in bars not steps).
  stats['pianoroll_track_lengths_in_bars'] = statistics.Histogram(
      'pianoroll_track_lengths_in_bars',
      [0, 1, 10, 20, 30, 40, 50, 100, 200, 500, 1000])

  # Allow only 1 program.
  programs = set()
  for note in quantized_sequence.notes:
    programs.add(note.program)
  if len(programs) > 1:
    stats['pianoroll_tracks_discarded_more_than_1_program'].increment()
    return [], list(stats.values())

  # Translate the quantized sequence into a PianorollSequence.
  pianoroll_seq = PianorollSequence(quantized_sequence=quantized_sequence,
                                    start_step=start_step)

  pianoroll_seqs = []
  num_steps = pianoroll_seq.num_steps

  if min_steps_discard is not None and num_steps < min_steps_discard:
    stats['pianoroll_tracks_discarded_too_short'].increment()
  elif max_steps_discard is not None and num_steps > max_steps_discard:
    stats['pianoroll_tracks_discarded_too_long'].increment()
  else:
    if max_steps_truncate is not None and num_steps > max_steps_truncate:
      stats['pianoroll_tracks_truncated_too_long'].increment()
      pianoroll_seq.set_length(max_steps_truncate)
    pianoroll_seqs.append(pianoroll_seq)
    stats['pianoroll_track_lengths_in_bars'].increment(
        num_steps // steps_per_bar)
  return pianoroll_seqs, list(stats.values())
def extract_pianoroll_sequences ( quantized_sequence , start_step = 0 , min_steps_discard = None , max_steps_discard = None , max_steps_truncate = None ) : if ( max_steps_discard , max_steps_truncate ) . count ( None ) == 0 : raise ValueError ( 'Only one of `max_steps_discard` and `max_steps_truncate` can be ' 'specified.' ) sequences_lib . assert_is_relative_quantized_sequence ( quantized_sequence ) # pylint: disable=g-complex-comprehension stats = dict ( ( stat_name , statistics . Counter ( stat_name ) ) for stat_name in [ 'pianoroll_tracks_truncated_too_long' , 'pianoroll_tracks_discarded_too_short' , 'pianoroll_tracks_discarded_too_long' , 'pianoroll_tracks_discarded_more_than_1_program' ] ) # pylint: enable=g-complex-comprehension steps_per_bar = sequences_lib . steps_per_bar_in_quantized_sequence ( quantized_sequence ) # Create a histogram measuring lengths (in bars not steps). stats [ 'pianoroll_track_lengths_in_bars' ] = statistics . Histogram ( 'pianoroll_track_lengths_in_bars' , [ 0 , 1 , 10 , 20 , 30 , 40 , 50 , 100 , 200 , 500 , 1000 ] ) # Allow only 1 program. programs = set ( ) for note in quantized_sequence . notes : programs . add ( note . program ) if len ( programs ) > 1 : stats [ 'pianoroll_tracks_discarded_more_than_1_program' ] . increment ( ) return [ ] , list ( stats . values ( ) ) # Translate the quantized sequence into a PianorollSequence. pianoroll_seq = PianorollSequence ( quantized_sequence = quantized_sequence , start_step = start_step ) pianoroll_seqs = [ ] num_steps = pianoroll_seq . num_steps if min_steps_discard is not None and num_steps < min_steps_discard : stats [ 'pianoroll_tracks_discarded_too_short' ] . increment ( ) elif max_steps_discard is not None and num_steps > max_steps_discard : stats [ 'pianoroll_tracks_discarded_too_long' ] . increment ( ) else : if max_steps_truncate is not None and num_steps > max_steps_truncate : stats [ 'pianoroll_tracks_truncated_too_long' ] . increment ( ) pianoroll_seq . set_length ( max_steps_truncate ) pianoroll_seqs . append ( pianoroll_seq ) stats [ 'pianoroll_track_lengths_in_bars' ] . increment ( num_steps // steps_per_bar ) return pianoroll_seqs , list ( stats . values ( ) )