nodes: 0, module; 1, function_definition; 2, function_name:group_and_sort_statements; 3, parameters; 4, block; 5, identifier:stmt_list; 6, default_parameter; 7, expression_statement; 8, function_definition; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, for_statement; 13, comment:# Sort the rows by count and agent names.; 14, function_definition; 15, expression_statement; 16, return_statement; 17, identifier:ev_totals; 18, None; 19, comment:"""Group statements by type and arguments, and sort by prevalence.
  
      Parameters
      ----------
      stmt_list : list[Statement]
          A list of INDRA statements.
      ev_totals : dict{int: int}
          A dictionary, keyed by statement hash (shallow) with counts of total
          evidence as the values. Including this will allow statements to be
          better sorted.
  
      Returns
      -------
      sorted_groups : list[tuple]
          A list of tuples containing a sort key, the statement type, and a list
          of statements, also sorted by evidence count, for that key and type.
          The sort key contains a count of statements with those argument, the
          arguments (normalized strings), the count of statements with those
          arguements and type, and then the statement type.
      """; 20, function_name:_count; 21, parameters; 22, block; 23, assignment; 24, assignment; 25, assignment; 26, pattern_list; 27, call; 28, comment:# Update the counts, and add key if needed.; 29, block; 30, function_name:process_rows; 31, parameters; 32, block; 33, assignment; 34, identifier:sorted_groups; 35, identifier:stmt; 36, if_statement; 37, identifier:stmt_rows; 38, call; 39, identifier:stmt_counts; 40, call; 41, identifier:arg_counts; 42, call; 43, identifier:key; 44, identifier:s; 45, identifier:_get_keyed_stmts; 46, argument_list; 47, expression_statement; 48, comment:# Keep track of the total evidence counts for this statement and the; 49, comment:# arguments.; 50, expression_statement; 51, comment:# Add up the counts for the arguments, pairwise for Complexes and; 52, comment:# Conversions. This allows, for example, a complex between MEK, ERK,; 53, comment:# and something else to lend weight to the interactions between MEK; 54, comment:# and ERK.; 55, if_statement; 56, identifier:stmt_rows; 57, for_statement; 58, identifier:sorted_groups; 59, call; 60, comparison_operator:ev_totals is None; 61, block; 62, else_clause; 63, identifier:defaultdict; 64, argument_list; 65, identifier:defaultdict; 66, argument_list; 67, identifier:defaultdict; 68, argument_list; 69, identifier:stmt_list; 70, call; 71, augmented_assignment; 72, comparison_operator:key[0] == 'Conversion'; 73, block; 74, else_clause; 75, pattern_list; 76, call; 77, block; 78, identifier:sorted; 79, argument_list; 80, identifier:ev_totals; 81, None; 82, return_statement; 83, block; 84, identifier:list; 85, lambda; 86, lambda; 87, attribute; 88, argument_list; 89, subscript; 90, call; 91, subscript; 92, string; 93, expression_statement; 94, for_statement; 95, block; 96, identifier:key; 97, identifier:stmts; 98, attribute; 99, argument_list; 100, expression_statement; 101, expression_statement; 102, expression_statement; 103, expression_statement; 104, if_statement; 105, expression_statement; 106, expression_statement; 107, expression_statement; 108, call; 109, keyword_argument; 110, keyword_argument; 111, call; 112, return_statement; 113, integer:0; 114, integer:0; 115, subscript; 116, identifier:append; 117, identifier:s; 118, identifier:stmt_counts; 119, identifier:key; 120, identifier:_count; 121, argument_list; 122, identifier:key; 123, integer:0; 124, string_content:Conversion; 125, assignment; 126, identifier:obj; 127, binary_operator:key[2] + key[3]; 128, block; 129, expression_statement; 130, identifier:stmt_rows; 131, identifier:items; 132, assignment; 133, assignment; 134, assignment; 135, assignment; 136, boolean_operator; 137, block; 138, assignment; 139, assignment; 140, yield; 141, identifier:process_rows; 142, argument_list; 143, identifier:key; 144, lambda; 145, identifier:reverse; 146, True; 147, identifier:len; 148, argument_list; 149, subscript; 150, identifier:stmt_rows; 151, identifier:key; 152, identifier:s; 153, identifier:subj; 154, subscript; 155, subscript; 156, subscript; 157, expression_statement; 158, augmented_assignment; 159, identifier:verb; 160, subscript; 161, identifier:inps; 162, subscript; 163, identifier:sub_count; 164, subscript; 165, identifier:arg_count; 166, subscript; 167, boolean_operator; 168, comparison_operator:len(inps) <= 2; 169, if_statement; 170, identifier:new_key; 171, tuple; 172, identifier:stmts; 173, call; 174, expression_list; 175, identifier:stmt_rows; 176, lambda_parameters; 177, subscript; 178, attribute; 179, identifier:ev_totals; 180, call; 181, identifier:key; 182, integer:1; 183, identifier:key; 184, integer:2; 185, identifier:key; 186, integer:3; 187, augmented_assignment; 188, subscript; 189, call; 190, identifier:key; 191, integer:0; 192, identifier:key; 193, slice; 194, identifier:stmt_counts; 195, identifier:key; 196, identifier:arg_counts; 197, identifier:inps; 198, comparison_operator:verb == 'Complex'; 199, comparison_operator:sub_count == arg_count; 200, call; 201, integer:2; 202, call; 203, block; 204, identifier:arg_count; 205, identifier:inps; 206, identifier:sub_count; 207, identifier:verb; 208, identifier:sorted; 209, argument_list; 210, identifier:new_key; 211, identifier:verb; 212, identifier:stmts; 213, identifier:tpl; 214, identifier:tpl; 215, integer:0; 216, identifier:stmt; 217, identifier:evidence; 218, attribute; 219, argument_list; 220, subscript; 221, call; 222, identifier:arg_counts; 223, subscript; 224, identifier:_count; 225, argument_list; 226, integer:1; 227, identifier:verb; 228, string; 229, identifier:sub_count; 230, identifier:arg_count; 231, identifier:len; 232, argument_list; 233, identifier:all; 234, argument_list; 235, continue_statement; 236, identifier:stmts; 237, keyword_argument; 238, keyword_argument; 239, identifier:stmt; 240, identifier:get_hash; 241, identifier:arg_counts; 242, tuple; 243, identifier:_count; 244, argument_list; 245, identifier:key; 246, slice; 247, identifier:s; 248, string_content:Complex; 249, identifier:inps; 250, list_comprehension; 251, identifier:key; 252, lambda; 253, identifier:reverse; 254, True; 255, identifier:subj; 256, identifier:obj; 257, identifier:s; 258, integer:1; 259, comparison_operator:len(set(ag.name for ag in s.agent_list())) > 2; 260, for_in_clause; 261, lambda_parameters; 262, binary_operator:_count(s) + 1/(1+len(s.agent_list())); 263, call; 264, integer:2; 265, identifier:s; 266, identifier:stmts; 267, identifier:s; 268, call; 269, binary_operator:1/(1+len(s.agent_list())); 270, identifier:len; 271, argument_list; 272, identifier:_count; 273, argument_list; 274, integer:1; 275, parenthesized_expression; 276, call; 277, identifier:s; 278, binary_operator:1+len(s.agent_list()); 279, identifier:set; 280, generator_expression; 281, integer:1; 282, call; 283, attribute; 284, for_in_clause; 285, identifier:len; 286, argument_list; 287, identifier:ag; 288, identifier:name; 289, identifier:ag; 290, call; 291, call; 292, attribute; 293, argument_list; 294, attribute; 295, argument_list; 296, identifier:s; 297, identifier:agent_list; 298, identifier:s; 299, identifier:agent_list
code: def group_and_sort_statements(stmt_list, ev_totals=None):
      """Group statements by type and arguments, and sort by prevalence.
  
      Parameters
      ----------
      stmt_list : list[Statement]
          A list of INDRA statements.
      ev_totals : dict{int: int}
          A dictionary, keyed by statement hash (shallow) with counts of total
          evidence as the values. Including this will allow statements to be
          better sorted.
  
      Returns
      -------
      sorted_groups : list[tuple]
          A list of tuples containing a sort key, the statement type, and a list
          of statements, also sorted by evidence count, for that key and type.
          The sort key contains a count of statements with those argument, the
          arguments (normalized strings), the count of statements with those
          arguements and type, and then the statement type.
      """
      def _count(stmt):
          if ev_totals is None:
              return len(stmt.evidence)
          else:
              return ev_totals[stmt.get_hash()]
  
      stmt_rows = defaultdict(list)
      stmt_counts = defaultdict(lambda: 0)
      arg_counts = defaultdict(lambda: 0)
      for key, s in _get_keyed_stmts(stmt_list):
          # Update the counts, and add key if needed.
          stmt_rows[key].append(s)
  
          # Keep track of the total evidence counts for this statement and the
          # arguments.
          stmt_counts[key] += _count(s)
  
          # Add up the counts for the arguments, pairwise for Complexes and
          # Conversions. This allows, for example, a complex between MEK, ERK,
          # and something else to lend weight to the interactions between MEK
          # and ERK.
          if key[0] == 'Conversion':
              subj = key[1]
              for obj in key[2] + key[3]:
                  arg_counts[(subj, obj)] += _count(s)
          else:
              arg_counts[key[1:]] += _count(s)
  
      # Sort the rows by count and agent names.
      def process_rows(stmt_rows):
          for key, stmts in stmt_rows.items():
              verb = key[0]
              inps = key[1:]
              sub_count = stmt_counts[key]
              arg_count = arg_counts[inps]
              if verb == 'Complex' and sub_count == arg_count and len(inps) <= 2:
                  if all([len(set(ag.name for ag in s.agent_list())) > 2
                          for s in stmts]):
                      continue
              new_key = (arg_count, inps, sub_count, verb)
              stmts = sorted(stmts,
                             key=lambda s: _count(s) + 1/(1+len(s.agent_list())),
                             reverse=True)
              yield new_key, verb, stmts
  
      sorted_groups = sorted(process_rows(stmt_rows),
                             key=lambda tpl: tpl[0], reverse=True)
  
      return sorted_groups