summaryrefslogtreecommitdiff
path: root/networkx/classes/function.py
diff options
context:
space:
mode:
authorDan Schult <dschult@colgate.edu>2018-07-20 00:53:31 -0400
committerGitHub <noreply@github.com>2018-07-20 00:53:31 -0400
commite46f4c1cc02feaa08c855ca211ef114acc8fd772 (patch)
tree4a39727be298fff1a2e1dc86906cc1961fd6abc5 /networkx/classes/function.py
parentcdbf49ee375a836cc16121dcd75334d9fda1cf18 (diff)
downloadnetworkx-e46f4c1cc02feaa08c855ca211ef114acc8fd772.tar.gz
Simplify the Graphview and SubGraphView system (#3073)
* Simplify the Graphview and SubGraphView system - add tests to show that extensions of base graph classes (only add new functions) should be able to use the Graph.subgraph and Graph.copy methods easily - Remove ReadOnlyGraph class in favor of existing freeze() function - Switch all GraphView classes to generic_graph_view function - Switch all SubGraph classes to subgraph_view function - Introduce deprecated functions that act like the deprecated classes. Still need to: - add docs - add tests - make sure backward compatible and marked as deprecated - remove GraphView and SubGraph construct from rest of codebase - update release docs Fixes #2889 Fixes #2793 Fixes #2796 Fixes #2741 * Ease subclassing for to_(un)directed - add to_directed_class indicator functions to base classes - start deprecating G.fresh_copy - update function.to(un)directed * Remove G.fresh_copy from code replace with __class__ Add deprecation warnings for GraphView classes, ReverseView and SubGraph. Also for fresh_copy function.
Diffstat (limited to 'networkx/classes/function.py')
-rw-r--r--networkx/classes/function.py59
1 files changed, 23 insertions, 36 deletions
diff --git a/networkx/classes/function.py b/networkx/classes/function.py
index 5997642c..06c0f278 100644
--- a/networkx/classes/function.py
+++ b/networkx/classes/function.py
@@ -183,6 +183,7 @@ def freeze(G):
G.remove_nodes_from = frozen
G.add_edge = frozen
G.add_edges_from = frozen
+ G.add_weighted_edges_from = frozen
G.remove_edge = frozen
G.remove_edges_from = frozen
G.clear = frozen
@@ -362,13 +363,7 @@ def induced_subgraph(G, nbunch):
[(0, 1), (1, 2)]
"""
induced_nodes = nx.filters.show_nodes(G.nbunch_iter(nbunch))
- if G.is_multigraph():
- if G.is_directed():
- return nx.graphviews.SubMultiDiGraph(G, induced_nodes)
- return nx.graphviews.SubMultiGraph(G, induced_nodes)
- if G.is_directed():
- return nx.graphviews.SubDiGraph(G, induced_nodes)
- return nx.graphviews.SubGraph(G, induced_nodes)
+ return nx.graphviews.subgraph_view(G, induced_nodes)
def edge_subgraph(G, edges):
@@ -413,7 +408,6 @@ def edge_subgraph(G, edges):
[(0, 1), (3, 4)]
"""
nxf = nx.filters
- nxg = nx.graphviews
edges = set(edges)
nodes = set()
for e in edges:
@@ -422,14 +416,14 @@ def edge_subgraph(G, edges):
if G.is_multigraph():
if G.is_directed():
induced_edges = nxf.show_multidiedges(edges)
- return nxg.SubMultiDiGraph(G, induced_nodes, induced_edges)
- induced_edges = nxf.show_multiedges(edges)
- return nxg.SubMultiGraph(G, induced_nodes, induced_edges)
- if G.is_directed():
- induced_edges = nxf.show_diedges(edges)
- return nxg.SubDiGraph(G, induced_nodes, induced_edges)
- induced_edges = nxf.show_edges(edges)
- return nxg.SubGraph(G, induced_nodes, induced_edges)
+ else:
+ induced_edges = nxf.show_multiedges(edges)
+ else:
+ if G.is_directed():
+ induced_edges = nxf.show_diedges(edges)
+ else:
+ induced_edges = nxf.show_edges(edges)
+ return nx.graphviews.subgraph_view(G, induced_nodes, induced_edges)
def restricted_view(G, nodes, edges):
@@ -475,19 +469,18 @@ def restricted_view(G, nodes, edges):
[(2, 3)]
"""
nxf = nx.filters
- nxg = nx.graphviews
- h_nodes = nxf.hide_nodes(nodes)
+ hide_nodes = nxf.hide_nodes(nodes)
if G.is_multigraph():
if G.is_directed():
- h_edges = nxf.hide_multidiedges(edges)
- return nxg.SubMultiDiGraph(G, h_nodes, h_edges)
- h_edges = nxf.hide_multiedges(edges)
- return nxg.SubMultiGraph(G, h_nodes, h_edges)
- if G.is_directed():
- h_edges = nxf.hide_diedges(edges)
- return nxg.SubDiGraph(G, h_nodes, h_edges)
- h_edges = nxf.hide_edges(edges)
- return nxg.SubGraph(G, h_nodes, h_edges)
+ hide_edges = nxf.hide_multidiedges(edges)
+ else:
+ hide_edges = nxf.hide_multiedges(edges)
+ else:
+ if G.is_directed():
+ hide_edges = nxf.hide_diedges(edges)
+ else:
+ hide_edges = nxf.hide_edges(edges)
+ return nx.graphviews.subgraph_view(G, hide_nodes, hide_edges)
@not_implemented_for('undirected')
@@ -496,9 +489,7 @@ def reverse_view(digraph):
Identical to digraph.reverse(copy=False)
"""
- if digraph.is_multigraph():
- return nx.graphviews.MultiReverseView(digraph)
- return nx.graphviews.ReverseView(digraph)
+ return nx.graphviews.reverse_view(digraph)
def to_directed(graph):
@@ -508,9 +499,7 @@ def to_directed(graph):
Note that graph.to_directed defaults to `as_view=False`
while this function always provides a view.
"""
- if graph.is_multigraph():
- return nx.graphviews.MultiDiGraphView(graph)
- return nx.graphviews.DiGraphView(graph)
+ return graph.to_directed()
def to_undirected(graph):
@@ -520,9 +509,7 @@ def to_undirected(graph):
Note that graph.to_undirected defaults to `as_view=False`
while this function always provides a view.
"""
- if graph.is_multigraph():
- return nx.graphviews.MultiGraphView(graph)
- return nx.graphviews.GraphView(graph)
+ return graph.to_undirected(as_view=True)
def create_empty_copy(G, with_data=True):