In [73]:
# 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 [82]:
series = call("io.crossbar.demo.wpad.objstore.get", "series", 12)
fig, ax = plt.subplots(1,3)
fig.set_size_inches(15, 5)
ax[0].scatter(series['T'], series['A4'], facecolor='r')
ax[1].scatter(series['T'], series['A5'], facecolor='g')
ax[2].scatter(series['T'], series['A6'], facecolor='b')
Out[82]:
In [ ]:
# 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", 11)
series
plt.scatter(np.array(series['T']) + 0.0, np.log(np.array(series['A0'])), facecolor='r')
plt.scatter(np.array(series['T']) + 0.2, np.log(np.array(series['A1'])), facecolor='g')
plt.scatter(np.array(series['T']) + 0.4, np.log(np.array(series['A2'])), facecolor='b')
plt.scatter(np.array(series['T']) + 0.6, np.log(np.array(series['A3'])), facecolor='y')
In [33]:
# Import libs and activate inline plotting
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [34]:
# Define a Wpad data series plotting helper
def plot_series(series, offset=4, length=3):
fig, ax = plt.subplots()
ind = np.arange(len(series['T']))
width, colors = 0.3, ['r', 'b', 'g', 'grey']
for i in range(length):
ax.bar(ind + width * i, series['A{}'.format(i + offset)], width, color=colors[i])
In [35]:
# Plot a Wpad data series: each of the three colors corresponds to one of the analog sensors
plot_series(series, 0, 4)
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 [62]:
# Fit a logit model to the first data series
from sklearn import linear_model
#X = np.matrix([series['A4'], series['A5'], series['A6']]).transpose()
X = np.matrix([series['A0'], series['A1'], series['A2'], series['A3']]).transpose()
y = series['T']
clf = linear_model.LogisticRegression(C=1.)
clf.fit(X, y)
Out[62]:
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 [38]:
# Dynamically access current sensor values (A4, A5, A6)
#call("io.crossbar.demo.wpad.1.get_last")["values"][4:7]
call("io.crossbar.demo.wpad.1.get_last")["values"][0:4]
Out[38]:
In [42]:
# Predict target var based on current sensor values
values = call("io.crossbar.demo.wpad.1.get_last")["values"][0:4]
#values = call("io.crossbar.demo.wpad.1.get_last")["values"][4:7]
clf.predict(values)[0]
Out[42]:
In [63]:
# Here are the model coefficients:
model = [list(row) for row in clf.coef_]
model
Out[63]:
In [41]:
# Upload the model coefficients
call("io.crossbar.demo.wpad.objstore.save", "model", model)
Out[41]:
In [29]:
series = call("io.crossbar.demo.wpad.objstore.get", "series", 6)
series
Out[29]:
In [36]:
fig, axs = plt.subplots(2,2)
fig.set_size_inches(10, 10)
axs[0][0].scatter(series['T'], series['A0'])
axs[1][0].scatter(series['T'], series['A1'])
axs[0][1].scatter(series['T'], series['A2'])
axs[1][1].scatter(series['T'], series['A3'])
Out[36]:
In [70]:
series = call("io.crossbar.demo.wpad.objstore.get", "series", 11)
models = {'A0': [95.848065712341779,
57.032814042098209,
1764.5904494757251,
435.92186791749396],
'A1': [128.56762208699021,
-11.380319227995484,
122.05944140193783,
19.048998297515116],
'A2': [181.76143053727156,
-117.68360457435577,
34.545688484235406,
15.763720159554804],
'A3': [133.63762724427815,
28.026724373199375,
92.727258995959318,
39.821232916028038]}
In [67]:
for key in ['A0', 'A1', 'A2', 'A3']:
series[key] = (np.exp((np.array(series[key]) - models[key][1]) / models[key][0]) - models[key][3]) / models[key][2]
In [71]:
from sklearn import linear_model
#X = np.matrix([series['A4'], series['A5'], series['A6']]).transpose()
X = np.matrix([series['A0'], series['A1'], series['A2'], series['A3']]).transpose()
y = series['T']
clf = linear_model.LogisticRegression(C=1.)
clf.fit(X, y)
Out[71]:
In [72]:
model = [list(row) for row in clf.coef_]
model
Out[72]:
In [ ]: