Note
To run this notebook in JupyterLab, load examples/ex6_1.ipynb
Warning: this is a work-in-progress draft
Discover community structure using igraph
and leidenalg
¶
See also: https://arxiv.org/abs/1810.08473
import igraph as ig
import leidenalg as la
Create a graph based on the Zachary karate club, a famous example used in network science:
G = ig.Graph.Famous("Zachary")
Find a partition, i.e., detect communities with modularity, then plot using python-igraph
and cairocffi
:
partition = la.find_partition(G, la.ModularityVertexPartition)
ig.plot(partition)
Integration with kglab
¶
import kglab
namespaces = {
"nom": "http://example.org/#",
"wtm": "http://purl.org/heals/food/",
"ind": "http://purl.org/heals/ingredient/",
"skos": "http://www.w3.org/2004/02/skos/core#",
}
kg = kglab.KnowledgeGraph(
name = "A recipe KG example based on Food.com",
base_uri = "https://www.food.com/recipe/",
namespaces = namespaces,
)
kg.load_rdf("../dat/recipes.ttl")
<kglab.kglab.KnowledgeGraph at 0x10f817350>
sparql = """
SELECT ?subject ?object
WHERE {
?subject rdf:type wtm:Recipe .
?subject wtm:hasIngredient ?object .
}
"""
subgraph = kglab.SubgraphMatrix(kg, sparql)
ig_graph = subgraph.build_ig_graph()
ig_graph.vs[0].attributes()
{'name': 'http://example.org/#Batter', 'label': 'http://example.org/#Batter'}
Serialize to GraphML
format:
with open("tmp.graphml", "w") as f:
ig_graph.write_graphml(f)
Community detection¶
component = ig_graph.components().subgraph(0)
partition = la.find_partition(component, la.ModularityVertexPartition)
ig.plot(partition, bbox=(600, 500), vertex_label_size=5, margin=50)
Let's look into the community that involves ind:Butter
, nom:Pancake
, etc.
ig.plot(partition.subgraph(2), bbox=(600, 500), vertex_label_size=7, margin=50)
Last update: 2021-01-21