Cognalysis MultiRate API Guide
With the introduction of Cognalysis MultiRate Version 5, users now have the ability to automatically create a DLL to assist in more easily implementing MultiRate models in other applications.
Starting with a fully-optimized model, simply select File…Create API in MultiRate, and the software will create four files for you:
- {model name}_COM.dll — the COM DLL file
- {model name}_COM.txt — the COM code underlying the compiled COM DLL
- {model name}_NET.dll — the DotNET DLL file
- {model name}_NET.txt — the DotNET code underlying the compiled .NET DLL
Program Specific Tutorials:
The below code shows how to use a MultiRate DLL in Python.
## MultiRate DLL Wrapper Code
#############################
# import 3rd party package
import clr
from datetime import datetime
# Update this value
path = r"G:\Tools\Cognalysis Software\MultiRate\Testing\Kristan Testing\Testfolder\wage_kEuro_1_NET.dll"
# Insert path to NET DLL
clr.AddReference(path)
import clr
# Import model name. Convention is Model_*name of multirate model
# Update this value
oMod = clr.Model_wage_kEuro_1()
def prediction(*args):
final = oMod.Predict(*args)
return [final[i] for i in range(len(final))]
# ls[0] = predicted value
# ls[1] = base factors
# ls[2-end] = factors for each variable
# Note: ls[0] = product(ls[1:])
def ticks(dt):
# Input Type: datetime object.
# Output Type: float
return (dt - datetime(1, 1, 1)).total_seconds() * 10000000
# Input: All arguments for DLL in proper order.
## Note: for a date field, the DLL requires the net defined Ticks. Use Ticks helper function.
# Output: Result list
joined_date = ticks(datetime(2018, 7, 16))
prediction_list = prediction(34, 0, "Right", 0, 4, 0, joined_date, 0, 26.9,0, True)
print(prediction_list)
Install the rClr package in R environment
Please note, version number is important. rClr does not work with R versions beyond v3.5.0. As of October 2021, the current version of R is v4.1.1. In particular, we will want to install the rClr package v.0.8.3, which is documented as compatible with R v3.5.X. So, you will need to install v3.5.0 of R
Unfortunately, rClr is not available via the standard R package managers; you’ll have to manually download and install from the v0.8.3 executable.
- Go to the rClr github Releases page and download the rClr_windows_pkgs.7z
- Unpack the contents of 7zinto a temporary directory. You will need this file: \rClr_windows_pkgs.7z\R_pkgs\bin\windows\contrib\3.5\rClr_0.8.3.zip. If you need help unpacking .7z files, download and install 7-Zip from www.7-zip.org
- Open up R.exe, and type: packages(pkgs=’c:/path/to/rClr_0.8.3.zip’) where c:/path/to/ is the full path describing the location of your extracted .zip file.
- This should complete the installation of rClr in BERT’s R environment. To test this, type library(rClr). If you receive no error message, then the rClr package has been successfully installed.
Example R Script:
library(rClr)
library(stringr)
KingCoHouseModel <- function(bedrooms,bathrooms,Curve_Mod){
f <- file.path(‘C:/Users/kristan/Documents/’, ‘kingco_1_a_NET.dll’) #the path to the DLL file
f <- path.expand(f)
stopifnot( file.exists(f) )
clrLoadAssembly(f)
obj<-clrNew(“Model_kingco_1_a”)
#arg is Model_{fn name}
#in this section, re-type all DotNET variables to matching types in R
#DotNET –> R
#Double->as.numeric()
#Integer->as.integer()
#String->as.character()
#Boolean–>as.logical()
bedrooms<-as.integer(bedrooms)
bathrooms<-as.numeric(bathrooms)
Curve_Mod<-as.logical(Curve_Mod)
#don’t include a _default for the exposure variable, if any on this DLL
bedrooms_default<-as.integer(0)
bathrooms_default<-as.integer(0)
result<-clrCall(obj,’Predict’,
bedrooms,bedrooms_default,
bathrooms, bathrooms_default,
Curve_Mod)
result
}
KingCoHouseModel(1,1.00, TRUE)