code: def mean_group_metric(
      metric_fn,
      group_key="group",
      value_key="value",
      return_subgroup_scores=False,
  ):
      """Returns a metric that averages `metric_fn` on sub-groups of results.
  
      The sub-groups are defined by aggregating results (targets and predictions)
      by accessing the feature specified by `group_key` in the target dicts.
  
      **WARNING**: Using this function can produce unreliable results if you do not
      pass in full groups. For example, if you evaluate over a random subsample of a
      validation set and do not retain all of the examples in each group, you may
      get results which aren't directly comparable to using the full validation set.
  
      Args:
        metric_fn: function, the metric to compute on the subgroups.
        group_key: string, the key for the grouping value in the target dictionary.
        value_key: string, the key for the value in the dictionaries.
        return_subgroup_scores: If true, include the scores for each sub-group.
      """
  
      def my_metric(targets, predictions):
          """Computes mean of `metric_fn` over subgroups of results."""
          grouped_values = collections.defaultdict(lambda: ([], []))
          for targ, pred in zip(targets, predictions):
              g = targ[group_key]
              grouped_values[g][0].append(targ[value_key])
              grouped_values[g][1].append(pred[value_key])
          group_scores = collections.defaultdict(list)
          for group, (targets, predictions) in grouped_values.items():
              for metric, score in metric_fn(
                  targets, predictions
              ).items():
                  group_scores[metric].append(score)
                  if return_subgroup_scores:
                      group_scores["%s-%s" % (group, metric)].append(
                          score
                      )
          return {
              metric: np.mean(scores)
              for metric, scores in group_scores.items()
          }
  
      return my_metric
  
imports: import collections
  import itertools
  import re
  import string
  from typing import Any, Dict, Mapping, Optional, Sequence, Tuple, Union
  from absl import logging
  import editdistance
  import flax
  import jax.numpy
  import numpy
  import sacrebleu
  import scipy.stats
  import seqio
  import sklearn.metrics
  from t5.evaluation import qa_utils
  import tensorflow.compat.v2
  from rouge_score import rouge_scorer
  from rouge_score import scoring
label_desc: Use image node_id 16 for calling a global function with example usage: mean_group_metric(metric_fn, group_key, value_key, return_subgroup_scores) and returns: my_metric, unknown