nodes: 0, module; 1, function_definition; 2, function_name:bound_weights; 3, parameters; 4, block; 5, identifier:weights; 6, default_parameter; 7, default_parameter; 8, expression_statement; 9, comment:# Copy weights to avoid side-effects; 10, expression_statement; 11, comment:# Remove weights outside of minimum and maximum; 12, if_statement; 13, comment:# If weights were removed, attach new endpoints where they would have; 14, comment:# appeared in the original curve; 15, if_statement; 16, if_statement; 17, return_statement; 18, identifier:minimum; 19, None; 20, identifier:maximum; 21, None; 22, comment:"""
      Bound a weight list so that all outcomes fit within specified bounds.
  
      The probability distribution within the ``minimum`` and ``maximum``
      values remains the same. Weights in the list with outcomes outside of
      ``minimum`` and ``maximum`` are removed.
      If weights are removed from either end, attach weights at the modified
      edges at the same weight (y-axis) position they had interpolated in the
      original list.
  
      If neither ``minimum`` nor ``maximum`` are set, ``weights`` will be
      returned unmodified. If both are set, ``minimum`` must be less
      than ``maximum``.
  
      Args:
          weights (list): the list of weights where each weight
              is a ``tuple`` of form ``(float, float)`` corresponding to
              ``(outcome, weight)``. Must be sorted in increasing order
              of outcomes
          minimum (float): Lowest allowed outcome for the weight list
          maximum (float): Highest allowed outcome for the weight list
  
      Returns:
          list: A list of 2-tuples of form ``(float, float)``,
          the bounded weight list.
  
      Raises:
          ValueError: if ``maximum < minimum``
  
      Example:
          >>> weights = [(0, 0), (2, 2), (4, 0)]
          >>> bound_weights(weights, 1, 3)
          [(1, 1), (2, 2), (3, 1)]
      """; 23, assignment; 24, boolean_operator; 25, block; 26, elif_clause; 27, elif_clause; 28, else_clause; 29, parenthesized_expression; 30, block; 31, parenthesized_expression; 32, block; 33, identifier:bounded_weights; 34, identifier:bounded_weights; 35, subscript; 36, comparison_operator:minimum is not None; 37, comparison_operator:maximum is not None; 38, if_statement; 39, expression_statement; 40, comparison_operator:minimum is not None; 41, block; 42, comparison_operator:maximum is not None; 43, block; 44, comment:# Both minimum and maximum are None - the bound list is the same; 45, comment:# as the original; 46, block; 47, boolean_operator; 48, expression_statement; 49, boolean_operator; 50, expression_statement; 51, identifier:weights; 52, slice; 53, identifier:minimum; 54, None; 55, identifier:maximum; 56, None; 57, comparison_operator:maximum < minimum; 58, block; 59, assignment; 60, identifier:minimum; 61, None; 62, expression_statement; 63, identifier:maximum; 64, None; 65, expression_statement; 66, return_statement; 67, comparison_operator:bounded_weights[0][0] > weights[0][0]; 68, comparison_operator:bounded_weights[0][0] != minimum; 69, call; 70, comparison_operator:bounded_weights[-1][0] < weights[-1][0]; 71, comparison_operator:bounded_weights[-1][0] != maximum; 72, call; 73, identifier:maximum; 74, identifier:minimum; 75, raise_statement; 76, identifier:bounded_weights; 77, list_comprehension; 78, assignment; 79, assignment; 80, identifier:bounded_weights; 81, subscript; 82, subscript; 83, subscript; 84, identifier:minimum; 85, attribute; 86, argument_list; 87, subscript; 88, subscript; 89, subscript; 90, identifier:maximum; 91, attribute; 92, argument_list; 93, identifier:ValueError; 94, identifier:bw; 95, for_in_clause; 96, if_clause; 97, identifier:bounded_weights; 98, list_comprehension; 99, identifier:bounded_weights; 100, list_comprehension; 101, subscript; 102, integer:0; 103, subscript; 104, integer:0; 105, subscript; 106, integer:0; 107, identifier:bounded_weights; 108, identifier:insert; 109, integer:0; 110, tuple; 111, subscript; 112, integer:0; 113, subscript; 114, integer:0; 115, subscript; 116, integer:0; 117, identifier:bounded_weights; 118, identifier:append; 119, tuple; 120, identifier:bw; 121, identifier:bounded_weights; 122, comparison_operator:minimum <= bw[0] <= maximum; 123, identifier:bw; 124, for_in_clause; 125, if_clause; 126, identifier:bw; 127, for_in_clause; 128, if_clause; 129, identifier:bounded_weights; 130, integer:0; 131, identifier:weights; 132, integer:0; 133, identifier:bounded_weights; 134, integer:0; 135, identifier:minimum; 136, call; 137, identifier:bounded_weights; 138, unary_operator; 139, identifier:weights; 140, unary_operator; 141, identifier:bounded_weights; 142, unary_operator; 143, identifier:maximum; 144, call; 145, identifier:minimum; 146, subscript; 147, identifier:maximum; 148, identifier:bw; 149, identifier:bounded_weights; 150, comparison_operator:minimum <= bw[0]; 151, identifier:bw; 152, identifier:bounded_weights; 153, comparison_operator:bw[0] <= maximum; 154, identifier:_linear_interp; 155, argument_list; 156, integer:1; 157, integer:1; 158, integer:1; 159, identifier:_linear_interp; 160, argument_list; 161, identifier:bw; 162, integer:0; 163, identifier:minimum; 164, subscript; 165, subscript; 166, identifier:maximum; 167, identifier:weights; 168, identifier:minimum; 169, identifier:weights; 170, identifier:maximum; 171, identifier:bw; 172, integer:0; 173, identifier:bw; 174, integer:0
code: def bound_weights(weights, minimum=None, maximum=None):
      """
      Bound a weight list so that all outcomes fit within specified bounds.
  
      The probability distribution within the ``minimum`` and ``maximum``
      values remains the same. Weights in the list with outcomes outside of
      ``minimum`` and ``maximum`` are removed.
      If weights are removed from either end, attach weights at the modified
      edges at the same weight (y-axis) position they had interpolated in the
      original list.
  
      If neither ``minimum`` nor ``maximum`` are set, ``weights`` will be
      returned unmodified. If both are set, ``minimum`` must be less
      than ``maximum``.
  
      Args:
          weights (list): the list of weights where each weight
              is a ``tuple`` of form ``(float, float)`` corresponding to
              ``(outcome, weight)``. Must be sorted in increasing order
              of outcomes
          minimum (float): Lowest allowed outcome for the weight list
          maximum (float): Highest allowed outcome for the weight list
  
      Returns:
          list: A list of 2-tuples of form ``(float, float)``,
          the bounded weight list.
  
      Raises:
          ValueError: if ``maximum < minimum``
  
      Example:
          >>> weights = [(0, 0), (2, 2), (4, 0)]
          >>> bound_weights(weights, 1, 3)
          [(1, 1), (2, 2), (3, 1)]
      """
      # Copy weights to avoid side-effects
      bounded_weights = weights[:]
      # Remove weights outside of minimum and maximum
      if minimum is not None and maximum is not None:
          if maximum < minimum:
              raise ValueError
          bounded_weights = [bw for bw in bounded_weights
                             if minimum <= bw[0] <= maximum]
      elif minimum is not None:
          bounded_weights = [bw for bw in bounded_weights
                             if minimum <= bw[0]]
      elif maximum is not None:
          bounded_weights = [bw for bw in bounded_weights
                             if bw[0] <= maximum]
      else:
          # Both minimum and maximum are None - the bound list is the same
          # as the original
          return bounded_weights
      # If weights were removed, attach new endpoints where they would have
      # appeared in the original curve
      if (bounded_weights[0][0] > weights[0][0] and
              bounded_weights[0][0] != minimum):
          bounded_weights.insert(0, (minimum, _linear_interp(weights, minimum)))
      if (bounded_weights[-1][0] < weights[-1][0] and
              bounded_weights[-1][0] != maximum):
          bounded_weights.append((maximum, _linear_interp(weights, maximum)))
      return bounded_weights