F(x) Table With Extra Input¶
The plethora of f(x) nodes all have a similar role as the Calculator Tables node. But where the Calculator Tables 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 one of the two classes
TablesWrapper. The first
one is easier and should be your default choice.
TableWrapper provides access to the input and output table with
self.out_table respectively. These variables are of
table.File, so use the Table API to read/write the data.
from sympathy.api import table_wrapper class MyCalculation(table_wrapper.TableWrapper): def execute(self): spam = self.in_table.get_column_to_array('spam') # My super advanced calculation that totally couldn't be # done in the :ref:`Calculator Tables` node: more_spam = spam + 1 self.out_table.set_column_from_array('more spam', more_spam)
When working with a list of tables you can use the same base class as above and
your code will conveniently be executed once for each table. Alternatively you
can use the class
sympathy.api.table_wrapper.TablesWrapper (note the s).
This class will give access to the entire list all at once. This is useful for
when you need your code to be aware of several different tables from the list
at once, or if you need to output a list with a different number of tables than
the input. If you don’t need any of these features you’re better off using the
TableWrapper base class.
When using the
TablesWrapper base class you should access the input and
output data with
respectively (which are of the type
table.FileList). An example function:
from sympathy.api import table_wrapper class MyCalculation(table_wrapper.TablesWrapper): def execute(self): for in_table in self.in_table_list: # Only input tables with a column named 'spam' will yield an # output table. if 'spam' not in in_table.column_names(): continue spam = in_table.get_column_to_array('spam') # My super advanced calculation that totally couldn't be # done in the :ref:`Calculator Tables` node: more_spam = spam + 1 out_table = self.out_table_list.create() out_table.set_column_from_array('more spam', more_spam) self.out_table_list.append(out_table)
Three of the f(x) nodes (F(x) Table With Extra Input,
F(x) Tables With Extra Input, F(x) Tables With Extras Input) have
an extra table input port. You can access the extra table(s) as
Testing your script¶
When writing an f(x) script it can be convenient to be able to run the script from inside Spyder without switching to Sympathy. To do this you should first export all the data that the f(x) node receives on its input port as a sydata file. Then add a code block like this to the end of your script file:
if __name__ == "__main__": from sympathy.api import table input_filename = r"/path/to/input_file.sydata" with table.File(filename=input_filename, mode='r') as input_file: output_file = table.File() MyCalculation(input_file, output_file).execute()
You can now step through the code, set up break points and inspect variables as you run the script. Note that this will only work if the script is meant to be run with the Clean output option selected.
When Clean output is enabled (the default) the output table will be empty when the functions are run.
When the Clean output setting is disabled 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.
By default (pass-through disabled) only the functions that you have manually selected in the configuration will be run when you execute the node, but with the pass-through setting enabled the node will run all the functions in the selected file. This can be convenient in some situations when new functions are added often.
Apply functions to a Table. Also passes an extra auxiliary Table to the functions.
- port1 : Datasource
Path to Python file with scripted functions.
- port2 : Tables
Table with data to apply functions to.
- port3 : Table
Table with the results from the applied functions
- Clean output
If disabled the incoming data will be copied to the output before running the nodes.
- Select functions
Choose one or many of the listed functions to apply to the content of the incoming Table.
- Enable pass-through
If disabled only selected functions are run. Enable this to override the functions selection and run all functions in the python file.