From 26e41ab683dcc676705c8914daf5ae9554966255 Mon Sep 17 00:00:00 2001 From: Mridul Seth Date: Fri, 3 Jul 2015 22:19:32 +0530 Subject: Update adjacency_iter() method to adjacency() and remove adjacency_list() --- networkx/algorithms/approximation/kcomponents.py | 2 +- .../approximation/tests/test_kcomponents.py | 6 ++-- networkx/algorithms/assortativity/pairs.py | 2 +- networkx/algorithms/clique.py | 4 +-- networkx/algorithms/operators/unary.py | 2 +- networkx/classes/digraph.py | 8 ++--- networkx/classes/graph.py | 39 +++------------------- networkx/classes/multidigraph.py | 8 ++--- networkx/classes/multigraph.py | 6 ++-- networkx/classes/tests/test_graph.py | 8 ++--- networkx/classes/tests/test_multigraph.py | 4 +-- networkx/classes/tests/test_timing.py | 2 +- networkx/convert.py | 4 +-- networkx/convert_matrix.py | 2 +- networkx/linalg/attrmatrix.py | 4 +-- networkx/readwrite/adjlist.py | 2 +- networkx/readwrite/json_graph/adjacency.py | 2 +- networkx/readwrite/multiline_adjlist.py | 8 ++--- 18 files changed, 40 insertions(+), 73 deletions(-) diff --git a/networkx/algorithms/approximation/kcomponents.py b/networkx/algorithms/approximation/kcomponents.py index f7b8a49d..49acea1d 100644 --- a/networkx/algorithms/approximation/kcomponents.py +++ b/networkx/algorithms/approximation/kcomponents.py @@ -312,7 +312,7 @@ class _AntiGraph(nx.Graph): (n in nbrs and nbrs[n].get(weight, 1))) return d_iter() - def adjacency_iter(self): + def adjacency(self): """Return an iterator of (node, adjacency set) tuples for all nodes in the dense graph. diff --git a/networkx/algorithms/approximation/tests/test_kcomponents.py b/networkx/algorithms/approximation/tests/test_kcomponents.py index d97743dc..627a5a35 100644 --- a/networkx/algorithms/approximation/tests/test_kcomponents.py +++ b/networkx/algorithms/approximation/tests/test_kcomponents.py @@ -237,10 +237,10 @@ class TestAntiGraph: for comp in ac: assert_true(comp in gc) - def test_adjacency_iter(self): + def test_adjacency(self): for G, A in self.GA: - a_adj = list(A.adjacency_iter()) - for n, nbrs in G.adjacency_iter(): + a_adj = list(A.adjacency()) + for n, nbrs in G.adjacency(): assert_true((n, set(nbrs)) in a_adj) def test_neighbors(self): diff --git a/networkx/algorithms/assortativity/pairs.py b/networkx/algorithms/assortativity/pairs.py index 8516fa4a..557148a1 100644 --- a/networkx/algorithms/assortativity/pairs.py +++ b/networkx/algorithms/assortativity/pairs.py @@ -45,7 +45,7 @@ def node_attribute_xy(G, attribute, nodes=None): else: nodes = set(nodes) node = G.node - for u,nbrsdict in G.adjacency_iter(): + for u,nbrsdict in G.adjacency(): if u not in nodes: continue uattr = node[u].get(attribute,None) diff --git a/networkx/algorithms/clique.py b/networkx/algorithms/clique.py index dcdfe3ef..3214d9d2 100644 --- a/networkx/algorithms/clique.py +++ b/networkx/algorithms/clique.py @@ -368,7 +368,7 @@ def project_down(B,create_using=None,name=None): if name is not None: G.name=name - for v,Bvnbrs in B.adjacency_iter(): + for v,Bvnbrs in B.adjacency(): if B.node_type[v]=="Bottom": G.add_node(v) for cv in Bvnbrs: @@ -391,7 +391,7 @@ def project_up(B,create_using=None,name=None): if name is not None: G.name=name - for v,Bvnbrs in B.adjacency_iter(): + for v,Bvnbrs in B.adjacency(): if B.node_type[v]=="Top": vname= -v #Change sign of name for Top Nodes G.add_node(vname) diff --git a/networkx/algorithms/operators/unary.py b/networkx/algorithms/operators/unary.py index 91a916c1..2bcc3f41 100644 --- a/networkx/algorithms/operators/unary.py +++ b/networkx/algorithms/operators/unary.py @@ -40,7 +40,7 @@ def complement(G, name=None): R.name = name R.add_nodes_from(G) R.add_edges_from(((n, n2) - for n, nbrs in G.adjacency_iter() + for n, nbrs in G.adjacency() for n2 in G if n2 not in nbrs if n != n2)) return R diff --git a/networkx/classes/digraph.py b/networkx/classes/digraph.py index b1fbe6f3..688a08e4 100644 --- a/networkx/classes/digraph.py +++ b/networkx/classes/digraph.py @@ -142,9 +142,9 @@ class DiGraph(Graph): 5 The fastest way to traverse all edges of a graph is via - adjacency_iter(), but the edges() method is often more convenient. + adjacency(), but the edges() method is often more convenient. - >>> for n,nbrsdict in G.adjacency_iter(): + >>> for n,nbrsdict in G.adjacency(): ... for nbr,eattr in nbrsdict.items(): ... if 'weight' in eattr: ... (n,nbr,eattr['weight']) @@ -1163,12 +1163,12 @@ class DiGraph(Graph): H.add_nodes_from(self) if reciprocal is True: H.add_edges_from( (u,v,deepcopy(d)) - for u,nbrs in self.adjacency_iter() + for u,nbrs in self.adjacency() for v,d in nbrs.items() if v in self.pred[u]) else: H.add_edges_from( (u,v,deepcopy(d)) - for u,nbrs in self.adjacency_iter() + for u,nbrs in self.adjacency() for v,d in nbrs.items() ) H.graph=deepcopy(self.graph) H.node=deepcopy(self.node) diff --git a/networkx/classes/graph.py b/networkx/classes/graph.py index 9967953e..490f5ee2 100644 --- a/networkx/classes/graph.py +++ b/networkx/classes/graph.py @@ -151,9 +151,9 @@ class Graph(object): 5 The fastest way to traverse all edges of a graph is via - adjacency_iter(), but the edges() method is often more convenient. + adjacency(), but the edges() method is often more convenient. - >>> for n,nbrsdict in G.adjacency_iter(): + >>> for n,nbrsdict in G.adjacency(): ... for nbr,eattr in nbrsdict.items(): ... if 'weight' in eattr: ... (n,nbr,eattr['weight']) @@ -1172,32 +1172,7 @@ class Graph(object): except KeyError: return default - def adjacency_list(self): - """Return an adjacency list representation of the graph. - - The output adjacency list is in the order of G.nodes(). - For directed graphs, only outgoing adjacencies are included. - - Returns - ------- - adj_list : lists of lists - The adjacency structure of the graph as a list of lists. - - See Also - -------- - adjacency_iter - - Examples - -------- - >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc - >>> G.add_path([0,1,2,3]) - >>> G.adjacency_list() # in order given by G.nodes() - [[1], [0, 2], [1, 3], [2]] - - """ - return list(map(list, iter(self.adj.values()))) - - def adjacency_iter(self): + def adjacency(self): """Return an iterator of (node, adjacency dict) tuples for all nodes. This is the fastest way to look at every edge. @@ -1209,15 +1184,11 @@ class Graph(object): An iterator of (node, adjacency dictionary) for all nodes in the graph. - See Also - -------- - adjacency_list - Examples -------- >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc >>> G.add_path([0,1,2,3]) - >>> [(n,nbrdict) for n,nbrdict in G.adjacency_iter()] + >>> [(n,nbrdict) for n,nbrdict in G.adjacency()] [(0, {1: {}}), (1, {0: {}, 2: {}}), (2, {1: {}, 3: {}}), (3, {2: {}})] """ @@ -1381,7 +1352,7 @@ class Graph(object): G.name = self.name G.add_nodes_from(self) G.add_edges_from(((u, v, deepcopy(data)) - for u, nbrs in self.adjacency_iter() + for u, nbrs in self.adjacency() for v, data in nbrs.items())) G.graph = deepcopy(self.graph) G.node = deepcopy(self.node) diff --git a/networkx/classes/multidigraph.py b/networkx/classes/multidigraph.py index 4a0eb9f7..f03dc746 100644 --- a/networkx/classes/multidigraph.py +++ b/networkx/classes/multidigraph.py @@ -151,9 +151,9 @@ class MultiDiGraph(MultiGraph,DiGraph): {2: {0: {'weight': 4}, 1: {'color': 'blue'}}} The fastest way to traverse all edges of a graph is via - adjacency_iter(), but the edges() method is often more convenient. + adjacency(), but the edges() method is often more convenient. - >>> for n,nbrsdict in G.adjacency_iter(): + >>> for n,nbrsdict in G.adjacency(): ... for nbr,keydict in nbrsdict.items(): ... for key,eattr in keydict.items(): ... if 'weight' in eattr: @@ -817,13 +817,13 @@ class MultiDiGraph(MultiGraph,DiGraph): H.add_nodes_from(self) if reciprocal is True: H.add_edges_from((u, v, key, deepcopy(data)) - for u, nbrs in self.adjacency_iter() + for u, nbrs in self.adjacency() for v, keydict in nbrs.items() for key, data in keydict.items() if self.has_edge(v, u, key)) else: H.add_edges_from((u, v, key, deepcopy(data)) - for u, nbrs in self.adjacency_iter() + for u, nbrs in self.adjacency() for v, keydict in nbrs.items() for key, data in keydict.items()) H.graph = deepcopy(self.graph) diff --git a/networkx/classes/multigraph.py b/networkx/classes/multigraph.py index 85d53a62..29bf362a 100644 --- a/networkx/classes/multigraph.py +++ b/networkx/classes/multigraph.py @@ -150,9 +150,9 @@ class MultiGraph(Graph): {2: {0: {'weight': 4}, 1: {'color': 'blue'}}} The fastest way to traverse all edges of a graph is via - adjacency_iter(), but the edges() method is often more convenient. + adjacency(), but the edges() method is often more convenient. - >>> for n,nbrsdict in G.adjacency_iter(): + >>> for n,nbrsdict in G.adjacency(): ... for nbr,keydict in nbrsdict.items(): ... for key,eattr in keydict.items(): ... if 'weight' in eattr: @@ -840,7 +840,7 @@ class MultiGraph(Graph): G = MultiDiGraph() G.add_nodes_from(self) G.add_edges_from((u, v, key, deepcopy(datadict)) - for u, nbrs in self.adjacency_iter() + for u, nbrs in self.adjacency() for v, keydict in nbrs.items() for key, datadict in keydict.items()) G.graph = deepcopy(self.graph) diff --git a/networkx/classes/tests/test_graph.py b/networkx/classes/tests/test_graph.py index 56bc7853..54c33d7a 100644 --- a/networkx/classes/tests/test_graph.py +++ b/networkx/classes/tests/test_graph.py @@ -47,10 +47,6 @@ class BaseGraphTester(object): f=lambda x:list(G.edges(x)) assert_raises((KeyError,networkx.NetworkXError), f, -1) - def test_adjacency_list(self): - G=self.K3 - assert_equal(G.adjacency_list(),[[1,2],[0,2],[0,1]]) - def test_weighted_degree(self): G=self.Graph() G.add_edge(1,2,weight=2) @@ -451,9 +447,9 @@ class TestGraph(BaseAttrGraphTester): assert_equal(G.name,"test") assert_equal(sorted(G.adj.items()),[(1, {2: {}}), (2, {1: {}})]) - def test_adjacency_iter(self): + def test_adjacency(self): G=self.K3 - assert_equal(dict(G.adjacency_iter()), + assert_equal(dict(G.adjacency()), {0: {1: {}, 2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}}) def test_getitem(self): diff --git a/networkx/classes/tests/test_multigraph.py b/networkx/classes/tests/test_multigraph.py index 35adbf07..174ca479 100644 --- a/networkx/classes/tests/test_multigraph.py +++ b/networkx/classes/tests/test_multigraph.py @@ -20,9 +20,9 @@ class BaseMultiGraphTester(BaseAttrGraphTester): assert_equal(G.get_edge_data(0,1,0),{}) - def test_adjacency_iter(self): + def test_adjacency(self): G=self.K3 - assert_equal(dict(G.adjacency_iter()), + assert_equal(dict(G.adjacency()), {0: {1: {0:{}}, 2: {0:{}}}, 1: {0: {0:{}}, 2: {0:{}}}, 2: {0: {0:{}}, 1: {0:{}}}}) diff --git a/networkx/classes/tests/test_timing.py b/networkx/classes/tests/test_timing.py index 6d45ee71..7c3fd893 100644 --- a/networkx/classes/tests/test_timing.py +++ b/networkx/classes/tests/test_timing.py @@ -41,7 +41,7 @@ all_tests=[ ('for n in G:\n for e in G.edges(n,data=True):\n pass', basic_setup, 3, 1) ), ('all_edges', - (('for n,nbrs in G.adjacency_iter():\n' + (('for n,nbrs in G.adjacency():\n' ' for nbr,data in nbrs.items():\n pass'), basic_setup, 3, 1) ), ('degree', ('for d in G.degree():\n pass', basic_setup, 3, 1) ), diff --git a/networkx/convert.py b/networkx/convert.py index fbecebaf..eba4c48d 100644 --- a/networkx/convert.py +++ b/networkx/convert.py @@ -265,10 +265,10 @@ def to_dict_of_dicts(G,nodelist=None,edge_data=None): dod={} if nodelist is None: if edge_data is None: - for u,nbrdict in G.adjacency_iter(): + for u,nbrdict in G.adjacency(): dod[u]=nbrdict.copy() else: # edge_data is not None - for u,nbrdict in G.adjacency_iter(): + for u,nbrdict in G.adjacency(): dod[u]=dod.fromkeys(nbrdict, edge_data) else: # nodelist is not None if edge_data is None: diff --git a/networkx/convert_matrix.py b/networkx/convert_matrix.py index cb49d34b..5ad20df3 100644 --- a/networkx/convert_matrix.py +++ b/networkx/convert_matrix.py @@ -367,7 +367,7 @@ def to_numpy_matrix(G, nodelist=None, dtype=None, order=None, else: # Graph or DiGraph, this is much faster than above M = np.zeros((nlen,nlen), dtype=dtype, order=order) + np.nan - for u,nbrdict in G.adjacency_iter(): + for u,nbrdict in G.adjacency(): for v,d in nbrdict.items(): try: M[index[u],index[v]] = d.get(weight,1) diff --git a/networkx/linalg/attrmatrix.py b/networkx/linalg/attrmatrix.py index df193bb9..df8684fd 100644 --- a/networkx/linalg/attrmatrix.py +++ b/networkx/linalg/attrmatrix.py @@ -258,7 +258,7 @@ def attr_matrix(G, edge_attr=None, node_attr=None, normalized=False, M = np.zeros((N,N), dtype=dtype, order=order) seen = set([]) - for u,nbrdict in G.adjacency_iter(): + for u,nbrdict in G.adjacency(): for v in nbrdict: # Obtain the node attribute values. i, j = index[node_value(u)], index[node_value(v)] @@ -421,7 +421,7 @@ def attr_sparse_matrix(G, edge_attr=None, node_attr=None, M = sparse.lil_matrix((N,N), dtype=dtype) seen = set([]) - for u,nbrdict in G.adjacency_iter(): + for u,nbrdict in G.adjacency(): for v in nbrdict: # Obtain the node attribute values. i, j = index[node_value(u)], index[node_value(v)] diff --git a/networkx/readwrite/adjlist.py b/networkx/readwrite/adjlist.py index 7a0aab6f..dd27d47f 100644 --- a/networkx/readwrite/adjlist.py +++ b/networkx/readwrite/adjlist.py @@ -76,7 +76,7 @@ def generate_adjlist(G, delimiter=' '): """ directed = G.is_directed() seen = set() - for s, nbrs in G.adjacency_iter(): + for s, nbrs in G.adjacency(): line = make_str(s) + delimiter for t, data in nbrs.items(): if not directed and t in seen: diff --git a/networkx/readwrite/json_graph/adjacency.py b/networkx/readwrite/json_graph/adjacency.py index 9349ab31..5d99dda3 100644 --- a/networkx/readwrite/json_graph/adjacency.py +++ b/networkx/readwrite/json_graph/adjacency.py @@ -74,7 +74,7 @@ def adjacency_data(G, attrs=_attrs): data['graph'] = list(G.graph.items()) data['nodes'] = [] data['adjacency'] = [] - for n, nbrdict in G.adjacency_iter(): + for n, nbrdict in G.adjacency(): data['nodes'].append(dict(chain(G.node[n].items(), [(id_, n)]))) adj = [] if multigraph: diff --git a/networkx/readwrite/multiline_adjlist.py b/networkx/readwrite/multiline_adjlist.py index 56e0bab4..f05f404a 100644 --- a/networkx/readwrite/multiline_adjlist.py +++ b/networkx/readwrite/multiline_adjlist.py @@ -87,7 +87,7 @@ def generate_multiline_adjlist(G, delimiter=' '): """ if G.is_directed(): if G.is_multigraph(): - for s, nbrs in G.adjacency_iter(): + for s, nbrs in G.adjacency(): nbr_edges = [(u, data) for u, datadict in nbrs.items() for key, data in datadict.items()] @@ -99,7 +99,7 @@ def generate_multiline_adjlist(G, delimiter=' '): else: yield make_str(u) + delimiter + make_str(d) else: # directed single edges - for s, nbrs in G.adjacency_iter(): + for s, nbrs in G.adjacency(): deg = len(nbrs) yield make_str(s) + delimiter + str(deg) for u, d in nbrs.items(): @@ -110,7 +110,7 @@ def generate_multiline_adjlist(G, delimiter=' '): else: # undirected if G.is_multigraph(): seen = set() # helper dict used to avoid duplicate edges - for s, nbrs in G.adjacency_iter(): + for s, nbrs in G.adjacency(): nbr_edges = [(u, data) for u, datadict in nbrs.items() if u not in seen @@ -125,7 +125,7 @@ def generate_multiline_adjlist(G, delimiter=' '): seen.add(s) else: # undirected single edges seen = set() # helper dict used to avoid duplicate edges - for s, nbrs in G.adjacency_iter(): + for s, nbrs in G.adjacency(): nbr_edges = [(u, d) for u, d in nbrs.items() if u not in seen] deg = len(nbr_edges) yield make_str(s) + delimiter + str(deg) -- cgit v1.2.1