How to import Python graphs into Memgraph
GQLAlchemy holds translators that can import Python graphs (NetworkX, PyG, DGL or TF-GNN graphs) into Memgraph. These translators take the Python graph object and translate it to the appropriate Cypher queries. The Cypher queries are then executed to create a graph inside Memgraph.
In this guide you will learn how to:
- Import NetworkX graph into Memgraph
- Import DOT graph into Memgraph
- Import PyG graph into Memgraph
- Import DGL graph into Memgraph
- Import TF-GNN graph into Memgraph
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.
Import NetworkX graph into Memgraph
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 networkx-graph.py in the code editor of your choice, with the following code:
import networkx as nx
from gqlalchemy import Memgraph
from gqlalchemy.transformations.translators.nx_translator import NxTranslator
memgraph = Memgraph()
memgraph.drop_database()
graph = nx.Graph()
graph.add_nodes_from([(1, {"labels": "First"}), (2, {"name": "Kata"}), 3])
graph.add_edges_from([(1, 2, {"type": "EDGE_TYPE", "date": "today"}), (1, 3)])
translator = NxTranslator()
for query in list(translator.to_cypher_queries(graph)):
memgraph.execute(query)
First, connect to a running Memgraph instance. Next, drop the database to be sure that it's empty. After that, create a simple NetworkX graph and add nodes and edges to it. In the end, call to_cypher_queries procedure on NxTranslator instance to transform the NetworkX graph to Cypher queries which will be executed in Memgraph.
To run it, open a command-line interpreter and run the following command:
python3 networkx-graph.py
Explore the graph
Connect to Memgraph via Memgraph Lab which is running at localhost:3000. Open the Query Execution section and write the following query:
MATCH (n)-[r]->(m)
RETURN n, r, m;
Click Run Query button to see the results.
.default})
The NetworkX node identification number maps to the id node property in Memgraph. The labels key is reserved for the node label in Memgraph, while the edge type key is reserved for the relationship type in Memgraph. If no type is defined, then the relationship will be of type TO in Memgraph. You can notice that the node with the property name Kata and property id 2 doesn't have a label. This happened because the node property key labels was not defined.
Import DOT graph into Memgraph
You can import DOT files by using GraphImporter with graph_type="NX". DOT parsing uses pydot and NetworkX under the hood.
Prerequisites
Except for the general prerequisites, install DOT parsing support:
pip install gqlalchemy[dot]
Create and run a Python script
Create a new Python script dot-graph.py with the following code:
from gqlalchemy.transformations.importing.graph_importer import GraphImporter
importer = GraphImporter(graph_type="NX")
# Import from a DOT file path.
importer.translate_dot_file("graph.dot")
# Or import directly from DOT content.
dot_data = 'digraph G { "A" [label="A"]; "B"; "A" -> "B" [fontsize="10"]; }'
importer.translate_dot_data(dot_data)
During DOT import, GQLAlchemy enriches parsed nodes and edges with DOT-specific metadata (for example dot_type, attributes_json, flattened attributes_* keys, sequence, and stable edge id values).
Import PyG graph into Memgraph
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 pyg-graph.py in the code editor of your choice, with the following code:
import torch
from gqlalchemy import Memgraph
from gqlalchemy.transformations.translators.pyg_translator import PyGTranslator
from torch_geometric.data import HeteroData
memgraph = Memgraph()
memgraph.drop_database()
graph = HeteroData()
graph[("user", "PLUS", "movie")].edge_index = torch.tensor(
[[0, 0, 1], [0, 1, 0]], dtype=torch.int32
)
graph[("user", "MINUS", "movie")].edge_index = torch.tensor(
[[2], [1]], dtype=torch.int32
)
# Set node features
graph["user"].prop1 = torch.randn(size=(3, 1))
graph["user"].prop2 = torch.randn(size=(3, 1))
graph["movie"].prop1 = torch.randn(size=(2, 1))
graph["movie"].prop2 = torch.randn(size=(2, 1))
graph["movie"].prop3 = torch.randn(size=(2, 1))
graph["movie"].x = torch.randn(size=(2, 1))
graph["movie"].y = torch.randn(size=(2, 1))
# Set edge features
graph[("user", "PLUS", "movie")].edge_prop1 = torch.randn(size=(3, 1))
graph[("user", "PLUS", "movie")].edge_prop2 = torch.randn(size=(3, 1))
graph[("user", "MINUS", "movie")].edge_prop1 = torch.randn(size=(1, 1))
translator = PyGTranslator()
for query in list(translator.to_cypher_queries(graph)):
memgraph.execute(query)
First, connect to a running Memgraph instance. Next, drop the database to be sure that it's empty. After that, create a simple PyG heterogeneous graph and add nodes and edges along with their features to it. The graph consist of three user nodes and two movie nodes, as well as two types of edges - PLUS and MINUS. The edge_index of a graph determines which nodes are connected by which edges. Provide a tensor, that is a multi-dimensional matrix, as a value of edge_index, to define edges. Each tensor element maps to one graph node - first row of matrix maps to user, while the second one to the movie nodes. Hence, user node 0 is connected to the movie node 0, user node 0 is connected to the movie node 1, and user node 1 is connected to the movie node 0, with edge of type PLUS. These integers are mapping to the values of the pyg_id nodes' property in Memgraph. Similarly, the edge of type MINUS is created between user node 2 and movie node 1. In the end, call to_cypher_queries procedure on PyGTranslator instance to transform the PysG graph to Cypher queries which will be executed in Memgraph.
To run it, open a command-line interpreter and run the following command:
python3 pyg-graph.py
Explore the graph
Connect to Memgraph via Memgraph Lab which is running at localhost:3000. Open the Query Execution section and write the following query:
MATCH (n)-[r]->(m)
RETURN n, r, m;
Click Run Query button to see the results.
.default})
You can notice that we have nodes labeled with user and movie and relationships of type PLUS and MINUS. Besides that, nodes and relationships have randomized array properties as well as pyg_id property.
Import DGL graph into Memgraph
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 dgl-graph.py in the code editor of your choice, with the following code:
import numpy as np
import dgl
import torch
from gqlalchemy import Memgraph
from gqlalchemy.transformations.translators.dgl_translator import DGLTranslator
memgraph = Memgraph()
memgraph.drop_database()
graph = dgl.heterograph(
{
("user", "PLUS", "movie"): (np.array([0, 0, 1]), np.array([0, 1, 0])),
("user", "MINUS", "movie"): (np.array([2]), np.array([1])),
}
)
# Set node features
graph.nodes["user"].data["prop1"] = torch.randn(size=(3, 1))
graph.nodes["user"].data["prop2"] = torch.randn(size=(3, 1))
graph.nodes["movie"].data["prop1"] = torch.randn(size=(2, 1))
graph.nodes["movie"].data["prop2"] = torch.randn(size=(2, 1))
graph.nodes["movie"].data["prop3"] = torch.randn(size=(2, 1))
# Set edge features
graph.edges[("user", "PLUS", "movie")].data["edge_prop1"] = torch.randn(size=(3, 1))
graph.edges[("user", "PLUS", "movie")].data["edge_prop2"] = torch.randn(size=(3, 1))
graph.edges[("user", "MINUS", "movie")].data["edge_prop1"] = torch.randn(size=(1, 1))
translator = DGLTranslator()
for query in list(translator.to_cypher_queries(graph)):
memgraph.execute(query)
First, connect to a running Memgraph instance. Next, drop the database to be sure that it's is empty. After that, create a simple DGL heterogeneous graph and add nodes and edges along with their features to it. The graph consist of three user nodes and two movie nodes, as well as two types of edges - PLUS and MINUS. To define nodes and edge between them we are providing appropriate NumPy arrays. Hence, user node 0 is connected to the movie node 0, user node 0 is connected to the movie node 1, and user node 1 is connected to the movie node 0, with edge of type PLUS. These integers are mapping to the values of the dgl_id properties in Memgraph. Similarly, the edge of type MINUS is created between user node 2 and movie node 1. In the end, call to_cypher_queries procedure on DGLTranslator instance to transform the DGL graph to Cypher queries which will be executed in Memgraph.
To run it, open a command-line interpreter and run the following command:
python3 dgl-graph.py
3. Explore the graph
Connect to Memgraph via Memgraph Lab which is running at localhost:3000. Open the Query Execution section and write the following query:
MATCH (n)-[r]->(m)
RETURN n, r, m;
Click Run Query button to see the results.
.default})
You can notice that we have nodes labeled with user and movie and relationships of type PLUS and MINUS. Besides that, nodes and relationships have randomized array properties ad well as dgl_id property.
Import TF-GNN graph into Memgraph
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 tfgnn-graph.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+
import tensorflow as tf
import tensorflow_gnn as tfgnn
from gqlalchemy import Memgraph
from gqlalchemy.transformations.translators.tfgnn_translator import TFGNNTranslator
memgraph = Memgraph()
memgraph.drop_database()
# Create a TF-GNN GraphTensor
graph_tensor = tfgnn.GraphTensor.from_pieces(
node_sets={
"user": tfgnn.NodeSet.from_fields(
sizes=[3],
features={
"name": tf.constant(["Alice", "Bob", "Charlie"]),
"age": tf.constant([25, 30, 35], dtype=tf.int64),
}
),
"movie": tfgnn.NodeSet.from_fields(
sizes=[2],
features={
"title": tf.constant(["Inception", "Matrix"]),
"rating": tf.constant([8.8, 8.7], dtype=tf.float32),
}
),
},
edge_sets={
"LIKES": tfgnn.EdgeSet.from_fields(
sizes=[3],
adjacency=tfgnn.Adjacency.from_indices(
source=("user", tf.constant([0, 0, 1])),
target=("movie", tf.constant([0, 1, 0])),
),
features={
"score": tf.constant([5, 4, 5], dtype=tf.int64),
}
),
},
)
translator = TFGNNTranslator()
for query in translator.to_cypher_queries(graph_tensor):
memgraph.execute(query)
First, connect to a running Memgraph instance. Next, drop the database to be sure that it's empty. After that, create a TF-GNN GraphTensor with user and movie node sets, and a LIKES edge set connecting users to movies. The GraphTensor includes node features (name, age for users; title, rating for movies) and edge features (score). In the end, call to_cypher_queries on TFGNNTranslator instance to transform the TF-GNN graph to Cypher queries which will be executed in Memgraph.
To run it, open a command-line interpreter and run the following command:
python3 tfgnn-graph.py
Explore the graph
Connect to Memgraph via Memgraph Lab which is running at localhost:3000. Open the Query Execution section and write the following query:
MATCH (n)-[r]->(m)
RETURN n, r, m;
Click Run Query button to see the results.
You can notice that we have nodes labeled with user and movie and relationships of type LIKES. Besides that, nodes and relationships have their respective properties as well as tfgnn_id property which maps to the node/edge index in the original TF-GNN GraphTensor.
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.