code: def calculate_metrics(
      frame_probs,
      onset_probs,
      frame_predictions,
      onset_predictions,
      offset_predictions,
      velocity_values,
      sequence_label,
      frame_labels,
      sequence_id,
      hparams,
      min_pitch,
      max_pitch,
      onsets_only=False,
      pitch_map=None,
  ):
      """Calculate metrics for a single example."""
  
      def make_metrics(
          note_precision,
          note_recall,
          note_f1,
          note_density,
          note_with_velocity_precision,
          note_with_velocity_recall,
          note_with_velocity_f1,
          note_with_offsets_precision,
          note_with_offsets_recall,
          note_with_offsets_f1,
          note_with_offsets_velocity_precision,
          note_with_offsets_velocity_recall,
          note_with_offsets_velocity_f1,
          processed_frame_predictions,
          frame_labels,
          onsets_only=False,
          prefix="",
      ):
          """Create a dict of onset, offset, frame and velocity metrics."""
  
          def _add_prefix(name):
              return "_".join(x for x in [prefix, name] if x)
  
          def _metrics(precision, recall, f1, name):
              """Create and return a dict of metrics."""
              metrics = {
                  _add_prefix(name) + "_precision": precision,
                  _add_prefix(name) + "_recall": recall,
                  _add_prefix(name) + "_f1_score": f1,
              }
              return metrics
  
          frame_metrics = calculate_frame_metrics(
              frame_labels=frame_labels,
              frame_predictions=processed_frame_predictions,
          )
          metrics = _metrics(
              frame_metrics["precision"],
              frame_metrics["recall"],
              frame_metrics["f1_score"],
              "frame",
          )
          metrics.update(
              {
                  _add_prefix("frame_accuracy"): frame_metrics[
                      "accuracy"
                  ],
                  _add_prefix(
                      "frame_accuracy_without_true_negatives"
                  ): frame_metrics["accuracy_without_true_negatives"],
                  _add_prefix("note_density"): note_density,
              }
          )
  
          metrics.update(
              _metrics(note_precision, note_recall, note_f1, "note")
          )
          metrics.update(
              _metrics(
                  note_with_velocity_precision,
                  note_with_velocity_recall,
                  note_with_velocity_f1,
                  "note_with_velocity",
              )
          )
          if not onsets_only:
              metrics.update(
                  _metrics(
                      note_with_offsets_precision,
                      note_with_offsets_recall,
                      note_with_offsets_f1,
                      "note_with_offsets",
                  )
              )
              metrics.update(
                  _metrics(
                      note_with_offsets_velocity_precision,
                      note_with_offsets_velocity_recall,
                      note_with_offsets_velocity_f1,
                      "note_with_offsets_velocity",
                  )
              )
          return metrics
  
      (
          note_precision,
          note_recall,
          note_f1,
          note_density,
          note_with_velocity_precision,
          note_with_velocity_recall,
          note_with_velocity_f1,
          note_with_offsets_precision,
          note_with_offsets_recall,
          note_with_offsets_f1,
          note_with_offsets_velocity_precision,
          note_with_offsets_velocity_recall,
          note_with_offsets_velocity_f1,
          processed_frame_predictions,
      ) = tf.py_func(
          functools.partial(
              _calculate_metrics_py,
              hparams=hparams,
              min_pitch=min_pitch,
              max_pitch=max_pitch,
              onsets_only=onsets_only,
          ),
          inp=[
              frame_probs,
              onset_probs,
              frame_predictions,
              onset_predictions,
              offset_predictions,
              velocity_values,
              sequence_label,
              frame_labels,
              sequence_id,
          ],
          Tout=([tf.float64] * 13) + [tf.float32],
          stateful=False,
      )
      metrics = make_metrics(
          note_precision,
          note_recall,
          note_f1,
          note_density,
          note_with_velocity_precision,
          note_with_velocity_recall,
          note_with_velocity_f1,
          note_with_offsets_precision,
          note_with_offsets_recall,
          note_with_offsets_f1,
          note_with_offsets_velocity_precision,
          note_with_offsets_velocity_recall,
          note_with_offsets_velocity_f1,
          processed_frame_predictions,
          frame_labels,
          onsets_only=onsets_only,
      )
  
      if pitch_map:
          for pitch, name in pitch_map.items():
              (
                  note_precision,
                  note_recall,
                  note_f1,
                  note_density,
                  note_with_velocity_precision,
                  note_with_velocity_recall,
                  note_with_velocity_f1,
                  note_with_offsets_precision,
                  note_with_offsets_recall,
                  note_with_offsets_f1,
                  note_with_offsets_velocity_precision,
                  note_with_offsets_velocity_recall,
                  note_with_offsets_velocity_f1,
                  processed_frame_predictions,
              ) = tf.py_func(
                  functools.partial(
                      _calculate_metrics_py,
                      hparams=hparams,
                      min_pitch=min_pitch,
                      max_pitch=max_pitch,
                      onsets_only=onsets_only,
                      restrict_to_pitch=pitch,
                  ),
                  inp=[
                      frame_probs,
                      onset_probs,
                      frame_predictions,
                      onset_predictions,
                      offset_predictions,
                      velocity_values,
                      sequence_label,
                      frame_labels,
                      sequence_id + name,
                  ],
                  Tout=([tf.float64] * 13) + [tf.float32],
                  stateful=False,
              )
              metrics.update(
                  make_metrics(
                      note_precision,
                      note_recall,
                      note_f1,
                      note_density,
                      note_with_velocity_precision,
                      note_with_velocity_recall,
                      note_with_velocity_f1,
                      note_with_offsets_precision,
                      note_with_offsets_recall,
                      note_with_offsets_f1,
                      note_with_offsets_velocity_precision,
                      note_with_offsets_velocity_recall,
                      note_with_offsets_velocity_f1,
                      processed_frame_predictions,
                      frame_labels,
                      onsets_only=onsets_only,
                      prefix="pitch/" + name,
                  )
              )
      return metrics
  
file_path: magenta/magenta/models/onsets_frames_transcription/metrics.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import functools
  from magenta.models.onsets_frames_transcription import constants
  from magenta.models.onsets_frames_transcription import data
  from magenta.models.onsets_frames_transcription import infer_util
  import mir_eval
  from note_seq import sequences_lib
  from note_seq.protobuf import music_pb2
  import numpy
  import tensorflow.compat.v1
label_desc: Use image node_id 5 for calling a global function with example usage: calculate_metrics(frame_probs, onset_probs, frame_predictions, onset_predictions, offset_predictions, velocity_values, sequence_label, frame_labels, sequence_id, hparams, min_pitch, max_pitch, onsets_only, pitch_map) and returns: metrics, metrics, str, metrics