How to export data from Memgraph into Python graphs
GQLAlchemy holds translators that can export Memgraph graphs into Python graphs (NetworkX, PyG, DGL or TF-GNN graphs). These translators create a Python graph instance from the graph stored in Memgraph.
In this guide you will learn how to:
- How to export data from Memgraph into Python graphs
- General prerequisites
- Export data from Memgraph into NetworkX graph
- Export data from Memgraph into PyG graph
- Export data from Memgraph into DGL graph
- Export data from Memgraph into TF-GNN graph
- Learn more
General prerequisites
You need Memgraph Platform running, which includes both the MAGE library and Memgraph Lab, a visual interface. To run it on Linux/macOS, run the following in your terminal:
curl https://install.memgraph.com | sh
To run it on Windows, execute the following command in the console:
iwr https://windows.memgraph.com | iex
The above command runs a script that downloads a Docker Compose file to your system, builds and starts memgraph-mage and memgraph-lab Docker services in two separate containers.
To export data from Memgraph, you first have to create a graph in Memgraph. To do that, expand this section and run the given Python script.
from gqlalchemy import Memgraph
memgraph = Memgraph()
memgraph.drop_database()
queries = []
queries.append(f"CREATE (m:Node {{id: 1, num: 80, edem: 30, lst: [2, 3, 3, 2]}})")
queries.append(f"CREATE (m:Node {{id: 2, num: 91, edem: 32, lst: [2, 2, 3, 3]}})")
queries.append(
f"CREATE (m:Node {{id: 3, num: 100, edem: 34, lst: [3, 2, 2, 3, 4, 4]}})"
)
queries.append(f"CREATE (m:Node {{id: 4, num: 12, edem: 34, lst: [2, 2, 2, 3, 5, 5]}})")
queries.append(
f"MATCH (n:Node {{id: 1}}), (m:Node {{id: 2}}) CREATE (n)-[r:CONNECTION {{edge_id: 1, edge_num: 99, edge_edem: 12, edge_lst: [0, 1, 0, 1, 0, 1, 0, 1]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 2}}), (m:Node {{id: 3}}) CREATE (n)-[r:CONNECTION {{edge_id: 2, edge_num: 99, edge_edem: 12, edge_lst: [0, 1, 0, 1]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 3}}), (m:Node {{id: 4}}) CREATE (n)-[r:CONNECTION {{edge_id: 3, edge_num: 99, edge_edem: 12, edge_lst: [1, 0, 1, 0, 1, 0, 1]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 4}}), (m:Node {{id: 1}}) CREATE (n)-[r:CONNECTION {{edge_id: 4, edge_num: 99, edge_edem: 12, edge_lst: [0, 1, 0, 1]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 1}}), (m:Node {{id: 3}}) CREATE (n)-[r:CONNECTION {{edge_id: 5, edge_num: 99, edge_edem: 12, edge_lst: [0, 1, 0, 1]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 2}}), (m:Node {{id: 4}}) CREATE (n)-[r:CONNECTION {{edge_id: 6, edge_num: 99, edge_edem: 12, edge_lst: [0, 1, 0, 1, 0, 0]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 4}}), (m:Node {{id: 2}}) CREATE (n)-[r:CONNECTION {{edge_id: 7, edge_num: 99, edge_edem: 12, edge_lst: [1, 1, 0, 0, 1, 1, 0, 1]}}]->(m)"
)
queries.append(
f"MATCH (n:Node {{id: 3}}), (m:Node {{id: 1}}) CREATE (n)-[r:CONNECTION {{edge_id: 8, edge_num: 99, edge_edem: 12, edge_lst: [0, 1, 0, 1]}}]->(m)"
)
for query in queries:
memgraph.execute(query)
Export data from Memgraph into NetworkX graph
Prerequisites
Except for the general prerequisites, you also need to install NetworkX Python library.
Create and run a Python script
Create a new Python script memgraph-to-nx.py, in the code editor of your choice, with the following code:
from gqlalchemy.transformations.translators.nx_translator import NxTranslator
translator = NxTranslator()
graph = translator.get_instance()
print(graph.number_of_edges())
print(graph.number_of_nodes())
To run it, open a command-line interpreter and run the following command:
python3 memgraph-to-nx.py
You will get the following output:
8
4
This means that the NetworkX graph has the correct number of nodes and edges. You can explore it more to see if it has all the required features.
Export data from Memgraph into PyG graph
Prerequisites
Except for the general prerequisites, you also need to install Pytorch Geometric Python library.
Create and run a Python script
Create a new Python script memgraph-to-pyg.py, in the code editor of your choice, with the following code:
from gqlalchemy.transformations.translators.pyg_translator import PyGTranslator
translator = PyGTranslator()
graph = translator.get_instance()
print(len(graph.edge_types))
print(len(graph.node_types))
source_node_label, edge_type, dest_node_label = ("Node", "CONNECTION", "Node")
can_etype = (source_node_label, edge_type, dest_node_label)
print(graph[source_node_label].num_nodes)
print(graph[can_etype].num_edges)
To run it, open a command-line interpreter and run the following command:
python3 memgraph-to-pyg.py
You will get the following output:
1
1
4
8
This means that the PyG graph has the correct number of node and edge types, as well as correct total number of nodes and edges. You can explore it more to see if it has all the required features.
Export data from Memgraph into DGL graph
Prerequisites
Except for the general prerequisites, you also need to install Deep Graph Library.
Create and run a Python script
Create a new Python script memgraph-to-dgl.py, in the code editor of your choice, with the following code:
from gqlalchemy.transformations.translators.dgl_translator import DGLTranslator
translator = DGLTranslator()
graph = translator.get_instance()
print(len(graph.canonical_etypes))
print(len(graph.ntypes))
source_node_label, edge_type, dest_node_label = ("Node", "CONNECTION", "Node")
can_etype = (source_node_label, edge_type, dest_node_label)
print(graph[can_etype].number_of_nodes())
print(graph[can_etype].number_of_edges())
print(len(graph.nodes[source_node_label].data.keys()))
print(len(graph.edges[(source_node_label, edge_type, dest_node_label)].data.keys()))
To run it, open a command-line interpreter and run the following command:
python3 memgraph-to-dgl.py
You will get the following output:
1
1
4
8
3
3
This means that the DGL graph has the correct number of node and edge types, total number of nodes and edges, as well as node and edge features. You can explore it more to see if it has all the required features.
Export data from Memgraph into TF-GNN graph
Prerequisites
Except for the general prerequisites, you also need to install TensorFlow GNN. You can install it with:
pip install tensorflow-gnn
Note: TF-GNN requires TensorFlow 2.x. For TensorFlow 2.16 and above, you may need to set TF_USE_LEGACY_KERAS=1 environment variable.
Create and run a Python script
Create a new Python script memgraph-to-tfgnn.py, in the code editor of your choice, with the following code:
import os
os.environ["TF_USE_LEGACY_KERAS"] = "1" # Required for TensorFlow 2.16+
from gqlalchemy.transformations.translators.tfgnn_translator import TFGNNTranslator
translator = TFGNNTranslator()
graph_tensor = translator.get_instance()
print("Node sets:", list(graph_tensor.node_sets.keys()))
print("Edge sets:", list(graph_tensor.edge_sets.keys()))
for node_set_name, node_set in graph_tensor.node_sets.items():
print(f"Node set '{node_set_name}': {node_set.sizes[0]} nodes")
print(f" Features: {list(node_set.features.keys())}")
for edge_set_name, edge_set in graph_tensor.edge_sets.items():
print(f"Edge set '{edge_set_name}': {edge_set.sizes[0]} edges")
print(f" Features: {list(edge_set.features.keys())}")
To run it, open a command-line interpreter and run the following command:
python3 memgraph-to-tfgnn.py
You will get output showing the node sets, edge sets, and their features. The TF-GNN GraphTensor can now be used directly with TensorFlow GNN models for graph neural network training.
Learn more
Head over to the Under the hood section to read about implementation details. If you want to learn more about using NetworkX with Memgraph with interesting resources and courses, head over to the Memgraph for NetworkX developers website. If you have any questions or want to connect with the Memgraph community, join our Discord server.