Tutorial: Adding a simple function
In this tutorial, you are writing a simple function to identify outlier values in the distance traveled by robots. Test this simple function using a script before you apply it to a sample device type in Maximo® Monitor.
Before you begin
- Install Python 3.9.x in your environment.
- Install IoT Functions in your local environment. Follow the instructions in the IoT Functions
readme.
Tip: Alternatively, to install IoT Functions and its dependencies, you can follow the steps in the Before you begin section of Tutorial: Adding a custom function. Clone the HelloWorld starter package and import the starter package into PyCharm. Install the requirements from PyCharm. - Complete
Tutorial: Adding expressions to add a
distance
metric to the robot device type in Maximo Monitor.
About this task
You are testing a simple function in your local environment to make sure that the calculation works and that the syntax is correct.
In the script, you create a sample device type, generate some sample data, and apply the simple
function to the sample data to create a new adjusted_distance
metric.
Steps
Complete these steps to apply a calculation to sample data using a simple function.
Step 1: Define your calculation
Define adjusted_distance as follows:
adjusted_distance = distance * 0.9
Step 2: Save your credentials to a file
Set credentials to connect to Maximo Monitor.
- Download
the
credentials_as.json
file. - Replace the variables with your data and then save the file to your local machine.
Step 3: Write a simple function in Python
Write a script,
save_simple_function_locally.py
, that adjusts the distance traveled by robots. In the script, connect to
Maximo Monitor using your credentials and save the function to a local model store. See the inline comments for instructions.
#Import packages and libraries
import datetime as dt
import json
import pandas as pd
import numpy as np
from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, func
import iotfunctions.bif as bif
from iotfunctions.metadata import EntityType, LocalEntityType
from iotfunctions.db import Database
from iotfunctions.dbtables import FileModelStore
#Connect to Maximo Monitor
with open('credentials_as.json', encoding='utf-8') as F:
credentials = json.loads(F.read())
db_schema = None
db = Database(credentials=credentials)
#Write the function
def f(df, parameters = None):
adjusted_distance = df['distance'] * 0.9
return adjusted_distance
#Save the function to a local model store
model_store = FileModelStore()
model_store.store_model('adjusted_distance', f)
Step 4: Save the function to a local file model store
Run the script
save_simple_function_locally.py
from your local environment. A file
KPI_MODEL_STOREadjusted_distance
is created in the same directory where you stored your script.
Step 5: Test your simple function in your local environment
Create a script called
test_my_simple_function_locally.py
. In the script, complete these steps:
- Import Python libraries and packages:
import datetime as dt import json import pandas as pd import numpy as np from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, func import iotfunctions.bif as bif from iotfunctions.metadata import EntityType, LocalEntityType from iotfunctions.db import Database from iotfunctions.dbtables import FileModelStore
- Connect to the Maximo Monitor:
with open('credentials_as.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None model_store = FileModelStore() db = Database(credentials=credentials, model_store=model_store)
- Create a sample device type and pass the simple function to it using the built-in PythonFunction from the
bif
module that you imported from IoT Functions.sample_entity_type = bif.PythonFunction( function_code = 'adjusted_distance', input_items = ['distance'], output_item = 'adjusted_distance', parameters = {} )
- Run the execute_local_test method of the sample device type to test the function in your local environment. Save the results to a csv file.
sample_entity_type.execute_local_test(db=db, generate_days=1, to_csv=True)
The script creates a new adjusted_distance
data item. Review the results in
the .csv file. If successful, you are ready add the simple function using the
Maximo Monitor UI.
Step 6: Apply the simple function to the Robot device type
- On the Data tab, click Creat metric.
- Select the
PythonFunction
function from the catalog. - Set the scope and click Next.
- Select
distance
as an input item. - Enter or paste in the simple function in the
function_code
field. Enter:def f(df, parameters = None): adjusted_distance = df['distance'] * 0.9 return adjusted_distance
- Click Next
- Specify
adjusted_distance
as an output data item. - Specify the data type as Number.
- Click Create
- In the Data item list, select
adjusted_distance
. - View the new
adjusted_distance
metric data.
Wait up to 5 minutes for Maximo Monitor to evaluate the simple function against the sample data.