code: def to_transition(
      trajectory: Trajectory,
      next_trajectory: Optional[Trajectory] = None,
  ) -> Transition:
      """Create a transition from a trajectory or two adjacent trajectories.
  
      **NOTE** If `next_trajectory` is not provided, tensors of `trajectory` are
      sliced along their *second* (`time`) dimension; for example:
  
      ```
      time_steps.step_type = trajectory.step_type[:,:-1]
      time_steps.observation = trajectory.observation[:,:-1]
      next_time_steps.observation = trajectory.observation[:,1:]
      next_time_steps. step_type = trajectory. next_step_type[:,:-1]
      next_time_steps.reward = trajectory.reward[:,:-1]
      next_time_steps. discount = trajectory. discount[:,:-1]
  
      ```
      Notice that reward and discount for time_steps are undefined, therefore filled
      with zero.
  
      Args:
        trajectory: An instance of `Trajectory`. The tensors in Trajectory must have
          shape `[B, T, ...]` when next_trajectory is `None`.  `discount` is assumed
          to be a scalar float; hence the shape of `trajectory.discount` must be
          `[B, T]`.
        next_trajectory: (optional) An instance of `Trajectory`.
  
      Returns:
        A tuple `(time_steps, policy_steps, next_time_steps)`.  The `reward` and
        `discount` fields of `time_steps` are filled with zeros because these
        cannot be deduced (please do not use them).
  
      Raises:
        ValueError: if `discount` rank is not within the range [1, 2].
      """
      _validate_rank(trajectory.discount, min_rank=1, max_rank=2)
  
      if next_trajectory is not None:
          _validate_rank(
              next_trajectory.discount, min_rank=1, max_rank=2
          )
  
      if next_trajectory is None:
          next_trajectory = tf.nest.map_structure(
              lambda t: composite.slice_from(t, axis=1, start=1),
              trajectory,
          )
          trajectory = tf.nest.map_structure(
              lambda t: composite.slice_to(t, axis=1, end=-1),
              trajectory,
          )
      policy_steps = policy_step.PolicyStep(
          action=trajectory.action,
          state=(),
          info=trajectory.policy_info,
      )
      # TODO(b/130244652): Consider replacing 0 rewards & discounts with ().
      time_steps = ts.TimeStep(
          trajectory.step_type,
          reward=tf.nest.map_structure(
              tf.zeros_like, trajectory.reward
          ),  # unknown
          discount=tf.zeros_like(trajectory.discount),  # unknown
          observation=trajectory.observation,
      )
      next_time_steps = ts.TimeStep(
          step_type=trajectory.next_step_type,
          reward=trajectory.reward,
          discount=trajectory.discount,
          observation=next_trajectory.observation,
      )
      return Transition(time_steps, policy_steps, next_time_steps)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  import pprint
  from typing import NamedTuple, Optional
  import numpy
  import tensorflow
  from tf_agents.trajectories import policy_step
  from tf_agents.trajectories import time_step
  from tf_agents.typing import types
  from tf_agents.utils import composite
  from tf_agents.utils import nest_utils
  from tf_agents.utils import value_ops
label_desc: Use image node_id 10 for calling a global function with example usage: to_transition(trajectory, next_trajectory) and returns: Transition