code: def prepare_ndgrid(hist, n_dim):
      """Get lists of all unique combinations of keys
  
      Used as input by get_ndgrid(hist).
  
      :param hist: input histogrammar histogram
      :return: n sorted lists of keys
      """
  
      if hist.n_dim < n_dim:
          warnings.warn(
              f"Input histogram only has {hist.n_dim} dimensions (<{n_dim}). Returning empty lists."
          )
          return [[] for _ in range(n_dim)]
  
      def get_hist_keys(h):
          if hasattr(h, "bins"):
              return set(h.bins.keys())
          elif hasattr(h, "values"):
              return set(range(len(h.values)))
          else:
              raise TypeError
  
      # SparselyBin or Categorize
      def keys_recursive(hist, hist_keys, idx):
          hist_keys[idx] |= get_hist_keys(hist)
          if (idx + 1) < len(hist_keys):
              if hasattr(hist, "bins"):
                  for h in hist.bins.values():
                      hist_keys = keys_recursive(h, hist_keys, idx + 1)
              elif hasattr(hist, "values"):
                  for h in hist.values:
                      hist_keys = keys_recursive(h, hist_keys, idx + 1)
              else:
                  raise TypeError
          return hist_keys
  
      keys = [set() for _ in range(n_dim)]
      keys = keys_recursive(hist, keys, 0)
      keys = [sorted(k) for k in keys]
      return keys
  
imports: from __future__ import annotations
  import warnings
  import histogrammar
  import numpy
  from histogrammar.util import get_hist_props
  from popmon.hist.hist_utils import get_bin_centers, is_numeric
  from popmon.stats.numpy import quantile
label_desc: Use image node_id 1 for calling a global function with example usage: prepare_ndgrid(hist, n_dim) and returns: keys, unknown, hist_keys, set, set