class_docstr: Computes label and prediction pairs for set matching.
docstr: Initialize the preprocessor for set matching.
  
  Example:
    Labels: ['sun', 'moon']
    Predictions: {
        'classes': ['sun', 'sea', 'light'],
    }
  
    The (label, prediction) tuples generated are:
      (1, 1) (TP for sun)
      (1, 0) (FN for moon)
      (0, 1) (FP for sea)
      (0, 1) (FP for light)
  
  Example with class weights:
  Note: The preporcessor supports class wise weights inside each example. The
  weight should be a numpy array stored in the features. The user could
  provide the corresponding classes of the weights. If it is not provided,
  then by default, the preprocessor assumes the weights are for labels. The
  classes and weights should be of the same length.
  
  For classes with specified weights, the final weights of the label
  prediction pair is class weight * example weight.
  For the classes not listed in the class-weight pairs, the weight will be
  the example_weight by default.
  
    'labels': ['sun', 'moon']
    'predictions': {
        'classes': ['sun', 'sea', 'light'],
        'scores': [1, 0.7, 0.3],
    }
    'example_weights': [0.1]
    'features': 'class_weights': [0.3, 0.4]
  
    The (label, prediction, example weight) tuples generated are:
      (1, 1, 0.03) (TP for sun with weight 0.3 * 0.1)
      (1, 0, 0.04) (FN for moon with weight 0.4 * 0.1)
      (0, 0.7, 0.1) (FP for sea with weight 0.1)
      (0, 0.3, 0.1) (FP for light with weight 0.1)
  
  Args:
    prediction_class_key: The key name of the classes in predictions.
    prediction_score_key: The key name of the scores in predictions.
    class_key: (Optional) The key name of the classes in class-weight pairs.
      If it is not provided, the classes are assumed to be the label classes.
    weight_key: (Optional) The key name of the weights of classes in
      class-weight pairs. The value in this key should be a numpy array of the
      same length as the classes in class_key. The key should be stored under
      the features key.
    top_k: (Optional) Used with a multi-class model to specify that the top-k
      values should be used to compute the confusion matrix. The net effect is
      that the non-top-k values are truncated and the matrix is then
      constructed from the average TP, FP, TN, FN across the classes. When
      top_k is used, metrics_specs.binarize settings must not be present.
    name: (Optional) name for the preprocessor.
    model_name: (Optional) model name (if multi-model evaluation).
code: def __init__(
      self,
      prediction_class_key: str,
      prediction_score_key: str,
      class_key: Optional[str] = None,
      weight_key: Optional[str] = None,
      top_k: Optional[int] = None,
      name: Optional[str] = None,
      model_name: str = "",
  ):
      """Initialize the preprocessor for set matching.
  
      Example:
        Labels: ['sun', 'moon']
        Predictions: {
            'classes': ['sun', 'sea', 'light'],
        }
  
        The (label, prediction) tuples generated are:
          (1, 1) (TP for sun)
          (1, 0) (FN for moon)
          (0, 1) (FP for sea)
          (0, 1) (FP for light)
  
      Example with class weights:
      Note: The preporcessor supports class wise weights inside each example. The
      weight should be a numpy array stored in the features. The user could
      provide the corresponding classes of the weights. If it is not provided,
      then by default, the preprocessor assumes the weights are for labels. The
      classes and weights should be of the same length.
  
      For classes with specified weights, the final weights of the label
      prediction pair is class weight * example weight.
      For the classes not listed in the class-weight pairs, the weight will be
      the example_weight by default.
  
        'labels': ['sun', 'moon']
        'predictions': {
            'classes': ['sun', 'sea', 'light'],
            'scores': [1, 0.7, 0.3],
        }
        'example_weights': [0.1]
        'features': 'class_weights': [0.3, 0.4]
  
        The (label, prediction, example weight) tuples generated are:
          (1, 1, 0.03) (TP for sun with weight 0.3 * 0.1)
          (1, 0, 0.04) (FN for moon with weight 0.4 * 0.1)
          (0, 0.7, 0.1) (FP for sea with weight 0.1)
          (0, 0.3, 0.1) (FP for light with weight 0.1)
  
      Args:
        prediction_class_key: The key name of the classes in predictions.
        prediction_score_key: The key name of the scores in predictions.
        class_key: (Optional) The key name of the classes in class-weight pairs.
          If it is not provided, the classes are assumed to be the label classes.
        weight_key: (Optional) The key name of the weights of classes in
          class-weight pairs. The value in this key should be a numpy array of the
          same length as the classes in class_key. The key should be stored under
          the features key.
        top_k: (Optional) Used with a multi-class model to specify that the top-k
          values should be used to compute the confusion matrix. The net effect is
          that the non-top-k values are truncated and the matrix is then
          constructed from the average TP, FP, TN, FN across the classes. When
          top_k is used, metrics_specs.binarize settings must not be present.
        name: (Optional) name for the preprocessor.
        model_name: (Optional) model name (if multi-model evaluation).
      """
      if not name:
          name = metric_util.generate_private_name_from_arguments(
              _DEFAULT_SET_MATCH_PREPROCESSOR_NAME, top_k=top_k
          )
      super().__init__(name=name)
      self._top_k = top_k
      self._model_name = model_name
      self._prediction_class_key = prediction_class_key
      self._prediction_score_key = prediction_score_key
      self._class_key = class_key
      self._weight_key = weight_key
      self._model_top_k_exceeds_prediction_length_distribution = (
          beam.metrics.Metrics.distribution(
              constants.METRICS_NAMESPACE,
              "top_k_exceeds_prediction_length",
          )
      )
  
file_path: model-analysis/tensorflow_model_analysis/metrics/preprocessors/set_match_preprocessors.py
imports: from typing import Iterator, Optional
  import apache_beam
  import numpy
  from tensorflow_model_analysis import constants
  from tensorflow_model_analysis.api import types
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.utils import util
label_desc: Use image node_id 1 to create a new SetMatchPreprocessor object from inherited base classes: metric_types with example: obj = SetMatchPreprocessor(prediction_class_key, prediction_score_key, class_key, weight_key, top_k, name, model_name)