code: def reservoir_sample(iterable, k, random=None):
      """Samples k elements with uniform probability from an iterable.
  
      Selects a subset of k elements from n input elements with uniform probability
      without needing to hold all n elements in memory at the same time. This
      implementation has max space complexity O(min(k, n)), i.e., we allocate up to
      min(k, n) elements to store the samples. This means that we only use ~n
      elements when n is smaller than k, which can be important when k is large. If
      n elements are added to this sampler, and n <= k, all n elements will be
      retained. If n > k, each added element will be retained with a uniform
      probability of k / n.
  
      The order of the k retained samples from our n elements is undefined. In
      particular that means that the elements in the returned list can occur in a
      different order than they appeared in the iterable.
  
      More details about reservoir sampling (and the specific algorithm used here
      called Algorithm R) can be found on wikipedia:
  
      https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_R
  
      Args:
        iterable: Python iterable. The iterable to sample from.
        k: int. The number of elements to sample.
        random: A random number generator or None.
  
      Returns:
        A list containing the k sampled elements.
  
      Raises:
        ValueError: If k is negative.
      """
      if k < 0:
          raise ValueError(
              "k must be nonnegative, but got {}".format(k)
          )
      if random is None:
          random = np.random
      sample = []
      for i, item in enumerate(iterable):
          if len(sample) < k:
              sample.append(item)
          else:
              j = random.randint(0, i + 1)
              if j < k:
                  sample[j] = item
      return sample
  
file_path: deepvariant/third_party/nucleus/util/utils.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import numpy
  from third_party.nucleus.protos import range_pb2
  from third_party.nucleus.util.python import utils
label_desc: Use image node_id 4 for calling a global function with example usage: reservoir_sample(iterable, k, random) and returns: sample