from py2neo import Graph
graph = Graph()
graph.neo4j_version
from scripts.vis import draw
options = {
"Officer": "name",
"Entity": "name",
"Intermediary": "name",
"Address": "address"
}
draw(graph, options, physics=True, limit=30)
%load_ext cypher
import plotly.plotly as py
from plotly.graph_objs import *
result = %cypher MATCH (e:Entity) \
RETURN e.name AS entity, size(()-[:BENEFICIARY_OF]->(e)) AS beneficiaries \
ORDER BY beneficiaries DESC \
LIMIT 5
df = result.get_dataframe()
df.head()
data = Data([Bar(x=df["entity"], y=df["beneficiaries"])])
py.image.ishow({'data': data})
result = %cypher MATCH (e:Entity) \
WITH e, size(()-[:SHAREHOLDER_OF]->(e)) AS shareholders \
ORDER BY shareholders DESC \
LIMIT 50 \
\
WITH collect(e) AS top_e \
UNWIND top_e AS e1 \
UNWIND top_e AS e2 \
\
MATCH (e1)<-[:SHAREHOLDER_OF]-(:Officer)-[:SHAREHOLDER_OF]->(e2) \
WHERE e1.name < e2.name \
RETURn e1.name, e2.name, count(*) AS weight \
ORDER BY weight DESC
df = result.get_dataframe()
df.head()
names = list(set(list(df["e1.name"]) + list(df["e2.name"])))
heat = [[0 for i in range(len(names))] for j in range(len(names))]
for idx, row in df.iterrows():
i = names.index(row["e1.name"])
j = names.index(row["e2.name"])
heat[i][j] = row["weight"]
import plotly.graph_objs as go
data = [go.Heatmap(z = heat, x = names, y = names)]
py.image.ishow({'data': data})
from igraph import Graph as IGraph
query = """
MATCH (o:Officer)
WITH o, size((o)-[:SHAREHOLDER_OF]->()) AS entities
ORDER BY entities DESC
LIMIT 100
WITH collect(o) AS top_o
UNWIND top_o AS o1
UNWIND top_o AS o2
MATCH (o1)-[:SHAREHOLDER_OF]->(:Entity)<-[:SHAREHOLDER_OF]-(o2)
WHERE o1.name < o2.name
RETURn o1.name, o2.name, count(*) AS weight
ORDER BY weight DESC
"""
data = graph.run(query)
ig = IGraph.TupleList(data, weights=True)
ig
$betweenness(v) = \sum_{s, t \in V} \frac{\sigma_{st}(v)}{\sigma_{st}}$
The betweenness centrality of a node $v$ is the number of shortest paths that pass through $v$, $\sigma_{st}(v)$, divided by the total number of shortest paths, $\sigma_{st}$.
between = [(node["name"], node.betweenness()) for node in ig.vs]
top = sorted(between, key=lambda x: x[1], reverse=True)
top[:5]
clusters = IGraph.community_walktrap(ig, weights="weight")
clusters = clusters.as_clustering()
len(clusters)
nodes = [{"id": node.index, "label": node["name"]} for node in ig.vs]
for node in nodes:
node["group"] = clusters.membership[node["id"]]
nodes[:5]
edges = [{"from": x[0], "to": x[1]} for x in ig.get_edgelist()]
edges[:5]
from scripts.vis import vis_network
vis_network(nodes, edges, physics=True)
import jgraph
query = """
MATCH (off:Officer)-[:REGISTERED_ADDRESS]->(:Address)<-[:REGISTERED_ADDRESS]-(ent:Entity)
RETURN id(off), id(ent)
LIMIT 100
"""
data = graph.run(query)
tup = [tuple(x) for x in data]
jgraph.draw(tup)