code: def filter_invalid_notes(min_pitch, max_pitch, kv):
      """Filter notes with out-of-range pitch from NoteSequence protos."""
      key, ns_str = kv
      ns = note_seq.NoteSequence.FromString(ns_str)
      valid_notes = [
          note
          for note in ns.notes
          if min_pitch <= note.pitch <= max_pitch
      ]
      if len(valid_notes) < len(ns.notes):
          del ns.notes[:]
          ns.notes.extend(valid_notes)
          Metrics.counter(
              "filter_invalid_notes", "out_of_range_pitch"
          ).inc()
      return key, ns.SerializeToString()
  
imports: import copy
  import functools
  import hashlib
  import logging
  import os
  import random
  import typing
  import apache_beam
  from apache_beam import typehints
  from apache_beam.metrics import Metrics
  from magenta.models.score2perf import music_encoders
  import note_seq
  from note_seq import chord_inference
  from note_seq import melody_inference
  from note_seq import sequences_lib
  import numpy
  from tensor2tensor.data_generators import generator_utils
  import tensorflow.compat.v1
label_desc: Use image node_id 2 for calling a global function with example usage: filter_invalid_notes(min_pitch, max_pitch, kv) and returns: key,  ns