code: def reconstruct_trajectories(
      particles, parent_indices, particles_dim=0, name=None
  ):
      """Reconstructs the ancestor trajectory that generated each final particle."""
      with tf.name_scope(name or "reconstruct_trajectories"):
          # Walk backwards to compute the ancestor of each final particle at time t.
          final_indices = smc_kernel._dummy_indices_like(
              parent_indices[-1]
          )  # pylint: disable=protected-access
          ancestor_indices = tf.scan(
              fn=lambda ancestor, parent: mcmc_util.index_remapping_gather(  # pylint: disable=g-long-lambda
                  parent,
                  ancestor,
                  axis=particles_dim,
                  indices_axis=particles_dim,
              ),
              elems=parent_indices[1:],
              initializer=final_indices,
              reverse=True,
          )
          ancestor_indices = tf.concat(
              [ancestor_indices, [final_indices]], axis=0
          )
  
      return tf.nest.map_structure(
          lambda part: mcmc_util.index_remapping_gather(  # pylint: disable=g-long-lambda
              part,
              ancestor_indices,
              axis=particles_dim + 1,
              indices_axis=particles_dim + 1,
          ),
          particles,
      )
  
file_path: probability/tensorflow_probability/python/experimental/mcmc/particle_filter.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.experimental.mcmc import sequential_monte_carlo_kernel
  from tensorflow_probability.python.experimental.mcmc import weighted_resampling
  from tensorflow_probability.python.internal import assert_util
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import docstring_util
  from tensorflow_probability.python.internal import loop_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.mcmc.internal import util
label_desc: Use image node_id 8 for calling a global function with example usage: reconstruct_trajectories(particles, parent_indices, particles_dim, name) and returns: tf