Tutorial: Adding expressions
In this tutorial, you are writing an expression to calculate the distance traveled by robots based on speed and travel time. Test the expression before you apply it to a device type in Maximo® Monitor.
About this task
You are testing the expression offline to make sure that the calculation works and that the syntax is correct.
In the test script, you create a sample device type, generating some sample data, and apply the
expression to the sample data to create a new distance metric.
Before you begin
Install Python 3.9.x in your environment.
Step 1: Define your expression
Calculate the distance traveled by robots. Two of the input data items of the robot sample device type are
speed and
travel_time.
Define distance as:
distance = speed * travel time
Step 2: Write your expression in Python
For example:
df['distance'] = df['speed']*df['travel_time']
Step 3: Test your expression
Create a script called
test_my_expression.py. In the script, complete these steps:
- Import Python libraries and packages. For example:
import datetime as dt import numpy as np import pandas as pd from collections import namedtuple - Define a
namedtupleto capture the meta data for the expression.
PythonExpression = namedtuple('Exp', 'output expression') - Create three sample robots,
robot_a,robot_b, androbot_c. Display the data.row_count = 5 data_items = ["speed","travel_time"] entities = ['robot_a','robotb','robot_c'] dfs = [] for e in entities: data = np.random.normal(100,10,(row_count,len(data_items))) df = pd.DataFrame(data=data,columns = data_items) df['id'] = e df['evt_timestamp'] = pd.date_range( end=dt.datetime.utcnow(), periods=row_count ) df = df.set_index(['id','evt_timestamp']) dfs.append(df) df = pd.concat(dfs) print(df) - Add the expression.
df["distance"] = df["speed"] * df["travel_time"] print(df) - Model the
PythonExpressionfunction and its metadata in the script. Because you are running the script offline, you must model the behavior of the function. Use a Python tuple with a new data item and a string expression.ex1 = PythonExpression('distance','df["speed"]*df["travel_time"]') - Evaluate the expression.
df[ex1.output] = eval(ex1.expression) print(df) - Run the script from the command line. For example:
python3 test_my_expression.py
The script creates a new
distance data item. If successful, you are ready to use the expression in a function on the
Maximo Monitor UI.
Step 4: Create a sample device type
- On the Setup page, on the Devices tab, click the plus (+) icon to add a new device type.
- Select the Robot sample type template.
- Assign a name to the device type.
- Click Create.
- Select the sample device type.
- Click Set up device type and then click Data.
Metrics takes up to 5 minutes to generate.
Step 5: Apply the expression to the device type
- From the Data tab, click Create metric.
- Select the
PythonExpressionfunction from the catalog. - Set the scope and then click Next.
- Remove
distance =from the function from earlier and in the expression field enter:df["speed"]*df["travel_time"] - Click Next.
- Specify
distanceas the output data item. - Click Create
- In the Data item list, select
distance. - View the new distance metric data. Wait up to 5 minutes for Maximo Monitor to evaluate the expression against the sample data.