Using python scripts to control module pipelines
This new feature allows to control module graphs via python scripts. You can:
- create, destroy, and connect modules
- change and retrieve the values of properties
- trigger triggers
- wait for new input on connectors
- wait for properties to get changed
This allows to build fully automatic batch processing pipelines.
How to load external modules from module toolboxes?
If you want to use modules from an external toolbox you must tell OpenWalnut how to load those modules. The OpenWalnut scripting interface is there a bit different from the Qt GUI. However there are two possibilities to tell the OW script interpreter where to search for additional modules.
- Using an existing
config.qt4gui
file. If you have such a an file in your OW config directory you may place a symlink/copy symlink to it with the nameconfig.script
. config.script. Then the script interpreter will load the same module toolboxes as the Qt GUI. On Linux and MacOS, you could do:
cd ~/.OpenWalnut
ln -s qt4gui.config config.script
On Windows, just copy the `config.qt4gui` file and rename it to `config.script`
- If you don't have such a config file, you might either create one by running and configuring the Qt GUI and then proceed as suggested above with a symlink/copy, or you can also create a plain text file named:
config.script
with the following line:additionalModulePaths=/home/you/path/to/your/toolbox/lib, /another/path/, /etc/pp
When the script interpreter fires up with debug log messages you should see something like this:
[Walnut] Reading config file from: "/home/you/.OpenWalnut/config.script"
How to write a python script for OpenWalnut?
import sys,time
data = rootContainer.createDataModule( sys.argv[1] )
gauss = rootContainer.create( "Gauss Filtering" )
writer = rootContainer.create( "Write NIfTI" )
writer.getInputConnector( "in" ).connect( gauss.getOutputConnector( "out" ) )
writer.getProperties().getProperty( "Filename" ).setFilename( sys.argv[2] )
gauss.getInputConnector( "in" ).connect( data.getOutputConnector( "out" ) )
writer.getInputConnector( "in" ).waitForInput()
time.sleep( 0.1 )
writer.getProperties().getProperty( "Do save" ).click()
time.sleep( 5 )
quit()
There are some good practices when working with the script interpreter:
- Wire the whole graph but ommit the data sources (e.g. data modules) in the beginning
- When all is wired (but the data sources) and all parameters are set, then you may wire the rest (data sources) and wait for specific events
- Keep in mind that writing to disk needs some time, so do a small sleep
- Each module might have connector names that differ from in and out, so run a Qt GUI version next to it to identify easily the connector names
- See source:resources/scripting/template.py for a rich featured example script
How to run a python script for OpenWalnut?
You might call the script interpreter directly from you build directory via: bin/openwalnut-script
. Then you append the option -f
and the path of the python script you want to execute. For example:
/home/you/repos/OW/build/bin/openwalnut-script -f ../resources/scripting/template.py ./inDir ./outDir