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

Note: To complete this tutorial, you use a sample device type template and data. Sample device types do not use the latest version of device types and do not support streaming data metrics. For more information, see Creating batch or streaming data metrics.

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:

  1. Import Python libraries and packages. For example:
    import datetime as dt
    import numpy as np
    import pandas as pd
    from collections import namedtuple
  2. Define a namedtupleto capture the meta data for the expression.
    PythonExpression = namedtuple('Exp', 'output expression')
  3. Create three sample robots, robot_a, robot_b, and robot_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)
  4. Add the expression.
    df["distance"] = df["speed"] * df["travel_time"]
    print(df)
  5. Model the PythonExpression function 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"]')
  6. Evaluate the expression.
    df[ex1.output] = eval(ex1.expression)
    print(df)
  7. 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

  1. On the Setup page, on the Devices tab, click the plus (+) icon to add a new device type.
  2. Select the Robot sample type template.
  3. Assign a name to the device type.
  4. Click Create.
  5. Select the sample device type.
  6. 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

  1. From the Data tab, click Create metric.
  2. Select the PythonExpression function from the catalog.
  3. Set the scope and then click Next.
  4. Remove distance = from the function from earlier and in the expression field enter:
    df["speed"]*df["travel_time"]
  5. Click Next.
  6. Specify distance as the output data item.
  7. Click Create
  8. In the Data item list, select distance.
  9. View the new distance metric data. Wait up to 5 minutes for Maximo Monitor to evaluate the expression against the sample data.
Note: A script, offline_sample_expressions.py, is also available in the IoT Functions repository to help you get familiar with the Python expression syntax that is used to build expressions in Maximo Monitor.