Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.9/dist-packages/networkx/algorithms/assortativity/mixing.py: 31%

42 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-20 07:00 +0000

1""" 

2Mixing matrices for node attributes and degree. 

3""" 

4import networkx as nx 

5from networkx.algorithms.assortativity.pairs import node_attribute_xy, node_degree_xy 

6from networkx.utils import dict_to_numpy_array 

7 

8__all__ = [ 

9 "attribute_mixing_matrix", 

10 "attribute_mixing_dict", 

11 "degree_mixing_matrix", 

12 "degree_mixing_dict", 

13 "mixing_dict", 

14] 

15 

16 

17@nx._dispatch(node_attrs="attribute") 

18def attribute_mixing_dict(G, attribute, nodes=None, normalized=False): 

19 """Returns dictionary representation of mixing matrix for attribute. 

20 

21 Parameters 

22 ---------- 

23 G : graph 

24 NetworkX graph object. 

25 

26 attribute : string 

27 Node attribute key. 

28 

29 nodes: list or iterable (optional) 

30 Unse nodes in container to build the dict. The default is all nodes. 

31 

32 normalized : bool (default=False) 

33 Return counts if False or probabilities if True. 

34 

35 Examples 

36 -------- 

37 >>> G = nx.Graph() 

38 >>> G.add_nodes_from([0, 1], color="red") 

39 >>> G.add_nodes_from([2, 3], color="blue") 

40 >>> G.add_edge(1, 3) 

41 >>> d = nx.attribute_mixing_dict(G, "color") 

42 >>> print(d["red"]["blue"]) 

43 1 

44 >>> print(d["blue"]["red"]) # d symmetric for undirected graphs 

45 1 

46 

47 Returns 

48 ------- 

49 d : dictionary 

50 Counts or joint probability of occurrence of attribute pairs. 

51 """ 

52 xy_iter = node_attribute_xy(G, attribute, nodes) 

53 return mixing_dict(xy_iter, normalized=normalized) 

54 

55 

56@nx._dispatch(node_attrs="attribute") 

57def attribute_mixing_matrix(G, attribute, nodes=None, mapping=None, normalized=True): 

58 """Returns mixing matrix for attribute. 

59 

60 Parameters 

61 ---------- 

62 G : graph 

63 NetworkX graph object. 

64 

65 attribute : string 

66 Node attribute key. 

67 

68 nodes: list or iterable (optional) 

69 Use only nodes in container to build the matrix. The default is 

70 all nodes. 

71 

72 mapping : dictionary, optional 

73 Mapping from node attribute to integer index in matrix. 

74 If not specified, an arbitrary ordering will be used. 

75 

76 normalized : bool (default=True) 

77 Return counts if False or probabilities if True. 

78 

79 Returns 

80 ------- 

81 m: numpy array 

82 Counts or joint probability of occurrence of attribute pairs. 

83 

84 Notes 

85 ----- 

86 If each node has a unique attribute value, the unnormalized mixing matrix 

87 will be equal to the adjacency matrix. To get a denser mixing matrix, 

88 the rounding can be performed to form groups of nodes with equal values. 

89 For example, the exact height of persons in cm (180.79155222, 163.9080892, 

90 163.30095355, 167.99016217, 168.21590163, ...) can be rounded to (180, 163, 

91 163, 168, 168, ...). 

92 

93 Definitions of attribute mixing matrix vary on whether the matrix 

94 should include rows for attribute values that don't arise. Here we 

95 do not include such empty-rows. But you can force them to appear 

96 by inputting a `mapping` that includes those values. 

97 

98 Examples 

99 -------- 

100 >>> G = nx.path_graph(3) 

101 >>> gender = {0: 'male', 1: 'female', 2: 'female'} 

102 >>> nx.set_node_attributes(G, gender, 'gender') 

103 >>> mapping = {'male': 0, 'female': 1} 

104 >>> mix_mat = nx.attribute_mixing_matrix(G, 'gender', mapping=mapping) 

105 >>> # mixing from male nodes to female nodes 

106 >>> mix_mat[mapping['male'], mapping['female']] 

107 0.25 

108 """ 

109 d = attribute_mixing_dict(G, attribute, nodes) 

110 a = dict_to_numpy_array(d, mapping=mapping) 

111 if normalized: 

112 a = a / a.sum() 

113 return a 

114 

115 

116@nx._dispatch(edge_attrs="weight") 

117def degree_mixing_dict(G, x="out", y="in", weight=None, nodes=None, normalized=False): 

118 """Returns dictionary representation of mixing matrix for degree. 

119 

120 Parameters 

121 ---------- 

122 G : graph 

123 NetworkX graph object. 

124 

125 x: string ('in','out') 

126 The degree type for source node (directed graphs only). 

127 

128 y: string ('in','out') 

129 The degree type for target node (directed graphs only). 

130 

131 weight: string or None, optional (default=None) 

132 The edge attribute that holds the numerical value used 

133 as a weight. If None, then each edge has weight 1. 

134 The degree is the sum of the edge weights adjacent to the node. 

135 

136 normalized : bool (default=False) 

137 Return counts if False or probabilities if True. 

138 

139 Returns 

140 ------- 

141 d: dictionary 

142 Counts or joint probability of occurrence of degree pairs. 

143 """ 

144 xy_iter = node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight) 

145 return mixing_dict(xy_iter, normalized=normalized) 

146 

147 

148@nx._dispatch(edge_attrs="weight") 

149def degree_mixing_matrix( 

150 G, x="out", y="in", weight=None, nodes=None, normalized=True, mapping=None 

151): 

152 """Returns mixing matrix for attribute. 

153 

154 Parameters 

155 ---------- 

156 G : graph 

157 NetworkX graph object. 

158 

159 x: string ('in','out') 

160 The degree type for source node (directed graphs only). 

161 

162 y: string ('in','out') 

163 The degree type for target node (directed graphs only). 

164 

165 nodes: list or iterable (optional) 

166 Build the matrix using only nodes in container. 

167 The default is all nodes. 

168 

169 weight: string or None, optional (default=None) 

170 The edge attribute that holds the numerical value used 

171 as a weight. If None, then each edge has weight 1. 

172 The degree is the sum of the edge weights adjacent to the node. 

173 

174 normalized : bool (default=True) 

175 Return counts if False or probabilities if True. 

176 

177 mapping : dictionary, optional 

178 Mapping from node degree to integer index in matrix. 

179 If not specified, an arbitrary ordering will be used. 

180 

181 Returns 

182 ------- 

183 m: numpy array 

184 Counts, or joint probability, of occurrence of node degree. 

185 

186 Notes 

187 ----- 

188 Definitions of degree mixing matrix vary on whether the matrix 

189 should include rows for degree values that don't arise. Here we 

190 do not include such empty-rows. But you can force them to appear 

191 by inputting a `mapping` that includes those values. See examples. 

192 

193 Examples 

194 -------- 

195 >>> G = nx.star_graph(3) 

196 >>> mix_mat = nx.degree_mixing_matrix(G) 

197 >>> mix_mat[0, 1] # mixing from node degree 1 to node degree 3 

198 0.5 

199 

200 If you want every possible degree to appear as a row, even if no nodes 

201 have that degree, use `mapping` as follows, 

202 

203 >>> max_degree = max(deg for n, deg in G.degree) 

204 >>> mapping = {x: x for x in range(max_degree + 1)} # identity mapping 

205 >>> mix_mat = nx.degree_mixing_matrix(G, mapping=mapping) 

206 >>> mix_mat[3, 1] # mixing from node degree 3 to node degree 1 

207 0.5 

208 """ 

209 d = degree_mixing_dict(G, x=x, y=y, nodes=nodes, weight=weight) 

210 a = dict_to_numpy_array(d, mapping=mapping) 

211 if normalized: 

212 a = a / a.sum() 

213 return a 

214 

215 

216def mixing_dict(xy, normalized=False): 

217 """Returns a dictionary representation of mixing matrix. 

218 

219 Parameters 

220 ---------- 

221 xy : list or container of two-tuples 

222 Pairs of (x,y) items. 

223 

224 attribute : string 

225 Node attribute key 

226 

227 normalized : bool (default=False) 

228 Return counts if False or probabilities if True. 

229 

230 Returns 

231 ------- 

232 d: dictionary 

233 Counts or Joint probability of occurrence of values in xy. 

234 """ 

235 d = {} 

236 psum = 0.0 

237 for x, y in xy: 

238 if x not in d: 

239 d[x] = {} 

240 if y not in d: 

241 d[y] = {} 

242 v = d[x].get(y, 0) 

243 d[x][y] = v + 1 

244 psum += 1 

245 

246 if normalized: 

247 for _, jdict in d.items(): 

248 for j in jdict: 

249 jdict[j] /= psum 

250 return d