code: def sample_policy(
      q_pi,
      policies,
      num_controls,
      action_selection="deterministic",
      alpha=16.0,
  ):
      """
      Samples a policy from the posterior over policies, taking the action (per control factor) entailed by the first timestep of the selected policy.
  
      Parameters
      ----------
      q_pi: 1D ``numpy.ndarray``
          Posterior beliefs over policies, i.e. a vector containing one posterior probability per policy.
      policies: ``list`` of 2D ``numpy.ndarray``
          ``list`` that stores each policy as a 2D array in ``policies[p_idx]``. Shape of ``policies[p_idx]``
          is ``(num_timesteps, num_factors)`` where ``num_timesteps`` is the temporal
          depth of the policy and ``num_factors`` is the number of control factors.
      num_controls: ``list`` of ``int``
          ``list`` of the dimensionalities of each control state factor.
      action_selection: string, default "deterministic"
          String indicating whether whether the selected policy is chosen as the maximum of the posterior over policies,
          or whether it's sampled from the posterior over policies.
      alpha: float, default 16.0
          Action selection precision -- the inverse temperature of the softmax that is used to scale the
          policy posterior before sampling. This is only used if ``action_selection`` argument is "stochastic"
  
      Returns
      ----------
      selected_policy: 1D ``numpy.ndarray``
          Vector containing the indices of the actions for each control factor
      """
  
      num_factors = len(num_controls)
  
      if action_selection == "deterministic":
          policy_idx = select_highest(q_pi)
      elif action_selection == "stochastic":
          log_qpi = spm_log_single(q_pi)
          p_policies = softmax(log_qpi * alpha)
          policy_idx = utils.sample(p_policies)
  
      selected_policy = np.zeros(num_factors)
      for factor_i in range(num_factors):
          selected_policy[factor_i] = policies[policy_idx][0, factor_i]
  
      return selected_policy
  
imports: import itertools
  import numpy
  from pymdp.maths import softmax, softmax_obj_arr, spm_dot, spm_wnorm, spm_MDP_G, spm_log_single, spm_log_obj_array
  from pymdp import utils
  import copy
label_desc: Use image node_id 13 for calling a global function with example usage: sample_policy(q_pi, policies, num_controls, action_selection, alpha) and returns: selected_policy