Examples of Model Processes¶
GoLang Model Example¶
Here is an example of a GoLang model in VOR Stream. First, create the following
testmod.strm
and tables.csv
files:
name testmod
type model
in input.csv -> input
node testmod(input)(output)
out output -> output.csv
tablename,varname,type,descr,arraylen,inherit,groupkey
input,,,Input table
input,num,num
output,,,Output table,,input
output,pd,num
Next, create input.csv
file with a single field in the playpens input directory
as follows:
num
1
1
2
2
Run vor create process testmod.strm
and make the following edits to
testmodU.go
file:
1) add the following lines to the import list:
```go
"frg.com/streamr/sdk"
log "github.com/sirupsen/logrus"
```
2) add the following line to the User Struct
```go
model sdk.ModelS
```
3) add the following lines to the init block:
```go
var err error
u.model, err = frgutil.GetModelUnitTest(u.hh)
log.Println("err is", err)
```
4) add the following lines to the worker function:
```go
err := frgutil.RunModelUnitTest(u.model, input, u.Output)
if err != nil {
frgutil.EndJob(u.hh)
}
Output.Post(u.Output)
```
Now log into the UI, navigate to the Models tab and create a new Go model. Then add the following code to the Script section of the model definition:
package main
import (
"log"
)
var output map[string]float64
func init() {
output = make(map[string]float64)
}
func Model(input map[string]interface{}, args ...interface{}) (map[string]float64, error) {
log.Printf("num %f\n", input["Num"])
parms := map[string]float64{$parms$}
output["Pd"] = parms["m"] * input["Num"].(float64) + parms["b"]
return output, nil
}
Additionally, in the Parameters section of the model definition define two
parameters b=1
and m=-2
. Save the model, noting the Model ID in the top
left corner of the screen.
Navigate back to your playpen and create the following joboptions.json
file in
the input directory, filling in the model ID you just created:
{
"system": {
"modelunittestid" : <model_ID>
}
}
The set-up is now complete, and you are ready to run the model with vor run
testmod
.
Python Model Example¶
Here's an analogous example of a Python model. Create the following testmod.strm
,
tables.csv
, and input.csv
files:
name testmod
type model
in input.csv -> input
node testmod(input)(output) lang=python
out output -> output.csv
input,,,Input table
input,num,num
output,,,Output table,,input
output,pd,num
num
1
1
2
2
Next, run vor create process testmod.strm
and make the following edits to
testmodU.py
file:
1) add the following line to the import list:
```python
from sdk import framework
```
2) add the following lines to the init block:
```python
modelID = self.hh.options['JobOptions']['system']['modelunittestid']
self.model = framework.ReadModelByID(modelID)
```
3) add the following lines to the worker function:
```python
obs = input.__dict__
self.model.Model.model( obs , self.model.parms)
if len(self.model.output) > 0:
self.Output.Post(Output.Output(**self.model.output))
```
Now log into the UI, navigate to the Models tab and create a new Python model. Add the following code to the Script section of the model definition:
import logging
class Model:
output = dict()
def __init__(self, output: dict()):
self.output = output
def model(self, input : dict(), parms : dict(), *argsv) :
logging.info("In model " + str(parms))
# copy to output for posting
self.output["Num"] = input["Num"]
self.output["Pd"] = parms["m"] * input["Num"] + parms["b"]
return
Additionally, in the Parameters section of the model definition define two
parameters b=1
and m=-2
. Save the model, noting the Model ID in the top
left corner of the screen.
Navigate back to your playpen and create the following joboptions.json
file in the input directory, filling in the new model ID you just created:
{
"system": {
"modelunittestid" : <model_ID>
}
}
The set-up is complete, and you are ready to run the model with vor run
testmod
.