def _get_metric_value()

in fairness_indicators/remediation/weight_utils.py [0:0]


def _get_metric_value(
    nested_dict: Mapping[Text, Mapping[Text, Any]], metric_name: Text) -> float:
  """Returns the value of the named metric from a slice's metrics.

  Args:
    nested_dict: Dictionary of metrics from slice.
    metric_name: Value to return from the metric slice.

  Returns:
    Percentage value of the baseline slice name requested.

  Raises:
    KeyError: If the metric name isn't found in the metrics dictionary or if the
      input metrics dictionary is empty.
    TypeError: If an unsupported value type is found within dictionary slice.
      passed.
  """
  for value in nested_dict.values():
    if metric_name in value['']:
      typed_value = value[''][metric_name]
      if 'doubleValue' in typed_value:
        return typed_value['doubleValue']
      if 'boundedValue' in typed_value:
        return typed_value['boundedValue']['value']
      raise TypeError('Unsupported value type: %s' % typed_value)
    else:
      raise KeyError('Key %s not found in %s' %
                     (metric_name, list(value[''].keys())))
  raise KeyError(
      'Unable to return a metric value because the dictionary passed is empty.')