Calculator List

../../../../../_images/calculator.svg

The calculator node can apply calculations on each data column in a list. The calculations are defined in the configuration GUI of the node. The calculations are written as extended Python code and can consist of simple arithmetic calculations, Python function calls or calls to functions defined in plugins.

Some commonly used operators and functions can be found under the function tree structure (labeled Common functions) and can be added to a calculation by double-clicking or dragging the function name to the calculation area. If you want more information about a function, hover its name and its documentation will appear as a tooltip.

The signal that you access in the calculations are returned as a numpy arrays. The same as if you had called get_column_to_array() from the Table API. This means that simple arithmetics and the functions from numpy and pandas work fine out of the box. But if you want to apply some other function which only works on a single element of the column you may need to use Python list comprehensions. For example:

filenames = np.array([os.path.basename(path) for path in ${filepaths}]

Calculations

You declare each calculation by typing a name in the text line labeled Signal name and entering the calculation in the textfield labeled Calculation. You can use any of the signals in the list Signal names in your calculation. To use a signal from an incoming table type ${name_of_signal} (only in Table calculator) or simply drag-and-drop the signal name from the list of available signals to the calculation. To use a signal from the incoming generic data use arg in a way that fits the data format as can be seen below:

  • Table - arg.col('signal1').data
  • Tables - arg[0].col('signal1').data
  • ADAF - arg.sys['system0']['raster0']['signal0'].y for signal values and arg.sys['system0']['raster0']['signal0'].t for the timeseries
  • ADAFs - arg[0].sys['system0']['raster0']['signal0'].y for signal values and arg[0].sys['system0']['raster0']['signal0'].t for the timeseries
  • Text - arg.dtext for the text
  • Texts - arg[0].dtext for the text
  • Datasource - arg for Datasource object
  • Datasources - arg[0] for Datasource in Datasources
  • Tuple - Objects are accessed as arg[0], arg[1], …

To add a function type (or drag-and-drop) a function name from the Avaliable functions tree structure. Note that any signal that you reference in the calculation must exist in all incoming data structures.

To add a new calculation, press the New button and the Calculation field as well as Signal name will be cleared. If your want to edit a calculation simply click on the calculation in the List of calculations. The signal name will then appear under Signal name and the calculation will appear in the Calculation field. The calculation will be updated in the Calculation field, List of calculations and preview simultaneously. To remove a calculation, mark a calculation in List of calculations and press the Remove button. The result of your calculation is written to a column in an outgoing table.

Here are a few example calculations if your using Table/Tables calculator:

New signal name = ${Old signal} + 1
area = ${width} * ${height}
result = (${signal0} == 2) & ca.change_up(${signal1})
index = np.arange(len(${some signal}))
sine = np.sin(${angle})

And here are the same examples if use the generic calculator with a Table as input:

New signal name = res.col('Old signal').data + 1
area = arg.col('width').data * arg.col('height').data
result = (arg.col('signal0').data == 2) & ca.change_up(arg.col('signal1).data)
index = np.arange(len(arg.col('some signal').data))
sine = np.sin(arg.col('angle').data)

As you can see in the last two examples the numpy module is available in the calculator under the name np. If something goes wrong when you define the calculations you will get an error message in the preview window marked with red.

The api of the incoming data type can be used in the calculator. For example if input is a table you can use Table API to get a list of a table’s column names:

table_names = table.column_names()

Output

When the option Put results in common outputs is enabled (the default) each input structure results in a single output table with all the new columns. This means that all the calculated columns must be the same length. When disabled each calculation instead generates a table with a single column. The length of the outgoing list therefore depends on the number of incoming structures and the number of operations that are applied to each structure. As an example, if the incoming list consist of five tables and there are two calculations, the number of tables in the outgoing list will be 5*2=10.

Note that the incoming columns never propagate to the output table. If the results of you calculations are of the same length as the input, and the option Put results in common outputs is enabled you can use the node VJoin Tables to add calculated results to the input table.

Each column of the output will have an calculation attribute with a string representation of the calculation used to create that column.

In the configuration, there is an option on how to handle exceptions (Action on calculation failure) produced by the node, for example missing signals or erroneous calculations.

In the list of calculations there is also the option to disable individual calculations, i.e., exclude them from the output. This makes it possible to make intermediary calculations that does not have the same lengths as the the calculations that are actually output by the node. This could for example be useful when creating constants.

Compatibility

Under python 2 the calculations are evaluated with future imports division and unicode_literals activated. This means that in both python 2 and python 3 the calculation 1/2 will give 0.5 as result, and the calculation ‘hello’ will result in a unicode-aware text object (unicode in python 2 and str in python 3). To get floor division use the operator // and to get a binary string (str in python 2 and bytes in python 3) use the syntax b'hello'.

class node_calculator.CalculatorGenericListNode[source]

The Generic calculator can use any type as its input and it is accessed by the keyword arg. Tuples can also be used either with the same datatypes e.g., (Table, Table), (ADAF, ADAF, ADAF), etc, or with different datatypes e.g., (Table, ADAF), (ADAF, ADAFs, Tables). The items in the tuple are accessed by index, like so: arg[0], arg[1], etc.

Calculated signals can be accessed with res.

These nodes do not support the previous ${} notation.

Inputs:
port0 : [<a>]

Generic Input

Outputs:
port1 : [table]

Tables with results from the calculation performed by the node.The length of the outgoing list depends on the number of incoming data and the number of defined calculations. For example, if the incoming list consist of five data inputs and 2 calculations have been defined, the number of outgoing Tables will be equal to 5*2=10.

Configuration:
List of calculations

List of calculations.

Put results in common outputs.

Gather all the results generated from an incoming data into a common output table. This requires that the results all have the same length. An error will be given if the lengths of the outgoing results differ.

Copy input

If enabled the incoming data will be copied to the output before running the calculations. This requires that the results will all have the same length. An exception will be raised if the lengths of the outgoing results differ.

Action on calculation failure

Decide how a failed calculation should be handled

Plugins

StdPlugin