def authenticate_and_create_graph(credentials): """ This function authenticates with neo4j and creates a py2neo graph object Arguments: credentials (dict): a dictionary of credentials used to authenticate, and then create, a py2neo graph object. Returns: a py2neo `Graph` object. """ host = credentials['host'] https_port = credentials['https_port'] http_port = credentials['http_port'] secure = credentials['secure'] neo4j_user = credentials['user'] neo4j_password = credentials['password'] authenticate( "{host}:{port}".format(host=host, port=https_port if secure else http_port), neo4j_user, neo4j_password, ) graph = Graph( bolt=True, password=neo4j_password, user=neo4j_user, https_port=https_port, http_port=http_port, host=host, secure=secure, ) return graph
def getTestedNeo4jDB(graphDBurl, graphDbCredentials): '''Gets a Neo4j url and returns a GraphDatabaseService to the database after having performed some trivial tests''' try: if graphDbCredentials: authenticate(*graphDbCredentials) graphDb = Graph(graphDBurl) #just fetch a Node to check we are connected #even in DRY RUN we should check Neo4j connectivity #but not in OFFLINE MODE if not OFFLINE_MODE: _ = iter(graphDb.match(limit=1)).next() except StopIteration: pass except SocketError as ex: raise DbNotFoundException(ex, "Could not connect to Graph DB %s." % graphDBurl) if not DRY_RUN and not OFFLINE_MODE: try: test_node = Node("TEST", data="whatever") graphDb.create(test_node) graphDb.delete(test_node) except Exception as ex: raise DBInsufficientPrivileges(\ "Failed on trivial operations in DB %s." % graphDBurl) return graphDb
def access_graphdb(): #Verifies that an existing Neo4j database exists and that it is #populated. Does not modify the database. if not is_service_running('neo4j'): print("Starting neo4j service....") os.system("sudo service neo4j start") time.sleep(5) authenticate("localhost:7474", "neo4j", "pining") try: #Just access graph and retrieve stats g = Graph("http://localhost:7474/db/data/") interactions = (g.data("MATCH p=()-[r:interacts_with]->() RETURN p")) member_ofs = (g.data("MATCH p=()-[r:member_of]->() RETURN p")) if not interactions or not member_ofs: sys.exit("Graph is empty or is missing expected relationships. " "Exiting.") else: print( "Graph contains %s protein interactions and %s OG memberships." % (len(interactions), len(member_ofs))) except (py2neo.packages.httpstream.http.SocketError, py2neo.database.status.Unauthorized) as e: print("**Error accessing the Neo4j database: %s" % e) print("**Please try accessing the server at http://localhost:7474/") sys.exit()
def FindSimilarRepositories(InputrepoK): #Sanitize input Inputrepo = bleach.clean(InputrepoK).strip() host = os.environ['LOCALNEO4JIPPORT'] login = os.environ['LOCALNEO4JLOGIN'] password = os.environ['LOCALNEO4JPASSWORD'] authenticate(host,login,password) graph = Graph(os.environ['neoURLlocal']) output = "" path1 = "<a href=\"/?q=repository " path2 = "&action=Search\" class=\"repositoryinfo\">" path3 = "</a>" #Find similar repository > 1 connections query1="MATCH (a {id:\"" + Inputrepo + "\"})" query2="-[r1:IS_ACTOR|IN_ORGANIZATION]->(match)<-[r2:IS_ACTOR|IN_ORGANIZATION]-(b) " query3="with b, collect (distinct match.id) as connections, collect (distinct type(r1)) as rel1 " query4="where length(connections) >= 1 return b.id,length(connections) as count,length(rel1) as rel " query5="order by length(connections) desc limit 5" query = query1 + query2 + query3 + query4 + query5 #print query a = graph.cypher.execute(query) for record in a: if (record['rel'] < 2): output += "<li>" + path1 + record['b.id'] + path2 + record['b.id'] + path3 + ": " + str(record['count']) + " contributors in common</li>" else: output += "<li>" + path1 + record['b.id'] + path2 + record['b.id'] + path3 + ": " + str(record['count']-1) + " contributors in common & belong to same organization</li>" if (len(output) > 0): return ("<ul>" + output + "</ul>") else: #Nothing found! return "<span class=\"text-danger\">You got me stumped!</span>"
def connect_to_db(self, uri, username, password): if (uri is None) or (username is None) or\ (password is None): raise ValueError('Null Argument detected') authenticate(uri, username, password) return Graph()
def createRelationshipWithProperties(): print("Start - Creating Relationships") # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") # Create Node with Properties amy = Node("FEMALE", name="Amy") # Create one more Node with Properties kristine = Node("FEMALE",name="Kristine") # Create one more Node with Properties sheryl = Node("FEMALE",name="Sheryl") #Define an Object of relationship which depicts the relationship between Amy and Kristine #We have also defined the properties of the relationship - "since=2005" #By Default the direction of relationships is left to right, i.e. the --> kristine_amy = Relationship(kristine,"FRIEND",amy,since=2005) #This relationship is exactly same as the earlier one but here we are using "Rev" #"py2neo.Rev = It is used to define the reverse relationship (<--) between given nodes amy_sheryl=Relationship(amy,Rev("FRIEND"),sheryl,since=2001) #Finally use graph Object and Create Nodes and Relationship #When we create Relationship between, then Nodes are also created. resultNodes = graph.create(kristine_amy,amy_sheryl) #Print the results (relationships) print("Relationship Created - ",resultNodes)
def main(): # Connect to neo4j authenticate("localhost:7474", "neo4j", "neo4jpw") graph = Graph("localhost:7474/db/data/") pathway_list = read_pathway_list("C:/Databases/KEGG/pathway.list") main_pathway = "Metabolism" use_pathways = [i[2] for i in pathway_list if i[0] in main_pathway] use_pathways.remove('01100') metabolic_reactions = read_kegg_xml("C:/Databases/KEGG/kgml/ko", use_pathways=use_pathways) # Read in files and create a new database reactions = kegg_reactions("C:/Databases/KEGG/reaction/reaction") enzymes = kegg_enzymes("C:/Databases/KEGG/enzyme/enzyme") rclass = kegg_rclass("C:/Databases/KEGG/rclass/rclass") compounds = kegg_compounds("C:/Databases/KEGG/compound/compound") triples = find_triples(rclass) # Create a new database using rclass triples # create_db_from_triples(triples, rclass, compounds, graph) # Create a new database using metabolic reactions parsed from xml files # create_db_from_xml(metabolic_reactions, graph, reactions, enzymes, compounds) # Create a new database using reactions create_db_from_reactions(reactions, graph, enzymes, compounds, rclass) # Test database test_database(graph) return 0
def getProvinceTime(province, time): authenticate("localhost:7474", "neo4j", "liyi193328") graph = Graph() if province == "": province = '.*' data = [] if time == 2004: ngoList = graph.cypher.execute( "match (n:ngo) where n.province =~ {province} return id(n),n.name;", {"province": province}) elif time == 2005: ngoList = graph.cypher.execute( "match (n:ngo) where n.province =~ {province} and toInt( n.time ) < 2006 return id(n),n.name;", {"province": province}) else: ngoList = graph.cypher.execute( "match (n:ngo) where n.province =~ {province} and n.time = {time} return id(n),n.name;", { "province": province, "time": time }) for perNgo in ngoList: row = {} row["id"] = perNgo[0] row['name'] = perNgo[1] data.append(row) return data
def set_graph(self, config_path): """Define graph instance with data from config file. Args: config_path(string): path to a config.json file. Returns: Graph or None if failed to define. """ self.graph = None with open(config_path) as config_data: config = json.load(config_data) host = config['host'] if host['use_ssl']: db_url = 'https://' elif host['use_bolt']: db_url = 'bolt://' else: db_url = 'http://' db_url += '%s:%d/%s' % (host['address'], host['port'], host['data_path']) authenticate(host['address'] + ':' + str(host['port']), user=host['username'], password=host['password']) self.graph = Graph(db_url) return self.graph
def import_api_data2(): authenticate("localhost:7474", "neo4j", "1111") graph = Graph() #graph.delete_all() # Uncomment on the first run! #graph.schema.create_uniqueness_constraint("Borjnuk", "id") #graph.schema.create_uniqueness_constraint("Obtaj", "id") #graph.schema.create_uniqueness_constraint("Property", "id") obtajenna = get_objects_art('obtaj') for api_obtaj in obtajenna: node_obtaj= graph.merge_one("Obtaj", "id", api_obtaj["id"]) node_obtaj["reason_doc"] = api_obtaj["reason_doc"] node_obtaj["cost_size"] = api_obtaj["cost_size"] for api_author in api_obtaj["borjnuku"]: node_borjnuk = graph.merge_one("Borjnuk", "id", api_author["id"]) node_borjnuk["name"] = api_author["name"] node_borjnuk["tel_number"] = api_author["tel_number"] node_borjnuk.push() graph.create_unique(Relationship(node_borjnuk, "obtajuetsa", node_obtaj)) for api_property in api_obtaj["properties"]: node_property = graph.merge_one("Property", "id", api_property["id"]) node_property["name"] = api_property["name_property"] node_property["ser_number"] = api_property["ser_number"] node_property.push() graph.create_unique(Relationship(node_property, "zakladena", node_obtaj)) node_obtaj.push()
def authenticate(self, username, password): try: from py2neo import authenticate authenticate(self.host_port, username, password) return True except ImportError: return False
def __init__(self): self.logger = open("zalando_logger","a") self.logger.write("Checking for directory...\n") self.logger.write("Creating Zalando Object...\nInitializing Object variables...\n") self.set_directory() self.BASE_URL = "https://www.zalando.co.uk" self.initial_page = ['https://www.zalando.co.uk/womens-clothing','https://www.zalando.co.uk/mens-clothing'] self.parameters = ['Women','Men'] self.subcategories = [dict() for x in range(2)] authenticate("localhost:7474","neo4j","awdr.;/") self.zalando_graph = Graph() try: self.zalando_graph.schema.create_uniqueness_constraint('Zalando', 'article_number') except Exception as e: self.logger.write("uniqueness constraint already set\n") self.count = 0 self.xml_root = Element('Properties') self.properties = open("clothing_properties.xml","w") self.logger.write("Zalando Object: " + str(self)+"\n") for i in range(len(self.initial_page)): page_html = requests.get(self.initial_page[i]).content page_soup = BeautifulSoup(page_html,"lxml") self.get_clothing_attributes(page_soup,i) self.build_zalando_database(page_soup,self.parameters[i]) self.set_attributes(i) self.properties.write(tostring(self.xml_root))
def addArgumentToGraph(argument): authenticate(settings.SECRET_NEO4J_DB_HOSTPORT, settings.SECRET_NEO4J_DB_USER, settings.SECRET_NEO4J_DB_PASSWORD) graph = Graph() tx = graph.begin() argumentNode = Node("Argument", argument_id=argument.id, name=argument.name) tx.merge(argumentNode, 'argument_id') for p in ArgumentPremise.objects.filter(argument_id=argument.id): premiseClaim = p.claim premiseClaimNode = Node("Claim", claim_id=premiseClaim.id, name=premiseClaim.name, content=premiseClaim.content) tx.merge(premiseClaimNode, 'claim_id') premiseOfRelationship = Relationship(premiseClaimNode, "Premise_Of", argumentNode, argumentpremise_id=p.id) tx.merge(premiseOfRelationship, 'argumentpremise_id') supportedClaim = argument.supported_claim supportedClaimNode = Node("Claim", claim_id=supportedClaim.id, name=supportedClaim.name, content=supportedClaim.content) tx.merge(supportedClaimNode, 'claim_id') supportsRelationship = Relationship(argumentNode, "Supports", supportedClaimNode, argument_id=argument.id) tx.merge(supportsRelationship, 'argument_id') tx.commit()
def query8(): hashtag = request.form['query8'] authenticate("localhost:7474", "neo4j", "qwertyuiop") g = Graph() a = g.data("""match(u:User)-[:POSTS]->(t:Tweet)-[:HAS_HASH]->(h:Hashtag) where h.hashtag=\'%s\' return h.hashtag as Hashtag,u.screen_name as User,Collect(t.tid) as Tid,count(*) as Tweet_count order by Tweet_count Desc LIMIT 3 """ % hashtag) page = " <style> table, th, td { border: 1px solid black; } </style>" page = page + "<table> <tr> <th> Hashtag </th> <th> User </th> <th> Tid's </th> <th> Tweet_Count </th> </tr> " for row in a: page = page + "<tr> " page += "<td> %s </td>" % repr(json.dumps(row['Hashtag'])) page += "<td> %s </td>" % repr(json.dumps(row['User'])) page += "<td> %s </td>" % repr(json.dumps(row['Tid'])) page += "<td> %s </td>" % repr(json.dumps(row['Tweet_count'])) page += "</tr>" page += "</table>" page = page + "<img src=\"/static/graph_query8.png\" width=1000px height=700px >" return page
def query1(): user = request.form['query1'] authenticate("localhost:7474", "neo4j", "qwertyuiop") g = Graph() a = g.data("""match (u1:User)<-[:MENTIONS]-(t:Tweet)-[:MENTIONS]-(u2:User) where u1.screen_name='%s' return u1.screen_name as User1,u2.screen_name as User2, Collect(t.tid) as Tid,count(*) as co_mentioncount Order by co_mentioncount DESC""" % user) page = " <style> table, th, td { border: 1px solid black; } </style>" page = page + "<table> <tr> <th> Mentioned_User1 </th> <th> Mentioned_User2 </th> <th> Tid's </th> <th> Total_Mentions </th> </tr> " for row in a: page = page + "<tr> " page += "<td> %s </td>" % repr(json.dumps(row['User1'])) page += "<td> %s </td>" % repr(json.dumps(row['User2'])) page += "<td> %s </td>" % repr(json.dumps(row['Tid'])) page += "<td> %s </td>" % repr(json.dumps(row['co_mentioncount'])) page += "</tr>" page += "</table>" page = page + "<img src=\"/static/graph_query1.png\" width=1000px height=700px >" return page
def __init__(self, **kwargs): super(BioKnowledgeDB, self).__init__(**kwargs) authenticate(NEO4J_HOST_PORT, NEO4J_USER, NEO4J_PWD) self.graph = Graph(NEO4J_URL) client = MongoClient(MONGODB_HOST, MONGODB_PORT) db = client.get_database(MONGODB_DBNAME) self.property_collection = db.get_collection(MONGODB_BIOLOGY_PROPERTY)
def following2(n): authenticate("localhost:7474", "neo4j", "parola") graph = Graph("http://localhost:7474/db/data/") followingResults = graph.cypher.execute("MATCH (a)-[r:FOLLOWS]->(b) WHERE id(a)= "+n+" RETURN b") followingName = [] for person in followingResults: cleanName = re.findall('"([^"]*)"', str(person[0])) cleanName[2] = str (int(cleanName[2]) - 1) followingName.append(cleanName) followerResults = graph.cypher.execute("MATCH (a)-[r:FOLLOWS]->(b) WHERE id(b)= "+n+" RETURN a") followerName = [] for person in followerResults: cleanName = re.findall('"([^"]*)"', str(person[0])) cleanName[2] = str (int(cleanName[2]) - 1) followerName.append(cleanName) thisUser = graph.cypher.execute("Match (joe) where id(joe)= "+n+" return joe") thisUserCleanName = re.findall('"([^"]*)"', str(thisUser[0][0])) userPostResult = graph.cypher.execute("MATCH (a)-[r:POSTED]->(b)<-[t:LIKES]-(q) WHERE id(a)="+n+" RETURN b.Name,b.id ,count(t) ORDER BY count(t) DESC") userPosts = [] for post in userPostResult: cleanName = re.findall('"([^"]*)"', str(post[0])) userPosts.append(userPostResult) recommendationsResult = graph.cypher.execute("MATCH (joe)-[:FOLLOWS*2..2]->(friend_of_friend) WHERE NOT (joe)-[:FOLLOWS]-(friend_of_friend) and friend_of_friend.id <> joe.id and id(joe) = "+n+" RETURN friend_of_friend.Name,friend_of_friend.Username,friend_of_friend.id,Count(*) ORDER BY COUNT(*) DESC , friend_of_friend.Name LIMIT 10") return render_template("index.html", title='Home', followings=followingName, followers=followerName, followingcount = len(followingName), followercount = len(followerName), thisUser = thisUserCleanName, posts=userPosts, recommendations = recommendationsResult)
def connectGraph(self): # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") return graph
def main(): define("host", default="127.0.0.1", help="Host IP") define("port", default=8080, help="Port") define("neo4j_host_port", default='127.0.0.1:7474', help="Neo4j Host and Port") define("neo4j_user_pwd", default='neo4j:neo4j', help="Neo4j User and Password") tornado.options.parse_command_line() print('Connecting to Neo4j at {0}...'.format(options.neo4j_host_port)) user = options.neo4j_user_pwd.split(':')[0] pwd = options.neo4j_user_pwd.split(':')[1] authenticate(options.neo4j_host_port, user, pwd) db = Graph('http://{0}/db/data/'.format(options.neo4j_host_port)) template_dir = os.getenv('OPENSHIFT_REPO_DIR', os.path.dirname(__file__)) template_dir = os.path.join(template_dir, 'templates') static_dir = os.getenv('OPENSHIFT_DATA_DIR', os.path.dirname(__file__)) static_dir = os.path.join(static_dir, 'static') settings = { 'static_path': static_dir, 'template_path': template_dir } application = Application([ (r'/image/([^/]*)', ImageHandler, dict(directory=cache_dir)), (r'/users/?', MainHandler, dict(db=db)), (r'/?', HomeHandler) ], **settings) print('Listening on {0}:{1}'.format(options.host, options.port)) application.listen(options.port, options.host) tornado.ioloop.IOLoop.instance().start()
def __init__(self): self.jsl_exporter = None self.pprnt_exporter = None self.files = {} authenticate('localhost:7474', 'neo4j', 'big-theta-team') self.graph = Graph('localhost:7474/db/data')
def upload_gene_interactions(neo_conf): http.socket_timeout = 10000000 user = neo_conf['user'] pw = neo_conf['pw'] host = neo_conf['host'] port = neo_conf['http_port'] db = neo_conf['db_name'] graph = neo_conf['graph_name'] auth_uri = '{}:{}'.format(host,port) authenticate(auth_uri, user, pw) db_uri = 'http://{}:{}@{}:{}/{}/{}'.format(user,pw,host,port,db,graph) g = Graph(db_uri, bolt=False) query = ''' USING PERIODIC COMMIT LOAD CSV FROM 'file:///home/calvin/Documents/bigdata/ad_knowledge_base/adkb/data/PPI.csv' AS row MERGE (p1:interactor {name: row[0]}) MERGE (p2:interactor {name: row[1]}) MERGE((p1)-[:INTERACTS_WITH]->(p2)); ''' g.run(query).dump()
def database_connection(): # the second is the user name for neo4j #the third is the password authenticate("localhost:7474", "neo4j", "test") global g g = Graph("http://localhost:7474/db/data/")
def GetNewGraph(self): ''' Class to new up a graph object so we can makes calls to the database ''' authenticate(self.neo4j_url, self.neo4j_user, self.neo4j_password) graph = Graph("http://" + self.neo4j_url) return graph
def filter_by_path(recommendations, user_interests): # print len(recommendations),len(user_interests) authenticate("localhost:7474", "neo4j", "password") res = [] g = Graph() min_len = -1 MAX_PATH = 3 for i in xrange(len(recommendations)): for j in xrange(len(user_interests)): query = "MATCH (from:Product { pid:'" + recommendations[i] + "' }), (to:Product { pid: '" + user_interests[j] + "'}) , path = shortestPath(from-[:TO*]->to ) RETURN path" results = g.cypher.execute(query) path_len = len(str(results).split(":TO")) - 1 # print "PATH LEN",path_len if path_len == 0: continue if path_len < MAX_PATH: min_len = path_len break # if min_len == -1 or path_len < min_len: # min_len = path_len # print "MIN LEN",min_len if min_len < MAX_PATH and min_len != -1: res.append(recommendations[i]) min_len = -1 return res
def connect(self): conf = self.get_config() authenticate(conf['host'] + ":" + conf['port'],conf['username'],conf["password"]) try: self.neo = Graph("http://" + conf['host'] + ":" + conf["port"] + "/db/data") except: print "Failed to connect!"
def getTestedNeo4jDB(graphDBurl, graphDbCredentials): '''Gets a Neo4j url and returns a GraphDatabaseService to the database after having performed some trivial tests''' try: if graphDbCredentials: authenticate(*graphDbCredentials) graphDb = Graph(graphDBurl) #just fetch a Node to check we are connected #even in DRY RUN we should check Neo4j connectivity #but not in OFFLINE MODE if not OFFLINE_MODE: _ = iter(graphDb.match(limit=1)).next() except StopIteration: pass except SocketError as ex: raise DbNotFoundException( ex, "Could not connect to Graph DB %s." % graphDBurl) if not DRY_RUN and not OFFLINE_MODE: try: test_node = Node("TEST", data="whatever") graphDb.create(test_node) graphDb.delete(test_node) except Exception as ex: raise DBInsufficientPrivileges(\ "Failed on trivial operations in DB %s." % graphDBurl) return graphDb
def connectGraph(): # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") return graph
def run(): print("\n=== Querying N-Order Interactions for Genes ===") authenticate("localhost:7474", "neo4j", "123456") graph = Graph() cypher = graph.cypher while True: query = input("\nPlease enter a Gene Entrez ID to query ('R' to return to main menu or 'X' to exit):\n> ") if query is 'R' or query is 'r': main.print_commands() break elif query is 'X' or query is 'x': print("Exiting program. Thank you for using Alzheimer’s Disease Knowledge Base.\n") quit() else: try: n = input("Please enter a positive integer as the N-Order:\n ") queryAOrder = "MATCH (gene:GeneA {entrezid:'" + query + "'})-[*" + n + "]-(GeneA) RETURN DISTINCT GeneA AS GenesYourQueryInteractsWith" queryBOrder = "MATCH (gene:GeneB {entrezid:'" + query + "'})-[*" + n + "]-(GeneB) RETURN DISTINCT GeneB AS GenesThatInteractWithYourQuery" print ("\n[=== Gene Interaction Query ===]") print ("Interactions for entrez ID " + query + " to the order of " + n + ":\n") print (cypher.execute(queryAOrder)) print (cypher.execute(queryBOrder)) except: print("Error: Invalid command or Entrez ID not found.")
def databasefilter(labelname, color='ALL'): print('--Database filter ALL/color--') from py2neo import Graph, Path, authenticate authenticate("localhost:7474", "neo4j", "japan123") graph = Graph() print(color) if (color != 'ALL'): color = '[\'' + color + '\']' print('databasefilter : color', color) tx = graph.cypher.execute( 'match (n) WHERE EXISTS(n.color) and n.name={x} and n.color={c} RETURN n.color,n.block_col,n.block_row,n.tlx,n.tly,n.brx,n.bry', c=color, x=labelname) else: tx = graph.cypher.execute( 'match (n) WHERE EXISTS(n.color) and n.name={x} RETURN n.color,n.block_col,n.block_row,n.tlx,n.tly,n.brx,n.bry', x=labelname) pixels = [] for i in tx: color = i['n.color'] tl = Point(int(i['n.tlx']), int(i['n.tly'])) br = Point(int(i['n.brx']), int(i['n.bry'])) p = Pixel(ast.literal_eval(color), tl, br) pixels.extend([p]) print('/t 1. retrieved all pixels. Total count is %d' % (len(pixels))) return pixels
def main(project_directory, ignore_files): authenticate("localhost:7474", "neo4j", "haruspex") graph_db = Graph() dossier = os.path.join(project_directory + "/pages") if os.path.exists(dossier): # Pour chaque fiche, analyser son contenu # et créer les noeuds/liens correspondants files = [f for f in listdir(dossier) if isfile(join(dossier, f))] for fiche in files: if (fiche not in ignore_files): fiche_analysee = analyseFiche(fiche, dossier, graph_db) ficheDocumentation(fiche_analysee, "references", dossier, ignore_files[0], graph_db) ficheDocumentation(fiche_analysee, "images", dossier, ignore_files[1], graph_db) else: files = [ f for f in listdir(project_directory) if (isfile(join(project_directory, f)) and f.endswith('.txt')) ] #TODO récupérer les métadonnées d'omeka sur les documents for document in files: print(document.strip(project_directory)) fiche = Fiche( document.replace(project_directory, '').replace('.txt', ''), '', '', '', '', '') fiche.create_node(graph_db)
def database_connection_test(prints=False): if prints: print '=' * 100 py2neo.authenticate(HOST, USER, PASS) try: graph = py2neo.Graph(GRAPH) if graph: if prints: print "databases is working..." return graph except Exception as err: print unidecode.unidecode(unicode(err.message)) try: print "trying to execute:", neo4j_path % "start" subprocess.call(neo4j_path % "start", shell=True) for x in range(0, 11): bar = '=' * x print '\rwaiting for server delay', '[', bar.ljust(10), ']', time.sleep(1) graph = py2neo.Graph(GRAPH) return graph except Exception as err: print "subprocess ERROR:", unidecode.unidecode(unicode(err)) raise err finally: if prints: print '=' * 100
def __init__(self): authenticate("localhost:7474", "neo4j", "vai") global graph graph = Graph("http://localhost:7474/db/data/") global minSimilarityScore minSimilarityScore=0.2 global fileName
def py2neo_view(request): """ py2neo (community driver) Pass through CQL query and return JSON response """ user, password = get_graphenedb_credentials() cqlQuery = request.body if not cqlQuery: return Response({'detail': 'Empty query'}, status=400) py2neo.authenticate(REST_ENDPOINT, user, password) graph = py2neo.Graph(BOLT_ENDPOINT, user=user, password=password, bolt=True, secure=True, http_port=24789, https_port=24780) try: results = graph.data(cqlQuery) return Response(results) except Exception as e: return Response({'detail': unicode(e)}, status=400)
def createRelationshipWithProperties(): print("Start - Creating Relationships") # Authenticate the user using py2neo.authentication # Ensure that you change the password 'sumit' as per your database configuration. py2neo.authenticate("localhost:7474", "neo4j", "sumit") # Connect to Graph and get the instance of Graph graph = Graph("http://localhost:7474/db/data/") # Create Node with Properties amy = Node("FEMALE", name="Amy") # Create one more Node with Properties kristine = Node("FEMALE", name="Kristine") # Create one more Node with Properties sheryl = Node("FEMALE", name="Sheryl") #Define an Object of relationship which depicts the relationship between Amy and Kristine #We have also defined the properties of the relationship - "since=2005" #By Default the direction of relationships is left to right, i.e. the --> kristine_amy = Relationship(kristine, "FRIEND", amy, since=2005) #This relationship is exactly same as the earlier one but here we are using "Rev" #"py2neo.Rev = It is used to define the reverse relationship (<--) between given nodes amy_sheryl = Relationship(amy, Rev("FRIEND"), sheryl, since=2001) #Finally use graph Object and Create Nodes and Relationship #When we create Relationship between, then Nodes are also created. resultNodes = graph.create(kristine_amy, amy_sheryl) #Print the results (relationships) print("Relationship Created - ", resultNodes)
def databasepatternfilter(x, y, labelname): print( '-- databasepatternfilter: retrieving pixels associated with the clicked pattern ' ) from py2neo import Graph, Path, authenticate authenticate("localhost:7474", "neo4j", "japan123") graph = Graph() t = x x = y y = t tx = graph.cypher.execute( 'match (n) WHERE EXISTS(n.color) and n.name={lb} and toFloat(n.tlx)<={x} and toFloat(n.brx)>={x} and toFloat(n.tly)<={y} and toFloat(n.bry)>={y} RETURN n.lbl,n.color', x=x, y=y, lb=labelname) print(tx[0]) color_val = tx[0]['n.color'] tx = tx[0]['n.lbl'] tx = graph.cypher.execute( 'match (n) WHERE EXISTS(n.color) and n.name={lb} and toFloat(n.lbl)={x} RETURN n.brx, n.bry,n.lbl,n.tlx,n.tly,n.color', x=int(tx), lb=labelname) pixels = [] for i in tx: tl = [int(i['n.tlx']), int(i['n.tly'])] br = [int(i['n.brx']), int(i['n.bry'])] color = i['n.color'] p = Pixel(ast.literal_eval(color), Point(tl[0], tl[1]), Point(br[0], br[1])) pixels.extend([p]) #for i in pixels: # print(str(i)) print('\t 1. Total pixels in the selected pattern is %d' % (len(pixels))) return pixels, color_val
def getClassLabelColorsFromDatabase(datasetName): print('--LOADING COLORDICT FROM DATABASE for classLabel') from py2neo import Graph, Path, authenticate, Node, Relationship authenticate("localhost:7474", "neo4j", "japan123") tx = Graph() opt = tx.cypher.execute( 'MATCH (n:colorClassLabel) where n.name={x} return keys(n)', x=datasetName) allColors = str(opt).split('[')[1].split(']')[0] #print(allColors) allColors = allColors.replace('\'', '') allColors = allColors.replace(' ', '') #print(allColors) allColors = allColors.replace('uH', 'H') #python2 allColors = allColors.replace('uname', 'name') #python2 allColors = allColors.split(',') print(allColors) allColors.remove('name') color_dict = {} for a in allColors: str1 = 'MATCH (n:colorClassLabel) where n.name=\'%s\' return n.%s' % ( datasetName, a) classLabel = tx.cypher.execute(str1) n = 'n.' + a classLabel = classLabel[0][n] color_dict[classLabel] = '#' + a[1:] return color_dict
def main(project_directory, ignore_files): authenticate("localhost:7474", "neo4j", "haruspex") graph_db = Graph() dossier = os.path.join(project_directory + "/pages") if os.path.exists(dossier): # Pour chaque fiche, analyser son contenu # et créer les noeuds/liens correspondants files = [f for f in listdir(dossier) if isfile(join(dossier, f))] for fiche in files: if (fiche not in ignore_files): fiche_analysee = analyseFiche(fiche, dossier, graph_db) ficheDocumentation(fiche_analysee, "references", dossier, ignore_files[0], graph_db) ficheDocumentation(fiche_analysee, "images", dossier, ignore_files[1], graph_db) else: files = [f for f in listdir(project_directory) if (isfile(join(project_directory, f)) and f.endswith('.txt'))] #TODO récupérer les métadonnées d'omeka sur les documents for document in files: print(document.strip(project_directory)) fiche = Fiche(document.replace(project_directory,'').replace('.txt', ''), '', '', '', '', '') fiche.create_node(graph_db)
def shortpath(n,j): # set up authentication parameters authenticate("localhost:7474", "neo4j", "parola") # connect to authenticated graph database graph = Graph("http://localhost:7474/db/data/") results = graph.cypher.execute("MATCH (martin:PERSON),(oliver:PERSON ),p = shortestPath((martin)-[*..15]-(oliver)) where martin.id= '"+j+"' and oliver.id= '"+n+"' RETURN p") return render_template("path.html",title='Home',relationship = results[0][0],node1=n,node2=j)
def dataretrieve(text): text=text.replace("?","") hit_yes_no=0 print ("Question :",text) authenticate("linguistic.technology:7474", "neo4j", "DtwAMjrk6zt1bHifYOJ6") graph = Graph("http://linguistic.technology:7474/db/data/") for i in range(len(patternsList)): match=re.match(patternsList[i],text, re.IGNORECASE) if match is not None: matchedX=list(match.groups())[-1] hit_yes_no=1 print ("Match :", matchedX) break if hit_yes_no: cypher=cypherList[i].replace("(.*)",matchedX.replace(" ","_")) print("Cypher :",cypher) result=graph.cypher.execute(cypher) print (result) #Hits Wikipedia only for factual questions if len(result)==0 and i<=2: print ("Here is what we could find from Wikipedia!!!\n") result=str(wikipedia.page(matchedX).summary.encode(sys.stdout.encoding, 'ignore')).split(".")[:2] print (".".join(result)) else: print ("No Matches found!") print ("-----------------------------------------------------------------------\n")
def loadColorFromDatabase(datasetname): print('--LOADING COLORDICT FROM DATABASE') from py2neo import Graph, Path, authenticate, Node, Relationship authenticate("localhost:7474", "neo4j", "japan123") tx = Graph() opt = tx.cypher.execute('MATCH (n:color) where n.name={x} return keys(n)', x=datasetname) allColors = str(opt).split('[')[1].split(']')[0] allColors = allColors.replace('\'', '') allColors = allColors.replace(' ', '') allColors = allColors.replace('uH', 'H') #python2 allColors = allColors.replace('uname', 'name') #python2 allColors = allColors.split(',') allColors.remove('name') #print('allColors in database are:',allColors) allSubspace = set([]) color_dict = {} for a in allColors: str1 = 'MATCH (n:color) where n.name=\'%s\' return n.%s' % ( datasetname, a) subspace = tx.cypher.execute(str1) n = 'n.' + a subspace = '[' + subspace[0][n] + ']' subspace = ast.literal_eval(subspace) color_dict[a] = subspace for i in subspace: #print(i) allSubspace.add(tuple(i)) subspaceList = [] for i in allSubspace: subspaceList.append(list(i)) return color_dict, subspaceList
def connect_to_db(db_type): print("Connecting to", db_type, "database . . . ", end="") sys.stdout.flush() db_conn = None try: if db_type == "mysql": url = "mysql://" + config.mysql_user + ":" + config.mysql_pass \ + "@" + "localhost:3306/" + config.mysql_name db_conn = sql.create_engine(url) # db_conn = MySQLdb.connect(user=config.mysql_user, \ # passwd=config.mysql_pass).cursor() elif db_type == "mongo": db_conn = MongoClient("localhost", 27017)[config.mongo_name] elif db_type == "neo4j": authenticate("localhost:7474", config.neo4j_user, config.neo4j_pass) db_conn = Graph() # db_conn = Graph(user=config.neo4j_user, # password=config.neo4j_pass) except: print("\n\nERROR : Can't connect to database") if db_conn is None: exit(1) print("Done!") return db_conn
def getClaimRecommendations(user): # delete the graph authenticate(settings.SECRET_NEO4J_DB_HOSTPORT, settings.SECRET_NEO4J_DB_USER, settings.SECRET_NEO4J_DB_PASSWORD) graph = Graph() query_conclusions = ''' // get all arguments supported by claims affirmed by the user MATCH (u:User {user_id:%s})-[:Affirms]->(sc1:Claim)-[:Premise_Of]->(a:Argument)-[:Supports]->(cc:Claim) , (sc:Claim)-[:Premise_Of]->(a) // get all supporting claims of those arguments WHERE NOT (u)-[:Affirms]->(cc) // where the user does not already affirm the argument's conclusion WITH u, a, cc, COLLECT(sc) AS supporting_claims WHERE ALL (sc IN supporting_claims WHERE (u)-[:Affirms]->(sc)) // where the user affirms all supporting claims of the argument RETURN cc.claim_id AS claim_id, 'recd_concl' AS rec_type ''' % (user.id) rec_list = list(graph.run(query_conclusions)) query_premises = ''' // get all arguments supported by claims affirmed by the user MATCH (u:User {user_id:%s})-[:Affirms]->(sc1:Claim)-[:Premise_Of]->(a:Argument)-[:Supports]->(cc:Claim) // get all supporting claims of those arguments , (sc:Claim)-[:Premise_Of]->(a) // where the user does affirm the argument's conclusion WHERE (u)-[:Affirms]->(cc) // return the premises that the user does not yet affirm WITH u, a, cc, sc WHERE NOT (u)-[:Affirms]->(sc) RETURN sc.claim_id AS claim_id, 'recd_premis' AS rec_type ''' % (user.id) # get query results as a list of named tuples rec_list += list(graph.run(query_premises)) return rec_list
def make_sequence(self): authenticate(settings.NeoHost, settings.NeoLog, settings.NeoPass) graph = Graph("{0}/db/data/".format(settings.NeoHost)) query = """MATCH (start:Video)-[:Jaccard*5..10]->(sequence:Video) WHERE start<>sequence MATCH p=shortestPath((start:Video)-[:Jaccard*]->(sequence:Video)) WHERE NONE (n IN nodes(p) WHERE size(filter(x IN nodes(p) WHERE n = x))> 1) RETURN EXTRACT(n IN NODES(p)|[n.id, n.rating]) LIMIT 100000""" r1 = graph.run(query).data() k = 0 for i in r1: #print(i.values) for video in i['EXTRACT(n IN NODES(p)|[n.id, n.rating])']: #print(video) self.seq_ids.append(k) self.video_ids.append(video[0]) self.ratings.append(video[1]) k+=1 data = {'sequence': self.seq_ids, 'video': self.video_ids, 'rating': self.ratings} df = pd.DataFrame(data) df = df[pd.notnull(df['video'])] print(df) dz = df.groupby('sequence')['rating'].std() print(dz) path = '{0}/{1}/'.format(settings.VideosDirPath, self.game) if not os.path.exists(path): os.makedirs(path) file_name = '{0}/sequences.csv'.format(path) df.to_csv(file_name, encoding='utf-8') summary_data = '{0}/summary.csv'.format(path) dz.to_csv(summary_data, encoding='utf-8') return
def set_graph(self, config_path): """ Parameters -------------- config_path path to a config.json with data needed to connect to a db Return -------------- None """ with open(config_path) as config_data: config = json.load(config_data) [str(x) for x in config] host = config['host'] db_url = 'http://' if host['use_ssl']: db_url = 'https://' db_url += host['address'] + ':' + str( host['port']) + '/' + host['data_path'] authenticate(host['address'] + ':' + str(host['port']), host['username'], host['password']) self.graph = Graph(db_url)
def getTopLeftCoordinates_Grid(x, y, labelname, imgType): print( '-- getTopLeftCoordinates_Grid: retrieving top left coordinates of grid ' ) from py2neo import Graph, Path, authenticate authenticate("localhost:7474", "neo4j", "japan123") graph = Graph() t = x x = y y = t tx = graph.cypher.execute( 'match (n) WHERE EXISTS(n.color) and n.name={lb} and toFloat(n.tlx)<={x} and toFloat(n.brx)>={x} and toFloat(n.tly)<={y} and toFloat(n.bry)>={y} RETURN n.block_row,n.block_col,n.color', x=x, y=y, lb=labelname + imgType) #print(tx[0],'block') color_val = tx[0]['n.color'] block_row = tx[0]['n.block_row'] block_col = tx[0]['n.block_col'] #labelname+imgType tx = graph.cypher.execute( 'match(n:n1) where n.name={name} return n.class_count', name=labelname + '.csv') class_count = tx[0]['n.class_count'].split(',') tlx = 0 tly = 0 #print('BBLOCK',block_row,block_col) for i in range(block_row): tlx = tlx + int(class_count[i]) for i in range(block_col): tly = tly + int(class_count[i]) return tlx, tly, block_row, block_col
def main(fichier_relations, dossier, ignore_files): authenticate("localhost:7474", "neo4j", "haruspex") graph_db = Graph() # Pour chaque fiche de mots-clés, analyser son contenu # et créer les liens correspondants par cooccurrence de mot-clés # avec les autres fiches analyseRelations(fichier_relations, dossier, graph_db)
def __init__(self, connectionString=None): # set up authentication parameters authenticate("localhost:7474", "neo4j", "123") self.connectionString = "http://localhost:7474/db/data/" self._graph_connection = Graph(connectionString) self.error_file = open("Dump/log/dberror.txt", "a") return
def relcount(): from py2neo import authenticate, Graph # set up authentication parameters authenticate("localhost:7474", "neo4j", "parola") # connect to authenticated graph database graph = Graph("http://localhost:7474/db/data/") results = graph.cypher.execute("MATCH ()-[r]->() RETURN count(r)") return str(results)
def __init__(self, config_file): config = ConfigParser.ConfigParser() config.read(config_file) authenticate(config.get('db', 'host'), config.get('db', 'username'), config.get('db', 'password')) self._graph = Graph() self.port = int(config.get('local', 'port')) self.auth = config.get('local', 'auth') self.com_port = int(config.get('local', 'com_port'))
def get_db(): """Opens a new database connection if there is none yet for the current application context. """ if not hasattr(g, 'graph_db'): authenticate("localhost:7474", "neo4j", "0000") g.graph_db = Graph() return g.graph_db
def handle(self, *args, **options): # pylint: disable=unused-argument """ Iterates through each course, serializes them into graphs, and saves those graphs to neo4j. """ # first, make sure that there's a valid neo4j configuration if settings.NEO4J_CONFIG is None: raise CommandError( "No neo4j configuration (NEO4J_CONFIG) defined in lms.auth.json." ) auth_params = ["{host}:{https_port}", "{user}", "{password}"] authenticate(*[param.format(**settings.NEO4J_CONFIG) for param in auth_params]) graph = Graph(**settings.NEO4J_CONFIG) mss = ModuleStoreSerializer() total_number_of_courses = len(mss.all_courses) for index, course in enumerate(mss.all_courses): # first, clear the request cache to prevent memory leaks RequestCache.clear_request_cache() log.info( "Now exporting %s to neo4j: course %d of %d total courses", course.id, index + 1, total_number_of_courses ) nodes, relationships = mss.serialize_course(course.id) log.info( "%d nodes and %d relationships in %s", len(nodes), len(relationships), course.id ) transaction = graph.begin() try: # first, delete existing course transaction.run( "MATCH (n:item) WHERE n.course_key='{}' DETACH DELETE n".format( six.text_type(course.id) ) ) # now, re-add it self.add_to_transaction(nodes, transaction) self.add_to_transaction(relationships, transaction) transaction.commit() except Exception: # pylint: disable=broad-except log.exception( "Error trying to dump course %s to neo4j, rolling back", six.text_type(course.id) ) transaction.rollback()
def authenticate(self, uri='localhost:7474'): ''' Authenticate ourselves to the neo4j database using our credentials ''' if self.isdefault: self.update() if DEBUG: print >> sys.stderr, 'AUTH WITH ("%s")' % str(self) py2neo.authenticate(uri, self.name, self.auth)
def getSocialFormation(id=0): authenticate("localhost:7474", "neo4j", "1111") graph = Graph(graph_DB_url) res = graph.cypher.execute("MATCH (n:SocialFormation {id:{id}}) RETURN n", {"id": id}) if 0 != len(res): return res else: return None
def connect(self): url = current_app.config['DATABASE'] u = current_app.config['USERNAME'] p = current_app.config['PASSWORD'] if u and p: authenticate(url.strip('http://'),u,p) graph = Graph(url+'/db/data') return graph
def newNode(user, photo): py2neo.authenticate("localhost:7474", "neo4j", "porkunja") Photos(name=photo).save() if not existUser(user): User(name=user).save() usernode = User.nodes.get(name=user) photonode = Photos.nodes.get(name=photo) usernode.uploader.connect(photonode) photonode.uploaded.connect(usernode) return
def main(): # Connect to Neo4j instance authenticate("localhost:7474", "neo4j", "qwerty1") graph = Graph() #Set up query to get list of all nodes by classification biomarker_query = ( "MATCH (n:Gene)-[:HAS_ABERRATION]->(a:Aberration {entity_class:'biomarker'}) " "WHERE NOT( (n)-[:HAS_ABERRATION]->({entity_class:'modifier'} ) ) " "RETURN DISTINCT ID(n) as id, n.name as name order by name" ) biomarker_query = "MATCH (n:Gene {entity_class:'biomarker'}) RETURN DISTINCT ID(n) as id, n.name as name order by name" modifier_query = ( "MATCH (n:Gene)-[:HAS_ABERRATION]->(a:Aberration {entity_class:'modifier'}) " "WHERE NOT( (n)-[:HAS_ABERRATION]->({entity_class:'biomarker'} ) ) " "RETURN DISTINCT ID(n) as id, n.name as name order by name" ) modifier_query = "MATCH (n:Gene {entity_class:'modifier'}) RETURN DISTINCT ID(n) as id, n.name as name order by name" bio_mod_query = ( "MATCH (n:Gene)-[:HAS_ABERRATION]->(a:Aberration {entity_class:'modifier'}) " "WHERE ( (n)-[:HAS_ABERRATION]->({entity_class:'biomarker'} ) ) " "RETURN DISTINCT ID(n) as id, n.name as name order by name" ) bio_mod_query = ("MATCH (n:Gene {entity_class:'modifier'}) RETURN DISTINCT ID(n) as id, n.name as name order by name " "UNION MATCH (n:Gene {entity_class:'biomarker'}) RETURN DISTINCT ID(n) as id, n.name as name order by name " ) drug_query = "MATCH (d:Drug) RETURN DISTINCT ID(d) as id, d.name as name order by name" gene_query = "MATCH (g:Gene) RETURN DISTINCT ID(g) as id, g.name as name order by name" allNode_query = "MATCH n RETURN DISTINCT ID(n) as id, n.name as name order by name" aberration_query = "MATCH a RETURN DISTINCT ID(a) as id, a.name as name order by name" print "Executing Queries..." biomarkers = graph.cypher.execute(biomarker_query) modifiers = graph.cypher.execute(modifier_query) bio_mods = graph.cypher.execute(bio_mod_query) drugs = graph.cypher.execute(drug_query) genes = graph.cypher.execute(gene_query) allNodes = graph.cypher.execute(allNode_query) aberrations = graph.cypher.execute( aberration_query ) #Make http request to neo4j to get degree for each. print "Writing to Files..." writeFile("pc2_druggene_biomarker_degrees.csv",biomarkers,graph) writeFile("pc2_druggene_modifier_degrees.csv",modifiers,graph) #writeFile("pc2_druggene_bio_mod_degrees.csv",bio_mods,graph) writeFile("pc2_druggene_drug_degrees.csv",drugs,graph) writeFile("pc2_druggene_gene_degrees.csv",genes,graph) writeFile("pc2_druggene_allnode_degrees.csv",allNodes,graph) #writeFile("pc2_aberration_degrees.csv",aberrations,graph) print "Done"
def __init__(self, host_port, user, password): '''Makes connection to Neo4j database''' # set up authentication parameters authenticate(host_port, user, password) # connect to authenticated graph database url = 'http://{}/db/data/'.format(host_port) self.graph = Graph(url) try: self.graph.schema.create_uniqueness_constraint('User', 'id') except: #ConstraintViolationException print 'Unique id on Node User already exists'
def start(): py2neo.authenticate(config('neo4j.server'), config('neo4j.username'), config('neo4j.password')) g = py2neo.Graph() r = redis.StrictRedis(host=config('redis.server'), port=config('redis.port'), db=config('redis.db')) number = 0 for node in g.cypher.execute("match (n:USER) where exists(n.gender) return n.domain as domain"): number += 1 print(number, node.domain) r.sadd(config('set.crawled_user'), node.domain)
def __init__(self, connection_alias, model_registry=registry, **connection_options): self._connection_alias = connection_alias url = connection_options.get('URL', 'http://localhost:7474/db/data/') user = connection_options.get('USER', 'neo4j') password = connection_options.get('PASSWORD', 'neo4j') self._is_test = connection_options.get('IS_TEST', False) self._registry = model_registry py2neo.authenticate(urlparse(url).netloc, user, password) self._graph = py2neo.Graph(url)