nodes: 0, module; 1, function_definition; 2, function_name:flatten; 3, parameters; 4, block; 5, identifier:rho; 6, identifier:pval; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, if_statement; 14, return_statement; 15, identifier:sortby; 16, string:"cor"; 17, comment:"""Flatten correlation and p-value matrix
  
      Parameters:
      -----------
      rho : ndarray
          Correlation Matrix
  
      pval : ndarray
          Matrix with p-values
  
      sortby : str
          sort the output table by
          - "cor"  the highest absolute correlation coefficient
          - "pval" the lowest p-value
  
      Return:
      -------
      tab : ndarray
          Table with (i, j, cor, pval) rows
  
      Example:
      --------
          from korr import pearson, flatten
          rho, pval = pearson(X)
          tab = flatten(rho, pval, sortby="pval")
          tab.values
      """; 18, assignment; 19, assignment; 20, assignment; 21, assignment; 22, comparison_operator:sortby == "cor"; 23, block; 24, elif_clause; 25, subscript; 26, identifier:n; 27, subscript; 28, identifier:idx; 29, call; 30, identifier:tab; 31, call; 32, subscript; 33, call; 34, identifier:sortby; 35, string:"cor"; 36, expression_statement; 37, expression_statement; 38, comparison_operator:sortby == "pval"; 39, block; 40, identifier:tab; 41, list; 42, attribute; 43, integer:0; 44, attribute; 45, argument_list; 46, attribute; 47, argument_list; 48, identifier:tab; 49, list; 50, attribute; 51, argument_list; 52, assignment; 53, call; 54, identifier:sortby; 55, string:"pval"; 56, expression_statement; 57, string:"i"; 58, string:"j"; 59, string:"cor"; 60, string:"pval"; 61, identifier:rho; 62, identifier:shape; 63, identifier:np; 64, identifier:triu_indices; 65, identifier:n; 66, keyword_argument; 67, identifier:pd; 68, identifier:DataFrame; 69, keyword_argument; 70, keyword_argument; 71, string; 72, string:"j"; 73, subscript; 74, identifier:astype; 75, identifier:int; 76, subscript; 77, call; 78, attribute; 79, argument_list; 80, call; 81, identifier:k; 82, integer:1; 83, identifier:columns; 84, list; 85, identifier:data; 86, subscript; 87, string_content:i; 88, identifier:tab; 89, list; 90, identifier:tab; 91, string; 92, attribute; 93, argument_list; 94, identifier:tab; 95, identifier:sort_values; 96, keyword_argument; 97, keyword_argument; 98, keyword_argument; 99, attribute; 100, argument_list; 101, string; 102, string; 103, string; 104, string; 105, attribute; 106, subscript; 107, subscript; 108, subscript; 109, subscript; 110, string; 111, string:"j"; 112, string_content:abscor; 113, identifier:np; 114, identifier:abs; 115, subscript; 116, identifier:by; 117, string; 118, identifier:inplace; 119, True; 120, identifier:ascending; 121, False; 122, identifier:tab; 123, identifier:sort_values; 124, keyword_argument; 125, keyword_argument; 126, keyword_argument; 127, string_content:i; 128, string_content:j; 129, string_content:cor; 130, string_content:pval; 131, identifier:np; 132, identifier:c_; 133, identifier:idx; 134, integer:0; 135, identifier:idx; 136, integer:1; 137, identifier:rho; 138, identifier:idx; 139, identifier:pval; 140, identifier:idx; 141, string_content:i; 142, identifier:tab; 143, string; 144, string_content:abscor; 145, identifier:by; 146, string; 147, identifier:inplace; 148, True; 149, identifier:ascending; 150, True; 151, string_content:cor; 152, string_content:pval
code: def flatten(rho, pval, sortby="cor"):
      """Flatten correlation and p-value matrix
  
      Parameters:
      -----------
      rho : ndarray
          Correlation Matrix
  
      pval : ndarray
          Matrix with p-values
  
      sortby : str
          sort the output table by
          - "cor"  the highest absolute correlation coefficient
          - "pval" the lowest p-value
  
      Return:
      -------
      tab : ndarray
          Table with (i, j, cor, pval) rows
  
      Example:
      --------
          from korr import pearson, flatten
          rho, pval = pearson(X)
          tab = flatten(rho, pval, sortby="pval")
          tab.values
      """
      n = rho.shape[0]
  
      idx = np.triu_indices(n, k=1)
  
      tab = pd.DataFrame(
          columns=['i', 'j', 'cor', 'pval'],
          data=np.c_[idx[0], idx[1], rho[idx], pval[idx]])
      tab[['i', "j"]] = tab[['i', "j"]].astype(int)
  
      if sortby == "cor":
          tab['abscor'] = np.abs(tab['cor'])
          tab.sort_values(by='abscor', inplace=True, ascending=False)
      elif sortby == "pval":
          tab.sort_values(by='pval', inplace=True, ascending=True)
  
      return tab[["i", "j", "cor", "pval"]]