code: def interpolate(
      i, points, fwd_possible, rwd_possible, contourpath=None
  ):
      """Interpolate a point on the path indicated by `points`.
  
      Given a sparsely sampled track (stored in .points),
      potentially encountering reflections,
      extract the corrdinates of the point with index `i`.
      That point may not have been evaluated yet.
  
      Parameters
      -----------
      i: int
          position on track to return.
      points: list of tuples (index, coordinate, direction, loglike)
          points on the path
      fwd_possible: bool
          whether the path could be extended in the positive direction.
      rwd_possible: bool
          whether the path could be extended in the negative direction.
      contourpath: ContourPath
          Use region to reflect. Not used at the moment.
  
      """
      points_before = [
          (j, xj, vj, Lj) for j, xj, vj, Lj in points if j <= i
      ]
      points_after = [
          (j, xj, vj, Lj) for j, xj, vj, Lj in points if j >= i
      ]
  
      # check if the point after is really after i
      if len(points_after) == 0 and not fwd_possible:
          # the path cannot continue, and i does not exist.
          # print("    interpolate_point %d: the path cannot continue fwd, and i does not exist." % i)
          j, xj, vj, Lj = max(points_before)
          return xj, vj, Lj, False
  
      # check if the point before is really before i
      if len(points_before) == 0 and not rwd_possible:
          # the path cannot continue, and i does not exist.
          k, xk, vk, Lk = min(points_after)
          # print("    interpolate_point %d: the path cannot continue rwd, and i does not exist." % i)
          return xk, vk, Lk, False
  
      if len(points_before) == 0 or len(points_after) == 0:
          # return None, None, None, False
          raise KeyError("cannot extrapolate outside path")
  
      j, xj, vj, Lj = max(points_before)
      k, xk, vk, Lk = min(points_after)
  
      # print("    interpolate_point %d between %d-%d" % (i, j, k))
      if j == i:  # we have this exact point in the chain
          return xj, vj, Lj, True
  
      assert not k == i  # otherwise the above would be true too
  
      # expand_to_step explores each reflection in detail, so
      # any points with change in v should have j == i
      # therefore we can assume:
      # assert (vj == vk).all()
      # this ^ is not true, because reflections on the unit cube can
      # occur, and change v without requiring a intermediate point.
  
      # j....i....k
      xl1, vj1 = extrapolate_ahead(
          i - j, xj, vj, contourpath=contourpath
      )
      xl2, vj2 = extrapolate_ahead(
          i - k, xk, vk, contourpath=contourpath
      )
      assert np.allclose(xl1, xl2), (xl1, xl2, i, j, k, xj, vj, xk, vk)
      assert np.allclose(vj1, vj2), (
          xl1,
          vj1,
          xl2,
          vj2,
          i,
          j,
          k,
          xj,
          vj,
          xk,
          vk,
      )
      xl = xl1
  
      # xl = interpolate_between_two_points(i, xj, j, xk, k)
      # the new point is then just a linear interpolation
      # w = (i - k) * 1. / (j - k)
      # xl = xj * w + (1 - w) * xk
  
      return xl, vj, None, True
  
imports: import numpy
  from numpy.linalg import norm
  import matplotlib.pyplot
label_desc: Use image node_id 11 for calling a global function with example usage: interpolate(i, points, fwd_possible, rwd_possible, contourpath) and returns: xl,  vj,  None,  True, xj,  vj,  Lj,  False, xk,  vk,  Lk,  False, xj,  vj,  Lj,  True