Skip to content

Commit

Permalink
ran over the graph algorithms conversion and made corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Baktir committed May 6, 2020
1 parent 1a5afb4 commit b816ad0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 70 deletions.
18 changes: 0 additions & 18 deletions examples/cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 32,5 @@ def cypher():
print(nxneo4j.betweenness_centrality(G))
print(nxneo4j.closeness_centrality(G))

config = {
"node_label": """\
MATCH (p:Person) RETURN id(p) AS id
""",
"relationship_type": """\
MATCH (p1:Person)-[:FRIENDS]-(p2:Person)
RETURN id(p1) AS source, id(p2) AS target
""",
"identifier_property": "name",
"graph": "cypher"
}

G = nxneo4j.Graph(driver, config)

print(nxneo4j.betweenness_centrality(G))
print(nxneo4j.closeness_centrality(G))


if __name__ == '__main__':
cypher()
18 changes: 0 additions & 18 deletions examples/networkx_vs_neo4j.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 9,8 @@
"pagerank": nx.pagerank,
"triangles": nx.triangles,
"clustering": nx.clustering,
"average_clustering": nx.average_clustering,
"label_propagation_communities": nx.algorithms.community.label_propagation_communities,
"shortest_path": nx.shortest_path,
"connected_components": nx.connected_components,
"number_connected_components": nx.number_connected_components
}

neo4j_functions = {
Expand All @@ -22,11 19,8 @@
"pagerank": nxneo4j.pagerank,
"triangles": nxneo4j.triangles,
"clustering": nxneo4j.clustering,
"average_clustering": nxneo4j.average_clustering,
"label_propagation_communities": nxneo4j.community.label_propagation_communities,
"shortest_path": nxneo4j.shortest_path,
"connected_components": nxneo4j.connected_components,
"number_connected_components": nxneo4j.number_connected_components
}

driver = GraphDatabase.driver(uri="bolt://localhost",auth=("neo4j","neo"))
Expand Down Expand Up @@ -60,24 54,12 @@ def execute_graph(G, functions):
clustering = functions["clustering"]
print("Clustering Coefficient: {0}".format(clustering(G)))

average_clustering = functions["average_clustering"]
print("Average Clustering Coefficient: {0}".format(average_clustering(G)))

lpa = functions["label_propagation_communities"]
print("Label Propagation: {0}".format(list(lpa(G))))

shortest_path = functions["shortest_path"]
print("Shortest Path: {0}".format(shortest_path(G, 1, 5, 'weight')))
print("Single Shortest Path: {0}".format(shortest_path(G, 1)))

connected_components = functions["connected_components"]
print("Connected Components: {0}".format(list(connected_components(G))))

number_connected_components = functions["number_connected_components"]
print("# Connected Components: {0}".format(number_connected_components(G)))



if __name__ == '__main__':
print("Neo4j")
execute_graph(nxneo4j.Graph(GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo"))),
Expand Down
41 changes: 7 additions & 34 deletions nxneo4j/base_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 11,7 @@ def __init__(self, driver, direction, config=None):
self.identifier_property = config.get("identifier_property", "id")

add_node_query = """\
MERGE (:`%s` {`%s`: $value })
MERGE (:`%s` {`%s`: $value })
"""

def base_params(self):
Expand Down Expand Up @@ -114,16 114,15 @@ def betweenness_centrality(self):
orientation: $direction,
properties: {}
}
})
}})
YIELD nodeId, centrality
RETURN gds.util.asNode(nodeId).`%s` AS node, centrality
ORDER BY centrality DESC, node ASC
"""

def closeness_centrality(self, wf_improved=True):
def closeness_centrality(self,wf_improved=True):
with self.driver.session() as session:
params = self.base_params()
params["wfImproved"] = wf_improved
query = self.closeness_centrality_query % self.identifier_property

result = {row["node"]: row["centrality"] for row in session.run(query, params)}
Expand Down Expand Up @@ -187,31 186,6 @@ def clustering(self):
result = {row["node"]: row["coefficient"] for row in session.run(query, params)}
return result

triangle_query = """\
CALL gds.alpha.triangleCount.stream({
nodeProjection: $nodeLabel,
relationshipProjection: {
relType: {
type: $relationshipType,
orientation: $direction,
properties: {}
}
},
clusteringCoefficientProperty: 'clusteringCoefficient'
})
YIELD nodeId, triangles, coefficient
WITH gds.util.asNode(nodeId) AS node, coefficient, triangles
RETURN node, triangles, coefficient
ORDER BY triangles DESC
"""

def average_clustering(self):
with self.driver.session() as session:
params = self.base_params()
query = self.triangle_query % (self.relationship_type)
result = session.run(query,params)
result.peek()["clusteringCoefficient"]

lpa_query = """\
CALL gds.labelPropagation.stream({
nodeProjection: $nodeLabel,
Expand All @@ -225,9 199,8 @@ def average_clustering(self):
relationshipWeightProperty: null
})
YIELD nodeId, communityId AS community
WITH gds.util.asNode(nodeId).`%s` AS node, community
RETURN node, community
ORDER BY community DESC
MATCH (n) WHERE id(n) = nodeId
RETURN community, collect(n.`%s`) AS nodes
"""

def label_propagation(self):
Expand All @@ -248,12 221,12 @@ def label_propagation(self):
relType: {
type: $relationshipType,
orientation: $direction,
properties: {$propertyName}
properties: {}
}
},
startNode: source,
endNode: target,
relationshipWeightProperty: null
relationshipWeightProperty: $propertyName
})
YIELD nodeId, cost
RETURN gds.util.asNode(nodeId).`%s` AS node, cost
Expand Down

0 comments on commit b816ad0

Please sign in to comment.