Lesson 1. Creating a "Hello World" User Operator Script

Instrument
DCA-X
N109x
UXR Scope
Flex Apps:
FlexDCA
FlexRT
Python

This Python example is the equivalent of a typical "Hello World" script. It is the simplest possible example designed to prove that your FlexDCA / Python environment is working correctly and to illustrate all of the basic steps required to create a user operator. After you've been successful with this example, try the Lesson 2. Creating a "Show Input Variables" User Operator Script.

Like all user operator Python scripts, HelloWorld.py consists of the following items:

  • A required algorithm function. Your script can have additional functions, but it must have the algorithm function as the main function.
  • FlexDCA passes a dictionary of input variables (variables), including the input waveform (variable['SrcData']) to the algorithm function.
  • Return a dictionary of measurement results to FlexDCA including the output waveform key:value pair: FiltData" : FiltData.

Procedure

  1. Make sure that you have fullfilled the basic requirements for Python.
  2. Copy the following XML listing into a text editor. Name the file HelloWorld.xml and save it in FlexDCA's user functions folder (\Documents\Keysight\FlexDCA\User Functions).
  3. HelloWorld.xml
    Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into your text editor!
    • <?xml version="1.0" encoding="utf-8"?>
    • <Function xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    • <Script>HelloWorld.py</Script>
    • <Name>Hello World!</Name>
    • <!-- <Icon>MyPicture.png</Icon> -->
    • <FunctionType>1 Source</FunctionType>
    • </Function>
    The <Icon> element is optional. This element allows you to place a picture on your user operator icon.
    Example without <Icon> element
    Example with <Icon> element
  4. Start Anaconda's Launcher application and then start the Python editor, Spyder. In Spyder, enter the following script. Name your script file HelloWorld.py and save it in FlexDCA's user measurements folder (\Documents\Keysight\FlexDCA\User Measurements). Spyder will keep line indentation correct for Python and will help to flag any typing mistakes. Color in the following script identifies:
    • Input waveform, SrcData, that is passed as a key:value pair in the variables dictionary.
    • This is a very simple script that doesn't even filter the waveform. It simply passes the input waveform to the output waveform, FiltData.
    • The Gain variable has been set to 2.0, so that you can more easily see the two waveforms displayed.
    • The print statement that can be used for troubleshooting by writing text to the Show Output dialog box.
    • The algorithm function is required in every Python user operator script. Notice that the algorithm function begins by initializing the nine input variables that are returned to FlexDCA in a dictionary (key:value pairs). The order that you list the key:value pairs is not import, however all key:value pairs must be returned.

    The algorithm function name must be written in lower case. Naming the function Algorithm.m or ALGORITHM, for example, will break the script.

    HelloWorld.py
    Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into the Spyder editor!
    def algorithm(variables):
        FiltData = []
        XOrg = 0.0
        XInc = 0.0
        Gain = 2.0
        FilterWidth = 0.0
        FilterDelay = 0.0
        XUnits = 'Same'
        YUnits = 'Same'
        ErrorMsg = ''
        
        SrcData = variables['SrcData']
        XOrg = variables['XOrg']
        XInc = variables['XInc']
        print 'Hello World!'FiltData = SrcData
        return { "FiltData" : FiltData,
                 "XOrg" : XOrg,
                 "XInc" : XInc,
                 "Gain" : Gain,
                 "FilterWidth" : FilterWidth,
                 "FilterDelay" : FilterDelay,
                 "XUnits" : XUnits,
                 "YUnits" : YUnits,
                 "ErrorMsg" : ErrorMsg
               }
    

    Do not use spaces or hyphens when selecting a filename for your script.

    Enter the above script exactly as written. Otherwise, the script will fail.

  5. Let's test your script in Spyder before we run it in FlexDCA:
    1. Click in the IPython console to select it.
    2. In Spyder's menu, click Run > Run to run the script.
    3. In the IPython console window shown above, type algorithm({'SrcData':'12345','XOrg':0.0,'XInc':1.1}) and press Enter. The following pictures shows what should be displayed in the window. Notice that "Hello World!" is displayed along with the returned dictionary listing. The order of the dictionary key:value pairs (ErrorMsg, FiltData, FilterDelay, FilterWidth, Gain, XInc, XOrg, XUnits, and YUnits) in the window does not matter.
  6. Place FlexDCA into Oscilloscope mode and display a single waveform.
  7. On FlexDCA's menu, click Measure > Waveform Signal Processing (Math).
  8. At the top of the Waveform Signal Processing dialog, select the User tab and drag a single-input User operator icon into the operator construction area as shown in the following picture.
  9. Click on the user operator to open the User Operator Setup dialog.
  10. In the dialog, click Browse and select your XML configuration file, HelloWorld.xml.
  11. In the Waveform Signal Processing dialog, drag a Function Color to the User Operator as shown in the following picture. This will display the output waveform of the function.
  12. Click Autoscale and the input and output waveforms should be displayed. Of course, your waveforms will be different from those shown here. The important thing is that both the input and output waveforms are displayed. Notice that the Signals legend identifies the operator's output waveform.
  13. On FlexDCA's menu, click Measure > Waveform Signal Processing (Math) to open the Waveform Signal Processing dialog if you have closed it.
  14. Click on your operator to open the User Operator Setup dialog, and click the Show Output button. This button only appears on the dialog after the measurement has been started.
  15. In the displayed Output dialog, you should see the output of the print statement in your HelloWorld.py script. One Hello World! is printed each time the operator is run. For this picture, FlexDCA was in Single acquisition mode with the Single button clicked three times. You can use this feature to troubleshoot your scripts.