code: def _sample_action_test(
      q_pi,
      policies,
      num_controls,
      action_selection="deterministic",
      alpha=16.0,
      seed=None,
  ):
      """
      Computes the marginal posterior over actions and then samples an action from it, one action per control factor.
      Internal testing version that returns the marginal posterior over actions, and also has a seed argument for reproducibility.
  
      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: ``str``, default "deterministic"
          String indicating whether whether the selected action is chosen as the maximum of the posterior over actions,
          or whether it's sampled from the posterior marginal over actions
      alpha: float, default 16.0
          Action selection precision -- the inverse temperature of the softmax that is used to scale the
          action marginals before sampling. This is only used if ``action_selection`` argument is "stochastic"
      seed: ``int``, default None
          The seed can be set to control the random sampling that occurs when ``action_selection`` is "deterministic" but there are more than one actions with the same maximum posterior probability.
  
  
      Returns
      ----------
      selected_policy: 1D ``numpy.ndarray``
          Vector containing the indices of the actions for each control factor
      p_actions: ``numpy.ndarray`` of dtype object
          Marginal posteriors over actions, after softmaxing and scaling with action precision. This distribution will be used to sample actions,
          if``action_selection`` argument is "stochastic"
      """
  
      num_factors = len(num_controls)
  
      action_marginals = utils.obj_array_zeros(num_controls)
  
      # weight each action according to its integrated posterior probability under all policies at the current timestep
      for pol_idx, policy in enumerate(policies):
          for factor_i, action_i in enumerate(policy[0, :]):
              action_marginals[factor_i][action_i] += q_pi[pol_idx]
  
      action_marginals = utils.norm_dist_obj_arr(action_marginals)
  
      selected_policy = np.zeros(num_factors)
      p_actions = utils.obj_array_zeros(num_controls)
      for factor_i in range(num_factors):
          if action_selection == "deterministic":
              p_actions[factor_i] = action_marginals[factor_i]
              selected_policy[factor_i] = _select_highest_test(
                  p_actions[factor_i], seed=seed
              )
          elif action_selection == "stochastic":
              log_marginal_f = spm_log_single(
                  action_marginals[factor_i]
              )
              p_actions[factor_i] = softmax(log_marginal_f * alpha)
              selected_policy[factor_i] = utils.sample(
                  p_actions[factor_i]
              )
  
      return selected_policy, p_actions
  
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 12 for calling a global function with example usage: _sample_action_test(q_pi, policies, num_controls, action_selection, alpha, seed) and returns: selected_policy,  p_actions