code: def _samples_from_counts(values, counts, total_number):
      """Construct sequences of values from tabulated numbers of counts."""
  
      extended_result_shape = ps.concat(
          [ps.shape(counts)[:-1], [total_number + 1]], axis=0
      )
      padded_counts = ps.concat(
          [ps.zeros_like(counts[..., :1]), counts[..., :-1]], axis=-1
      )
      edge_positions = ps.cumsum(padded_counts, axis=-1)
  
      # We need to scatter `values` into an array according to
      # the given `counts`.
      # Because the final result typically consists of sequences of samples
      # that are constant in blocks, we can scatter just the finite
      # differences of the values (which become the 'edges' of the blocks)
      # and then cumulatively sum them back up
      # at the end. (Reminiscent of run length encoding.)
      # Eg. suppose we have values = `[0, 2, 1]`
      # and counts = `[2, 3, 4]`
      # Then the output we require is `[0, 0, 2, 2, 2, 1, 1, 1, 1]`.
      # The finite differences of the input are:
      #   `[0, 2, -1]`.
      # The finite differences of the output are:
      #   `[0, 0, 2, 0, 0, -1, 0, 0, 0]`.
      # The latter is the former scattered into a larger array.
      #
      # So the algorithm is essentially
      # compute finite differences -> scatter -> undo finite differences
      edge_heights = _finite_differences(values)
      edges = _scatter_nd_batch(
          edge_positions[..., tf.newaxis],
          edge_heights,
          extended_result_shape,
          batch_dims=ps.rank_from_shape(ps.shape(counts)) - 1,
      )
  
      result = tf.cumsum(edges, axis=-1)[..., :-1]
      return result
  
file_path: probability/tensorflow_probability/python/experimental/mcmc/weighted_resampling.py
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.distributions import exponential
  from tensorflow_probability.python.distributions import uniform
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.math.gradient import value_and_gradient
  from tensorflow_probability.python.mcmc.internal import util
label_desc: Use image node_id 8 for calling a global function with example usage: _samples_from_counts(values, counts, total_number) and returns: result