code: def shade(
      agg,
      cmap=["lightblue", "darkblue"],
      color_key=Sets1to3,
      how="eq_hist",
      alpha=255,
      min_alpha=40,
      span=None,
      name=None,
      color_baseline=None,
      rescale_discrete_levels=False,
  ):
      """Convert a DataArray to an image by choosing an RGBA pixel color for each value.
  
      Requires a DataArray with a single data dimension, here called the
      "value", indexed using either 2D or 3D coordinates.
  
      For a DataArray with 2D coordinates, the RGB channels are computed
      from the values by interpolated lookup into the given colormap
      ``cmap``.  The A channel is then set to the given fixed ``alpha``
      value for all non-zero values, and to zero for all zero values.
      A dictionary ``color_key`` that specifies categories (values in ``agg``)
      and corresponding colors can be provided to support discrete coloring
      2D aggregates, i.e aggregates with a single category per pixel,
      with no mixing. The A channel is set the given ``alpha`` value for all
      pixels in the categories specified in ``color_key``, and to zero otherwise.
  
      DataArrays with 3D coordinates are expected to contain values
      distributed over different categories that are indexed by the
      additional coordinate. Such an array would reduce to the
      2D-coordinate case if collapsed across the categories (e.g. if one
      did ``aggc.sum(dim='cat')`` for a categorical dimension ``cat``).
      The RGB channels for the uncollapsed, 3D case are mixed from separate
      values over all categories. They are computed by averaging the colors
      in the provided ``color_key`` (with one color per category),
      weighted by the array's value for that category.
      The A channel is then computed from the array's total value
      collapsed across all categories at that location, ranging from the
      specified ``min_alpha`` to the maximum alpha value (255).
  
      Parameters
      ----------
      agg : DataArray
      cmap : list of colors or matplotlib.colors.Colormap, optional
          The colormap to use for 2D agg arrays. Can be either a list of
          colors (specified either by name, RGBA hexcode, or as a tuple
          of ``(red, green, blue)`` values.), or a matplotlib colormap
          object.  Default is ``["lightblue", "darkblue"]``.
      color_key : dict or iterable
          The colors to use for a categorical agg array. In 3D case, it can be
          either a ``dict`` mapping from field name to colors, or an
          iterable of colors in the same order as the record fields,
          and including at least that many distinct colors. In 2D case,
          ``color_key`` must be a ``dict`` where all keys are categories,
          and values are corresponding colors. Number of categories does not
          necessarily equal to the number of unique values in the agg DataArray.
      how : str or callable, optional
          The interpolation method to use, for the ``cmap`` of a 2D
          DataArray or the alpha channel of a 3D DataArray. Valid
          strings are 'eq_hist' [default], 'cbrt' (cube root), 'log'
          (logarithmic), and 'linear'. Callables take 2 arguments - a
          2-dimensional array of magnitudes at each pixel, and a boolean
          mask array indicating missingness. They should return a numeric
          array of the same shape, with ``NaN`` values where the mask was
          True.
      alpha : int, optional
          Value between 0 - 255 representing the alpha value to use for
          colormapped pixels that contain data (i.e. non-NaN values).
          Also used as the maximum alpha value when alpha is indicating
          data value, such as for single colors or categorical plots.
          Regardless of this value, ``NaN`` values are set to be fully
          transparent when doing colormapping.
      min_alpha : float, optional
          The minimum alpha value to use for non-empty pixels when
          alpha is indicating data value, in [0, 255].  Use a higher value
          to avoid undersaturation, i.e. poorly visible low-value datapoints,
          at the expense of the overall dynamic range. Note that ``min_alpha``
          will not take any effect when doing discrete categorical coloring
          for 2D case as the aggregate can have only a single value to denote
          the category.
      span : list of min-max range, optional
          Min and max data values to use for 2D colormapping,
          and 3D alpha interpolation, when wishing to override autoranging.
      name : string name, optional
          Optional string name to give to the Image object to return,
          to label results for display.
      color_baseline : float or None
          Baseline for calculating how categorical data mixes to
          determine the color of a pixel. The color for each category is
          weighted by how far that category's value is above this
          baseline value, out of the total sum across all categories'
          values. A value of zero is appropriate for counts and for
          other physical quantities for which zero is a meaningful
          reference; each category then contributes to the final color
          in proportion to how much each category contributes to the
          final sum.  However, if values can be negative or if they are
          on an interval scale where values e.g. twice as far from zero
          are not twice as high (such as temperature in Fahrenheit), then
          you will need to provide a suitable baseline value for use in
          calculating color mixing.  A value of None (the default) means
          to take the minimum across the entire aggregate array, which
          is safe but may not weight the colors as you expect; any
          categories with values near this baseline will contribute
          almost nothing to the final color. As a special case, if the
          only data present in a pixel is at the baseline level, the
          color will be an evenly weighted average of all such
          categories with data (to avoid the color being undefined in
          this case).
      rescale_discrete_levels : boolean, optional
          If ``how='eq_hist`` and there are only a few discrete values,
          then ``rescale_discrete_levels=True`` decreases the lower
          limit of the autoranged span so that the values are rendering
          towards the (more visible) top of the ``cmap`` range, thus
          avoiding washout of the lower values.  Has no effect if
          ``how!=`eq_hist``. Default is False.
      """
      if not isinstance(agg, xr.DataArray):
          raise TypeError("agg must be instance of DataArray")
      name = agg.name if name is None else name
  
      if not ((0 <= min_alpha <= 255) and (0 <= alpha <= 255)):
          raise ValueError(
              f"min_alpha ({min_alpha}) and alpha ({alpha}) must be between 0 and 255"
          )
  
      if rescale_discrete_levels and how != "eq_hist":
          rescale_discrete_levels = False
  
      if agg.ndim == 2:
          if color_key is not None and isinstance(color_key, dict):
              return _apply_discrete_colorkey(
                  agg, color_key, alpha, name, color_baseline
              )
          else:
              return _interpolate(
                  agg,
                  cmap,
                  how,
                  alpha,
                  span,
                  min_alpha,
                  name,
                  rescale_discrete_levels,
              )
      elif agg.ndim == 3:
          return _colorize(
              agg,
              color_key,
              how,
              alpha,
              span,
              min_alpha,
              name,
              color_baseline,
              rescale_discrete_levels,
          )
      else:
          raise ValueError("agg must use 2D or 3D coordinates")
  
imports: from __future__ import annotations
  from collections.abc import Iterator
  from io import BytesIO
  import warnings
  import numpy
  import numba
  import toolz
  import xarray
  import dask.array
  from PIL.Image import fromarray
  from datashader.colors import rgb, Sets1to3
  from datashader.utils import nansum_missing, ngjit
label_desc: Use image node_id 9 for calling a global function with example usage: shade(agg, cmap, color_key, how, alpha, min_alpha, span, name, color_baseline, rescale_discrete_levels) and returns: _apply_discrete_colorkey, _interpolate, _colorize