Low Level API for Query
The QueryWrapper Class
The QueryWrapper class provides low level query support.
A main feature of the QueryWrapper queries can be cascaded and are composable.
- class neuroarch.query.QueryWrapper(graph, query, init_nodes={}, execute=True, executed=False, edges=False, debug=False)[source]
Gremlin/OrientDB SQL query wrapper.
This class encapsulates Gremlin and OrientDB SQL queries that return nodes with functionality that permits the query to be composed with other queries using various operators. The query mechanism can also extract edges between the requested nodes.
- Parameters:
graph (pyorient.ogm.Graph) – Graph object.
query (QueryString, tuple, or list) – If a QueryString, contains a single node query. If a tuple/list, must contain an operator followed by two operands, which in turn may either be tuples/lists formatted as previously described or QueryString instances.
init_nodes (set or dict) – Node records with which to initialize the query cache. If dict, the keys are the record IDs and the values the records.
execute (bool) – Whether to execute the query immediately upon instantiation.
executed (bool) – Whether the query has been executed.
edges (bool) – If True, extract edges between requested nodes.
debug (bool) – Run in debug mode. If true, will print execution time.
- add_connecting_synapses(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- add_connecting_synapses1(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- property edges
Set of edge records between nodes retrieved by query.
- property edges_as_list
List of edge records between nodes retrieved by query.
- property edges_as_objs
List of pyorient objects corresponding to edge records between nodes retrieved by query.
- execute(edges=False, force=False, connect=False)[source]
Execute the query.
- Parameters:
edges (bool) – If True, retrieve edges between nodes as well as nodes.
force (bool) – Execute the query even if node/edge results have already been cached.
connect (bool) – Create new query_results node and connect it to the nodes returned by the query.
- property executed
Whether the query has been executed.
- classmethod from_elements(graph, *obj_list, **kwargs)[source]
Construct a QueryWrapper given a list of pyorient OGM objects.
- Parameters:
graph (pyorient.ogm.Graph) – Graph object.
obj_list (list of neuroarch.models.Node or neuroarch.models.Relationship) – List of pyorient OGM objects.
- Returns:
result – QueryWrapper instance.
- Return type:
- classmethod from_objs(graph, objs, debug=False)[source]
Construct a QueryWrapper given a list of OGM objects
- Parameters:
graph (pyorient.ogm.Graph) – Graph object.
obj (list) – List of orientdb objs
- Returns:
result – QueryWrapper instance.
- Return type:
- classmethod from_rids(graph, *rid_list, **kwargs)[source]
Construct a QueryWrapper given a list of OrientDB RIDs.
- Parameters:
graph (pyorient.ogm.Graph) – Graph object.
rid_list (list) – List of RIDs expressed as strings, e.g., ‘#12:0’.
- Returns:
result – QueryWrapper instance.
- Return type:
- get_as(as_type='df', force_rid=False, edges=True, edge_class='', deepcopy=False)[source]
Return view of query results.
- Parameters:
as_type ({'df', 'nx', 'obj'}) – If ‘df’, return results as a tuple of Pandas DataFrame instances. If ‘nx’, return a single NetworkX MultiDiGraph instance. If ‘obj’, return a tuple of pyorient OGM object lists.
force_rid (bool) – If True, always use the OrientDB RID as the node identifier or index in the returned graph or DataFrame. Otherwise, use ‘id’ property. Ignored if as_type == ‘obj’.
- Returns:
result – Query results in specified format.
- Return type:
tuple or networkx.MultiDiGraph
- get_connecting_synapses(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- get_nodes(as_type='obj', force_rid=False, deepcopy=False)[source]
Return view of nodes in query results.
- Parameters:
as_type ({'df', 'obj'}) – If ‘df’, return nodes as a Pandas DataFrame instances. If ‘obj’, return a iist of pyorient OGM objects.
force_rid (bool) – If True, always use the OrientDB RID as the node identifier or index in the returned graph or DataFrame. Otherwise, use ‘id’ property. Ignored if as_type == ‘obj’.
- Returns:
Nodes in the query results in specified format.
- Return type:
result
- property node_objs
List of pyorient objects corresponding to node records. This property do not retrieve again data from the database.
- property node_rids
- property nodes
Set of node records retrieved by query.
- property nodes_as_list
List of node records retrieved by query.
- property nodes_as_objs
List of pyorient objects corresponding to node records retrieved by query. This is different from QueryWrapper.node_objs in that this property retrieves the nodes from database, whereas QueryWrapper.node_objs uses the records stored in self.nodes.
- owned_by(levels=1, **kwargs)[source]
Retrieve all nodes at a specified number of levels above the current query.
- Parameters:
levels (int) – Number of ownership edges between the current query and the nodes to be retrieved.
cls (str or iterable) – Node class or classes to retrieve.
- Returns:
result – Query containing requested nodes.
- Return type:
- owns(levels=1, **kwargs)[source]
Retrieve all nodes at a specified number of levels below the current query.
- Parameters:
levels (int) – Number of ownership edges between the current query and the nodes to be retrieved.
cls (str or iterable) – Node class or classes to retrieve.
- Returns:
result – Query containing requested nodes.
- Return type:
- post_synaptic_neurons(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- post_synaptic_neurons_with_synapse_count(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- pre_synaptic_neurons(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- pre_synaptic_neurons_with_synapse_count(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
- property query_string
The encapsulated query string(s).
- tag_query_result_node(tag, permanent_flag, **kwargs)[source]
Create a new QueryResult node and connect it to all of the cached nodes of the query via HasQueryResults edges.
- traverse_owned_by(**kwargs)[source]
Retrieve all traversed nodes up to a specified number of levels above the current query.
- Parameters:
max_levels (int) – Number of ownership edges between the current query and the nodes to be retrieved.
cls (str or iterable) – Node class or classes to retrieve.
- Returns:
result – Query containing requested nodes.
- Return type:
- traverse_owned_by_get_toplevel()[source]
Find associated LPU or Pattern that of the nodes encompassed by a query object.
- Returns:
result – QueryWrappers broken out by LPU/Pattern containing nodes of the origin query
- Return type:
dict
- traverse_owns(**kwargs)[source]
Retrieve all traversed nodes down to a specified number of levels below the current query.
- Parameters:
max_levels (int) – Number of ownership edges between the current query and the nodes to be retrieved.
cls (str or iterable) – Node class or classes to retrieve.
- Returns:
result – Query containing requested nodes.
- Return type:
Support Functions
Convert a graph between NetworkX and OrientDB
- neuroarch.conv.nx.as_nx(nodes=[], edges=[], force_rid=False, deepcopy=True)[source]
Converts OrientDB Gremlin query results into a NetworkX MultiDiGraph.
- Parameters:
nodes (list of pyorient.otypes.OrientRecord) – OrientDB node query results.
edges (list of pyorient.otypes.OrientRecord) – OrientDB edge query results.
force_rid (bool) – If True, always use the OrientDB RID as the node identifier in the returned graph. Otherwise, use ‘id’ property as the node identifier if it is present.
Results –
------- –
g (networkx.MultiDiGraph) – Constructed multigraph containing query results. The OrientDB class of each node and edge is stored in the ‘class’ attribute of the corresponding nodes and edges in the result g.
- neuroarch.conv.nx.nx_to_orient(client, g)[source]
Converts NetworkX MultiDiGraph to OrientDB graph.
- Parameters:
client (pyorient.orient.OrientDB) – OrientDB interface.
g (networkx.MultiDiGraph) – Graph to convert to OrientDB.
Notes
The ‘class’ attribute of each node and edge in g is assumed to be the OrientDB class name to use when creating the corresponding nodes and edges in the database. If no ‘class’ attribute is specified, the node and edge class names are assumed to be ‘V’ and ‘E’, respectively.
Node IDs are discarded upon creation of the new graph.
- neuroarch.conv.nx.orient_to_nx(client, node_query='', edge_query='', force_rid=False)[source]
Query OrientDB and return results as a NetworkX MultiDiGraph.
- Parameters:
client (pyorient.orient.OrientDB) – OrientDB interface.
node_query (str) – Gremlin query that returns a collection of nodes.
edge_query (str) – Gremlin query that returns a collection of edges.
force_rid (bool) – If True, always use the OrientDB RID as the node identifier in the returned graph. Otherwise, use ‘id’ property as the node identifier if it is present.
Results –
------- –
g (networkx.MultiDiGraph) – Constructed multigraph containing query results. The OrientDB class of each node and edge is stored in the ‘class’ attribute of the corresponding nodes and edges in the result g.
Convert a graph between Pandas and OrientDB.
- neuroarch.conv.pd.as_pandas(nodes=[], edges=[], force_rid=False, deepcopy=True)[source]
Converts OrientDB Gremlin query results into Pandas DataFrame.
- Parameters:
nodes (list of pyorient.otypes.OrientRecord) – OrientDB node query results.
edges (list of pyorient.otypes.OrientRecord) – OrientDB edge query results.
force_rid (bool) – If True, always use the OrientDB RID as the index value in the returned DataFrame of node data. Otherwise, use the ‘id’ property as the index value if it is present.
- Returns:
df_node, df_edge – DataFrame instances containing query results. The OrientDB class of each node and edge is stored in the ‘class’ column of the corresponding DataFrame instance.
- Return type:
pandas.DataFrame
- neuroarch.conv.pd.orient_to_pandas(client, node_query='', edge_query='', force_rid=False)[source]
Query OrientDB and return results as Pandas DataFrames.
- Parameters:
client (pyorient.orient.OrientDB) – OrientDB interface.
node_query (str) – Gremlin query that returns a collection of nodes.
edge_query (str) – Gremlin query that returns a collection of edges.
force_rid (bool) – If True, always use the OrientDB RID as the index value in the returned DataFrame of node data. Otherwise, use the ‘id’ property as the index value if it is present.
- Returns:
df_node, df_edge – DataFrame instances containing query results. The OrientDB class of each node and edge is stored in the ‘class’ column of the corresponding DataFrame instance.
- Return type:
pandas.DataFrame
- neuroarch.conv.pd.pandas_to_orient(client, df_node, df_edge)[source]
Loads Pandas DataFrames into OrientDB database.
- Parameters:
client (pyorient.orient.OrientDB) – OrientDB interface.
df_node (pandas.DataFrame) – Tables containing the properties of each node and edge to convert.
df_edge (pandas.DataFrame) – Tables containing the properties of each node and edge to convert.
Notes
Node IDs are discarded upon creation of the new graph