Skip to content

Instantly share code, notes, and snippets.

@nicysneiros
Created September 14, 2016 21:37
Show Gist options
  • Save nicysneiros/5a34f09a0f4e080988f4ddf60874969b to your computer and use it in GitHub Desktop.
Save nicysneiros/5a34f09a0f4e080988f4ddf60874969b to your computer and use it in GitHub Desktop.
from py2neo import Graph
from igraph import Graph as IGraph
graph = Graph()
query = '''
MATCH (c1:Character)-[r:INTERACTS]->(c2:Character)
RETURN c1.name, c2.name, r.weight AS weight
'''
ig = IGraph.TupleList(graph.run(query), weights=True)
clusters = IGraph.community_walktrap(ig, weights="weight").as_clustering()
nodes = [{"name": node["name"]} for node in ig.vs]
for node in nodes:
idx = ig.vs.find(name=node["name"]).index
node["community"] = clusters.membership[idx]
write_clusters_query = '''
UNWIND {nodes} AS n
MATCH (c:Character) WHERE c.name = n.name
SET c.community = toInt(n.community)
'''
graph.run(write_clusters_query, nodes=nodes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment