An overview of Python scripting in Modeler 16

Python concept

Modeler scripts are used to automate the creation of streams, construction and configuration of nodes, stream execution and managing the execution results such as saving models to file or a content repository. A major new feature in Modeler 16 is the introduction of Python as the default scripting language. Python replaces the original bespoke language Modeler has had for over 15 years. Although existing legacy scripts will continue to work in Modeler 16, moving to Python offers a number of advantages:

  • It is popular language making it easier to find staff with relevant experience or training to improve existing skills
  • It offers a more modern approach to development including error handling, function definitions and object-oriented development style, allowing scripts to be more robust in deployed environments
  • It supports a number of standard add-on packages including file input/output functions and XML processing

As well as the advantages of the Python language, Modeler 16 offers a better development environment in which to build scripts:

  • A new editor with syntax highlighting
  • A debugging tab that allows expressions to be evaluated in the context of the most recently executed script
  • Auto-suggestion for function names – try typing a letter or two and pressing CTRL+SPACE to see what functions start with those characters

It’s worth noting that the first two features are also supported for legacy scripting.

We can compare the scripting styles by looking at the default scripts for the standard “druglearn” stream which can be found in the Modeler client installation under the “Demos/streams” folder.

The legacy script to execute the two model builder nodes is: 

    execute 'Drug':c50
    execute 'Drug':neuralnetwork

 
The equivalent script in Python is:    

    diagram = modeler.script.diagram()
    models = []
    diagram.findByType("c50", "Drug").run(models)
    diagram.findByType("neuralnetwork", "Drug").run(models)

 
There are a number of differences:

  • The Python script is based around function calls while the legacy script is command-oriented
  • The Python script includes modules to group related functions together (in this case the “modeler.script” module)
  • The Python script allows the results of the execution to be captured directly in a variable (the “models” list variable shown in the Python script)

I’ve written another post about how to write a standalone Python script in Modeler. If you would like more information about Modeler Python scripting in the meantime, you can find the online help here or download a PDF of the Modeler Scripting API Reference.