| 36 | |
| 37 | @patch_docstring(networkx.convert.to_networkx_graph) |
| 38 | def 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: |