nodes: 0, module; 1, function_definition; 2, function_name:assortativity_bin; 3, parameters; 4, block; 5, identifier:CIJ; 6, default_parameter; 7, expression_statement; 8, if_statement; 9, comment:# compute assortativity; 10, expression_statement; 11, expression_statement; 12, expression_statement; 13, expression_statement; 14, return_statement; 15, identifier:flag; 16, integer:0; 17, string; 18, comparison_operator:flag == 0; 19, comment:# undirected version; 20, block; 21, else_clause; 22, assignment; 23, assignment; 24, assignment; 25, assignment; 26, identifier:r; 27, string_content:The assortativity coefficient is a correlation coefficient between the
      degrees of all nodes on two opposite ends of a link. A positive
      assortativity coefficient indicates that nodes tend to link to other
      nodes with the same or similar degree.
  
      Parameters
      ----------
      CIJ : NxN np.ndarray
          binary directed/undirected connection matrix
      flag : int
          0 : undirected graph; degree/degree correlation
          1 : directed graph; out-degree/in-degree correlation
          2 : directed graph; in-degree/out-degree correlation
          3 : directed graph; out-degree/out-degree correlation
          4 : directed graph; in-degree/in-degreen correlation
  
      Returns
      -------
      r : float
          assortativity coefficient
  
      Notes
      -----
      The function accepts weighted networks, but all connection
      weights are ignored. The main diagonal should be empty. For flag 1
      the function computes the directed assortativity described in Rubinov
      and Sporns (2010) NeuroImage.; 28, identifier:flag; 29, integer:0; 30, expression_statement; 31, expression_statement; 32, expression_statement; 33, expression_statement; 34, expression_statement; 35, comment:# directed version; 36, block; 37, identifier:term1; 38, binary_operator:np.sum(degi * degj) / K; 39, identifier:term2; 40, call; 41, identifier:term3; 42, binary_operator:np.sum(.5 * (degi * degi + degj * degj)) / K; 43, identifier:r; 44, binary_operator:(term1 - term2) / (term3 - term2); 45, assignment; 46, assignment; 47, assignment; 48, assignment; 49, assignment; 50, expression_statement; 51, expression_statement; 52, expression_statement; 53, if_statement; 54, call; 55, identifier:K; 56, attribute; 57, argument_list; 58, call; 59, identifier:K; 60, parenthesized_expression; 61, parenthesized_expression; 62, identifier:deg; 63, call; 64, pattern_list; 65, call; 66, identifier:K; 67, call; 68, identifier:degi; 69, subscript; 70, identifier:degj; 71, subscript; 72, assignment; 73, assignment; 74, assignment; 75, comparison_operator:flag == 1; 76, block; 77, elif_clause; 78, elif_clause; 79, elif_clause; 80, else_clause; 81, attribute; 82, argument_list; 83, identifier:np; 84, identifier:square; 85, binary_operator:np.sum(.5 * (degi + degj)) / K; 86, attribute; 87, argument_list; 88, binary_operator:term1 - term2; 89, binary_operator:term3 - term2; 90, identifier:degrees_und; 91, argument_list; 92, identifier:i; 93, identifier:j; 94, attribute; 95, argument_list; 96, identifier:len; 97, argument_list; 98, identifier:deg; 99, identifier:i; 100, identifier:deg; 101, identifier:j; 102, pattern_list; 103, call; 104, pattern_list; 105, call; 106, identifier:K; 107, call; 108, identifier:flag; 109, integer:1; 110, expression_statement; 111, expression_statement; 112, comparison_operator:flag == 2; 113, block; 114, comparison_operator:flag == 3; 115, block; 116, comparison_operator:flag == 4; 117, block; 118, block; 119, identifier:np; 120, identifier:sum; 121, binary_operator:degi * degj; 122, call; 123, identifier:K; 124, identifier:np; 125, identifier:sum; 126, binary_operator:.5 * (degi * degi + degj * degj); 127, identifier:term1; 128, identifier:term2; 129, identifier:term3; 130, identifier:term2; 131, identifier:CIJ; 132, identifier:np; 133, identifier:where; 134, comparison_operator:np.triu(CIJ, 1) > 0; 135, identifier:i; 136, identifier:id; 137, identifier:od; 138, identifier:deg; 139, identifier:degrees_dir; 140, argument_list; 141, identifier:i; 142, identifier:j; 143, attribute; 144, argument_list; 145, identifier:len; 146, argument_list; 147, assignment; 148, assignment; 149, identifier:flag; 150, integer:2; 151, expression_statement; 152, expression_statement; 153, identifier:flag; 154, integer:3; 155, expression_statement; 156, expression_statement; 157, identifier:flag; 158, integer:4; 159, expression_statement; 160, expression_statement; 161, raise_statement; 162, identifier:degi; 163, identifier:degj; 164, attribute; 165, argument_list; 166, float:.5; 167, parenthesized_expression; 168, call; 169, integer:0; 170, identifier:CIJ; 171, identifier:np; 172, identifier:where; 173, comparison_operator:CIJ > 0; 174, identifier:i; 175, identifier:degi; 176, subscript; 177, identifier:degj; 178, subscript; 179, assignment; 180, assignment; 181, assignment; 182, assignment; 183, assignment; 184, assignment; 185, call; 186, identifier:np; 187, identifier:sum; 188, binary_operator:.5 * (degi + degj); 189, binary_operator:degi * degi + degj * degj; 190, attribute; 191, argument_list; 192, identifier:CIJ; 193, integer:0; 194, identifier:od; 195, identifier:i; 196, identifier:id; 197, identifier:j; 198, identifier:degi; 199, subscript; 200, identifier:degj; 201, subscript; 202, identifier:degi; 203, subscript; 204, identifier:degj; 205, subscript; 206, identifier:degi; 207, subscript; 208, identifier:degj; 209, subscript; 210, identifier:ValueError; 211, argument_list; 212, float:.5; 213, parenthesized_expression; 214, binary_operator:degi * degi; 215, binary_operator:degj * degj; 216, identifier:np; 217, identifier:triu; 218, identifier:CIJ; 219, integer:1; 220, identifier:id; 221, identifier:i; 222, identifier:od; 223, identifier:j; 224, identifier:od; 225, identifier:i; 226, identifier:od; 227, identifier:j; 228, identifier:id; 229, identifier:i; 230, identifier:id; 231, identifier:j; 232, string; 233, binary_operator:degi + degj; 234, identifier:degi; 235, identifier:degi; 236, identifier:degj; 237, identifier:degj; 238, string_content:Flag must be 0-4; 239, identifier:degi; 240, identifier:degj
code: def assortativity_bin(CIJ, flag=0):
      '''
      The assortativity coefficient is a correlation coefficient between the
      degrees of all nodes on two opposite ends of a link. A positive
      assortativity coefficient indicates that nodes tend to link to other
      nodes with the same or similar degree.
  
      Parameters
      ----------
      CIJ : NxN np.ndarray
          binary directed/undirected connection matrix
      flag : int
          0 : undirected graph; degree/degree correlation
          1 : directed graph; out-degree/in-degree correlation
          2 : directed graph; in-degree/out-degree correlation
          3 : directed graph; out-degree/out-degree correlation
          4 : directed graph; in-degree/in-degreen correlation
  
      Returns
      -------
      r : float
          assortativity coefficient
  
      Notes
      -----
      The function accepts weighted networks, but all connection
      weights are ignored. The main diagonal should be empty. For flag 1
      the function computes the directed assortativity described in Rubinov
      and Sporns (2010) NeuroImage.
      '''
      if flag == 0:  # undirected version
          deg = degrees_und(CIJ)
          i, j = np.where(np.triu(CIJ, 1) > 0)
          K = len(i)
          degi = deg[i]
          degj = deg[j]
      else:  # directed version
          id, od, deg = degrees_dir(CIJ)
          i, j = np.where(CIJ > 0)
          K = len(i)
  
          if flag == 1:
              degi = od[i]
              degj = id[j]
          elif flag == 2:
              degi = id[i]
              degj = od[j]
          elif flag == 3:
              degi = od[i]
              degj = od[j]
          elif flag == 4:
              degi = id[i]
              degj = id[j]
          else:
              raise ValueError('Flag must be 0-4')
  
      # compute assortativity
      term1 = np.sum(degi * degj) / K
      term2 = np.square(np.sum(.5 * (degi + degj)) / K)
      term3 = np.sum(.5 * (degi * degi + degj * degj)) / K
      r = (term1 - term2) / (term3 - term2)
      return r