nodes: 0, module; 1, function_definition; 2, function_name:good_sequences_to_track; 3, parameters; 4, block; 5, identifier:flow; 6, default_parameter; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, function_definition; 12, function_definition; 13, function_definition; 14, expression_statement; 15, comment:# frames; 16, expression_statement; 17, for_statement; 18, return_statement; 19, identifier:motion_threshold; 20, float:1.0; 21, comment:"""Get list of good frames to do tracking in.
  
      Looking at the optical flow, this function chooses a span of frames
      that fulfill certain criteria.
      These include
          * not being too short or too long
          * not too low or too high mean flow magnitude
          * a low max value (avoids motion blur)
      Currently, the cost function for a sequence is hard coded. Sorry about that.
      
      Parameters
      -------------
      flow : ndarray
              The optical flow magnitude
      motion_threshold : float
              The maximum amount of motion to consider for sequence endpoints.
              
      Returns
      ------------
      sequences : list
              Sorted list of (a, b, score) elements (highest scpre first) of sequences
              where a sequence is frames with frame indices in the span [a, b].
      """; 22, assignment; 23, assignment; 24, pattern_list; 25, call; 26, block; 27, function_name:mean_score_func; 28, parameters; 29, block; 30, function_name:max_score_func; 31, parameters; 32, block; 33, function_name:length_score_func; 34, parameters; 35, block; 36, assignment; 37, assignment; 38, pattern_list; 39, call; 40, block; 41, call; 42, identifier:endpoints; 43, list; 44, identifier:in_low; 45, False; 46, identifier:i; 47, identifier:val; 48, identifier:enumerate; 49, argument_list; 50, if_statement; 51, identifier:m; 52, expression_statement; 53, expression_statement; 54, expression_statement; 55, return_statement; 56, identifier:m; 57, expression_statement; 58, expression_statement; 59, if_statement; 60, identifier:l; 61, expression_statement; 62, expression_statement; 63, expression_statement; 64, return_statement; 65, identifier:min_length; 66, integer:5; 67, identifier:sequences; 68, list; 69, identifier:k; 70, identifier:i; 71, identifier:enumerate; 72, argument_list; 73, for_statement; 74, identifier:sorted; 75, argument_list; 76, identifier:flow; 77, comparison_operator:val < motion_threshold; 78, block; 79, else_clause; 80, assignment; 81, assignment; 82, assignment; 83, binary_operator:normpdf(m, mu, sigma) / top_val; 84, assignment; 85, assignment; 86, comparison_operator:m <= mu; 87, block; 88, else_clause; 89, assignment; 90, assignment; 91, assignment; 92, binary_operator:normpdf(l, mu, sigma) / top_val; 93, subscript; 94, identifier:j; 95, subscript; 96, block; 97, identifier:sequences; 98, keyword_argument; 99, keyword_argument; 100, identifier:val; 101, identifier:motion_threshold; 102, if_statement; 103, block; 104, identifier:mu; 105, integer:15; 106, identifier:sigma; 107, integer:8; 108, identifier:top_val; 109, call; 110, call; 111, identifier:top_val; 112, identifier:mu; 113, integer:40; 114, identifier:sigma; 115, integer:8; 116, identifier:m; 117, identifier:mu; 118, return_statement; 119, block; 120, identifier:mu; 121, integer:30; 122, identifier:sigma; 123, integer:10; 124, identifier:top_val; 125, call; 126, call; 127, identifier:top_val; 128, identifier:endpoints; 129, slice; 130, identifier:endpoints; 131, slice; 132, expression_statement; 133, if_statement; 134, expression_statement; 135, expression_statement; 136, expression_statement; 137, expression_statement; 138, expression_statement; 139, if_statement; 140, expression_statement; 141, expression_statement; 142, identifier:key; 143, lambda; 144, identifier:reverse; 145, True; 146, not_operator; 147, block; 148, if_statement; 149, expression_statement; 150, identifier:normpdf; 151, argument_list; 152, identifier:normpdf; 153, argument_list; 154, float:1.; 155, expression_statement; 156, return_statement; 157, identifier:normpdf; 158, argument_list; 159, identifier:normpdf; 160, argument_list; 161, unary_operator; 162, binary_operator:k+1; 163, assignment; 164, comparison_operator:length < min_length; 165, block; 166, assignment; 167, assignment; 168, assignment; 169, assignment; 170, call; 171, comparison_operator:min(m_score, mx_score, l_score) < 0.2; 172, block; 173, assignment; 174, call; 175, lambda_parameters; 176, subscript; 177, identifier:in_low; 178, expression_statement; 179, expression_statement; 180, identifier:in_low; 181, block; 182, assignment; 183, identifier:mu; 184, identifier:mu; 185, identifier:sigma; 186, identifier:m; 187, identifier:mu; 188, identifier:sigma; 189, assignment; 190, binary_operator:normpdf(m, mu, sigma) / top_val; 191, identifier:mu; 192, identifier:mu; 193, identifier:sigma; 194, identifier:l; 195, identifier:mu; 196, identifier:sigma; 197, integer:1; 198, identifier:k; 199, integer:1; 200, identifier:length; 201, binary_operator:j - i; 202, identifier:length; 203, identifier:min_length; 204, continue_statement; 205, identifier:seq; 206, subscript; 207, identifier:m_score; 208, call; 209, identifier:mx_score; 210, call; 211, identifier:l_score; 212, call; 213, attribute; 214, argument_list; 215, call; 216, float:0.2; 217, continue_statement; 218, identifier:score; 219, binary_operator:m_score + mx_score + l_score; 220, attribute; 221, argument_list; 222, identifier:x; 223, identifier:x; 224, integer:2; 225, call; 226, assignment; 227, expression_statement; 228, comment:# Previous was last in a low spot; 229, identifier:in_low; 230, False; 231, identifier:top_val; 232, call; 233, call; 234, identifier:top_val; 235, identifier:j; 236, identifier:i; 237, identifier:flow; 238, slice; 239, identifier:mean_score_func; 240, argument_list; 241, identifier:max_score_func; 242, argument_list; 243, identifier:length_score_func; 244, argument_list; 245, identifier:logger; 246, identifier:debug; 247, binary_operator:"%d, %d scores: (mean=%.5f, max=%.5f, length=%.5f)" % (i,j,m_score, mx_score, l_score); 248, identifier:min; 249, argument_list; 250, binary_operator:m_score + mx_score; 251, identifier:l_score; 252, identifier:sequences; 253, identifier:append; 254, tuple; 255, attribute; 256, argument_list; 257, identifier:in_low; 258, True; 259, call; 260, identifier:normpdf; 261, argument_list; 262, identifier:normpdf; 263, argument_list; 264, identifier:i; 265, binary_operator:j+1; 266, call; 267, call; 268, identifier:length; 269, string:"%d, %d scores: (mean=%.5f, max=%.5f, length=%.5f)"; 270, tuple; 271, identifier:m_score; 272, identifier:mx_score; 273, identifier:l_score; 274, identifier:m_score; 275, identifier:mx_score; 276, identifier:i; 277, identifier:j; 278, identifier:score; 279, identifier:endpoints; 280, identifier:append; 281, identifier:i; 282, attribute; 283, argument_list; 284, identifier:mu; 285, identifier:mu; 286, identifier:sigma; 287, identifier:m; 288, identifier:mu; 289, identifier:sigma; 290, identifier:j; 291, integer:1; 292, attribute; 293, argument_list; 294, attribute; 295, argument_list; 296, identifier:i; 297, identifier:j; 298, identifier:m_score; 299, identifier:mx_score; 300, identifier:l_score; 301, identifier:endpoints; 302, identifier:append; 303, binary_operator:i-1; 304, identifier:np; 305, identifier:mean; 306, identifier:seq; 307, identifier:np; 308, identifier:max; 309, identifier:seq; 310, identifier:i; 311, integer:1
code: def good_sequences_to_track(flow, motion_threshold=1.0):
      """Get list of good frames to do tracking in.
  
      Looking at the optical flow, this function chooses a span of frames
      that fulfill certain criteria.
      These include
          * not being too short or too long
          * not too low or too high mean flow magnitude
          * a low max value (avoids motion blur)
      Currently, the cost function for a sequence is hard coded. Sorry about that.
      
      Parameters
      -------------
      flow : ndarray
              The optical flow magnitude
      motion_threshold : float
              The maximum amount of motion to consider for sequence endpoints.
              
      Returns
      ------------
      sequences : list
              Sorted list of (a, b, score) elements (highest scpre first) of sequences
              where a sequence is frames with frame indices in the span [a, b].
      """
      endpoints = []
      in_low = False
      for i, val in enumerate(flow):
          if val < motion_threshold:
              if not in_low:
                  endpoints.append(i)
                  in_low = True
          else:
              if in_low:
                  endpoints.append(i-1) # Previous was last in a low spot
              in_low = False
      
      def mean_score_func(m):
          mu = 15
          sigma = 8
          top_val = normpdf(mu, mu, sigma)
          return normpdf(m, mu, sigma) / top_val
      
      def max_score_func(m):
          mu = 40
          sigma = 8
          if m <= mu:
              return 1.
          else:
              top_val = normpdf(mu, mu, sigma)
              return normpdf(m, mu, sigma) / top_val
      
      def length_score_func(l):
          mu = 30
          sigma = 10
          top_val = normpdf(mu, mu, sigma)
          return normpdf(l, mu, sigma) / top_val
      
      min_length = 5 # frames
      sequences = []
      for k, i in enumerate(endpoints[:-1]):
          for j in endpoints[k+1:]:
              length = j - i
              if length < min_length:
                  continue
              seq = flow[i:j+1]
              m_score = mean_score_func(np.mean(seq))
              mx_score = max_score_func(np.max(seq))
              l_score = length_score_func(length)
              logger.debug("%d, %d scores: (mean=%.5f, max=%.5f, length=%.5f)" % (i,j,m_score, mx_score, l_score))
              if min(m_score, mx_score, l_score) < 0.2:
                  continue
              
              score = m_score + mx_score + l_score 
              sequences.append((i, j, score))
  
      return sorted(sequences, key=lambda x: x[2], reverse=True)