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]
clear()[source]

Clear cached query results.

copy_models(func, in_flag, copies=1000)[source]
diff_save(new_df_nodes, new_df_edges, full_replace=False)[source]
diff_save_as(new_df_nodes, new_df_edges, **kwargs)[source]
direct_path_to(q, synapse_threshold=10, max_hops=2)[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.

classmethod empty_query(graph, debug=False)[source]
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.

execute_edges(edge_class='')[source]
property executed

Whether the query has been executed.

export_graph(graph_name, as_type='df', stored_as='gpickle', compression='')[source]
find_matching_ports_from_selector(other)[source]
classmethod from_elements(graph, *obj_list, **kwargs)[source]

Construct a QueryWrapper given a list of pyorient OGM objects.

Parameters:
Returns:

result – QueryWrapper instance.

Return type:

neuroarch.query.QueryWrapper

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:

neuroarch.query.QueryWrapper

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:

neuroarch.query.QueryWrapper

classmethod from_tag(graph, tag, debug=False)[source]
gen_traversal_in(*args, **kwargs)[source]
gen_traversal_out(*args, **kwargs)[source]
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_connected_ports()[source]
get_connecting_synapsemodels()[source]
get_connecting_synapses(N=None, rel='>', include_inferred=True, include_fragments=False, high_prob=False)[source]
get_data(as_type='df', edges=True, deepcopy=True, **kwargs)[source]
get_data_qw(**kwargs)[source]
get_data_rids(as_type='df', **kwargs)[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

has(**kwargs)[source]
import_graph(graph_name, as_type='df', stored_as='csv', compression='')[source]
classmethod multi_traverse_owned_by_toplevel(queryObjList)[source]
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:

QueryWrapper

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:

QueryWrapper

path_to(q, synapse_threshold=10, max_hops=2, exclude_path=True, timeout=120)[source]
path_to2(q, synapse_threshold=10, max_hops=2, exclude_path=True, timeout=30)[source]
path_to_neurons(unames, synapse_threshold=10, max_hops=2)[source]
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]
query(**kwargs)[source]
property query_string

The encapsulated query string(s).

synapses_to(q, synapse_threshold=10)[source]
tag_clean_up(older_than)[source]
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:

QueryWrapper

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:

QueryWrapper

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