Introduction
Algorithm call syntax in the Python client is similar to what you already know—with some minor differences.
This lesson covers syntax, execution modes, and working with results.
What You’ll Learn
By the end of this lesson, you’ll be able to:
-
Call algorithms using Python syntax
-
Choose the right execution mode for your task
-
Chain algorithms together using mutate mode
-
Stream combined results from projections
-
Estimate memory requirements before running
Algorithm Syntax
The pattern is: gds[.tier].<algorithm>.<mode>(G, **config)
# Louvain community detection with stream mode
result = gds.louvain.stream(G) # (1)
# Louvain with mutate mode
result = gds.louvain.mutate(G, mutateProperty="community") # (2)
# Beta algorithm
result = gds.beta.node2vec.stream(G, embeddingDimension=64) # (3)-
Pattern:
gds.<algorithm>.<mode>(G)— the Graph objectGis always the first argument -
Mode-specific config params are passed as keyword arguments (e.g.
mutateProperty) -
Pre-release algorithms use a tier prefix:
gds.beta.orgds.alpha.
Execution Modes Compared
| Mode | Returns | Side Effect |
|---|---|---|
|
DataFrame with per-node/relationship results |
None |
|
Series with summary statistics |
Adds property to projection |
|
Series with summary statistics |
Writes property to Neo4j |
|
Series with summary statistics |
None |
Stream Mode
Returns results as a DataFrame—perfect for analysis and exploration.
df = gds.louvain.stream(G)
# df contains nodeId and communityId columns
print(df.head())Stream Mode: Working with Results
# Work with results using pandas
community_sizes = df.groupby("communityId").size().reset_index(name="size")
print(community_sizes.nlargest(10, "size"))Mutate Mode
Adds results to the projection (not the database). Useful for chaining algorithms.
result = gds.louvain.mutate(
G,
mutateProperty="community" # (1)
)
print(f"Nodes processed: {result['nodePropertiesWritten']}")
print(f"Communities found: {result['communityCount']}") # (2)
print(f"Modularity: {result['modularity']:.4f}")-
mutatePropertynames the new property added to the in-memory projection — choose a descriptive name -
The returned
resultdict contains algorithm-specific metadata like community count and modularity score
Mutate Mode: Verifying Properties
# Property is now available in the projection
print(G.node_properties("User")) # includes 'community'
print(G.node_properties("Movie")) # includes 'community'Write Mode
Writes results directly to Neo4j—useful for persisting results.
result = gds.louvain.write(
G,
writeProperty="community" # (1)
)
print(f"Wrote to {result['nodePropertiesWritten']} nodes") # (2)
print(f"Communities found: {result['communityCount']}")-
writePropertynames the property written back to Neo4j nodes — unlike mutate, this persists in the database -
Check
nodePropertiesWrittento verify all expected nodes received the property
Write Mode: Verifying in Database
df = gds.run_cypher(""" # (1)
MATCH (u:User)
WHERE u.community IS NOT NULL
RETURN u.community AS community, count(*) AS userCount
ORDER BY userCount DESC
LIMIT 5
""")
display(df) # (2)-
After
.write(), the property is in the database — you can query it with standard Cypher -
display()renders a formatted table in Jupyter notebooks; useprint()in scripts
Stats Mode
Returns only statistics—useful for tuning configuration before persisting.
result = gds.louvain.stats(G)
print(f"Community count: {result['communityCount']}")
print(f"Modularity: {result['modularity']:.4f}")
print(f"Compute time: {result['computeMillis']}ms")Chaining Algorithms
You can chain algorithms together—each algorithm’s output becoming input for the next.
Chaining Algorithms
Use mutate mode to chain algorithms together without writing to the database.
# First: find communities
gds.louvain.mutate(G, mutateProperty="community") # (1)
# Second: calculate degree centrality
gds.degree.mutate(G, mutateProperty="degree") # (2)-
Each
.mutate()call adds a new property to the in-memory projection -
Both properties (
communityanddegree) now coexist on the projection and can be streamed together
Streaming Combined Results
# Stream all results together
df = gds.graph.nodeProperties.stream( # (1)
G,
node_properties=["community", "degree"], # (2)
listNodeLabels=True # (3)
)
print(df.head(10))-
gds.graph.nodeProperties.stream()retrieves mutated properties from the projection as a DataFrame -
Pass a list of property names to stream multiple algorithm results in a single DataFrame
-
listNodeLabels=Trueadds a column showing each node’s labels — useful for filtering by type
Memory Estimation
Estimate algorithm memory requirements before running—useful for large graphs.
estimate = gds.louvain.mutate.estimate( # (1)
G,
mutateProperty="community_new"
)
print(f"Required memory: {estimate['requiredMemory']}") # (2)
print(f"Node count: {estimate['nodeCount']}")
print(f"Relationship count: {estimate['relationshipCount']}")-
.estimate()is available on every algorithm and mode — call it before running on large graphs -
Returns a human-readable memory estimate (e.g. "512 MB") so you can verify the server has enough heap
Summary
-
Algorithm syntax:
gds.<algorithm>.<mode>(G, **config) -
Stream mode returns DataFrames; mutate/write/stats modes return Series
-
Chain algorithms using mutate mode
-
Use
.stats()to tune configuration before persisting -
Use
.estimate()to check memory requirements
Next: Introduction to the Cora citation network dataset.