code: def coarsen(reduction, x, axes, trim_excess=False, **kwargs):
      """Coarsen array by applying reduction to fixed size neighborhoods
  
      Parameters
      ----------
      reduction: function
          Function like np.sum, np.mean, etc...
      x: np.ndarray
          Array to be coarsened
      axes: dict
          Mapping of axis to coarsening factor
  
      Examples
      --------
      >>> x = np.array([1, 2, 3, 4, 5, 6])
      >>> coarsen(np.sum, x, {0: 2})
      array([ 3,  7, 11])
      >>> coarsen(np.max, x, {0: 3})
      array([3, 6])
  
      Provide dictionary of scale per dimension
  
      >>> x = np.arange(24).reshape((4, 6))
      >>> x
      array([[ 0,  1,  2,  3,  4,  5],
             [ 6,  7,  8,  9, 10, 11],
             [12, 13, 14, 15, 16, 17],
             [18, 19, 20, 21, 22, 23]])
  
      >>> coarsen(np.min, x, {0: 2, 1: 3})
      array([[ 0,  3],
             [12, 15]])
  
      You must avoid excess elements explicitly
  
      >>> x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
      >>> coarsen(np.min, x, {0: 3}, trim_excess=True)
      array([1, 4])
      """
      # Insert singleton dimensions if they don't exist already
      for i in range(x.ndim):
          if i not in axes:
              axes[i] = 1
  
      if trim_excess:
          ind = tuple(
              slice(0, -(d % axes[i]))
              if d % axes[i]
              else slice(None, None)
              for i, d in enumerate(x.shape)
          )
          x = x[ind]
  
      # (10, 10) -> (5, 2, 5, 2)
      newshape = tuple(
          concat(
              [(x.shape[i] // axes[i], axes[i]) for i in range(x.ndim)]
          )
      )
  
      return reduction(
          x.reshape(newshape),
          axis=tuple(range(1, x.ndim * 2, 2)),
          **kwargs,
      )
  
imports: from __future__ import annotations
  import contextlib
  from collections.abc import Container, Iterable, Sequence
  from functools import wraps
  from numbers import Integral
  import numpy
  from tlz import concat
  from dask.core import flatten
label_desc: Use image node_id 2 for calling a global function with example usage: coarsen(reduction, x, axes, trim_excess) and returns: reduction