Commit bc60e0a6 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

python-louvain submodule added

parent 741514c3
.DS_Store .DS_Store
.stack-work .stack-work
.stack-work-profile
.idea .idea
*.log *.log
tmp/ tmp/
__pycache__
[submodule "python-louvain"]
path = python-louvain
url = https://github.com/taynaud/python-louvain
[submodule "src/Data/Graph/Clustering/python/python-louvain"]
path = src/Data/Graph/Clustering/python/python-louvain
url = https://github.com/taynaud/python-louvain
Subproject commit 381b7db8196f43de98d5279746173b50fbb2bea9
Subproject commit 381b7db8196f43de98d5279746173b50fbb2bea9
#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.join(os.environ['PWD'], 'python', 'python-louvain'))
import networkx as nx
from community.community_louvain import generate_dendrogram, partition_at_level, __one_level, __modularity, best_partition
from community.community_status import Status
def communities(parted):
ret = {}
for n, c in parted.items():
ret.setdefault(c, [])
ret[c].append(n)
return ret
simpleGraph = nx.Graph()
simpleGraph.add_edges_from([
(1, 2, {'weight': 1.0}),
(2, 3, {'weight': 2.0}),
])
dendo = generate_dendrogram(simpleGraph)
part = partition_at_level(dendo, len(dendo) - 1)
print((simpleGraph.nodes, simpleGraph.edges))
print(dendo)
print(part)
status = Status()
status.init(simpleGraph, 'weight', None)
__one_level(simpleGraph, status, 'weight', 1.0)
new_mod = __modularity(status)
print(new_mod)
print(best_partition(simpleGraph))
cuiller = nx.Graph()
cuiller.add_edges_from([
(2, 1, {'weight': 1}),
(1, 2, {'weight': 1}),
(1, 4, {'weight': 1}),
(4, 1, {'weight': 1}),
(2, 3, {'weight': 1}),
(3, 2, {'weight': 1}),
(3, 4, {'weight': 1}),
(4, 3, {'weight': 1}),
(4, 5, {'weight': 1}),
(5, 4, {'weight': 1}),
])
print(best_partition(cuiller))
karateEdges = [(1,2,1.0),(1,3,1.0),(1,4,1.0),(1,5,1.0),(1,6,1.0),(1,7,1.0),(1,8,1.0),(1,9,1.0),(1,11,1.0),(1,12,1.0),(1,13,1.0),(1,14,1.0),(1,18,1.0),(1,20,1.0),(1,22,1.0),(1,32,1.0),(2,3,1.0),(2,4,1.0),(2,8,1.0),(2,14,1.0),(2,18,1.0),(2,20,1.0),(2,22,1.0),(2,31,1.0),(3,4,1.0),(3,8,1.0),(3,9,1.0),(3,10,1.0),(3,14,1.0),(3,28,1.0),(3,29,1.0),(3,33,1.0),(4,8,1.0),(4,13,1.0),(4,14,1.0),(5,7,1.0),(5,11,1.0),(6,7,1.0),(6,11,1.0),(6,17,1.0),(7,17,1.0),(9,31,1.0),(9,33,1.0),(9,34,1.0),(10,34,1.0),(14,34,1.0),(15,33,1.0),(15,34,1.0),(16,33,1.0),(16,34,1.0),(19,33,1.0),(19,34,1.0),(20,34,1.0),(21,33,1.0),(21,34,1.0),(23,33,1.0),(23,34,1.0),(24,26,1.0),(24,28,1.0),(24,30,1.0),(24,33,1.0),(24,34,1.0),(25,26,1.0),(25,28,1.0),(25,32,1.0),(26,32,1.0),(27,30,1.0),(27,34,1.0),(28,34,1.0),(29,32,1.0),(29,34,1.0),(30,33,1.0),(30,34,1.0),(31,33,1.0),(31,34,1.0),(32,33,1.0),(32,34,1.0),(33,34,1.0)]
karate = nx.Graph()
karate.add_edges_from([(s, t, {'weight': w}) for (s, t, w) in karateEdges])
karate_bp = best_partition(karate)
print(karate_bp)
print(communities(karate_bp))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment