code: def _to_tf_dataset(
      ds: _Dataset,
      min_length: int,
      is_batched: bool = False,
  ) -> tf.data.Dataset:
      """Converts any iterable to a small tf.data.Dataset to use visualizations.
  
      Warning: this util function is not optimized, so it should only be used for a
      small number of records (i.e., small `min_length`).
  
      Args:
        ds: Any dataset as an iterable.
        min_length: The minimum number of examples to generate.
        is_batched: Whether the data is batched.
  
      Returns:
        the tf.data.Dataset of cardinality at least `min_length`.
      """
      if isinstance(ds, tf.data.Dataset) and not isinstance(
          ds, Iterable
      ):
          if is_batched:
              return ds.unbatch()
          else:
              return ds
      tf_dataset = None
      if is_batched:
          from_tensor = tf.data.Dataset.from_tensor_slices
      else:
          from_tensor = tf.data.Dataset.from_tensors
      for record in ds:
          if tf_dataset is None:
              tf_dataset = from_tensor(record)
          else:
              tf_dataset = tf_dataset.concatenate(from_tensor(record))
          # Terminate if `tf_dataset` reached at least the expected `min_length`.
          if tf_dataset.cardinality().numpy() >= min_length:
              break
      if tf_dataset is None:
          raise ValueError(
              "Empty dataset, could not generate a valid tf.data.Dataset."
          )
      return tf_dataset
  
file_path: datasets/tensorflow_datasets/core/visualization/show_examples.py
imports: from __future__ import annotations
  from collections.abc import Iterable
  import typing
  from typing import Any, Union
  from tensorflow_datasets.core import dataset_info
  from tensorflow_datasets.core import lazy_imports_lib
  from tensorflow_datasets.core import splits
  from tensorflow_datasets.core import utils
  from tensorflow_datasets.core.utils.lazy_imports_utils import tensorflow
  from tensorflow_datasets.core.visualization import graph_visualizer
  from tensorflow_datasets.core.visualization import image_visualizer
  from tensorflow_metadata.proto.v0 import statistics_pb2
label_desc: Use image node_id 1 for calling a global function with example usage: _to_tf_dataset(ds, min_length, is_batched) and returns: tf_dataset, ds, ds