nodes: 0, module; 1, function_definition; 2, function_name:find_valley_range; 3, parameters; 4, block; 5, identifier:vector; 6, default_parameter; 7, expression_statement; 8, comment:# http://users.monash.edu.au/~dengs/resource/papers/icme08.pdf; 9, comment:# find min and max with mode = wrap; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, if_statement; 15, return_statement; 16, identifier:min_distance; 17, integer:4; 18, comment:"""
      Internal finder peaks and valley ranges.
      Returns UNSORTED indices of maxima in input vector.
      Returns range of valleys before and after maximum
      """; 19, assignment; 20, assignment; 21, assignment; 22, comparison_operator:len(maxima)>len(minima); 23, block; 24, comparison_operator:len(maxima)==len(minima); 25, block; 26, else_clause; 27, expression_list; 28, identifier:mode; 29, string:"wrap"; 30, identifier:minima; 31, call; 32, identifier:maxima; 33, call; 34, call; 35, call; 36, if_statement; 37, call; 38, call; 39, expression_statement; 40, if_statement; 41, block; 42, identifier:maxima; 43, identifier:minima; 44, identifier:valley_range; 45, identifier:local_minima; 46, argument_list; 47, identifier:local_maxima; 48, argument_list; 49, identifier:len; 50, argument_list; 51, identifier:len; 52, argument_list; 53, comparison_operator:vector[maxima[0]] >= vector[maxima[-1]]; 54, block; 55, else_clause; 56, identifier:len; 57, argument_list; 58, identifier:len; 59, argument_list; 60, assignment; 61, comparison_operator:minima[0] < maxima[0]; 62, block; 63, else_clause; 64, expression_statement; 65, identifier:vector; 66, identifier:min_distance; 67, identifier:mode; 68, identifier:vector; 69, identifier:min_distance; 70, identifier:mode; 71, identifier:maxima; 72, identifier:minima; 73, subscript; 74, subscript; 75, expression_statement; 76, block; 77, identifier:maxima; 78, identifier:minima; 79, identifier:valley_range; 80, call; 81, subscript; 82, subscript; 83, expression_statement; 84, block; 85, assignment; 86, identifier:vector; 87, subscript; 88, identifier:vector; 89, subscript; 90, assignment; 91, expression_statement; 92, attribute; 93, argument_list; 94, identifier:minima; 95, integer:0; 96, identifier:maxima; 97, integer:0; 98, assignment; 99, expression_statement; 100, identifier:valley_range; 101, call; 102, identifier:maxima; 103, integer:0; 104, identifier:maxima; 105, unary_operator; 106, identifier:maxima; 107, subscript; 108, assignment; 109, identifier:numpy; 110, identifier:asarray; 111, binary_operator:[minima[ii+1] - minima[ii] for ii in range(len(minima)-1)] + [len(vector)-minima[-1]+minima[0]]; 112, identifier:minima; 113, call; 114, assignment; 115, attribute; 116, argument_list; 117, integer:1; 118, identifier:maxima; 119, slice; 120, identifier:maxima; 121, subscript; 122, list_comprehension; 123, list; 124, attribute; 125, argument_list; 126, identifier:minima; 127, call; 128, identifier:numpy; 129, identifier:asarray; 130, list_comprehension; 131, integer:1; 132, identifier:maxima; 133, slice; 134, binary_operator:minima[ii+1] - minima[ii]; 135, for_in_clause; 136, binary_operator:len(vector)-minima[-1]+minima[0]; 137, identifier:numpy; 138, identifier:asarray; 139, binary_operator:list(minima) + [minima[0]]; 140, attribute; 141, argument_list; 142, binary_operator:minima[ii+1] - minima[ii]; 143, for_in_clause; 144, unary_operator; 145, subscript; 146, subscript; 147, identifier:ii; 148, call; 149, binary_operator:len(vector)-minima[-1]; 150, subscript; 151, call; 152, list; 153, identifier:numpy; 154, identifier:asarray; 155, binary_operator:list(minima) + [minima[-1]]; 156, subscript; 157, subscript; 158, identifier:ii; 159, call; 160, integer:1; 161, identifier:minima; 162, binary_operator:ii+1; 163, identifier:minima; 164, identifier:ii; 165, identifier:range; 166, argument_list; 167, call; 168, subscript; 169, identifier:minima; 170, integer:0; 171, identifier:list; 172, argument_list; 173, subscript; 174, call; 175, list; 176, identifier:minima; 177, binary_operator:ii+1; 178, identifier:minima; 179, identifier:ii; 180, identifier:range; 181, argument_list; 182, identifier:ii; 183, integer:1; 184, binary_operator:len(minima)-1; 185, identifier:len; 186, argument_list; 187, identifier:minima; 188, unary_operator; 189, identifier:minima; 190, identifier:minima; 191, integer:0; 192, identifier:list; 193, argument_list; 194, subscript; 195, identifier:ii; 196, integer:1; 197, call; 198, call; 199, integer:1; 200, identifier:vector; 201, integer:1; 202, identifier:minima; 203, identifier:minima; 204, unary_operator; 205, identifier:len; 206, argument_list; 207, identifier:len; 208, argument_list; 209, integer:1; 210, identifier:maxima; 211, identifier:minima
code: def find_valley_range(vector, min_distance = 4):
      """
      Internal finder peaks and valley ranges.
      Returns UNSORTED indices of maxima in input vector.
      Returns range of valleys before and after maximum
      """
      
      # http://users.monash.edu.au/~dengs/resource/papers/icme08.pdf
      # find min and max with mode = wrap
      mode = "wrap"
      minima = local_minima(vector,min_distance,mode)
      maxima = local_maxima(vector,min_distance,mode)
  
      if len(maxima)>len(minima):
          if vector[maxima[0]] >= vector[maxima[-1]]:
              maxima=maxima[1:]
          else:
              maxima=maxima[:-1]
          
      if len(maxima)==len(minima):
          valley_range = numpy.asarray([minima[ii+1] - minima[ii] for ii in range(len(minima)-1)] + [len(vector)-minima[-1]+minima[0]])
          if minima[0] < maxima[0]:
              minima = numpy.asarray(list(minima) + [minima[0]])
          else:
              minima = numpy.asarray(list(minima) + [minima[-1]])       
      else:
          valley_range = numpy.asarray([minima[ii+1] - minima[ii] for ii in range(len(maxima))])
      
      return maxima, minima, valley_range