aiida.orm.implementation.general.calculation package

class aiida.orm.implementation.general.calculation.AbstractCalculation[source]

Bases: aiida.orm.mixins.Sealable

This class provides the definition of an “abstract” AiiDA calculation. A calculation in this sense is any computation that converts data into data.

You will typically use one of its subclasses, often a JobCalculation for calculations run via a scheduler.

FAILED_KEY = '_failed'
FINISHED_KEY = '_finished'
__dir__()[source]

Allow to list all valid attributes, adding also the use_* methods

__getattr__(name)[source]

Expand the methods with the use_* calls. Note that this method only gets called if ‘name’ is not already defined as a method. Returning None will then automatically raise the standard AttributeError exception.

__module__ = 'aiida.orm.implementation.general.calculation'
_cacheable = False
_get_objects_to_hash()[source]

Return a list of objects which should be included in the hash.

_hash_ignored_attributes

A class that, when used as a decorator, works as if the two decorators @property and @classmethod where applied together (i.e., the object works as a property, both for the Class and for any of its instance; and is called with the class cls rather than with the instance as its first argument).

_hash_ignored_inputs = ['CALL']
_is_valid_cache()[source]
_linking_as_output(dest, link_type)[source]

An output of a calculation can only be a data.

Parameters:dest – a Data object instance of the database
Raise:ValueError if a link from self to dest is not allowed.

Replace a link.

Parameters:
  • src – a node of the database. It cannot be a Calculation object.
  • label (str) – Name of the link.
_updatable_attributes = ('_sealed', 'state', '_finished', '_failed')
_use_methods = {'code': {'linkname': 'code', 'additional_parameter': None, 'docstring': 'Choose the code to use', 'valid_types': <class 'aiida.orm.implementation.django.code.Code'>}}

Add a link with a code as destination.

You can use the parameters of the base Node class, in particular the label parameter to label the link.

Parameters:
  • src – a node of the database. It cannot be a Calculation object.
  • label (str) – Name of the link. Default=None
  • link_type – The type of link, must be one of the enum values form LinkType
get_code()[source]

Return the code for this calculation, or None if the code was not set.

get_linkname(link, *args, **kwargs)[source]

Return the linkname used for a given input link

Pass as parameter “NAME” if you would call the use_NAME method. If the use_NAME method requires a further parameter, pass that parameter as the second parameter.

has_failed()[source]

Returns whether the Calculation has failed.

has_finished()[source]

Determine if the calculation is finished for whatever reason. This may be because it finished successfully or because of a failure.

Returns:True if the job has finished running, False otherwise.
Return type:bool
has_finished_ok()[source]

Returns whether the Calculation has finished successfully.

logger

Get the logger of the Calculation object, so that it also logs to the DB.

Returns:LoggerAdapter object, that works like a logger, but also has the ‘extra’ embedded
aiida.orm.implementation.general.calculation._parse_single_arg(function_name, additional_parameter, args, kwargs)[source]

Verifies that a single additional argument has been given (or no additional argument, if additional_parameter is None). Also verifies its name.

Parameters:
  • function_name – the name of the caller function, used for the output messages
  • additional_parameter – None if no additional parameters should be passed, or a string with the name of the parameter if one additional parameter should be passed.
Returns:

None, if additional_parameter is None, or the value of the additional parameter

Raises:

TypeError – on wrong number of inputs

Submodules

class aiida.orm.implementation.general.calculation.inline.InlineCalculation(**kwargs)[source]

Bases: aiida.orm.implementation.django.calculation.Calculation

Subclass used for calculations that are automatically generated using the make_inline wrapper/decorator.

This is used to automatically create a calculation node for a simple calculation

__abstractmethods__ = frozenset([])
__module__ = 'aiida.orm.implementation.general.calculation.inline'
_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 38
_abc_registry = <_weakrefset.WeakSet object>
_cacheable = True
_logger = <logging.Logger object>
_plugin_type_string = 'calculation.inline.InlineCalculation.'
_query_type_string = 'calculation.inline.'
get_desc()[source]

Returns a string with infos retrieved from a InlineCalculation node’s properties. :return: description string

get_function_name()[source]

Get the function name.

Returns:a string
has_failed()[source]

Returns whether the Calculation has failed.

has_finished_ok()[source]

Returns whether the Calculation has finished successfully.

aiida.orm.implementation.general.calculation.inline.make_inline(func)[source]

This make_inline wrapper/decorator takes a function with specific requirements, runs it and stores the result as an InlineCalculation node. It will also store all other nodes, including any possibly unstored input node! The return value of the wrapped calculation will also be slightly changed, see below.

The wrapper:

  • checks that the function name ends with the string '_inline'
  • checks that each input parameter is a valid Data node (can be stored or unstored)
  • runs the actual function
  • gets the result values
  • checks that the result value is a dictionary, where the key are all strings and the values are all unstored data nodes
  • creates an InlineCalculation node, links all the kwargs as inputs and the returned nodes as outputs, using the keys as link labels
  • stores all the nodes (including, possibly, unstored input nodes given as kwargs)
  • returns a length-two tuple, where the first element is the InlineCalculation node, and the second is the dictionary returned by the wrapped function

To use this function, you can use it as a decorator of a wrapped function:

@make_inline
def copy_inline(source):
    return {copy: source.copy()}

In this way, every time you call copy_inline, the wrapped version is actually called, and the return value will be a tuple with the InlineCalculation instance, and the returned dictionary. For instance, if s is a valid Data node, with the following lines:

c, s_copy_dict = copy_inline(source=s)
s_copy = s_copy_dict['copy']

c will contain the new InlineCalculation instance, s_copy the (stored) copy of s (with the side effect that, if s was not stored, after the function call it will be automatically stored).

Note:If you use a wrapper, make sure to write explicitly in the docstrings that the function is going to store the nodes.

The second possibility, if you want that by default the function does not store anything, but can be wrapped when it is necessary, is the following. You simply define the function you want to wrap (copy_inline in the example above) without decorator:

def copy_inline(source):
   return {copy: source.copy()}

This is a normal function, so to call it you will normally do:

s_copy_dict = copy_inline(s)

while if you want to wrap it, so that an InlineCalculation is created, and everything is stored, you will run:

c, s_copy_dict = make_inline(f)(s=s)

Note that, with the wrapper, all the parameters to f() have to be passed as keyworded arguments. Moreover, the return value is different, i.e. (c, s_copy_dict) instead of simply s_copy_dict.

Note

EXTREMELY IMPORTANT! The wrapped function MUST have the following requirements in order to be reproducible. These requirements cannot be enforced, but must be followed when writing the wrapped function.

  • The function MUST NOT USE information that is not passed in the kwargs. In particular, it cannot read files from the hard-drive (that will not be present in another user’s computer), it cannot connect to external databases and retrieve the current entries in that database (that could change over time), etc.
  • The only exception to the above rule is the access to the AiiDA database for the parents of the input nodes. That is, you can take the input nodes passed as kwargs, and use also the data given in their inputs, the inputs of their inputs, … but you CANNOT use any output of any of the above-mentioned nodes (that could change over time).
  • The function MUST NOT have side effects (creating files on the disk, adding entries to an external database, …).

Note

The function will also store:

  • the source of the function in an attribute “source_code”, and the first line at which the function appears (attribute “first_line_source_code”), as returned by inspect.getsourcelines;
  • the full source file in “source_file”, if it is possible to retrieve it (this will be set to None otherwise, e.g. if the function was defined in the interactive shell).

For this reason, try to keep, if possible, all the code to be run within the same file, so that it is possible to keep the provenance of the functions that were run (if you instead call a function in a different file, you will never know in the future what that function did). If you call external modules and you matter about provenance, if would be good to also return in a suitable dictionary the version of these modules (e.g., after importing a module XXX, you can check if the module defines a variable XXX.__version__ or XXX.VERSION or something similar, and store it in an output node).

Todo:

For the time being, I am storing the function source code and the full source code file in the attributes of the calculation. To be moved to an input Code node!

Note:

All nodes will be stored, including unstored input nodes!!

Parameters:

kwargs – all kwargs are passed to the wrapped function

Returns:

a length-two tuple, where the first element is the InlineCalculation node, and the second is the dictionary returned by the wrapped function. All nodes are stored.

Raises:
  • TypeError – if the return value is not a dictionary, the keys are not strings, or the values are not data nodes. Raise also if the input values are not data nodes.
  • ModificationNotAllowed – if the returned Data nodes are already stored.
  • Exception – All other exceptions from the wrapped function are not catched.
class aiida.orm.implementation.general.calculation.work.WorkCalculation(**kwargs)[source]

Bases: aiida.orm.implementation.django.calculation.Calculation

Used to represent a calculation generated by a Process from the new workflows system.

ABORTED_KEY = '_aborted'
DO_ABORT_KEY = '_do_abort'
__abstractmethods__ = frozenset([])
__module__ = 'aiida.orm.implementation.general.calculation.work'
_abc_cache = <_weakrefset.WeakSet object>
_abc_negative_cache = <_weakrefset.WeakSet object>
_abc_negative_cache_version = 38
_abc_registry = <_weakrefset.WeakSet object>
_logger = <logging.Logger object>
_plugin_type_string = 'calculation.work.WorkCalculation.'
_query_type_string = 'calculation.work.'
_updatable_attributes = ('_sealed', 'state', '_finished', '_failed', '_aborted', '_do_abort')
has_aborted()[source]

Returns True if the work calculation was killed and is

Returns:True if the calculation was killed, False otherwise.
Return type:bool
has_failed()[source]

Returns True if the work calculation failed because of an exception, False otherwise

Returns:True if the calculation has failed, False otherwise.
Return type:bool
has_finished()[source]

Determine if the calculation is finished for whatever reason. This may be because it finished successfully or because of a failure.

Returns:True if the job has finished running, False otherwise.
Return type:bool
has_finished_ok()[source]

Returns True if the work calculation finished normally, False otherwise (could be that it’s still running)

Returns:True if finished successfully, False otherwise.
Return type:bool
kill()[source]

Kill a WorkCalculation and all its children.