code: def _lift_metrics(
      num_buckets: Optional[int] = None,
      left: Optional[float] = None,
      right: Optional[float] = None,
      name: Optional[str] = None,
      eval_config: Optional[config_pb2.EvalConfig] = None,
      model_name: str = "",
      output_name: str = "",
      aggregation_type: Optional[metric_types.AggregationType] = None,
      sub_key: Optional[metric_types.SubKey] = None,
      class_weights: Optional[Dict[int, float]] = None,
      example_weighted: bool = False,
      ignore_out_of_bound_examples: bool = False,
  ) -> metric_types.MetricComputations:
      """Returns computations for lift metrics."""
      if eval_config is None or not eval_config.cross_slicing_specs:
          raise ValueError(
              "tfma.CrossSlicingSpec with a baseline and at least one comparison "
              "slicing spec must be provided for Lift metrics"
          )
  
      if num_buckets is None:
          num_buckets = DEFAULT_NUM_BUCKETS
  
      key = metric_types.MetricKey(
          name=name,
          model_name=model_name,
          output_name=output_name,
          sub_key=sub_key,
          example_weighted=example_weighted,
      )
  
      computations = calibration_histogram.calibration_histogram(
          eval_config=eval_config,
          num_buckets=num_buckets,
          left=left,
          right=right,
          model_name=model_name,
          output_name=output_name,
          sub_key=sub_key,
          aggregation_type=aggregation_type,
          class_weights=class_weights,
          example_weighted=example_weighted,
          prediction_based_bucketing=False,
          fractional_labels=False,
      )
      metric_key = computations[-1].keys[-1]
  
      def cross_slice_comparison(
          baseline_metrics: Dict[metric_types.MetricKey, Any],
          comparison_metrics: Dict[metric_types.MetricKey, Any],
      ) -> Dict[metric_types.MetricKey, Any]:
          """Returns lift metrics values."""
          baseline_histogram = baseline_metrics[metric_key]
          comparison_histogram = comparison_metrics[metric_key]
  
          baseline_bucket = {}
          comparison_bucket = {}
          bucket_ids = set()
  
          for bucket in baseline_histogram:
              baseline_bucket[bucket.bucket_id] = bucket
              bucket_ids.add(bucket.bucket_id)
  
          for bucket in comparison_histogram:
              comparison_bucket[bucket.bucket_id] = bucket
              bucket_ids.add(bucket.bucket_id)
  
          baseline_pred_values = 0.0
          comparison_pred_values = 0.0
          comparison_num_examples = 0.0
  
          for bucket_id in bucket_ids:
              if ignore_out_of_bound_examples:
                  # Ignore buckets having examples with out of bound label values.
                  if bucket_id <= 0 or bucket_id > num_buckets:
                      continue
              num_examples = 0.0
              if bucket_id in comparison_bucket:
                  num_examples = comparison_bucket[
                      bucket_id
                  ].weighted_examples
                  comparison_pred_values += comparison_bucket[
                      bucket_id
                  ].weighted_predictions
                  comparison_num_examples += num_examples
  
              if bucket_id in baseline_bucket:
                  # To compute background/baseline re-weighted average prediction values.
                  # Background re-weighting is done by dividing the in-slice ground truth
                  # density by the background density so that the marginal ground truth
                  # distributions of in-slice items and background items appear similar.
                  weight = (
                      num_examples
                      / baseline_bucket[bucket_id].weighted_examples
                  )
                  baseline_pred_values += (
                      weight
                      * baseline_bucket[bucket_id].weighted_predictions
                  )
  
          lift_value = (
              comparison_pred_values - baseline_pred_values
          ) / comparison_num_examples
          return {key: lift_value}
  
      cross_slice_computation = (
          metric_types.CrossSliceMetricComputation(
              keys=[key], cross_slice_comparison=cross_slice_comparison
          )
      )
  
      computations.append(cross_slice_computation)
      return computations
  
file_path: model-analysis/tensorflow_model_analysis/addons/fairness/metrics/lift.py
imports: from typing import Any, Dict, Optional
  from tensorflow_model_analysis.metrics import calibration_histogram
  from tensorflow_model_analysis.metrics import metric_types
  from tensorflow_model_analysis.metrics import metric_util
  from tensorflow_model_analysis.proto import config_pb2
label_desc: Use image node_id 1 for calling a global function with example usage: _lift_metrics(num_buckets, left, right, name, eval_config, model_name, output_name, aggregation_type, sub_key, class_weights, example_weighted, ignore_out_of_bound_examples) and returns: computations, dict