Ejemplo n.º 1
0
Archivo: node.py Proyecto: tyjch/Neo
    def __init__(self):
        database = Database()
        identifier = str(database.store_id)
        time_created = str(database.store_creation_time)

        self.property_graph = "{} : {}".format(str(identifier),
                                               str(time_created))
Ejemplo n.º 2
0
def query2():
    db = Database("bolt://localhost:7687")
    graph = Graph("bolt://localhost:7687", auth=("neo4j", "firstproject"))

    # Query Limits set to 100 (25x4), to increase number of disease / compounds pairs, set "LIMIT" to higher value for the following 4 queries:

    query2List = graph.run(
        "MATCH p=(compound:Compound)-[RESEMBLES]->(:Compound)-[u:UPREGULATES_CuG]->(gene:Gene)<-[DOWNREGULATES_AdG]-(:Anatomy)<-[LOCALIZES_DlA]-(disease:Disease) RETURN compound.name,disease.name LIMIT 25"
    ).data()

    query2List.append(
        graph.run(
            "MATCH p=(compound:Compound)-[RESEMBLES]->(:Compound)-[DOWNREGULATES_CdG]->(gene:Gene)<-[UPREGULATES_AuG]-(:Anatomy)<-[LOCALIZES_DlA]-(disease:Disease) RETURN compound.name,disease.name LIMIT 25"
        ).data())

    query2List.append(
        graph.run(
            "MATCH p=(compound:Compound)-[DOWNREGULATES_CdG]->(gene:Gene)<-[UPREGULATES_AuG]-(:Anatomy)<-[LOCALIZES_DlA]-(disease:Disease) RETURN compound.name,disease.name LIMIT 25"
        ).data())

    query2List.append(
        graph.run(
            "MATCH p=(compound:Compound)-[UPREGULATES_CuG]->(gene:Gene)<-[DOWNREGULATES_AdG]-(:Anatomy)<-[LOCALIZES_DlA]-(disease:Disease) RETURN compound.name,disease.name LIMIT 25"
        ).data())

    # query2List = list( dict.fromkeys(query2List) )

    print(
        "The following compounds may treat their respective diseases and should be researched more.\n\n"
    )
    print(query2List)

    start()
Ejemplo n.º 3
0
def _convert(data: GraphDataMixin,
             from_fmt: str,
             to_fmt: str,
             safe: bool = True):
    # Initialize the new data structure
    if to_fmt == 'neo4j':
        # TODO: Only compatible with default config file for now
        uri, username, password = _load_neo4j_config()
        db = Database(uri, auth=(username, password))
        new_data = Neo4jData(db)
    elif to_fmt == 'networkx':
        new_data = NetworkXData()
    elif to_fmt == 'graphsage':
        raise NotImplementedError
    elif to_fmt == 'dgl':
        raise NotImplementedError

    # Populate nodes and edges
    nodes = data.nodes
    edges = data.edges
    #ipdb.set_trace()
    new_data.add_nodes(nodes)
    new_data.add_edges(edges)

    return new_data
Ejemplo n.º 4
0
    def connect2db():
        """
        Internal method to create a database connection. This method is called during object initialization.
        Database initialization variables need to be set in environment.

        :return: Database handle and cursor for the database.
        """
        neo4j_config = {
            'user': os.environ["NEO4J_USER"],
            'password': os.environ["NEO4J_PWD"]
        }
        if os.environ.get("NEO4J_HOST"):
            host = os.environ["NEO4J_HOST"]
            neo4j_config['host'] = host
        # Check that Neo4J is running the expected Neo4J Store - to avoid accidents...
        connected_db = Database(**neo4j_config)
        if connected_db.config["dbms.active_database"] != os.environ[
                'NEO4J_DB']:
            msg = "Connected to Neo4J database {d}, but expected to be connected to {n}"\
                .format(d=connected_db.config["dbms.active_database"], n=os.environ['NEO4J_DB'])
            # current_app.logger cannot be used because this method is called during create_app
            # so current_app is not available now.
            # current_app.logger.fatal(msg)
            print(msg)
            raise SystemExit()
        # Connect to Graph
        graph = Graph(**neo4j_config)
        return graph
Ejemplo n.º 5
0
def check_connection(addr, port, user, dbpw):
    try:
        Database(f"bolt://{addr}:{port}", auth=(user, dbpw)).kernel_version
    except ServiceUnavailable:
        return -1
    except (AuthError, AttributeError):
        return 1
    return 0
Ejemplo n.º 6
0
    def __init__(self, user, password):

        self.database = Database("bolt://localhost:7687",
                                 user="******",
                                 password="******")
        self.graph = Graph("bolt://localhost:7687",
                           user="******",
                           password="******")
        self.hashtag_counts_df = None
        self.tweets = None
Ejemplo n.º 7
0
 def build_connector():
     params = {
         'host': os.getenv('TASK_DB_HOST', 'taskdb'),
         'port': int(os.getenv('TASK_DB_PORT', 7687)),
         'user': os.getenv('TASK_DB_USER', 'neo4j'),
         'password': os.getenv('TASK_DB_PASSWORD', 'neo4jpass'),
         'scheme': os.getenv('TASK_DB_SCHEME', 'bolt')
     }
     try:
         db = Database(**params)
         graph = db.default_graph
         matcher = NodeMatcher(graph)
         return matcher
     except Exception as e:
         print('Unable to connect to task db.', str(e))
Ejemplo n.º 8
0
    def _connect2db():
        """
        Internal method to create a database connection. This method is called during object initialization.

        :return: Database handle and cursor for the database.
        """
        logging.debug("Creating Neostore object.")
        neo4j_config = {
            'user': os.getenv("NEO4J_USER"),
            'password': os.getenv("NEO4J_PWD"),
        }
        # Check that Neo4J is running the expected Neo4J Store - to avoid accidents...
        connected_db = Database(**neo4j_config)
        dbname = connected_db.config["dbms.active_database"]
        if dbname != os.getenv("NEO4J_DB"):
            msg = "Connected to Neo4J database {d}, but expected to be connected to {n}"\
                .format(d=dbname, n=os.getenv("NEO4J_DB"))
            logging.fatal(msg)
            raise SystemExit(msg)
        # Connect to Graph
        graph = Graph(**neo4j_config)
        # Check that we are connected to the expected Neo4J Store - to avoid accidents...
        return graph
Ejemplo n.º 9
0
    def from_neo4j(cls, config_file: str = None):
        """Load a connection to a Neo4j graph database and use it to
        instantiate a comptox_ai.graph.io.Neo4j object.

        NOTE: All we do here is create a driver for the graph database; the
        Neo4j constructor handles building the node index and other important
        attributes. This is different from most of the other formats, where
        the attributes are provided by the constructor

        Parameters
        ----------
        config_file : str, default None
            Path to a ComptoxAI configuration file. If None, ComptoxAI will
            search for a configuration file in the default location. For more
            information, refer to http://comptox.ai/docs/guide/building.html.
        
        Raises
        ------
        RuntimeError
            If the data in the configuration file does not point to a valid
            Neo4j graph database.

        See Also
        --------
        comptox_ai.graph.Neo4jData
        """
        print("Parsing Neo4j configuration...")
        uri, username, password = _load_neo4j_config()
        print("  URI:", uri)

        print("Creating database connection via py2neo...")
        database = Database(uri, auth=(username, password))
        print("Connected to database, now reading contents")
        neo4j_data = Neo4jData(database=database)

        return cls(data=neo4j_data)
Ejemplo n.º 10
0
def main():
    '''
    main function initiates a kafka consumer, initialize the tweetdata database.
    Consumer consumes tweets from producer extracts features, cleanses the tweet text,
    and loads the data into a Neo4j database
    '''
    # set-up a Kafka consumer
    consumer = KafkaConsumer("twitter")
    db = Database("bolt://localhost:7687", user="******", password="******")
    g = Graph("bolt://localhost:7687", user="******", password="******")

    for msg in consumer:

        try:
            dict_data = json.loads(msg.value.decode('utf-8'))

            tweet_text = dict_data["text"]

            try:
                sentiment_data = get_sentiment_data(tweet_text, english=False)
            except:
                sentiment_data = get_sentiment_data(tweet_text, english=True)
            tweet_id = dict_data["id_str"]
            location = dict_data["user"]["location"]
            screen_name = dict_data["user"]["screen_name"]

            #print(dict_data)
            hashtags = get_hashtag_list(dict_data)
            print("id: ", tweet_id)
            print("screen name: ", screen_name)
            print("location: ", location)
            print("sentiment: ", sentiment_data['sentiment'])
            print("text: ", tweet_text)
            #print("Hashtags: ", hashtags)
            # add text and sentiment info to neo4j

            tx = g.begin()
            tweet_node = Node("Tweet",
                              id=tweet_id,
                              screen_name=screen_name,
                              location=location,
                              text=tweet_text,
                              sentiment=sentiment_data['sentiment'],
                              polarity=sentiment_data['polarity'])

            try:
                tx.merge(tweet_node, "Tweet", "id")
            except:
                continue

            #print(dict(tweet_node))

            for hashtag in hashtags:

                hashtag_node = Node("Hashtag", name=hashtag)
                #print(dict(hashtag_node))
                tx.merge(hashtag_node, "Hashtag", "name")
                tweet_hashtag_rel = Relationship(tweet_node, "INCLUDES",
                                                 hashtag_node)
                tx.merge(tweet_hashtag_rel, "Hashtag", "name")

            tx.commit()

            print('\n')
        except:
            print('Tweet not found')
Ejemplo n.º 11
0
from py2neo import Database
from py2neo import Graph, Node
import numpy as np
""" Connection to Neo4j Database """
dvdrental = "bolt://localhost:7687"
db = Database(dvdrental)
graph = Graph(password='******')
db = graph.begin(autocommit=False)

ret = graph.run('''match (f:Film)-[fr:FILM_RENTAL]->(r:Rental)
match (c:Customer)-[cr:CUSTOMER_RENTAL]->(r)
where fr.ID=cr.ID
with count(*) as rented_times

match (f:Film)-[in_cat:IN_CATEGORY]->(cat:Category)
with f.ID as film_ID, f.title as film_title, cat.name as category_name, rented_times
return film_ID, film_title, category_name, rented_times''').to_table()
print(ret)

db.commit()
Ejemplo n.º 12
0
from py2neo import Graph, Database

db = Database()
graph = Graph(user="******")


def louvainAlgo():
    query = """
    CALL algo.louvain.stream('paper', 'has_citation', {})
    YIELD nodeId, community
    RETURN algo.getNodeById(nodeId).title AS user, community
    ORDER BY community;
    """
    return graph.run(query).data()


if __name__ == '__main__':
    list_community = louvainAlgo()
    print("List of papers and their page rank score ")
    for val in list_community:
        print(val)

Ejemplo n.º 13
0
from py2neo import Database, Graph

# 建立连接
db = Database("http://116.62.17.253:17474")
graph = Graph("bolt://116.62.17.253:17687",
              username="******",
              password="******")

ans = graph.run('match(p) return p').to_ndarray()
print(ans)
Ejemplo n.º 14
0
def get_default_graph():
    database = Database()
    graph = database.default_graph
    return graph
Ejemplo n.º 15
0
from py2neo import Database
db = Database("bolt://carbonate.uits.iu.edu:7687")
Ejemplo n.º 16
0
# -*- coding: utf-8 -*-
"""...
"""
from py2neo import Database, Graph
from py2neo import Node, Relationship

if __name__ == '__main__':
    default_db = Database(uri='bolt://localhost:7687')
    default_db.forget_all()
    # conf = default_db.config()
    # grap = default_db.default_graph()
    auth = ('neo4j', 'alxfed')
    data = Graph(auth=auth, secure=False) # host='bolt://localhost:7687', encrypted=False,
    data.delete_all()
    a = Node("Person", name="Alice", age=33)
    a.remove_label('Person')
    b = Node("Person", name="Bob", age=44)
    KNOWS = Relationship.type("KNOWS")
    data.merge(KNOWS(a, b), "Person", "name")
    print('\ndone')
Ejemplo n.º 17
0
from googletrans import Translator
import speech_recognition as sr
from gtts import gTTS
import nltk
import playsound
from py2neo import Graph
from py2neo import Database, NodeMatch
from py2neo import cypher
import py2neo
import neo4j
import time
db = Database("http://localhost:7474/")
graph = Graph("http://localhost:7474/")
graph.begin()
graph.database = db


def gen_res(number):
    li = []
    li1 = []
    li2 = []
    for rel in graph.match((), r_type="indicates"):
        s = rel.start_node["sid"]
        if int(s) == number:
            li1.append(rel.end_node)
            li2.append(rel.end_node['label'])
    print(number)
    for i in li1:
        a = []
        a.append(i['symptom1'])
        a.append(i['symptom2'])
Ejemplo n.º 18
0
                        choices=[True, False],
                        help='If true, delete neo4j db')
    args_dict = vars(parser.parse_args())
    # parse args_dict
    delete_database = args_dict['delete_database']
    env_path = args_dict['env_path']
    mln_dict_src = args_dict['mln_dict_src']
    verbose = args_dict['verbose']
    # import MLN, which requires that CORENLP_HOME dir is an environment variable
    load_dotenv(env_path)
    CORENLP_HOME = os.getenv('CORENLP_HOME')
    from multivac.pymln.semantic import MLN
    # define neo4j variables
    NEO4J_URI = os.getenv('NEO4J_URI')
    NEO4J_PASSWORD = os.getenv('NEO4J_PASSWORD')
    db = Database(uri=NEO4J_URI, password=NEO4J_PASSWORD)
    graph = Graph(database=db)
    if delete_database:
        graph.delete_all()
    # instantiate graph
    node_matcher = NodeMatcher(graph)
    # load mln data
    MLN.load_mln(mln_dict_src)
    with open(mln_dict_src, 'rb') as f:
        mln_dict = pickle.load(f)

    # @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    # populate the graph database with mln data
    for cluster_id, cluster_object in mln_dict['clusts'].items():
        if verbose:
            print('cluster_id: {}'.format(cluster_id))
Ejemplo n.º 19
0
import os
import json
import logging
from py2neo import Graph, Database

# Default location of the configuration and dataset files
DEFAULT_CONFIG_PATH = 'config/movielens_config.json'
DEFAULT_DATASET_PATH = 'dataset/movies.csv'

# Define graph database objects
db = Database("bolt://127.0.0.1:7687")
url = "bolt://localhost:7687"
graph = Graph(url)

# Log the processes
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()


def get_config():
    """
    Function to obtain the main parameters of source dataset and connections to the database from a configuration file
    """
    dir_name, running_filename = os.path.split(os.path.abspath(__file__))
    #file_dir = os.path.dirname(dir_name)
    config_file_name = DEFAULT_CONFIG_PATH
    config_path = os.path.join(dir_name, config_file_name)

    with open(config_path) as config_file:
        config = json.load(config_file)
    return config