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 Analytics Service.
distance
KPI to the robot device type in Analytics Service.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
KPI.
Complete these steps to apply a calculation to sample data using a simple function.
Define adjusted_distance as follows:
adjusted_distance = distance * 0.9
Set credentials to connect to Analytics Service.
credentials_as.json
file in your working directory. From the user interface, go to the Usage tab. credentials_as.json
file.Note: Take care not to save the credentials file to any external repository.
Write a script, save_simple_function_locally.py
, that adjusts the distance traveled by robots. In the script, connect to Analytics Service 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 the service
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)
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.
Create a script called test_my_simple_function_locally.py
. In the script, complete these steps:
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 service:
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)
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 = {}
)
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 Analytics Service UI.PythonFunction
function from the catalog.distance
as an input item.function_code
field. Enter:def f(df, parameters = None):
adjusted_distance = df['distance'] * 0.9
return adjusted_distance
adjusted_distance
as the new KPI name.adjusted_distance
. adjusted_distance
metric data in the Details & derived data pane. Wait up to 5 minutes for Analytics Service to evaluate the simple function against the sample data.