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

32 statements  

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

1"""Generators of x-y pairs of node data.""" 

2import networkx as nx 

3 

4__all__ = ["node_attribute_xy", "node_degree_xy"] 

5 

6 

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

8def node_attribute_xy(G, attribute, nodes=None): 

9 """Returns iterator of node-attribute pairs for all edges in G. 

10 

11 Parameters 

12 ---------- 

13 G: NetworkX graph 

14 

15 attribute: key 

16 The node attribute key. 

17 

18 nodes: list or iterable (optional) 

19 Use only edges that are incident to specified nodes. 

20 The default is all nodes. 

21 

22 Returns 

23 ------- 

24 (x, y): 2-tuple 

25 Generates 2-tuple of (attribute, attribute) values. 

26 

27 Examples 

28 -------- 

29 >>> G = nx.DiGraph() 

30 >>> G.add_node(1, color="red") 

31 >>> G.add_node(2, color="blue") 

32 >>> G.add_edge(1, 2) 

33 >>> list(nx.node_attribute_xy(G, "color")) 

34 [('red', 'blue')] 

35 

36 Notes 

37 ----- 

38 For undirected graphs each edge is produced twice, once for each edge 

39 representation (u, v) and (v, u), with the exception of self-loop edges 

40 which only appear once. 

41 """ 

42 if nodes is None: 

43 nodes = set(G) 

44 else: 

45 nodes = set(nodes) 

46 Gnodes = G.nodes 

47 for u, nbrsdict in G.adjacency(): 

48 if u not in nodes: 

49 continue 

50 uattr = Gnodes[u].get(attribute, None) 

51 if G.is_multigraph(): 

52 for v, keys in nbrsdict.items(): 

53 vattr = Gnodes[v].get(attribute, None) 

54 for _ in keys: 

55 yield (uattr, vattr) 

56 else: 

57 for v in nbrsdict: 

58 vattr = Gnodes[v].get(attribute, None) 

59 yield (uattr, vattr) 

60 

61 

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

63def node_degree_xy(G, x="out", y="in", weight=None, nodes=None): 

64 """Generate node degree-degree pairs for edges in G. 

65 

66 Parameters 

67 ---------- 

68 G: NetworkX graph 

69 

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

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

72 

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

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

75 

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

77 The edge attribute that holds the numerical value used 

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

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

80 

81 nodes: list or iterable (optional) 

82 Use only edges that are adjacency to specified nodes. 

83 The default is all nodes. 

84 

85 Returns 

86 ------- 

87 (x, y): 2-tuple 

88 Generates 2-tuple of (degree, degree) values. 

89 

90 

91 Examples 

92 -------- 

93 >>> G = nx.DiGraph() 

94 >>> G.add_edge(1, 2) 

95 >>> list(nx.node_degree_xy(G, x="out", y="in")) 

96 [(1, 1)] 

97 >>> list(nx.node_degree_xy(G, x="in", y="out")) 

98 [(0, 0)] 

99 

100 Notes 

101 ----- 

102 For undirected graphs each edge is produced twice, once for each edge 

103 representation (u, v) and (v, u), with the exception of self-loop edges 

104 which only appear once. 

105 """ 

106 nodes = set(G) if nodes is None else set(nodes) 

107 if G.is_directed(): 

108 direction = {"out": G.out_degree, "in": G.in_degree} 

109 xdeg = direction[x] 

110 ydeg = direction[y] 

111 else: 

112 xdeg = ydeg = G.degree 

113 

114 for u, degu in xdeg(nodes, weight=weight): 

115 # use G.edges to treat multigraphs correctly 

116 neighbors = (nbr for _, nbr in G.edges(u) if nbr in nodes) 

117 for _, degv in ydeg(neighbors, weight=weight): 

118 yield degu, degv