code: def group(
      metrics_df: pd.DataFrame,
      plotters: Collection[Plotter],
      metadata: Collection[BenchmarkMetadata],
      spec: GroupingSpec,
  ) -> Sequence[
      Tuple[Tuple[str, ...], pd.DataFrame, Collection[Plotter]]
  ]:
      """
      Group the given data according to the given specification.
      """
      result = _group(metrics_df, plotters, metadata, spec.by)
  
      if spec.minimise:
          # Terribly inefficient, but it's probably fast enough.
          for candidate_by in _iter_by(spec.by):
              candidate_result = _group(
                  metrics_df, plotters, metadata, candidate_by
              )
              if len(result) == len(candidate_result):
                  return candidate_result
  
      return result
  
imports: import heapq
  from dataclasses import dataclass
  from enum import Enum
  from typing import Any, Collection, Iterator, List, Sequence, Tuple, Type
  import pandas
  from benchmark.metadata import BenchmarkMetadata
  from benchmark.plotter_api import Plotter
label_desc: Use image node_id 1 for calling a global function with example usage: group(metrics_df, plotters, metadata, spec) and returns: result, candidate_result