Neo4j Graph Data Science
The neo4j-viz library provides a convenience method for importing data from the Neo4j Graph Data Science (GDS) library.
It requires and additional dependency to be installed, which you can do by running:
pip install neo4j-viz[gds]
Once you have installed the additional dependency, you can use the from_gds method
to import projections from the GDS library.
The from_gds method takes two mandatory positional parameters:
-
An initialized
GraphDataScienceobject for the connection to the GDS instance, and -
A
Graphrepresenting the projection that one wants to import.
The optional max_node_count parameter can be used to limit the number of nodes that are imported from the projection.
By default, it is set to 10.000, meaning that if the projection has more than 10.000 nodes, from_gds will sample from it using random walk with restarts, to get a smaller graph that can be visualized.
If you want to have more control of the sampling, such as choosing a specific start node for the sample, you can call a sampling method yourself and passing the resulting projection to from_gds.
The node_properties parameter is also optional, and should be a list of additional node properties of the projection that you want to include in the visualization.
The default is None, which means that all properties of the nodes in the projection will be included.
Apart from being visible through on-hover tooltips, these properties could be used to color the nodes, or give captions to them in the visualization, or simply included in the nodes' Node.properties maps without directly impacting the visualization.
If you want to include node properties stored at the Neo4j database, you can include them in the visualization by using the db_node_properties parameter.
Example
In this small example, we import a graph projection from the GDS library, that has the node properties "pagerank" and "componentId". We use the "pagerank" property to determine the size of the nodes, and the "componentId" property to color the nodes.
from graphdatascience import GraphDataScience
from neo4j_viz.gds import from_gds
gds = GraphDataScience(...)
G = gds.graph.project(...)
# Compute the PageRank and Weakly Connected Components
gds.pageRank.mutate(G, mutateProperty="pagerank")
gds.wcc.mutate(G, mutateProperty="componentId")
# Import the projection into a `VisualizationGraph`
# Make sure to include `pagerank` and `componentId`
VG = from_gds(
gds,
G,
size_property="pagerank",
additional_node_properties=["componentId"],
)
# Color the nodes by the `componentId` property, so that the nodes are
# colored by the connected component they belong to
VG.color_nodes(property="componentId")
See the Visualizing Neo4j Graph Data Science (GDS) Graphs tutorial for a more extensive example.