This notebook allows the modeling for calibrating weighing pads, as well as generating parameters for the model to use in a component which consumes the weighing pad data.
Documentation for the use is inline with the respective function blocks.
The general principle is:
In [1]:
# Define a helper function to call into Wpad procedures via the Crossbar.io REST bridge
#rpc_url = "https://demo.crossbar.io/call"
rpc_url = "http://localhost:8080/call"
import requests, json
def call(proc, *args, **kwargs):
payload = {
"procedure": proc,
"args": args,
"kwargs": kwargs
}
r = requests.post(rpc_url, data=json.dumps(payload),
headers={'content-type': 'application/json'})
res = r.json()
return res['args'][0]
In [2]:
# Get a data series previously stored from the Wpad Lab UI: a dict with keys "A0" - "A6" and "T"
# is returned with arrays of ints with the analog sensors readings (0-1023) and the user defined
# target value (if any was provided)
series = call("io.crossbar.demo.wpad.objstore.get", "series", 4)
series
Out[2]:
In [3]:
# Import libs and activate inline plotting
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [4]:
# Define a Wpad data series plotting helper
def plot_series(series):
fig, ax = plt.subplots()
ind = np.arange(len(series['T']))
width, colors = 0.3, ['r', 'b', 'g']
for i in range(3):
ax.bar(ind + width * i, series['A{}'.format(i + 4)], width, color=colors[i])
In [5]:
# Plot a Wpad data series: each of the three colors corresponds to one of the analog sensors
plot_series(series)
In [6]:
# Load a second data series
series2 = call("io.crossbar.demo.wpad.objstore.get", "series", 3)
series2
Out[6]:
In [7]:
plot_series(series2)
In [8]:
# Fit a logit model to the first data series
from sklearn import linear_model
X = np.matrix([series['A4'], series['A5'], series['A6']]).transpose()
y = series['T']
clf = linear_model.LogisticRegression(C=1.)
clf.fit(X, y)
Out[8]:
In [9]:
# Check prediction error on second data series
from sklearn import metrics
X2 = np.matrix([series2['A4'], series2['A5'], series2['A6']]).transpose()
y2 = series2['T']
pred = clf.predict(X2)
s = metrics.mean_absolute_error(y2, pred)
s
Out[9]:
In [10]:
# Dynamically access current sensor values (A4, A5, A6)
call("io.crossbar.demo.wpad.1.get_last")["values"][4:7]
Out[10]:
In [11]:
# Predict target var based on current sensor values
values = call("io.crossbar.demo.wpad.1.get_last")["values"][4:7]
clf.predict(values)[0]
Out[11]:
In [12]:
# Here are the model coefficients:
model = [list(row) for row in clf.coef_]
model
Out[12]:
In [13]:
# Upload the model coefficients
call("io.crossbar.demo.wpad.objstore.save", "model", model)
Out[13]:
In [13]:
In [ ]: