Bulk Calculations in Python Models
By default, Python Models have a single 'calculation' function that is run on every 'sample' of data.
However, ML models in particular work much better working on bulk data. For this reason, the framework also includes the option of using a bulk calculation function that works on Pandas dataframes rather than individual data dictionaries.
Example
This example uses an AI model.
import os import ardifunctions import pandas as pd import numpy as np import json defaults = {} dfunc = ardifunctions.DynamicFunction("http://localhost/s/default") dfunc.query = "'Machine' ASSET AIPOINTS" dfunc.Defaults(defaults) dfunc.ParseParameters() import ardiai import tensorflow as tf #Load the AI Models mymodel = ardiai.AIData(os.path.dirname(__file__) + "/aidata.json") mymodel.LoadConfig(os.path.dirname(__file__) + "/model.json") tflow = tf.keras.models.load_model(os.path.dirname(__file__) + '/model.keras') #Register all the variable names, so we know what to load from the UI varnames = [] for c in dtatop.icolumns: varnames.append(c.name) dfunc.variables = varnames def SingleSearch(factors,results): if factors['Running'] == 0: return {"AI Output 1": 0,"AI Output 2": 0} try: inp = dtatop.DictToInput(factors) outputs = topmodel.predict(np.array([inp]),verbose=False) modeldata = dtatop.OutputDict(outputs[0]) except: return {"AI Output 1": 0,"AI Output 2": 0} return modeldata def MultiSearch(frame,context): inputs,indexes = mymodel.FrameToInputs(frame,training=False) outputs = tflow.predict(inputs,verbose=False) for v in indexes: if indexes[v] is None: context.Write(v,{"AI Output 1": 0,"AI Output 2": 0}) else: resp = dtatop.OutputDict(outputs[indexes[v]]) context.Write(v,resp) dfunc.SingleCalc = SingleSearch dfunc.BulkCalc = MultiSearch dfunc.Execute()