An AiiDA database stores a graph of connected entities, which can be queried with the QueryBuilder class.
QueryBuilder
Before starting to write a query, it helps to:
Once you are clear about what you want and how you can get it, the QueryBuilder will build an SQL-query for you.
There are two ways of using the QueryBuilder:
In the appender method, you construct your query step by step using the QueryBuilder.append() method.
QueryBuilder.append()
In the queryhelp approach, you construct a dictionary that defines your query and pass it to the QueryBuilder.
Both APIs provide the same functionality - the appender method may be more suitable for interactive use, e.g., in the verdi shell, whereas the queryhelp method can be useful in scripting. In this section we will focus on the basics of the appender method. For more advanced queries or more details on the queryhelp, see the topics section on advanced querying.
verdi shell
Using the append() method of the QueryBuilder, you can query for the entities you are interested in. Suppose you want to query for calculation job nodes in your database:
append()
from aiida.orm import QueryBuilder qb = QueryBuilder() # Instantiating instance. One instance -> one query qb.append(CalcJobNode) # Setting first vertex of path
If you are interested in instances of different classes, you can also pass an iterable of classes. However, they have to be of the same ORM-type (e.g. all have to be subclasses of Node):
Node
qb = QueryBuilder() # Instantiating instance. One instance -> one query qb.append([CalcJobNode, WorkChainNode]) # Setting first vertices of path, either WorkChainNode or Job.
Note
Processes have both a run-time Process that executes them and a Node that stores their data in the database (see the corresponding topics section for a detailed explanation). The QueryBuilder allows you to pass either the Node class (e.g. CalcJobNode) or the Process class (e.g. CalcJob), which will automatically select the right entity for the query. Using either CalcJobNode or CalcJob will produce the same query results.
Process
CalcJobNode
CalcJob
Once you have appended the entity you want to query for to the QueryBuilder, the next question is how to get the results. There are several ways to obtain data from a query:
qb = QueryBuilder() # Instantiating instance qb.append(CalcJobNode) # Setting first vertices of path first_row = qb.first() # Returns a list (!) of the results of the first row all_results_d = qb.dict() # Returns all results as a list of dictionaries all_results_l = qb.all() # Returns a list of lists
In case you are working with a large dataset, you can also return your query as a generator:
all_res_d_gen = qb.iterdict() # Return a generator of dictionaries # of all results all_res_l_gen = qb.iterall() # Returns a generator of lists
This will retrieve the data in batches, and you can start working with the data before the query has completely finished. For example, you can iterate over the results of your query in a for loop:
for entry in qb.iterall(): # do something with a single entry in the query result
Usually you do not want to query for all entities of a certain class, but rather filter the results based on certain properties. Suppose you do not want all CalcJobNode data, but only those that are finished:
finished
qb = QueryBuilder() # Initialize a QueryBuilder instance qb.append( CalcJobNode, # Append a CalcJobNode filters={ # Specify the filters: 'attributes.process_state': 'finished', # the process is finished }, )
You can apply multiple filters to one entity in a query. Say you are interested in all calculation jobs in your database that are finished and have exit_status == 0:
exit_status == 0
qb = QueryBuilder() # Initialize a QueryBuilder instance qb.append( CalcJobNode, # Append a CalcJobNode filters={ # Specify the filters: 'attributes.process_state': 'finished', # the process is finished AND 'attributes.exit_status': 0 # has exit_status == 0 }, )
In case you want to query for calculation jobs that satisfy one of these conditions, you can use the or operator:
or
qb = QueryBuilder() qb.append( CalcJobNode, filters={ 'or':[ {'attributes.process_state': 'finished'}, {'attributes.exit_status': 0} ] }, )
If we had written and instead of or in the example above, we would have performed the exact same query as the previous one, because and is the default behavior if you provide several filters as key-value pairs in a dictionary to the filters argument. In case you want all calculation jobs with state finished or excepted, you can also use the in operator:
and
filters
excepted
in
qb = QueryBuilder() qb.append( CalcJobNode, filters={ 'attributes.process_state': {'in': ['finished', 'excepted']} }, )
You can negate a filter by adding an exclamation mark in front of the operator. So, to query for all calculation jobs that are not a finished or excepted state:
qb = QueryBuilder() qb.append( CalcJobNode, filters={ 'attributes.process_state': {'!in': ['finished', 'excepted']} }, )
The above rule applies to all operators. For example, you can check non-equality with !==, since this is the equality operator (==) with a negation prepended.
!==
==
A complete list of all available operators can be found in the advanced querying section.
It is possible to query for data based on its relationship to another entity in the database. Imagine you are not interested in the calculation jobs themselves, but in one of the outputs they create. You can build upon your initial query for all CalcJobNode’s in the database using the relationship of the output to the first step in the query:
qb = QueryBuilder() qb.append(CalcJobNode, tag='calcjob') qb.append(Int, with_incoming='calcjob')
In the first append call, we query for all CalcJobNode’s in the database, and tag this step with the unique identifier 'calcjob'. Next, we look for all Int nodes that are an output of the CalcJobNode’s found in the first step, using the with_incoming relationship argument. The Int node was created by the CalcJobNode and as such has an incoming create link.
append
'calcjob'
Int
with_incoming
In the context of our query, we are building a path consisting of vertices (i.e. the entities we query for) connected by edges defined by the relationships between them. The complete set of all possible relationships you can use query for, as well as the entities that they connect to, can be found in the advanced querying section.
The tag identifier can be any alphanumeric string, it is simply a label used to refer to a previous vertex along the query path when defining a relationship.
tag
By default, the QueryBuilder returns the instances of the entities corresponding to the final append to the query path. For example:
The above code snippet will return all Int nodes that are outputs of any CalcJobNode. However, you can also project other entities in the path by adding project='*' to the corresponding append() call:
project='*'
qb = QueryBuilder() qb.append(CalcJobNode, tag='calcjob', project='*') qb.append(Int, with_incoming='calcjob')
This will return all CalcJobNode’s that have an Int output node.
However, in many cases we are not interested in the entities themselves, but rather their PK, UUID, attributes or some other piece of information stored by the entity. This can be achieved by providing the corresponding column to the project keyword argument:
project
qb = QueryBuilder() qb.append(CalcJobNode, tag='calcjob') qb.append(Int, with_incoming='calcjob', project='id')
In the above example, executing the query returns all PK’s of the Int nodes which are outputs of all CalcJobNode’s in the database. Moreover, you can project more than one piece of information for one vertex by providing a list:
qb = QueryBuilder() qb.append(CalcJobNode, tag='calcjob') qb.append(Int, with_incoming='calcjob', project=['id', 'attributes.value'])
For the query above, qb.all() will return a list of lists, for which each element corresponds to one entity and contains two items: the PK of the Int node and its value. Finally, you can project information for multiple vertices along the query path:
qb.all()
qb = QueryBuilder() qb.append(CalcJobNode, tag='calcjob', project='*') qb.append(Int, with_incoming='calcjob', project=['id', 'attributes.value'])
All projections must start with one of the columns of the entities in the database, or project the instances themselves using '*'. Examples of columns we have encountered so far are id, uuid and attributes. If the column is a dictionary, you can expand the dictionary values using a dot notation, as we have done in the previous example to obtain the attributes.value. This can be used to project the values of nested dictionaries as well.
'*'
id
uuid
attributes
attributes.value
Be aware that for consistency, QueryBuilder.all() / iterall() always returns a list of lists, even if you only project one property of a single entity. Use QueryBuilder.all(flat=True) to return the query result as a flat list in this case.
QueryBuilder.all()
iterall()
QueryBuilder.all(flat=True)
As mentioned in the beginning, this section provides only a brief introduction to the QueryBuilder’s basic functionality. To learn about more advanced queries, please see the corresponding topics section.
The QueryBuilder is the generic way of querying for data in AiiDA. For certain common queries, shortcuts have been added to the AiiDA python API to save you a couple of lines of code.
The provenance graph in AiiDA is a directed graph. The vertices of the graph are the nodes, and the edges that connect them are called links. Since the graph is directed, any node can have incoming and outgoing links that connect it to neighboring nodes.
To discover the neighbors of a given node, you can use the methods get_incoming() and get_outgoing(). They have the exact same interface but will return the neighbors connected to the current node with a link coming into it or with links going out of it, respectively. For example, for a given node, to inspect all the neighboring nodes from which a link is incoming to the node:
get_incoming()
get_outgoing()
node
node.get_incoming()
This will return an instance of the LinkManager. From that manager, you can request the results in a specific format. If you are only interested in the neighboring nodes themselves, you can call the all_nodes method:
LinkManager
all_nodes
node.get_incoming().all_nodes()
This will return a list of Node instances that correspond to the nodes that are neighbors of node, where the link is going towards node. Calling the all() method of the manager instead will return a list of LinkTriple named tuples. These tuples contain, in addition to the neighboring node, also the link label and the link type with which they are connected to the origin node. For example, to list all the neighbors of a node from which a link is incoming:
all()
LinkTriple
for link_triple in node.get_incoming().all(): print(link_triple.node, link_triple.link_type, link_triple.link_label)
Note that the LinkManager provides many convenience methods to get information from the neigboring nodes, such as all_link_labels() if you only need the list of link labels.
all_link_labels()
The get_incoming() and get_outgoing() methods accept various arguments that allow one to filter what neighboring nodes should be matched:
node_class: accepts a subclass of Node, only neighboring nodes with a class that matches this will be returned link_type: accepts a value of LinkType, only neighboring nodes that are linked with this link type will be returned link_label_filter: accepts a string expression (with optional wildcards using the syntax of SQL LIKE patterns, see below), only neighboring nodes that are linked with a link label that matches the pattern will be returned
node_class: accepts a subclass of Node, only neighboring nodes with a class that matches this will be returned
node_class
link_type: accepts a value of LinkType, only neighboring nodes that are linked with this link type will be returned
link_type
LinkType
link_label_filter: accepts a string expression (with optional wildcards using the syntax of SQL LIKE patterns, see below), only neighboring nodes that are linked with a link label that matches the pattern will be returned
link_label_filter
LIKE
As an example:
node.get_incoming(node_class=Data, link_type=LinkType.INPUT_CALC, link_label_filter='output%node_').all_nodes()
will return only neighboring data nodes that are linked to the node with a link of type LinkType.INPUT_CALC and where the link label matches the pattern 'output%node_'. Reminder on the syntax of SQL LIKE patterns: the % character matches any string of zero or more characters, while the _ character matches exactly one character. These two special characters can be escaped by prepending them with a backslash (note that when putting a backslash in a Python string you have to escape the backslash itself, so you will need two backslashes: e.g., to match exactly a link label a_b you need to pass link_label_filter='a\\_b').
LinkType.INPUT_CALC
'output%node_'
%
_
a_b
link_label_filter='a\\_b'
The get_incoming() and get_outgoing() methods, described in the previous section, can be used to access all neighbors from a certain node and provide advanced filtering options. However, often one doesn’t need this expressivity and simply wants to retrieve all neighboring nodes with a syntax that is as succint as possible. A prime example is to retrieve the inputs or outputs of a process. Instead of using get_incoming() and get_outgoing(), to get the inputs and outputs of a process_node one can do:
process_node
inputs = process_node.inputs outputs = process_node.outputs
These properties do not return the actual inputs and outputs directly, but instead return an instance of NodeLinksManager. The reason is because through the manager, the inputs or outputs are accessible through their link label (that, for inputs and outputs of processes, is unique) and can be tab-completed. For example, if the process_node has an output with the label result, it can be retrieved as:
NodeLinksManager
result
process_node.outputs.result
The inputs or outputs can also be accessed through key dereferencing:
process_node.outputs['result']
If there is no neighboring output with the given link label, a NotExistentAttributeError or NotExistentKeyError will be raised, respectively.
NotExistentAttributeError
NotExistentKeyError
The inputs and outputs properties are only defined for ProcessNode’s. This means that you cannot chain these calls, because an input or output of a process node is guaranteed to be a Data node, which does not have inputs or outputs.
inputs
outputs
ProcessNode
Data
Similar to the inputs and outputs properties of process nodes, there are some more properties that make exploring the provenance graph easier:
called(): defined for ProcessNode’s and returns the list of process nodes called by this node. If this process node did not call any other processes, this property returns an empty list. caller(): defined for ProcessNode’s and returns the process node that called this node. If this node was not called by a process, this property returns None. creator(): defined for Data nodes and returns the process node that created it. If the node was not created by a process, this property returns None.
called(): defined for ProcessNode’s and returns the list of process nodes called by this node. If this process node did not call any other processes, this property returns an empty list.
called()
caller(): defined for ProcessNode’s and returns the process node that called this node. If this node was not called by a process, this property returns None.
caller()
None
creator(): defined for Data nodes and returns the process node that created it. If the node was not created by a process, this property returns None.
creator()
Using the creator and inputs properties, one can easily move up the provenance graph. For example, starting from some data node that represents the result of a long workflow, one can move up the provenance graph to find an initial input node of interest: result.creator.inputs.some_input.creator.inputs.initial_input.
creator
result.creator.inputs.some_input.creator.inputs.initial_input
CalcJobNode’s provide the res() property, that can give easy access to the results of the calculation job. The requirement is that the CalcJob class that produced the node, defines a default output node in its spec. This node should be a Dict output that will always be created. An example is the TemplatereplacerCalculation plugin, that has the output_parameters output that is specified as its default output node.
res()
Dict
TemplatereplacerCalculation
output_parameters
The res() property will give direct easy access to all the keys within this dictionary output. For example, the following:
list(node.res)
will return a list of all the keys in the output node. Individual keys can then be accessed through attribute dereferencing:
node.res.some_key
In an interactive shell, the available keys are also tab-completed. If you type node.res. followed by the tab key twice, a list of the available keys is printed.
node.res.
The res() property is really just a shortcut to quickly and easily access an attribute of the default output node of a calculation job. For example, if the default output node link label is output_parameters, then node.res.some_key is exactly equivalent to node.outputs.output_parameters.dict.some_key. That is to say, when using res, one is accessing attributes of one of the output nodes, and not of the calculation job node itself.
node.outputs.output_parameters.dict.some_key
res