class_docstr: Determines optimal thresholds for classifiers given constraints.
class_docstr_tok: ["Determines","optimal","thresholds","for","classifiers","given","constraints","."]
code: def get_thresholds_for_pred_key(
      self,
      pred_key,
      i,
      margins_to_try,
      dataset_results,
      faceted_results,
      config,
  ):
      # Find the optimal threshold for the entire dataset
      metrics_list = [
          result[i]["metrics"] for result in dataset_results
      ]
      costs = [
          self.get_cost(metrics_for_threshold, config.cost_ratio)
          for metrics_for_threshold in metrics_list
      ]
      single_threshold = np.argmin(costs) / 100
  
      if not (facets := config.facets):
          return {"pred_key": pred_key, "thresholds": {}}
  
      faceted_thresholds = {}
      faceted_costs = {}
      faceted_measures = {}
  
      # If there is only a single facet, return the single best threshold for
      # this prediction key.
      facets_keys = list(facets.keys())
      if len(facets_keys) == 1:
          faceted_thresholds[facets_keys[0]] = {
              "Single": single_threshold,
          }
          return {
              "pred_key": pred_key,
              "thresholds": faceted_thresholds,
          }
  
      # Loop through all facets of the dataset.
      for facet_key in facets_keys:
          # Find the optimal threshold for each facet individually.
          faceted_metrics_list = [
              result[i]["metrics"]
              for result in faceted_results[facet_key]
          ]
          costs = [
              self.get_cost(metrics_for_threshold, config.cost_ratio)
              for metrics_for_threshold in faceted_metrics_list
          ]
          ind_threshold = np.argmin(costs) / 100
          faceted_thresholds[facet_key] = {
              "Single": single_threshold,
              "Individual": ind_threshold,
          }
  
          # Store the error costs and fairness measure scores for each measure for
          # the current facet across all possible margin values.
          measures = {}
          for measure_key in self.fairness_measures:
              measures[measure_key] = [
                  self.fairness_measures[measure_key](
                      metrics_for_threshold
                  )
                  for metrics_for_threshold in faceted_metrics_list
              ]
          faceted_costs[facet_key] = costs
          faceted_measures[facet_key] = measures
  
      # Follows the same logic used in https://github.com/PAIR-code/what-if-tool
      # for calculating these thresholds.
      #
      # For all fairness measures:
      #   For all margins for first facet:
      #     For all other facets:
      #        Find margin with closest fairness measure of first facet at
      #        current margin
      #     Calculate overall cost for these margin settings settings across the
      #     facets.
      #   Save the margin settings that correspond to the lowest overall cost
      #   across faceted results.
      first_facet = facets_keys[0]
      for measure_key in self.fairness_measures:
          first_facet_costs = []
          first_facet_thresholds = []
          for threshold_idx in range(len(margins_to_try)):
              first_facet_measure = faceted_measures[first_facet][
                  measure_key
              ][threshold_idx]
              cost = faceted_costs[first_facet][threshold_idx]
              cur_thresholds = [threshold_idx / 100]
              for facet_to_check in facets_keys[1:]:
                  distances_to_first_facet_measure = [
                      abs(measure - first_facet_measure)
                      for measure in faceted_measures[facet_to_check][
                          measure_key
                      ]
                  ]
                  threhold_idx_for_facet = np.argmin(
                      distances_to_first_facet_measure
                  )
                  cost += faceted_costs[facet_to_check][
                      threhold_idx_for_facet
                  ]
                  cur_thresholds.append(threhold_idx_for_facet / 100)
              first_facet_costs.append(cost)
              first_facet_thresholds.append(cur_thresholds)
          min_measure_thresholds_idx = np.argmin(first_facet_costs)
          measure_thresholds = first_facet_thresholds[
              min_measure_thresholds_idx
          ]
          for t_idx, facet_key in enumerate(facets_keys):
              faceted_thresholds[facet_key][
                  measure_key
              ] = measure_thresholds[t_idx]
  
      return {"pred_key": pred_key, "thresholds": faceted_thresholds}
  
imports: from collections.abc import Mapping, Sequence
  import math
  from typing import Optional
  import attr
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.components import metrics
  from lit_nlp.lib import utils
  import numpy
label_desc: Use image node_id 5 for calling the Thresholder obj's underlying member method code with example usage: obj.get_thresholds_for_pred_key(pred_key, i, margins_to_try, dataset_results, faceted_results, config) and returns: dict, dict, dict