# Calculator Table¶

The Calculator nodes can perform calculation on Table(s). Accessing a column can be done either by using the ${} notation (${signal}) or by the Table API (table.col(‘signal’).data).

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 don’t propagate to the output table by default.
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
HJoin Tables to add calculated results to the input table, or you can
simply use the option ‘Copy input’ to do this automatically.

Example calculations:

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

The whole input table is available in the calculations as table. This allows the entire Table API to be used. For example you can get a list of a table’s column names:

```
table_names = table.column_names()
```

*Ports*:

Inputs:

port0: table

Input Table

Outputs:

port1: table

Table with results from the calculations.

*Configuration*:

calc_list- List of calculations.
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.
fail_strategy- Decide how a failed calculation should be handled

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

## 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 simply drag-and-drop the signal
name from the list of available signals to the calculation field.
To use a signal from the incoming generic data use *arg* in a way that fits the
data format as can be seen below:

To add a function, drag-and-drop it 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 you 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.

If something goes wrong when you define the calculations you will get an error or warning message in the preview window and at the top of the window.

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 signals that you access in the calculations are returned as 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 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 (the contrived) example:

```
filenames = np.array([value + value for value in signal])
```

where signal is a table column.

## Output¶

Each column of the output will have a *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 for 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'`

.