Python is an easy-to-learn, open-source interpreter language for which a large number of libraries are available on topics such as artificial intelligence, statistics, numerics and much more. An introduction to the Python programming language is not the subject of this documentation. You can find these at https://www.python.org/doc/, for example.
Python can be extended very flexibly, which enables elegant integration into FlexPro. The array data types required for processing measurement data are provided by the well-known Python library NumPy. These are the basis for data exchange with FlexPro, which is why NumPy is also installed when FlexPro is installed. You can find information on NumPy at https://numpy.org/doc/stable/.
The flexpro Module
When Python is initialized in FlexPro, the Python module flexpro is registered, which is the interface to FlexPro.
The Python classes flexpro.DataObject and flexpro.Object represent FlexPro data objects and other FlexPro objects so that you can access their properties. You create objects of these classes with the flexpro.object() function, to which you pass the path and the name in the object database of the object you want to access as an argument.
o = flexpro.object(r'\Data\Dataset1') creates a DataObject that represents the dataset "\Data\Dataset1".
The variable this is already initialized in every Python formula and contains the DataObject that represents the Python formula itself.
this.name returns the name of the Python formula as a string.
this.data = np.linspace(0, 10, 100) returns a linear data series as the result of the formula.
Note In FPScript, each statement returns a result that is automatically assigned to the formula as the result. This is not the case in Python and you must use an explicit assignment this.data = ... . This also applies to intermediate results that you want to see as the result of the formula in the Watch window during debugging with the debugger.
FlexPro data differs from Python or NumPy data in that it can have a unit and can have composite data structures with Y, X and Z components. Lists are also available in Python, but these do not support named list elements. The flexpro module therefore provides the flexpro.Data type, which can represent FlexPro data with all its properties.
You can create a flexpro.Data object with simple or composite FlexPro data using the flexpro.Data() function.
flexpro.Data(80.3, "dB") creates a scalar floating point value with the unit "dB".
This example creates a sine signal with unitized Y and X components and returns it as the result of the formula:
x = np.linspace(0, 10, 100)
y = np.sin(x)
this.data = flexpro.Data( y, "V", x, "s")
If you want to omit the units, e.g. because you have already entered them in the formula's properties dialog box, you must name the arguments:
this.data = flexpro.Data( value_y = y, value_x = x)
You can create a FlexPro list with two named list elements as follows, for example:
list = flexpro.named_list("Signal1", s1, "Signal2", s2)
Appending to such a list is also supported:
list += flexpro.named_list("Signal3", s3)
You can create lists without element names either with the Python operator [ ... ] or with the flexpro.list() function. The latter is somewhat more efficient because no conversion to the FlexPro data format is necessary when transferring to FlexPro.
The data property of the flexpro.DataObject class returns the data of the data object as a flexpro.Data object.
data = flexpro.object(r'\Data Set').data
If you want to access the data directly, use the call function:
data = flexpro.call(r'Data Set')
You call an FPScript or Python function by passing the arguments to call:
data = flexpro.call(r'\MyFunction', flexpro.call(r'\Data Set'), flexpro.Data(5, 'V'))
The flexPro.Data class has the properties value and unit. value provides the numerical data as a Python or NumPy array data type and unit provides the unit as a string.
flexpro.Data(1.3, "V").value returns 1.3 as a floating point number.
flexpro.Data(1.3, "V").unit returns "V" as a string.
For a composite data structure, first select the component:
signal = flexpro.call(r'\Data\Signal1')
signal.y returns a flexpro.Data object that represents the Y component with unit.
signal.y.value returns the data of the Y component as a NumPy array.
signal.y.unit returns "V" as a string.
If a list is available, first select the element:
list["Signal1"] returns a flexpro.Data object that represents the data of the list element "Signal1".
You must use a numerical index for unnamed lists or non-unique names:
list[0]
FlexPro attaches the header information of a data object to a flexpro.Data object that originates from a data object. These are essentially the properties that you can specify on the General tab of the data object.
You can have read and write access to these properties:
data = flexpro.call(r'\Signal')
data.unit_y returns the Y-unit of the data object from which the value originates, i.e. from "Signal".
data.comments_x = "Time" Assigns an X-comment to the data. However, write access is not to the "Signal" data object from which data originates, but to a local copy of this attribute assigned to the Data object.
See Also