code: def _get_binary_sparse_metric(metric: tf.metrics.Metric):
      """Helper method to create a BinarySparse version of a tf.keras.Metric.
  
      BinarySparse is an implementation where the update_state(y_true, y_pred) takes
      in shapes y_true=(batch_size, 1) y_pred=(batch_size, 2). Note that this only
      supports the binary classification case, and that class_id=0 is the negative
      class and class_id=1 is the positive class.
  
      Currently supported tf.metric.Metric classes
        1. BinarySparseRecallAtPrecision
        2. BinarySparsePrecisionAtRecall
  
      Args:
        metric: A tf.metric.Metric class for which we want to generate a
          BinarySparse version of this metric.
  
      Returns:
        A class for the BinarySparse version of the specified tf.metrics.Metric
      """
  
      class BinarySparseMetric(metric):
          """A BinarySparse wrapper class for a tf.keras.Metric.
  
          This class has the same parameters and functions as the underlying
          metric class. For example, the parameters for BinarySparseRecallAtPrecision
          is the same as tf.keras.metrics.RecallAtPrecision. The only new constraint
          is that class_id must be set to 1 (or not specified) for the Binary metric.
          """
  
          def __init__(self, *args, **kwargs):
              if "class_id" in kwargs and kwargs["class_id"] != 1:
                  raise ValueError(
                      f"Custom BinarySparseMetric for class:{metric.__name__} is "
                      "only supported for class_id=1, got class_id="
                      f'{kwargs["class_id"]} instead'
                  )
              else:
                  kwargs["class_id"] = 1
              super().__init__(*args, **kwargs)
  
          def update_state(self, y_true, y_pred, sample_weight=None):
              y_true = tf.cast(tf.reshape(y_true, [-1]), tf.int32)
              y_true_one_hot = tf.one_hot(y_true, 2)
              return super().update_state(
                  y_true_one_hot, y_pred, sample_weight=sample_weight
              )
  
      return BinarySparseMetric
  
imports: import tensorflow
label_desc: Use image node_id 1 for calling a global function with example usage: _get_binary_sparse_metric(metric) and returns: BinarySparseMetric, super