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 [103]:
# get series training data for wpad
datasets = {}
series = call("io.crossbar.demo.wpad.objstore.get", "series", 8)
datasets['A0'] = (np.array(series['A0']), np.array(series['T']))
series = call("io.crossbar.demo.wpad.objstore.get", "series", 10)
datasets['A1'] = (np.array(series['A1']), np.array(series['T']))
series = call("io.crossbar.demo.wpad.objstore.get", "series", 7)
datasets['A2'] = (np.array(series['A2']), np.array(series['T']))
series = call("io.crossbar.demo.wpad.objstore.get", "series", 9)
datasets['A3'] = (np.array(series['A3']), np.array(series['T']))
In [89]:
# import some libs
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [111]:
# plot samples for each series in a scatterplot
fig, axs = plt.subplots(1, 4)
fig.set_size_inches(14, 5)
i = 0
for key in sorted(datasets.keys()):
x = datasets[key][1]
y = datasets[key][0]
axs[i].scatter(x, y)
axs[i].set_title("Sensor {}".format(key))
axs[i].set_xlim([-0.5, 6.5])
axs[i].set_ylim([0, 1023])
i += 1
In [73]:
# import some libs for curve fitting
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
In [143]:
# the scatterplots above suggest that the data follows a logarithmic shape .. a function taking the following form:
from IPython.display import display, Math, Latex
display(Math(r'y = f(x) = a \log(cx + d) + b'))
In [144]:
# shows how to fit a curve to a single series
from scipy.optimize import curve_fit
x = datasets['A0'][1]
y = datasets['A0'][0]
# this is the function we try to fit to the data (see above)
def func(x, a, b, c, d):
return a * np.log(c * x + d) + b
popt, pcov = curve_fit(func, x, y)
xn = np.linspace(0,6,500)
plt.plot(x, y, 'ko')
plt.plot(xn, func(xn, *popt), 'r-')
Out[144]:
In [145]:
# the estimated values for our function parameters a, b, c and d are here:
popt
Out[145]:
In [147]:
models = {}
fig, axs = plt.subplots(1, 4)
fig.set_size_inches(14, 5)
xn = np.linspace(0,7,500)
i = 0
for key in sorted(datasets.keys()):
x = datasets[key][1]
y = datasets[key][0]
axs[i].scatter(x, y)
axs[i].set_title("Sensor {}".format(key))
axs[i].set_xlim([-0.5, 7.5])
axs[i].set_ylim([0, 1023])
def func(x, a, b, c, d):
return a * np.log(c * x + d) + b
popt, pcov = curve_fit(func, x, y)
axs[i].plot(xn, func(xn, *popt), 'r-')
models[key] = list(popt)
i += 1
In [148]:
#
models
Out[148]:
In [149]:
display(Math(r'x = f(y) = \frac{\exp(\frac{y - b)}{a} - d)}{c}'))
In [ ]: