F(x)

../../../../../_images/fx.svg

Apply functions to an item.

Functions based on Fx will be invoked once on the item. The functions available are the ones where arg_types of the function matches the type of the item port (port2).

Ref. nodes:F(x) List
Input ports:
port1:

datasource

Path to Python file with scripted functions.

port2:

<a>

Item with data to apply functions on

Output ports:
port3:

<a>

Item with the results from the applied functions

Configuration:
Copy input (copy_input)
If enabled the incoming data will be copied to the output before running the nodes.
Select functions (selected_functions)
Choose one or many of the listed functions to apply to the content of the incoming item.
Python code (code)
Python code block, input is called arg, output res.

The F(x) nodes all have a similar role as the Calculator List node. But where the Calculator List node shines when the calculations are simple, the f(x) nodes are better suited for more advanced calculations since the code is kept in a separate python file. You can place this python file anywhere, but it might be a good idea to keep it in the same folder as your workflow or in a subfolder to that folder.

The script file

When writing a “function” (it is actually a python class) you need to inherit from Fx. The Fx provides access to the input and output with self.arg and self.res respectively. These variables are of the same type as the input on port2. Consult the API for that type to figure out relevant operations.

The field arg_types is a list containing string representations of types (as shown in port tooltips) that you intend your script to support and determines the types for which the function is available.

Example using class to define calculation:

from sympathy.api import fx

class MyCalculation(fx.Fx):
    arg_types = ['table']

    def execute(self):
        spam = self.arg['spam']

        # My super advanced calculation that totally couldn't be
        # done in the :ref:`Calculator Lists` node:
        more_spam = spam + 1

        self.res['more spam'] = more_spam

Alternative example using decorator:

from sympathy.api import fx

@fx.decorator(['table'])
def my_calculation(arg, res):
    spam = arg['spam']

    # My super advanced calculation that totally couldn't be
    # done in the :ref:`Calculator Lists` node:
    more_spam = spam + 1

    res['more spam'] = more_spam

The same script file can be used with both F(x) and F(x) List nodes.

A quick way to get the skeleton for a function is to use the function wizard that is started by clicking File->Wizards->New Function.

Script without separate file

For short scripts that are not intended to be shared between different nodes it can convenient not having to create an external file. To enable this feature, the datasource port first needs to be deleted. Once there is no datasource port, the node will show a code editor when configured.

The format is the same, the only exception is that all calculations (matching input type) will be executed.

Debugging your script

F(x) scripts can be debugged in spyder by following these simple steps:

  1. Open the script file in spyder and place a breakpoint somewhere in the execute method that you want to debug.
  2. Go back to Sympathy and right-click and choose Debug on the f(x) node with that function selected.
  3. Make sure that the file node_fx_selector.py is the active file in spyder and press Debug file (Ctrl+F5).
  4. A third python file will open as the debugging starts. Press Continue (Ctrl+F12) to arrive at the breakpoint in your f(x) script. From here you can step through your code however you want to.

Configuration

When Copy input is disabled (the default) the output table will be empty when the functions are run.

When the Copy input setting is enabled the entire input table will get copied to the output before running the functions in the file. This is useful when your functions should only add a few columns to a data table, but in this case you must make sure that the output has the same number of rows as the input.

class node_fx_selector.Fx[source]

Example flows