code: def auto_pivot(
      df: pd.DataFrame,
      stringify_slices: bool = True,
      collapse_column_names: bool = True,
  ) -> pd.DataFrame:
      """Automatically pivots a metric or plots DataFrame.
  
      Given a DataFrame provided by metrics/plots_as_dataframes, one can
      automatically pivot the table on all non-unique metric_key columns, with the
      slices as the index columns, metric_values as the values columns.
      E.g., given this raw DataFrame:
  
      |    | metric_values| metric_keys                    |               slices |
      |    | double_value |           name                 |       age |      sex |
      |---:|-------------:|:-------------------------------|----------:|:---------|
      |  0 |         0.1  | mean_absolute_error            |       nan | nan      |
      |  1 |         0.02 | mean_squared_logarithmic_error |        38 | male     |
  
      Since the only non-unique metric_key column is metric_keys.name, auto_pivot
      with stringify_slices set to True will generates the following DataFrame:
  
      |    slices      | 'mean_absolute_error' | 'mean_squared_logarithmic_error' |
      |:---------------|---------------------------------------------------------:|
      |Overall         |                   0.1 |                              nan |
      |sex:male; age:38|                   nan |                             0.02 |
  
      Args:
        df: a DataFrame from one of the MetricsDataFrames or PlotsDataFrames.
        stringify_slices: stringify all the slice columns and collapse them into one
          column by concatenating the corresponding strings. This is turned on by
          default.
        collapse_column_names: collapsing the multi-index column names by removing
          layer(s) with only the same string. This is turned on by default.
      Returns:
        A DataFrame that pivoted from the metrics DataFrame or plots DataFrame.
      """
      if _SLICES in df:
          if _PLOT_KEYS in df and _PLOT_DATA in df:
              return _auto_pivot(
                  df,
                  _plot_columns,
                  stringify_slices=stringify_slices,
                  collapse_column_names=collapse_column_names,
              )
          elif _METRIC_KEYS in df and _METRIC_VALUES in df:
              return _auto_pivot(
                  df,
                  _metric_columns,
                  stringify_slices=stringify_slices,
                  collapse_column_names=collapse_column_names,
              )
  
      raise NotImplementedError(
          "Only a metrics or a plots DataFrame is supported. This DataFrame has"
          f"the following columns: {df.columns}"
      )
  
imports: import collections
  import dataclasses
  import itertools
  from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
  import numpy
  import pandas
  from tensorflow_model_analysis.proto import metrics_for_slice_pb2
  from google.protobuf import wrappers_pb2
  from google.protobuf import descriptor
  from google.protobuf import message
label_desc: Use image node_id 9 for calling a global function with example usage: auto_pivot(df, stringify_slices, collapse_column_names) and returns: _auto_pivot, _auto_pivot