def cypher(): driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo")) with driver.session() as session: session.run(create_friends_query) config = { "node_label": "Person", "relationship_type": "FRIENDS", "identifier_property": "name" } G = nxneo4j.Graph(driver, config) 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))
def get_harmonic_centrality(self): with self._driver.session() as session: G = nxneo4j.Graph(self._driver, config) sorted_hc = sorted(nxneo4j.centrality.harmonic_centrality(G).items(), key=lambda x: x[1], reverse=True) for name, score in sorted_hc[:10]: print(name, score) return sorted_hc
def get_page_rank(self): with self._driver.session() as session: G = nxneo4j.Graph(self._driver, config) sorted_pagerank = sorted(nxneo4j.centrality.pagerank(G).items(), key=lambda x: x[1], reverse=True) for name, score in sorted_pagerank[:10]: print(name, score) return sorted_pagerank
def cypher(): driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo")) nodes = """\ MATCH (c:Category) RETURN id(c) AS id """ relationships = """\ MATCH (c1:Category)<-[:IN_CATEGORY]-()-[:IN_CATEGORY]->(c2:Category) RETURN id(c1) AS source, id(c2) AS target, count(*) AS weight """ config = { "node_label": nodes, "relationship_type": relationships, "identifier_property": "name", "graph": "cypher" } G = nxneo4j.Graph(driver, config) for community in nxneo4j.label_propagation_communities(G): print(community) pr = nxneo4j.pagerank(G) sorted_pr = sorted(pr.items(), key=operator.itemgetter(1), reverse=True) for category, score in sorted_pr[:10]: print(category, score)
def get_closeness_centrality(self): with self._driver.session() as session: G = nxneo4j.Graph(self._driver, config) sorted_cc = sorted(nxneo4j.centrality.closeness_centrality(G, wf_improved=False).items(), key=lambda x: x[1], reverse=True) for name, score in sorted_cc[:10]: print(name, score) return sorted_cc
def _run_internal(self): G = nxneo4j.Graph(self.graph.driver, config={ 'node_label': self.node_label, 'relationship_type': None, 'identifier_property': 'uri' }) pr = sorted(nxneo4j.centrality.pagerank(G).items(), key=lambda x: x[1], reverse=True) self.algorithm_results = pr
import pytest from neo4j import GraphDatabase, basic_auth import nxneo4j import networkx as nx from networkx.exception import NetworkXError driver = GraphDatabase.driver("bolt://localhost:7687") config = { "node_label": "Food", "relationship_type": "CONTAINS", "identifier_property": "name" } G = nxneo4j.Graph(driver, config) def test_clear(): G.clear() assert len(G) == 0 assert len(G.edges) == 0 def test_add_node(): G.clear() G.add_node("Strawberry", color="red") assert len(G) == 1 assert len(G.edges) == 0 G.add_node("Blackberry", color="black") assert len(G) == 2 G.add_node("Strawberry", sweet=True)
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"))), neo4j_functions) print() print("networkx") execute_graph(nx.Graph(), networkx_functions)
def get_shortest_path(self, p1, p2): with self._driver.session() as session: G = nxneo4j.Graph(self._driver, config) return nxneo4j.path_finding.shortest_path(G, p1, p2)
#%% Import e inicialização da conexão from neo4j import GraphDatabase import nxneo4j as nx driver = GraphDatabase.driver(uri="bolt://localhost:7687", auth=("neo4j", "test")) config = { 'node_label': 'Pesquisador', 'relationship_type': 'COLABOROU_COM', 'identifier_property': 'id' } G = nx.Graph(driver, config) # %% Pagerank nx.pagerank(G) # %% Betweenness centrality nx.betweenness_centrality(G) # %% Closeness centrality nx.closeness_centrality(G) # %% Componentes conectados list(nx.connected_components(G))
# %% Import e inicialização da conexão from neo4j import GraphDatabase import nxneo4j as nx import wget, os import pandas as pd import numpy as np driver = GraphDatabase.driver(uri="bolt://localhost:7687", auth=("neo4j", "test")) G = nx.Graph(driver) # %% Primeiros testes G.delete_all() G.add_node(1) #single node G.add_nodes_from([2, 3, 4]) #multiple nodes G.add_edge(1, 2) #single edge G.add_edges_from([(2, 3), (3, 4)]) #multiple edges G.add_node('Mike', gender='M', age=17) G.add_edge('Mike', 'Jenny', type='friends', weight=3) nx.draw(G) # %% Deleta os dados existentes e insere os novos dados G.delete_all() colaboracao = pd.read_csv('dados\collaboration.edgelist.txt', delimiter='\t', header=None) lista_df = np.array_split(colaboracao, 20) # %% for resultado in lista_df:
# Trying to make a better model by using graph algorithms # query = """ # LOAD CSV WITH HEADERS FROM 'file:///my_got.csv' AS line # WITH line # WHERE line.house IS NOT NULL # MERGE(person:Person {Name: line.name}) # MERGE(house:House {Name: line.house}) # MERGE(person) - [:belongs_to] -> (house) # """ #_to_neo4jDB = graph.run(query) print("-------------------------------------------") G = netneo.Graph(driver) G.delete_all() G.load_got() G.identifier_property = 'name' G.relationship_type = '*' G.node_label = 'Character' # Graph Algorithm 1 # the most influential characters # PageRank Algorithm # query = ''' # CALL gds.pageRank.stream('prGraph') # YIELD nodeId, score # RETURN gds.util.asNode(nodeId).Name AS name, score as pageRank # ORDER BY pageRank DESC