Loading [MathJax]/extensions/tex2jax.js
STAG Python  1.2.1
Spectral Toolkit of Algorithms for Graphs
All Classes Namespaces Files Functions Variables Pages
stag.neo4j.Neo4jGraph Class Reference
Note
This documentation is not for the latest library version. Documentation for the latest version is available here.
Inheritance diagram for stag.neo4j.Neo4jGraph:
stag.graph.LocalGraph

Description

Represent a neo4j database as a stag.graph.LocalGraph object.

This provides 'local' access to the graph only. Given a node, we can query its neighbors and its degree. The graph does not support weighted edges, and it does not distinguish between edge types or directions.

The neo4j <id> attributes are used as the STAG vertex ids.

All methods which make calls to the database backend have a small cache of recent queries, meaning that consecutive queries for the same node will be fast.

Public Member Functions

def __init__ (self, str uri, str username, str password)
 Connect to a neo4j database backend.
 
def degree (self, v)
 Equivalent to stag.neo4j.Neo4jGraph.degree_unweighted.
 
int degree_unweighted (self, int v)
 Query the degree of the node with the given neo4j ID.
 
List[graph.Edgeneighbors (self, int v)
 Fetch the neighbors of the node with the given Neo4j node ID.
 
List[int] neighbors_unweighted (self, int v)
 Fetch the neighbors of the node with the given Neo4j node ID.
 
List[str] query_node_labels (self, int node_id)
 Query the labels of the given node.
 
int query_id (self, str property_name, str property_value)
 Find the Neo4j ID of a node with the given property.
 
str query_property (self, int id, str property_name)
 Find the requested property of a Neo4j node.
 
bool vertex_exists (self, int v)
 Given a vertex ID, returns true or false to indicate whether the vertex exists in the graph.
 
- Public Member Functions inherited from stag.graph.LocalGraph
def __init__ (self)
 Default LocalGraph constructor.
 
float degree (self, int v)
 Given a vertex v, return its weighted degree.
 
int degree_unweighted (self, int v)
 Given a vertex v, return its unweighted degree.
 
List[Edgeneighbors (self, int v)
 Given a vertex v, return a list of edges representing the neighbors of v.
 
List[int] neighbors_unweighted (self, int v)
 Given a vertex v, return a list of neighbors of v.
 
List[float] degrees (self, List[int] vertices)
 Given a list of vertices, return a list of their weighted degrees.
 
List[int] degrees_unweighted (self, List[int] vertices)
 Given a list of vertices, return a list of their unweighted degrees.
 
bool vertex_exists (self, int v)
 Given a vertex ID, returns true or false to indicate whether the vertex exists in the graph.
 

Public Attributes

 driver
 The neo4j database driver object used to query the underlying database.
 

Constructor & Destructor Documentation

◆ __init__()

def stag.neo4j.Neo4jGraph.__init__ (   self,
str  uri,
str  username,
str  password 
)

Connect to a neo4j database backend.

For example, assuming that there is a Neo4j database running locally, the graph object can be created as follows.

import stag.neo4j
g = stag.neo4j.Neo4jGraph("bolt://localhost:7687", "neo4j", "password")
Represent a neo4j database as a stag.graph.LocalGraph object.
Definition: neo4j.py:26
Definition: neo4j.py:1

It is also possible to connect to a remote Neo4j database by passing the relevant uri to the constructor.

Parameters
urithe location of the neo4j database
usernamethe neo4j username
passwordthe neo4j password

Reimplemented from stag.graph.LocalGraph.

Member Function Documentation

◆ degree()

def stag.neo4j.Neo4jGraph.degree (   self,
  v 
)

◆ degree_unweighted()

int stag.neo4j.Neo4jGraph.degree_unweighted (   self,
int  v 
)

Query the degree of the node with the given neo4j ID.

Reimplemented from stag.graph.LocalGraph.

◆ neighbors()

List[graph.Edge] stag.neo4j.Neo4jGraph.neighbors (   self,
int  v 
)

Fetch the neighbors of the node with the given Neo4j node ID.

The returned stag.graph.Edge objects all have weight 1.

Reimplemented from stag.graph.LocalGraph.

◆ neighbors_unweighted()

List[int] stag.neo4j.Neo4jGraph.neighbors_unweighted (   self,
int  v 
)

Fetch the neighbors of the node with the given Neo4j node ID.

Returns the Neo4j node IDs of the neighboring nodes.

Reimplemented from stag.graph.LocalGraph.

◆ query_node_labels()

List[str] stag.neo4j.Neo4jGraph.query_node_labels (   self,
int  node_id 
)

Query the labels of the given node.

For example, using the Neo4j movie database example, you can query whether a given node represents a person or a movie as follows.

>>> import stag.neo4j
>>> g = stag.neo4j.Neo4jGraph("bolt://localhost:7687", "neo4j", "password")
>>> labels = g.query_node_labels(0)
['Movie']
>>> labels = g.query_node_labels(1)
['Person']

◆ query_id()

int stag.neo4j.Neo4jGraph.query_id (   self,
str  property_name,
str  property_value 
)

Find the Neo4j ID of a node with the given property.

For example, using the Neo4j movie database example you can find the Neo4j node corresponding to a given movie as follows.

import stag.neo4j
g = stag.neo4j.Neo4jGraph("bolt://localhost:7687", "neo4j", "password")
node_id = g.query_id("title", "Avatar")

This will make a query to the database to find a node with the given property. This query will be slow unless the database has an index for property_name. For more information, see the Neo4j documentation on creating an index.

Parameters
property_namethe node property to query
property_valuethe value of the node property to search for
Returns
the Neo4j node ID of a node with the given property value. Returns None if there is no node in the dataabase with the requested property.

◆ query_property()

str stag.neo4j.Neo4jGraph.query_property (   self,
int  id,
str  property_name 
)

Find the requested property of a Neo4j node.

For example, using the Neo4j movie database example, you can find the title of a movie node as follows.

import stag.neo4j
g = stag.neo4j.Neo4jGraph("bolt://localhost:7687", "neo4j", "password")
title = g.query_property(1, "title")
Parameters
idthe Neo4j node ID to query
property_namethe name of the property to query
Returns
the value of the requested property on the given node. Returns None if the requested property is not set on the requested node, or if the node id does not exist.

◆ vertex_exists()

bool stag.neo4j.Neo4jGraph.vertex_exists (   self,
int  v 
)

Given a vertex ID, returns true or false to indicate whether the vertex exists in the graph.

Parameters
vthe vertex index to check
Returns
a boolean indicating whether there exists a vertex with the given index

Reimplemented from stag.graph.LocalGraph.

Member Data Documentation

◆ driver

stag.neo4j.Neo4jGraph.driver

The neo4j database driver object used to query the underlying database.