code: def build_sampler(
      opts: argparse.Namespace,
      n_data_samples: Union[int, Mapping[str, int]],
      is_training: bool = False,
      get_item_metadata: Optional[Callable[[int], Dict]] = None,
      *args,
      **kwargs,
  ) -> Sampler:
      """Helper function to build data sampler from command-line arguments
  
      Args:
          opts: Command-line arguments
          n_data_samples: Number of data samples. It can be an integer specifying number of data samples for a given task
              or a mapping of task name and data samples per task in case of a chain sampler.
          get_item_metadata: A callable that provides sample metadata, given sample index.
          is_training: Training mode or not. Defaults to False.
  
      Returns:
          Data sampler over which we can iterate.
      """
      sampler_name = getattr(opts, "sampler.name")
      is_distributed = getattr(opts, "ddp.use_distributed")
  
      if (
          is_distributed
          and sampler_name.split("_")[-1] != "ddp"
          and sampler_name != "chain_sampler"
      ):
          # In case of a DDP environment, add `_ddp` to sampler name if not present
          # with an exception to chain_sampler (which is nothing but a loop over existing samplers)
          sampler_name = sampler_name + "_ddp"
  
      sampler = SAMPLER_REGISTRY[sampler_name](
          opts,
          n_data_samples=n_data_samples,
          is_training=is_training,
          get_item_metadata=get_item_metadata,
      )
      return sampler
  
imports: import argparse
  from typing import Callable, Dict, Mapping, Optional, Union
  from torch.utils.data.sampler import Sampler
  from data.sampler.base_sampler import BaseSampler, BaseSamplerDDP
  from utils.registry import Registry
label_desc: Use image node_id 1 for calling a global function with example usage: build_sampler(opts, n_data_samples, is_training, get_item_metadata) and returns: sampler