Python Operator Samples

About the Sample

In this sample, the Python and Python Instance operators work to create Python instances and execute Python code. The operators enable Python-centric teams to reuse their code without major rewrites to execute event processing. This includes execution of models produced with SciPy and TensorFlow.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, select FileLoad StreamBase Sample.

  • In the search field, enter wits to narrow the list of samples.

  • Select Performing operations using Python from the Large Data Storage and Analysis category.

  • Click OK.

StreamBase Studio creates a single project containing the sample files.

Samples Setup

Before running the samples, open the supplied adapter-configurations.xml, which is located in src/main/resources, and edit the configuration to point to your installed version of Python.

To run the TensorFlow sample, you must have the following modules installed into your Python:

  1. TensorFlow

  2. OpenCV-Python

Running the Version Sample

This sample demonstrates performing a very basic Python call and returning a result.

  1. In the Project Explorer, open the sample you just loaded.

  2. Open the src/main/eventflow folder and double-click to open sample-version.sbapp. Make sure the application is the currently active tab in the EventFlow Editor.

  3. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

    If you see red marks, wait a moment for the project in Studio to load its features.

    If red marks do not resolve themselves in a moment, select the project and right-click MavenUpdate Project from the context menu.

  4. In the Manual Input view, select the Input input stream.

    Click Send Data.

  5. In the Application Output view, look for the OutputStream tuples for the selected operation, which gives the current Python's version information.

  6. When done, press F9 or click the Stop Running Application button.

Running the Local Instance Sample

This sample demonstrates using a local instance of the Python engine — versus a global instance — that can be accessed by all running modules. The input takes an integer that determines how many Fibonacci series to calculate and output.

  1. In the Project Explorer view, double-click to open sample-local-instance.sbapp. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Manual Input view, select the InputStream input stream.

    Enter 100 into the fib field.

    Click Send Data.

  4. In the Application Output view, look for the OutputStream tuples for the selected operation, which gives the current Python's version information.

  5. When done, press F9 or click the Stop Running Application button.

Running the TensorFlow Sample

This sample demonstrates using TensorFlow to run a model in multiple thread instances, evaluate model input, and receive model output. This sample requires that the Python installation is configured to handle TensorFlow as mentioned above.

The sample uses a worker module to enable eight operations to occur simultaneously. Each instance has its own instance of Python and initializes the model within each instance. See the sample-tensorflow-worker.sbapp

  1. In the Project Explorer view, double-click to open sample-tensorflow.sbapp. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Application Output view, look for the OutputStream tuples for the selected operation, which gives the current Python's version information.

  4. When done, press F9 or click the Stop Running Application button.

Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids permission problems. The default workspace location for this sample is:

studio-workspace/sample_python

See Default Installation Directories for the location of studio-workspace on your system.