code: def impose_bounds(bounds, index=None, clip=True, nearest=True):
      """generate a function where bounds=[min,max] on a sequence are imposed
  
      Examples:
        >>> sequence = [0.123, 1.244, -4.755, 10.731, 6.207]
        >>>
        >>> @impose_bounds((0,5))
        ... def simple(x):
        ...   return x
        ...
        >>> simple(sequence)
        [0.123, 1.244, 0.0, 5.0, 5.0]
        >>>
        >>> @impose_bounds((0,5), index=(0,2,4))
        ... def double(x):
        ...   return [i*2 for i in x]
        ...
        >>> double(sequence)
        [0.246, 2.488, 0.0, 21.462, 10.0]
        >>>
        >>> @impose_bounds((0,5), index=(0,2,4), clip=False)
        ... def square(x):
        ...   return [i*i for i in x]
        ...
        >>> square(sequence)
        [0.015129, 1.547536, 14.675791119810688, 115.154361, 1.399551896073788]
        >>>
        >>> @impose_bounds([(0,5),(7,10)])
        ... def simple(x):
        ...   return x
        ...
        >>> simple(sequence)
        [0.123, 1.244, 0.0, 10.0, 7.0]
        >>>
        >>> @impose_bounds([(0,5),(7,10)], nearest=False)
        ... def simple(x):
        ...   return x
        ...
        >>> simple(sequence)
        [0.123, 1.244, 0.0, 5.0, 5.0]
        >>> simple(sequence)
        [0.123, 1.244, 7.0, 10.0, 5.0]
        >>>
        >>> @impose_bounds({0:(0,5), 2:(0,5), 4:[(0,5),(7,10)]})
        ... def simple(x):
        ...   return x
        ...
        >>> simple(sequence)
        [0.123, 1.244, 0.0, 10.731, 7.0]
        >>>
        >>> @impose_bounds({0:(0,5), 2:(0,5), 4:[(0,5),(7,10)]}, index=(0,2))
        ... def simple(x):
        ...   return x
        ...
        >>> simple(sequence)
        [0.123, 1.244, 0.0, 10.731, 6.207]
      """
      ### bounds is a dict, index filters the dict
      ### keys of bounds are the index, if index=None apply to all
      ### *but* if bounds is given as list (of tuples), apply to seleted index
      if isinstance(index, int):
          index = (index,)
      # bounds are list, index is tuple => {i:bounds} for each i in index
      if type(bounds) is not dict:
          if index is not None:
              bounds = dict((i, bounds) for i in index)
              index = None
          # bounds are list, index is None => {None:bounds} apply to all index
          else:
              bounds = {None: bounds}
      # bounds are dict, index is None => do nothing -- is the preferred input
      # bounds are dict, index is tuple => filter bounds, then as above
      else:
          if (
              None in bounds and len(bounds) > 1
          ):  # has either entries or None
              msg = (
                  "bounds got multiple entries for '%d'"
                  % set(bounds).difference((None,)).pop()
              )
              raise TypeError(msg)
          if index is not None:
              if (
                  None in index and len(index) > 1
              ):  # has either entries or None
                  msg = (
                      "index got multiple entries for '%d'"
                      % set(index).difference((None,)).pop()
                  )
                  raise TypeError(msg)
              if None in bounds:
                  bounds = dict((i, bounds[None]) for i in index)
              else:
                  index = set(index).intersection(bounds)
                  bounds = dict((i, bounds[i]) for i in index)
              index = None
  
      clip = [clip]
      nearest = [nearest]
  
      def _clip(clipped=True):
          clip[0] = clipped
  
      def _near(nearer=True):
          nearest[0] = nearer
  
      def dec(f):
          def func(x, *args, **kwds):
              if isinstance(x, ndarray):
                  xtype = asarray
              else:
                  xtype = type(x)
              xp = x
              for i in bounds:
                  xp = bounded(xp, bounds[i], i, clip[0], nearest[0])
              return f(xtype(xp), *args, **kwds)
  
          func.clip = _clip
          func.nearest = _near
          func.__bounds__ = bounds
          func.__wrapped__ = f
          func.__doc__ = f.__doc__
          return func
  
      return dec
  
imports: from mystic.math.measures import *
  from mystic.math import almostEqual
  from numpy import sum
  from numpy import asfarray, asarray, choose, zeros, ones, ndarray
  from numpy import shape, broadcast, empty, atleast_1d
  from numpy import round, abs
  from random import randrange, shuffle
  from numpy import array, intersect1d, inf, isnan, where, choose, clip
  from numpy.random import uniform, choice
  from numpy import maximum, minimum
  from numpy import round, abs
  from mystic.tools import synchronized, connected
  from mystic.math.discrete import product_measure
label_desc: Use image node_id 23 for calling a global function with example usage: impose_bounds(bounds, index, clip, nearest) and returns: dec, func, f