code: def flat_datasets_from_benchmark(
      benchmark: CLScenario, include_leaf_transforms: bool = True
  ):
      """
      Obtain a list of flattened datasets from a benchmark.
  
      In practice, this function will traverse all the
      datasets in the benchmark to find the leaf datasets.
      A dataset can be traversed and flattened to (one or more) leaf
      dataset(s) if all subset and dataset concatenations point to a
      single leaf dataset and if transformations are the same across
      all paths.
  
      Traversing the dataset means traversing :class:`AvalancheDataset`
      as well as PyTorch :class:`Subset` and :class:`ConcatDataset` to
      obtain the leaf datasets, the indices, and the transformations chain.
  
      Note: this means that datasets will be plain PyTorch datasets,
      not :class:`AvalancheDataset` (Avalanche datasets are traversed).
  
      In common benchmarks, this returns one dataset for the train
      and one dataset for test.
  
      :param benchmark: The benchmark to traverse.
      :param include_leaf_transforms: If True, include the transformations
          found in the leaf dataset in the transforms list. Defaults to True.
      :return: The list of leaf datasets. Each element in the list is
          a tuple `(dataset, indices, transforms)`.
      """
      single_datasets = dataset_list_from_benchmark(benchmark)
      leaves = leaf_datasets(
          AvalancheDataset(single_datasets),
          include_leaf_transforms=include_leaf_transforms,
      )
  
      result = []
      for dataset, indices_and_transforms in leaves.items():
          # Check that all transforms are the same
          first_transform = indices_and_transforms[0][1]
          same_transforms = all(
              [first_transform == t for _, t in indices_and_transforms]
          )
  
          if not same_transforms:
              for indices, transforms in indices_and_transforms:
                  result.append((dataset, indices, transforms))
              continue
  
          flat_indices = [i for i, _ in indices_and_transforms]
  
          result.append((dataset, flat_indices, first_transform))
      return result
  
file_path: avalanche/avalanche/benchmarks/utils/dataset_traversal_utils.py
imports: from collections import OrderedDict, defaultdict, deque
  from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, TypeVar, Union
  from avalanche.benchmarks.scenarios.generic_scenario import CLScenario
  from avalanche.benchmarks.utils.data import _FlatDataWithTransform, AvalancheDataset
  from avalanche.benchmarks.utils.dataset_definitions import IDataset
  from avalanche.benchmarks.utils.dataset_utils import find_list_from_index
  from avalanche.benchmarks.utils.flat_data import FlatData
  from torch.utils.data import Subset, ConcatDataset, Dataset
  from avalanche.benchmarks.utils.transform_groups import EmptyTransformGroups
  from avalanche.benchmarks.utils.transforms import TupleTransform
  from torchvision.datasets.vision import StandardTransform
label_desc: Use image node_id 2 for calling a global function with example usage: flat_datasets_from_benchmark(benchmark, include_leaf_transforms) and returns: result