Created
September 14, 2016 21:37
-
-
Save nicysneiros/5a34f09a0f4e080988f4ddf60874969b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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