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

This Python example is the equivalent of a typical "Hello World" script. It is the simplest possible example designed to prove that your Infiniium / 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.
- Infiniium passes a dictionary of input variables (variables), including the input waveform (variable['SrcData']) to the algorithm function.
- Return a dictionary of measurement results to Infiniium including the output waveform key:value pair: FiltData" : FiltData.

## Procedure

1. Make sure that you have fulfilled the basic requirements for Python. See Installing Python.
2. Copy the following XML listing into a text editor. Name the file HelloWorld.xml and save it in Infiniium's user functions folder (\Documents\Keysight\Infiniium\User Functions).

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

3. Copy the following script into a text editor. Name your script file HelloWorld.py and save it in Infiniium's user functions folder (\Documents\Keysight\Infiniium\User Functions). Items to note in the script are:

- Input waveform, SrcData, that is passed as a key:value pair in the variables dictionary.
- This is a very simple script that does not 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 User Operator 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 Infiniium 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 title 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 your editor.
    import numpy as np

    def algorithm(variables):

        # Print to User Operator Output window.
        print('Hello World!')

        # Inputs copied to the outputs.
        SrcData = variables['SrcData'] if 'SrcData' in variables else np.empty(0, dtype=np.float32)
        FiltData = SrcData   # Output data same as input data.
        XOrg = variables['XOrg'] if 'XOrg' in variables else 0.0
        XInc = variables['XInc'] if 'XInc' in variables else 0.0

        # Other variables customized.
        Gain = 2.0
        FilterWidth = 0.0
        FilterDelay = 0.0
        XUnits = 'Same'
        YUnits = 'Same'
        ErrorMsg = ''

        # Output these variables.
        output_variables = {
            "FiltData" : FiltData,
            "XOrg" : XOrg,
            "XInc" : XInc,
            "Gain" : Gain,
            "FilterWidth" : FilterWidth,
            "FilterDelay" : FilterDelay,
            "XUnits" : XUnits,
            "YUnits" : YUnits,
            "ErrorMsg" : ErrorMsg
        }
        return output_variables

    # When testing from the command line.
    if __name__ == "__main__":
        data = np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float32)
        print(algorithm({'SrcData': data, 'XOrg': 0.1, 'XInc': 1.1}))

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

4. Let's test your script before we run it in Infiniium:

1. In a Command Prompt window, change to the user functions folder (\Documents\Keysight\Infiniium\User Functions).

2. Enter the command: `python HelloWorld.py`

   You should see the output:

       Hello World!
       {'FiltData': array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=float32), 'XOrg': 0.1,
        'XInc': 1.1, 'Gain': 2.0, 'FilterWidth': 0.0, 'FilterDelay': 0.0,
        'XUnits': 'Same', 'YUnits': 'Same', 'ErrorMsg': ''}

   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.

5. Display a single waveform.
6. Click the button on the lower-right corner of the screen.
7. At the top of the Functions dialog box, select the User tab and drag a single-input User operator icon into the operator construction area as shown in the following picture.
8. Click on the user operator to open the User Operator Setup dialog box.
9. In the dialog box, click Browse and select your XML configuration file, HelloWorld.xml.
10. In the Functions dialog box, drag a Function Color to the User Operator as shown in the following picture. This will display the output waveform of the function.
11. Click Auto Scale 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.
12. Click the button on the lower-right corner of the screen.
13. Click on your operator to open the User Operator Setup dialog box, and click the Show Output... button. This button appears in the dialog box only after the function has been added. If the dialog box is already open, close it, and reopen it to see the Show Output... button.
14. In the displayed User Operator Output dialog box, 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, Infiniium was in Single acquisition mode with the Single button clicked three times. You can use the User Operator Output to troubleshoot your scripts.