Altair® Panopticon

 

Best Practices on Working with Python Transform in Panopticon

When applying a transform with Python in Panopticon for small and simple cases, you have the option of typing or pasting the code directly into the Transforms window. However, when you have several different transforms, or when each transform is applied in several data tables, it is highly recommended to follow the steps outlined below on how to apply functional programming and the D.R.Y. principle (Don’t Repeat Yourself) to the Python transforms in Panopticon.

Steps:

1.     Save your code in py-files for Python. This option gives you the freedom to work on the code using the IDE of your choice (i.e., PyCharm, Spyder, Atom etc.).

2.     Instead of using an imperative coding approach, define one or several functions in the file, which when invoked, runs your code, takes a data frame as an input argument, and then returns the resulting data frame.

3.     In the Transforms window of Panopticon, reference this external code file at the very top:

from sys import path
path.append("path/to/your/folder/")
import YourPythonFile

You can then invoke (call) any function that you have defined in your code file with a function call in the transform code window. Ideally, the function will return the transformed data frame.

4.     The path to the external code file needs to be valid both from the point of view of Panopticon Designer on your local workstation, and from the point of view of Panopticon Real Time you publish to. This can be assured by introducing a global parameter in Panopticon Real Time under the Parameters tab.

For example, you can name the parameter Python_code_path and define its value as the full path to the folder that contains your code files. Next, on Panopticon Real Time, define a global parameter with the same name, but with a value that is the path to the server-side folder containing your code files. Copy the code files to the server-side folder then edit the path specified in your sourcing call in the transform so that it contains the parameter. For example:

from sys import path
path.append("{Python_code_path}")
import YourPythonFile


This will achieve a path reference to your code file which is valid in both the Designer and Server. It is also useful when promoting or migrating a Panopticon workbook from one server environment to another.

 

   NOTE

·           If there is a need to apply different transforms to different data sets, you can solve this by defining several different functions in your code file.

·           For very similar functions, avoid repeating the same code in a file by factoring out the common parts and placing them in a separate function, which can be invoked by the other functions.

·           For a transform that needs to have different outputs based on certain conditions or variables, this can be controlled by adding another input parameter to the function. Depending on the argument given to that parameter, you can make the function do things differently by evaluating a condition. In addition, this argument can – if you want to – be supplied via a Panopticon parameter and thereby be put under a dashboard end-user control.