code: def format_metrics(metrics: Union[Dict, List]) -> Dict[str, Metric]:
      """Properly format iterable `metrics` to contain instances of :class:`Metric`
  
      Parameters
      ----------
      metrics: Dict, List
          Iterable describing the metrics to be recorded, along with a means to compute the value of
          each metric. Should be of one of the two following forms:
  
          List Form:
  
          * ["<metric name>", "<metric name>", ...]:
            Where each value of the list is a string that names an attribute in :mod:`sklearn.metrics`
          * [`Metric`, `Metric`, ...]:
            Where each value of the list is an instance of :class:`Metric`
          * [(<\*args>), (<\*args>), ...]:
            Where each value of the list is a tuple of arguments that will be used to instantiate a
            :class:`Metric`. Arguments given in tuples must be in order expected by :class:`Metric`
  
          Dict Form:
  
          * {"<metric name>": <metric_function>, ...}:
            Where each key is a name for the corresponding metric callable, which is used to compute
            the value of the metric
          * {"<metric name>": (<metric_function>, <direction>), ...}:
            Where each key is a name for the corresponding metric callable and direction, all of which
            are used to instantiate a :class:`Metric`
          * {"<metric name>": "<sklearn metric name>", ...}:
            Where each key is a name for the metric, and each value is the name of the attribute in
            :mod:`sklearn.metrics` for which the corresponding key is an alias
          * {"<metric name>": None, ...}:
            Where each key is the name of the attribute in :mod:`sklearn.metrics`
          * {"<metric name>": `Metric`, ...}:
            Where each key names an instance of :class:`Metric`. This is the internally-used format to
            which all other formats will be converted
  
          Metric callable functions should expect inputs of form (target, prediction), and should
          return floats. See the documentation of :class:`Metric` for information regarding expected
          parameters and types
  
      Returns
      -------
      metrics_dict: Dict
          Cast of `metrics` to a dict, in which values are instances of :class:`Metric`
  
      Examples
      --------
      >>> format_metrics(["roc_auc_score", "f1_score"])  # doctest: +ELLIPSIS
      {'roc_auc_score': Metric(roc_auc_score, <function roc_auc_score at 0x...>, max), 'f1_score': Metric(f1_score, <function f1_score at 0x...>, max)}
      >>> format_metrics([Metric("log_loss"), Metric("r2_score", direction="min")])  # doctest: +ELLIPSIS
      {'log_loss': Metric(log_loss, <function log_loss at 0x...>, min), 'r2_score': Metric(r2_score, <function r2_score at 0x...>, min)}
      >>> format_metrics({"log_loss": Metric("log_loss"), "r2_score": Metric("r2_score", direction="min")})  # doctest: +ELLIPSIS
      {'log_loss': Metric(log_loss, <function log_loss at 0x...>, min), 'r2_score': Metric(r2_score, <function r2_score at 0x...>, min)}
      >>> format_metrics([("log_loss", None), ("my_r2_score", "r2_score", "min")])  # doctest: +ELLIPSIS
      {'log_loss': Metric(log_loss, <function log_loss at 0x...>, min), 'my_r2_score': Metric(my_r2_score, <function r2_score at 0x...>, min)}
      >>> format_metrics({"roc_auc": sk_metrics.roc_auc_score, "f1": sk_metrics.f1_score})  # doctest: +ELLIPSIS
      {'roc_auc': Metric(roc_auc, <function roc_auc_score at 0x...>, max), 'f1': Metric(f1, <function f1_score at 0x...>, max)}
      >>> format_metrics({"log_loss": (None, ), "my_r2_score": ("r2_score", "min")})  # doctest: +ELLIPSIS
      {'log_loss': Metric(log_loss, <function log_loss at 0x...>, min), 'my_r2_score': Metric(my_r2_score, <function r2_score at 0x...>, min)}
      >>> format_metrics({"roc_auc": "roc_auc_score", "f1": "f1_score"})  # doctest: +ELLIPSIS
      {'roc_auc': Metric(roc_auc, <function roc_auc_score at 0x...>, max), 'f1': Metric(f1, <function f1_score at 0x...>, max)}
      >>> format_metrics({"roc_auc_score": None, "f1_score": None})  # doctest: +ELLIPSIS
      {'roc_auc_score': Metric(roc_auc_score, <function roc_auc_score at 0x...>, max), 'f1_score': Metric(f1_score, <function f1_score at 0x...>, max)}
      """
      if metrics and isinstance(metrics, dict):
          if all(isinstance(_, Metric) for _ in metrics.values()):
              return metrics
  
          metrics = [
              (k,) + (v if isinstance(v, (tuple, Metric)) else (v,))
              for k, v in metrics.items()
          ]
      elif not (metrics and isinstance(metrics, list)):
          raise TypeError(
              f"`metrics` must be a non-empty list or dict. Received: {metrics}"
          )
  
      metrics_dict = {}
  
      for value in metrics:
          if not isinstance(value, Metric):
              if not isinstance(value, tuple):
                  value = (value,)
  
              metrics_dict[value[0]] = Metric(*value)
          else:
              metrics_dict[value.name] = value
  
      if not all(metrics_dict):
          raise TypeError(
              f"`metrics` keys must all be truthy. Received: {metrics_dict}"
          )
  
      return metrics_dict
  
imports: from collections import OrderedDict
  from contextlib import suppress
  import numpy
  import pandas
  from typing import Dict, Iterable, List, Tuple, Union
  from sklearn import metrics
  from sklearn.utils.multiclass import type_of_target, unique_labels
label_desc: Use image node_id 1 for calling a global function with example usage: format_metrics(metrics) and returns: metrics_dict, metrics