![]() |
SIMCA has the ability to automate/facilitate tasks using Python scripts. In order to use Python scripts you must have a license that allows it. Click File | Help to see your license information.
Much of the functionality relating to Python can be accessed from the Developer tab.
The Python interpreter is embedded in SIMCA and is accessible through the Python Console, script favorites and Add-Ins tab.
SIMCA with the Add-Ins tab, Favorites and Python console
The descriptions that follow assume that you have some experience with Python. If you have little or no experience with Python we recommend that you follow one of the tutorials in the Python tutorials section.
On the Developer tab you can:
If your license allows scripting but you cannot see the Developer tab, click File | Options | Customize and in the Customize the Ribbon list, select the Developer check box.
The console fills much the same function as the Python interactive interpreter and IDLE. Python commands can be executed directly in the console:
Output from script favorites and add-ins will also be printed in the console.
The easiest way to create a script is to click Create new script on the Developer tab. This will create a new .py file with the necessary boiler plate code.
After writing your scripts you can make them accessible in SIMCA in the Favorites pane but not on the Add-Ins tab.
To create a script accessible on the Add-Ins tab, click Create new add-in.
Python scripts can be added to the Favorites pane, either by dropping .py files in the pane or by right clicking and selecting Add script favorites and browsing for them. When you click a script favorite, the script is executed.
A tutorial on how to write script favorites can be found here
To create an add-in, click Create new add-in on the Developer tab. A new .py file is then created with the necessary boiler plate code.
Add-ins are more complex to write than scripts created using Create new script but enables you to extend the functionality of SIMCA in a better way.
There are three different types of add-ins available; Ribbon, preprocessing and file format.
Add-Ins are automatically added if the script file is located in one of the directories that Python searches for modules and contains the required functions.
Directories to search in can be added or deleted by clicking Set paths on the Developer tab.
The status and or errors of all loaded add-ins can be seen in Create new add-in | Verify add-ins
Ribbon add-ins enables you to create new buttons that executes scripts. To create a ribbon add-in, click Create new add-in under Create new add-in tab.
For a detailed description on how to write ribbon add-ins see the tutorial or the reference documentation.
If a preprocessing add-in is found it will appear as a new preprocessing under Data | Preprocessing (formerly known as spectral filters)
To create a new preprocessing add-in click Create new add-in | create new preprocessing add-in and a new script is created for you with the necessary functions.
The most important function is transform, the default function implemented just takes the square of all values. The matrix returned must always have the same number of rows
as the input data_collection but other than that anything is possible.
If self.generates_new_variables is False then the number of columns is constant, if it is True the number of columns can change and get_variable_name is called for each column.
A project created using a preprocessing add-in will have that script embedded in the project file, prediction will therefore work on other computers without the script.
If the script depends on external packages and you want to use the project on another computer, you can freeze the current environment to the project and get an option to restore the same environment on the new computer.
For a detailed description on how to write preprocessing add-ins see the reference documentation.
To create a new file format add-in click Create new add-in | create new file format add-in and a new script is created for you with the necessary functions.
The most important functions are read and write, the default script has a in a comment an example of a csv reader implemented, just remove the """'s to test it.
For a detailed description on how to write file format add-ins see the reference documentation.
These describe how to use Python in SIMCA and the functionality available.
SIMCA comes with a complete version of Python 3.7.1 including all standard libraries.
If you want to install Python separately on your computer it is recommended that you install the same version (can be downloaded here) for the same platform (x86 or x86-x64) as your SIMCA. If SIMCAs title bar says '32-bit' you need the x86 version, otherwise the x86-x64 version.
SIMCA has been run successfully with other versions of Python installed (like 2.7) but this has not been extensively tested.
External python libraries are best handled with umpypkg. umpypkg lets you install any library available in the Python Package Index. For a detailed description on virtual environments and pypi integration see the reference documentation.
To install numpy (a commonly used linear algebra library), run umpypkg in the SIMCA console.
>>> import umpypkg as pkg
>>> pkg.install('numpy')
Behind a firewall it may be necessary to load the libraries from another location like this.
>>> pkg.install('numpy', from_path='[The directory where to look for 'pkg']')
If you have scripts on your computer, for example scripts you have written, you can add the directory where you saved them to Python paths in SIMCA. It is recommended that you use packages written for the same version of python as SIMCA uses.
It is not necessary to have a debugger when writing scripts but it can be very useful. If you want to have a debugger software you need to find one that can use remote access. See for example Remote debuging with ptvsd and VS Code.
Python is relatively easy to learn but at the same time has all the features needed to write well structured, readable and maintainable code. It also comes with 'batteries included' meaning that there are libraries for everything under the sun. The standard library covers things like XML parsing, http servers and graphical user interfaces. If the standard library doesn't have the functionality you need there are a plethora of other free libraries available for things like linear algebra, statistics, encryption, graphics, databases, content management systems and much more.
R is licensed under the GPL which means that it can not be embedded in an application unless the application as a whole is licensed under the GPL. Since SIMCA can not be open sourced it prohibits the use of R.
Even if that wasn't the case, R is not a very good choise since it is intended primarily as a domain specific language for statistics and is not very good as a general purpose programing language. It seems like our customers primarily needs scripting for getting data into SIMCA, analyze it in SIMCA and get results out. If you need to do scientific computing outside of SIMCA, Python has the neccesary libraries like SciPy.
We feel that Python is easier to learn, better structured and has better library support than other scripting languages.
Copyright© Sartorius Stedim Data Analytics AB |
SIMCA, part of the Umetrics® Suite of Data Analytics Solutions |
SIMCA® Multivariate Data Analytics Solution |