MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / from_networkx

Function from_networkx

numpy_ml/tests/test_utils.py:225–238  ·  view source on GitHub ↗

Convert a networkx graph to my graph representation

(G_nx)

Source from the content-addressed store, hash-verified

223
224
225def from_networkx(G_nx):
226 """Convert a networkx graph to my graph representation"""
227 V = list(G_nx.nodes)
228 edges = list(G_nx.edges)
229 is_weighted = "weight" in G_nx[edges[0][0]][edges[0][1]]
230
231 E = []
232 for e in edges:
233 if is_weighted:
234 E.append(Edge(e[0], e[1], G_nx[e[0]][e[1]]["weight"]))
235 else:
236 E.append(Edge(e[0], e[1]))
237
238 return DiGraph(V, E) if nx.is_directed(G_nx) else UndirectedGraph(V, E)
239
240
241def to_networkx(G):

Callers

nothing calls this directly

Calls 3

EdgeClass · 0.90
DiGraphClass · 0.90
UndirectedGraphClass · 0.90

Tested by

no test coverage detected