Compute.infomap()#
#Compute.infomap(
node: Producer
max_levels: int = 1,
max_sweeps: int = 20,
level_tolerance: float = 0.01,
sweep_tolerance: float = 0.0001,
teleportation_rate: float = 0.15,
visit_rate_tolerance: float = 1e-15,
randomization_seed: int | None = None,
) -> Expression
Assign a community label to node
using the Infomap algorithm.
Infomap leverages principles from information theory to detect communities within networks.
It employs the map equation, a powerful tool that quantifies the information needed to describe
random walks on a network. This method aims to identify the most efficient encoding of network structure,
thereby optimizing for the most compact representation of community divisions.
Nodes assigned to the same community are more likely to interact or communicate with one another
as information flows through the network.
Must be called in a rule or query context.
Supported Graph Types#
Graph Type | Supported | Notes |
---|---|---|
Directed | Yes | |
Undirected | Yes | |
Weighted | Yes | Only positive weights are supported. |
Unweighted | Yes |
Parameters#
Name | Type | Description | Range |
---|---|---|---|
node | Producer | A node in the graph. | Vertex set. |
max_levels | int | The maximum number of levels at which to optimize. Default is 1 . | Positive integer. |
max_sweeps | int | The maximum number of iterations to run at each level. Default is 20 . | Non-negative integer. |
level_tolerance | float | The minimum change in the map equation required to continue to the next level. Default is 0.01 . | Small positive number or zero. |
sweep_tolerance | float | The minimum change in the map equation required to continue to the next sweep. Default is 0.0001 . | Small positive number or zero. |
teleportation_rate | float | The non-zero probability of teleporting to a random node. Default is 0.15 . | [1e-4, 1] . |
visit_rate_tolerance | float | The minimum change in the visit rate required to continue to the next sweep. Default is 1e-15 . | Small positive number. |
randomization_seed | int or None | The seed for the algorithm’s random number generator. Default is None . | None or non-negative integer. |
Returns#
Returns an Expression object that produces
the integer community label assigned by Infomap to node
as an integer value.
Example#
Use .infomap()
to assign community labels to nodes in a graph using the Infomap algorithm.
You access the .infomap()
method from a Graph
object’s
.compute
attribute:
#import relationalai as rai
from relationalai.std import alias
from relationalai.std.graphs import Graph
# Create a model named "socialNetwork" with a Person type.
model = rai.Model("socialNetwork")
Person = model.Type("Person")
# Add some people to the model and connect them with a multi-valued `follows` property.
with model.rule():
alice = Person.add(name="Alice")
bob = Person.add(name="Bob")
carol = Person.add(name="Carol")
daniel = Person.add(name="Daniel")
evelyn = Person.add(name="Evelyn")
alice.follows.add(bob)
carol.follows.add(daniel)
# Create a directed graph with Person nodes and edges between followers.
# Note that graphs are directed by default.
graph = Graph(model)
graph.Node.extend(Person)
graph.Edge.extend(Person.follows)
# Find the community label for a single person using the Infomap algorithm.
with model.query() as select:
community = graph.compute.infomap(Person(name="Alice"))
response = select(alias(community, "community_label"))
print(response.results)
# Output:
# community_label
# 0 2
# Find the community label for each person in the graph.
with model.query() as select:
person = Person()
community = graph.compute.infomap(person)
response = select(person.name, alias(community, "community_label"))
print(response.results)
# name community_label
# 0 Alice 2
# 1 Bob 2
# 2 Carol 1
# 3 Daniel 1
# 4 Evelyn 3
In this example, .infomap()
finds three communities in the graph:
Alice and Bob are in one community, and Carol and Daniel are in another.
Evelyn is an isolated node and has been assigned a unique community ID.