Using simple functions from the UI

For your calculations in Analytics Service, you can type or paste a simple function into the UI as part of your calculations. You must understand how to write the simple function before you add it.

What is a simple function

A simple function in Analytics Service is a Python code block. It has two arguments as inputs; a dataframe and a dictionary of parameters. Unlike custom functions, simple functions produce a single data item (or KPI) as its output.

Sometimes, you can write an expression to perform a basic calculation rather than a simple function. Use a simple function if you want to add multiple lines of code or if you want to add some control logic to your code.

Use PythonFunction from the function catalog to add a simple function.

Writing simple functions

Calculations in Analytics Service use the Pandas library (see Working with calculations). Before you write simple functions for Analytics Service, familiarize yourself with data structures, operations, and syntax in Pandas.

Important

When you enter a simple function on the UI, you are not writing a new Python function. Instead, the simple function is an input parameter to a built-in function. You must tailor the syntax of your code block accordingly.

Example

In company Acme, the operations manager is keeping a close eye on the performance of a newly introduced robotic arm. The distance traveled by each robotic arm during testing is being calcuated. The operations manager asks that outlier values be flagged for each robotic arm for further analysis. The manager is interested in any distances that are too high.

A data scientist defines a new is_distance_high KPI as follows:

is_distance_high = True where distance > threshold

The data scientist defines the threshold as follows:

threshold = (mean(distance) + 2) * std(distance)

An analyst writes the following simple function for the calculation:

def f(df,parameters = None):
    import numpy as np
    threshold = df['distance'].mean() + 2 * df['distance'].std()
    output = np.where(df['distance']>threshold,1,0)
    return output

Before the analyst can paste the code block into the UI, the function must adhere to these rules:

The analyst adds a new calculation using PythonFunction from the catalog. In the input fields, the analyst selects distance and pastes the code block into the function_code input field. The analyst labels the output parameter as is_distance_high.

The simple functions that you paste into the UI is available in-memory in Analytics Service.

Testing simple functions

Test your simple function in your own environment before you use them in Analytics Service.

Use a file model store to persist a function locally while you are testing. See Tutorial: Adding a simple function through the UI for a step-by-step example of how to test a simple function from a model store.

More information