MCPcopy Create free account
hub / github.com/alibaba/GraphScope / to_networkx_graph

Function to_networkx_graph

python/graphscope/nx/convert.py:38–169  ·  view source on GitHub ↗
(data, create_using=None, multigraph_input=False)

Source from the content-addressed store, hash-verified

36
37@patch_docstring(networkx.convert.to_networkx_graph)
38def to_networkx_graph(data, create_using=None, multigraph_input=False): # noqa: C901
39 # graphscope graph
40 if isinstance(data, graphscope.Graph):
41 if create_using is None:
42 raise nx.NetworkXError(
43 "Use None to convert graphscope graph to networkx graph."
44 )
45 # check session and direction compatible
46 if data.session_id != create_using.session_id:
47 raise nx.NetworkXError(
48 "The source graph is not loaded in session {}."
49 % create_using.session_id
50 )
51 if data.is_directed() != create_using.is_directed():
52 if data.is_directed():
53 msg = "The source graph is a directed graph, can't be used to init nx.Graph. You may use nx.DiGraph"
54 else:
55 msg = "The source graph is a undirected graph, can't be used to init nx.DiGraph. You may use nx.Graph"
56 raise nx.NetworkXError(msg)
57 create_using._key = data.key
58 create_using._schema = data.schema
59 create_using._op = data.op
60 if create_using._default_label is not None:
61 try:
62 create_using._default_label_id = (
63 create_using._schema.get_vertex_label_id(
64 create_using._default_label
65 )
66 )
67 except KeyError:
68 raise nx.NetworkXError(
69 "default label {} not existed in graph."
70 % create_using._default_label
71 )
72 create_using._graph_type = data.graph_type
73 return
74
75 # networkx graph or graphscope.nx graph
76 if hasattr(data, "adj"):
77 try:
78 result = nx.from_dict_of_dicts(
79 data.adj,
80 create_using=create_using,
81 multigraph_input=data.is_multigraph(),
82 )
83 if hasattr(data, "graph"): # data.graph should be dict-like
84 result.graph.update(data.graph)
85 if hasattr(data, "nodes"): # data.nodes should be dict-like
86 result.add_nodes_from(data.nodes.items())
87 return result
88 except Exception as err:
89 raise nx.NetworkXError(
90 "Input is not a correct NetworkX-like graph."
91 ) from err
92
93 # dict of dicts/lists
94 if isinstance(data, dict):
95 try:

Callers 4

__init__Method · 0.90
__init__Method · 0.90
test_graphMethod · 0.90

Calls 7

add_nodes_fromMethod · 0.80
itemsMethod · 0.80
warnMethod · 0.80
is_directedMethod · 0.45
get_vertex_label_idMethod · 0.45
is_multigraphMethod · 0.45
updateMethod · 0.45

Tested by 2

test_graphMethod · 0.72