MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / from_networkx

Function from_networkx

easygraph/convert.py:324–352  ·  view source on GitHub ↗

Convert a NetworkX graph to an EasyGraph graph. Args: g (Union[nx.Graph, nx.DiGraph]): A NetworkX graph Returns: Union[Graph, DiGraph]: Converted EasyGraph graph

(g: "Union[nx.Graph, nx.DiGraph]")

Source from the content-addressed store, hash-verified

322
323
324def from_networkx(g: "Union[nx.Graph, nx.DiGraph]") -> "Union[Graph, DiGraph]":
325 """Convert a NetworkX graph to an EasyGraph graph.
326
327 Args:
328 g (Union[nx.Graph, nx.DiGraph]): A NetworkX graph
329
330 Returns:
331 Union[Graph, DiGraph]: Converted EasyGraph graph
332 """
333 # try:
334 # import networkx as nx
335 # except ImportError:
336 # raise ImportError("NetworkX not found. Please install it.")
337 if g.is_directed():
338 G = eg.DiGraph()
339 else:
340 G = eg.Graph()
341
342 # copy attributes
343 G.graph = deepcopy(g.graph)
344
345 nodes_with_edges = set()
346 for v1, v2, _ in g.edges:
347 G.add_edge(v1, v2)
348 nodes_with_edges.add(v1)
349 nodes_with_edges.add(v2)
350 for node in set(g.nodes) - nodes_with_edges:
351 G.add_node(node)
352 return G
353
354
355def to_dgl(g: "Union[Graph, DiGraph]"):

Callers 2

from_dglFunction · 0.85
from_pygFunction · 0.85

Calls 6

add_edgeMethod · 0.95
add_nodeMethod · 0.95
DiGraphMethod · 0.80
GraphMethod · 0.80
addMethod · 0.80
is_directedMethod · 0.45

Tested by

no test coverage detected