def annotate_eval_result_plots()

in model_card_toolkit/utils/graphics.py [0:0]


def annotate_eval_result_plots(model_card: model_card_module.ModelCard,
                               eval_result: tfma.EvalResult) -> None:
  """Annotates visualizations for every metric in eval_result.

  This function generates barcharts for sliced metrics, encoded as base64 text
  strings, and appends them to
  model_card.quantitative_analysis.graphics.collection.

  Args:
    model_card: The model card object.
    eval_result: A `tfma.EvalResult`.
  """

  # get all metric and slice names
  metrics = set()
  slices_keys = set()
  for slicing_metric in eval_result.slicing_metrics:
    slices_key, _ = stringify_slice_key(slicing_metric[0])
    if slices_key != 'Overall':
      slices_keys.add(slices_key)
    for output_name in slicing_metric[1]:
      for sub_key in slicing_metric[1][output_name]:
        metrics.update(slicing_metric[1][output_name][sub_key].keys())

  # generate barcharts based on metrics and slices
  graphs = []
  if not slices_keys:
    slices_keys.add('')
  for metric in metrics:
    for slices_key in slices_keys:
      graph = _extract_graph_data_from_slicing_metrics(
          eval_result.slicing_metrics, metric, slices_key)
      graph = _draw_histogram(graph)
      if graph is not None:
        graphs.append(graph)

  # annotate model_card with generated graphs
  model_card.quantitative_analysis.graphics.collection.extend([
      model_card_module.Graphic(name=graph.name, image=graph.base64str)
      for graph in graphs
  ])