code: def _count_variants(
      spec: Dict, presets: Optional[List[Dict]] = None
  ) -> int:
      # Helper function: Deep update dictionary
      def deep_update(d, u):
          for k, v in u.items():
              if isinstance(v, Mapping):
                  d[k] = deep_update(d.get(k, {}), v)
              else:
                  d[k] = v
          return d
  
      total_samples = 0
      total_num_samples = spec.get("num_samples", 1)
      # For each preset, overwrite the spec and count the samples generated
      # for this preset
      for preset in presets:
          preset_spec = copy.deepcopy(spec)
          deep_update(preset_spec["config"], preset)
          total_samples += _count_spec_samples(preset_spec, 1)
          total_num_samples -= 1
  
      # Add the remaining samples
      if total_num_samples > 0:
          total_samples += _count_spec_samples(spec, total_num_samples)
      return total_samples
  
imports: import copy
  import logging
  import re
  from collections.abc import Mapping
  from typing import Any, Dict, Generator, Iterable, List, Optional, Tuple
  import numpy
  import random
  from ray.tune.search.sample import Categorical, Domain, Function, RandomState
  from ray.util.annotations import DeveloperAPI, PublicAPI
label_desc: Use image node_id 9 for calling a global function with example usage: _count_variants(spec, presets) and returns: total_samples, d