code: def set_ndgrid(hist, keys, n_dim):
      """Set n-d grid of first n dimensions of input histogram
  
      Used as input by get_ndgrid(hist).
  
      :param hist: input histogrammar histogram
      :param list keys: list with unique keys per dim
      :return: filled nd numpy grid
      """
      shape = [len(k) for k in reversed(keys)]
      grid = np.zeros(shape)
  
      if hist.n_dim < n_dim:
          warnings.warn(
              f"Input histogram only has {hist.n_dim} dimensions (<{n_dim}). Returning empty grid."
          )
          return grid
  
      def flatten(histogram, keys, grid, dim: int = 0, prefix=None):
          if prefix is None:
              prefix = []
  
          if len(keys) == len(prefix):
              grid[tuple(prefix)] = histogram.entries
          elif hasattr(histogram, "bins"):
              for k, h in histogram.bins.items():
                  if k not in keys[dim]:
                      continue
                  i = keys[dim].index(k)
                  flatten(h, keys, grid, dim + 1, [i, *prefix])
          elif hasattr(histogram, "values"):
              for i, h in enumerate(histogram.values):
                  flatten(h, keys, grid, dim + 1, [i, *prefix])
          else:
              raise TypeError
  
      flatten(hist, keys, grid)
      return grid
  
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 3 for calling a global function with example usage: set_ndgrid(hist, keys, n_dim) and returns: grid, grid