code: def make_finalize(bases, agg, schema, cuda, partitioned):
      arg_lk = dict((k, v) for (v, k) in enumerate(bases))
      if isinstance(agg, summary):
          calls = []
          for key, val in zip(agg.keys, agg.values):
              f = make_finalize(bases, val, schema, cuda, partitioned)
              try:
                  # Override bases if possible
                  bases = val._build_bases(cuda, partitioned)
              except AttributeError:
                  pass
              inds = [arg_lk[b] for b in bases]
              calls.append((key, f, inds))
  
          def finalize(bases, cuda=False, **kwargs):
              data = {
                  key: finalizer(get(inds, bases), cuda, **kwargs)
                  for (key, finalizer, inds) in calls
              }
  
              # Copy x and y range attrs from any DataArray (their ranges are all the same)
              # to set on parent Dataset
              name = agg.keys[0]  # Name of first DataArray.
              attrs = {
                  attr: data[name].attrs[attr]
                  for attr in ("x_range", "y_range")
              }
  
              return xr.Dataset(data, attrs=attrs)
  
          return finalize
      else:
          return agg._build_finalize(schema)
  
imports: from __future__ import annotations
  from itertools import count
  import logging
  from typing import TYPE_CHECKING
  from toolz import unique, concat, pluck, get, memoize
  from numba import literal_unroll
  import numpy
  import xarray
  from .antialias import AntialiasCombination
  from .reductions import SpecialColumn, UsesCudaMutex, by, category_codes, summary
  from .utils import isnull, ngjit, nanmax_in_place, nanmin_in_place, nansum_in_place, nanfirst_in_place, nanlast_in_place, nanmax_n_in_place_3d, nanmax_n_in_place_4d, nanmin_n_in_place_3d, nanmin_n_in_place_4d, nanfirst_n_in_place_3d, nanfirst_n_in_place_4d, nanlast_n_in_place_3d, nanlast_n_in_place_4d, row_min_in_place, row_min_n_in_place_3d, row_min_n_in_place_4d, row_max_in_place, row_max_n_in_place_3d, row_max_n_in_place_4d
label_desc: Use image node_id 10 for calling a global function with example usage: make_finalize(bases, agg, schema, cuda, partitioned) and returns: finalize, agg, xr