class_docstr: A sum tree data structure for storing replay priorities.
  A sum tree is a complete binary tree whose leaves contain values called
  priorities. Internal nodes maintain the sum of the priorities of all leaf
  nodes in their subtree.
  For capacity = 4, the tree may look like this:
               +---+
               |2.5|
               +-+-+
                 |
         +-------+--------+
         |                |
       +-+-+            +-+-+
       |1.5|            |1.0|
       +-+-+            +-+-+
         |                |
    +----+----+      +----+----+
    |         |      |         |
  +-+-+     +-+-+  +-+-+     +-+-+
  |0.5|     |1.0|  |0.5|     |0.5|
  +---+     +---+  +---+     +---+
  This is stored in a list of numpy arrays:
  self.nodes = [ [2.5], [1.5, 1], [0.5, 1, 0.5, 0.5] ]
  For conciseness, we allocate arrays as powers of two, and pad the excess
  elements with zero values.
  This is similar to the usual array-based representation of a complete binary
  tree, but is a little more user-friendly.
class_docstr_tok: ["A","sum","tree","data","structure","for","storing","replay","priorities",".","A","sum","tree","is","a","complete","binary","tree","whose","leaves","contain","values","called","priorities",".","Internal","nodes","maintain","the","sum","of","the","priorities","of","all","leaf","nodes","in","their","subtree",".","For","capacity","=","4",",","the","tree","may","look","like","this",":","+","--","-+","|2.5|","+-+-+","|","+","--","--","--","-+","--","--","--","--","+","|","|","+-+-+","+-+-+","|1.5|","|1.0|","+-+-+","+-+-+","|","|","+","--","--","+","--","--","+","+","--","--","+","--","--","+","|","|","|","|","+-+-+","+-+-+","+-+-+","+-+-+","|0.5|","|1.0|","|0.5|","|0.5|","+","--","-+","+","--","-+","+","--","-+","+","--","-+","This","is","stored","in","a","list","of","numpy","arrays",":","self.nodes","=","[","[","2.5","]",",","[","1.5",",","1","]",",","[","0.5",",","1",",","0.5",",","0.5","]","]","For","conciseness",",","we","allocate","arrays","as","powers","of","two",",","and","pad","the","excess","elements","with","zero","values",".","This","is","similar","to","the","usual","array-based","representation","of","a","complete","binary","tree",",","but","is","a","little","more","user-friendly","."]
docstr: Performs stratified sampling using the sum tree.
  Let R be the value at the root (total value of sum tree). This method will
  divide [0, R) into batch_size segments, pick a random number from each of
  those segments, and use that random number to sample from the sum_tree. This
  is as specified in Schaul et al. (2015).
  Args:
    batch_size: int, the number of strata to use.
  Returns:
    list of batch_size elements sampled from the sum tree.
  Raises:
    Exception: If the sum tree is empty (i.e. its node values sum to 0).
docstr_tok: ["Performs","stratified","sampling","using","the","sum","tree",".","Let","R","be","the","value","at","the","root","(","total","value","of","sum","tree",")",".","This","method","will","divide","[","0",",","R",")","into","batch_size","segments",",","pick","a","random","number","from","each","of","those","segments",",","and","use","that","random","number","to","sample","from","the","sum_tree",".","This","is","as","specified","in","Schaul","et","al",".","(","2015",")",".","Args",":","batch_size",":","int",",","the","number","of","strata","to","use",".","Returns",":","list","of","batch_size","elements","sampled","from","the","sum","tree",".","Raises",":","Exception",":","If","the","sum","tree","is","empty","(","i.e",".","its","node","values","sum","to","0",")","."]
code: def stratified_sample(self, batch_size: int) -> List[int]:
      """Performs stratified sampling using the sum tree.
      Let R be the value at the root (total value of sum tree). This method will
      divide [0, R) into batch_size segments, pick a random number from each of
      those segments, and use that random number to sample from the sum_tree. This
      is as specified in Schaul et al. (2015).
      Args:
        batch_size: int, the number of strata to use.
      Returns:
        list of batch_size elements sampled from the sum tree.
      Raises:
        Exception: If the sum tree is empty (i.e. its node values sum to 0).
      """
      if self._total_priority() == 0.0:
          raise Exception("Cannot sample from an empty sum tree.")
  
      bounds = np.linspace(0.0, 1.0, batch_size + 1)
      assert len(bounds) == batch_size + 1
      segments = [(bounds[i], bounds[i + 1]) for i in range(batch_size)]
      query_values = [random.uniform(x[0], x[1]) for x in segments]
      return [self.sample(query_value=x) for x in query_values]
  
file_path: ReAgent/reagent/replay_memory/sum_tree.py
imports: import math
  import random
  from typing import List, Optional
  import numpy
label_desc: Use image node_id 4 for calling the SumTree obj's underlying member method code with example usage: obj.stratified_sample(batch_size) and returns: unknown