code: def idxs_map(idxs, cmd, *args, **kwargs):
      """
      Return the cmd applied at positions idxs, by retrieving args and kwargs
      from the (idxs, vals) pair elements of `args` and `kwargs`.
  
      N.B. args and kwargs may generally include information for more idx values
      than are requested by idxs.
      """
      # XXX: consider insisting on sorted idxs
      # XXX: use np.searchsorted instead of dct
  
      if 0:  # these should all be true, but evaluating them is slow
          for ii, (idxs_ii, vals_ii) in enumerate(args):
              for jj in idxs:
                  assert jj in idxs_ii
          for kw, (idxs_kw, vals_kw) in list(kwargs.items()):
              for jj in idxs:
                  assert jj in idxs_kw
  
      args_imap = []
      for idxs_j, vals_j in args:
          if len(idxs_j):
              args_imap.append(dict(list(zip(idxs_j, vals_j))))
          else:
              args_imap.append({})
  
      kwargs_imap = {}
      for kw, (idxs_j, vals_j) in list(kwargs.items()):
          if len(idxs_j):
              kwargs_imap[kw] = dict(list(zip(idxs_j, vals_j)))
          else:
              kwargs_imap[kw] = {}
  
      f = scope._impls[cmd]
      rval = []
      for ii in idxs:
          try:
              args_nn = [arg_imap[ii] for arg_imap in args_imap]
          except:
              ERR("args_nn %s" % cmd)
              ERR("ii %s" % ii)
              ERR("arg_imap %s" % str(args_imap))
              ERR("args_imap %s" % str(args_imap))
              raise
          try:
              kwargs_nn = dict(
                  [
                      (kw, arg_imap[ii])
                      for kw, arg_imap in list(kwargs_imap.items())
                  ]
              )
          except:
              ERR("args_nn %s" % cmd)
              ERR("ii %s" % ii)
              ERR("kw %s" % kw)
              ERR("arg_imap %s" % str(args_imap))
              raise
          try:
              rval_nn = f(*args_nn, **kwargs_nn)
          except:
              ERR("error calling impl of %s" % cmd)
              raise
          rval.append(rval_nn)
      return rval
  
imports: from __future__ import print_function
  from __future__ import absolute_import
  from builtins import str
  from builtins import zip
  from builtins import range
  from builtins import object
  import sys
  import numpy
  from .pyll import Apply
  from .pyll import as_apply
  from .pyll import dfs
  from .pyll import toposort
  from .pyll import scope
  from .pyll import stochastic
label_desc: Use image node_id 4 for calling a global function with example usage: idxs_map(idxs, cmd) and returns: rval