Skip to content

Commit 3f98022

Browse files
eriknwAlex-Markham
authored andcommitted
Allow graph generators and conversion functions to be dispatched (networkx#6876)
* Allow graph generators and conversion functions to be dispatched For now, the `backend` argument *must* be used to dispatch to a backend. Global config for automatic backends *does not* work on graph generators, readers, converters, or any function that does not have any graph inputs. For example, `G = nx.from_pandas_edgelist(df, backend="cugraph")` * Test dispatching of graph creation function * oops fix * Dispatch more graph creation (i.e. non-Graph input) functions * Don't dispatch `to_networkx_graph`--it specifically creates a NetworkX graph and not a backend graph
1 parent 2550b64 commit 3f98022

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+292
-29
lines changed

networkx/algorithms/bipartite/edgelist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def generate_edgelist(G, delimiter=" ", data=True):
146146
yield delimiter.join(map(str, edge))
147147

148148

149+
@nx._dispatch(name="bipartite_parse_edgelist", graphs=None)
149150
def parse_edgelist(
150151
lines, comments="#", delimiter=None, create_using=None, nodetype=None, data=True
151152
):
@@ -267,6 +268,7 @@ def parse_edgelist(
267268

268269

269270
@open_file(0, mode="rb")
271+
@nx._dispatch(name="bipartite_read_edgelist", graphs=None)
270272
def read_edgelist(
271273
path,
272274
comments="#",

networkx/algorithms/bipartite/generators.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
@nodes_or_number([0, 1])
24+
@nx._dispatch(graphs=None)
2425
def complete_bipartite_graph(n1, n2, create_using=None):
2526
"""Returns the complete bipartite graph `K_{n_1,n_2}`.
2627
@@ -66,6 +67,7 @@ def complete_bipartite_graph(n1, n2, create_using=None):
6667

6768

6869
@py_random_state(3)
70+
@nx._dispatch(name="bipartite_configuration_model", graphs=None)
6971
def configuration_model(aseq, bseq, create_using=None, seed=None):
7072
"""Returns a random bipartite graph from two given degree sequences.
7173
@@ -136,6 +138,7 @@ def configuration_model(aseq, bseq, create_using=None, seed=None):
136138
return G
137139

138140

141+
@nx._dispatch(name="bipartite_havel_hakimi_graph", graphs=None)
139142
def havel_hakimi_graph(aseq, bseq, create_using=None):
140143
"""Returns a bipartite graph from two given degree sequences using a
141144
Havel-Hakimi style construction.
@@ -210,6 +213,7 @@ def havel_hakimi_graph(aseq, bseq, create_using=None):
210213
return G
211214

212215

216+
@nx._dispatch(graphs=None)
213217
def reverse_havel_hakimi_graph(aseq, bseq, create_using=None):
214218
"""Returns a bipartite graph from two given degree sequences using a
215219
Havel-Hakimi style construction.
@@ -283,6 +287,7 @@ def reverse_havel_hakimi_graph(aseq, bseq, create_using=None):
283287
return G
284288

285289

290+
@nx._dispatch(graphs=None)
286291
def alternating_havel_hakimi_graph(aseq, bseq, create_using=None):
287292
"""Returns a bipartite graph from two given degree sequences using
288293
an alternating Havel-Hakimi style construction.
@@ -361,6 +366,7 @@ def alternating_havel_hakimi_graph(aseq, bseq, create_using=None):
361366

362367

363368
@py_random_state(3)
369+
@nx._dispatch(graphs=None)
364370
def preferential_attachment_graph(aseq, p, create_using=None, seed=None):
365371
"""Create a bipartite graph with a preferential attachment model from
366372
a given single degree sequence.
@@ -432,6 +438,7 @@ def preferential_attachment_graph(aseq, p, create_using=None, seed=None):
432438

433439

434440
@py_random_state(3)
441+
@nx._dispatch(graphs=None)
435442
def random_graph(n, m, p, seed=None, directed=False):
436443
"""Returns a bipartite random graph.
437444
@@ -518,6 +525,7 @@ def random_graph(n, m, p, seed=None, directed=False):
518525

519526

520527
@py_random_state(3)
528+
@nx._dispatch(graphs=None)
521529
def gnmk_random_graph(n, m, k, seed=None, directed=False):
522530
"""Returns a random bipartite graph G_{n,m,k}.
523531

networkx/algorithms/bipartite/matrix.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def biadjacency_matrix(
110110
raise nx.NetworkXError(f"Unknown sparse array format: {format}") from err
111111

112112

113+
@nx._dispatch(graphs=None)
113114
def from_biadjacency_matrix(A, create_using=None, edge_attribute="weight"):
114115
r"""Creates a new bipartite graph from a biadjacency matrix given as a
115116
SciPy sparse array.

networkx/algorithms/community/label_propagation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
]
1414

1515

16-
@nx._dispatch(edge_attrs="weight")
1716
@py_random_state("seed")
17+
@nx._dispatch(edge_attrs="weight")
1818
def fast_label_propagation_communities(G, *, weight=None, seed=None):
1919
"""Returns communities in `G` as detected by fast label propagation.
2020

networkx/algorithms/core.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def _core_subgraph(G, k_filter, k=None, core=None):
142142
return G.subgraph(nodes).copy()
143143

144144

145-
@nx._dispatch
145+
@nx._dispatch(preserve_all_attrs=True)
146146
def k_core(G, k=None, core_number=None):
147147
"""Returns the k-core of G.
148148
@@ -195,7 +195,7 @@ def k_filter(v, k, c):
195195
return _core_subgraph(G, k_filter, k, core_number)
196196

197197

198-
@nx._dispatch
198+
@nx._dispatch(preserve_all_attrs=True)
199199
def k_shell(G, k=None, core_number=None):
200200
"""Returns the k-shell of G.
201201
@@ -255,6 +255,7 @@ def k_filter(v, k, c):
255255
return _core_subgraph(G, k_filter, k, core_number)
256256

257257

258+
@nx._dispatch(preserve_all_attrs=True)
258259
def k_crust(G, k=None, core_number=None):
259260
"""Returns the k-crust of G.
260261
@@ -314,7 +315,7 @@ def k_crust(G, k=None, core_number=None):
314315
return G.subgraph(nodes).copy()
315316

316317

317-
@nx._dispatch
318+
@nx._dispatch(preserve_all_attrs=True)
318319
def k_corona(G, k, core_number=None):
319320
"""Returns the k-corona of G.
320321
@@ -371,7 +372,7 @@ def func(v, k, c):
371372

372373
@not_implemented_for("directed")
373374
@not_implemented_for("multigraph")
374-
@nx._dispatch
375+
@nx._dispatch(preserve_all_attrs=True)
375376
def k_truss(G, k):
376377
"""Returns the k-truss of `G`.
377378

networkx/algorithms/graphical.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
]
1515

1616

17+
@nx._dispatch(graphs=None)
1718
def is_graphical(sequence, method="eg"):
1819
"""Returns True if sequence is a valid degree sequence.
1920
@@ -92,6 +93,7 @@ def _basic_graphical_tests(deg_sequence):
9293
return dmax, dmin, dsum, n, num_degs
9394

9495

96+
@nx._dispatch(graphs=None)
9597
def is_valid_degree_sequence_havel_hakimi(deg_sequence):
9698
r"""Returns True if deg_sequence can be realized by a simple graph.
9799
@@ -181,6 +183,7 @@ def is_valid_degree_sequence_havel_hakimi(deg_sequence):
181183
return True
182184

183185

186+
@nx._dispatch(graphs=None)
184187
def is_valid_degree_sequence_erdos_gallai(deg_sequence):
185188
r"""Returns True if deg_sequence can be realized by a simple graph.
186189
@@ -271,6 +274,7 @@ def is_valid_degree_sequence_erdos_gallai(deg_sequence):
271274
return True
272275

273276

277+
@nx._dispatch(graphs=None)
274278
def is_multigraphical(sequence):
275279
"""Returns True if some multigraph can realize the sequence.
276280
@@ -321,6 +325,7 @@ def is_multigraphical(sequence):
321325
return True
322326

323327

328+
@nx._dispatch(graphs=None)
324329
def is_pseudographical(sequence):
325330
"""Returns True if some pseudograph can realize the sequence.
326331
@@ -367,6 +372,7 @@ def is_pseudographical(sequence):
367372
return sum(deg_sequence) % 2 == 0 and min(deg_sequence) >= 0
368373

369374

375+
@nx._dispatch(graphs=None)
370376
def is_digraphical(in_sequence, out_sequence):
371377
r"""Returns True if some directed graph can realize the in- and out-degree
372378
sequences.

networkx/algorithms/shortest_paths/dense.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ def floyd_warshall_predecessor_and_distance(G, weight="weight"):
167167
return dict(pred), dict(dist)
168168

169169

170+
@nx._dispatch(graphs=None)
170171
def reconstruct_path(source, target, predecessors):
171172
"""Reconstruct a path from source to target using the predecessors
172173
dict as returned by floyd_warshall_predecessor_and_distance

networkx/algorithms/threshold.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def weights_to_creation_sequence(
301301

302302

303303
# Manipulating NetworkX.Graphs in context of threshold graphs
304+
@nx._dispatch(graphs=None)
304305
def threshold_graph(creation_sequence, create_using=None):
305306
"""
306307
Create a threshold graph from the creation sequence or compact

networkx/algorithms/tournament.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def hamiltonian_path(G):
151151

152152

153153
@py_random_state(1)
154+
@nx._dispatch(graphs=None)
154155
def random_tournament(n, seed=None):
155156
r"""Returns a random tournament graph on `n` nodes.
156157

networkx/algorithms/tree/coding.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def _make_tuple(T, root, _parent):
128128
return _make_tuple(T, root, None)
129129

130130

131+
@nx._dispatch(graphs=None)
131132
def from_nested_tuple(sequence, sensible_relabeling=False):
132133
"""Returns the rooted tree corresponding to the given nested tuple.
133134
@@ -313,6 +314,7 @@ def parents(u):
313314
return result
314315

315316

317+
@nx._dispatch(graphs=None)
316318
def from_prufer_sequence(sequence):
317319
r"""Returns the tree corresponding to the given Prüfer sequence.
318320

0 commit comments

Comments
 (0)