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'))


$$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]:
[<matplotlib.lines.Line2D at 0x2b7179aef550>]

In [145]:
# the estimated values for our function parameters a, b, c and d are here:

popt


Out[145]:
array([   95.84806571,    57.03281404,  1764.59044948,   435.92186792])

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]:
{'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 [149]:
display(Math(r'x = f(y) = \frac{\exp(\frac{y - b)}{a} - d)}{c}'))


$$x = f(y) = \frac{\exp(\frac{y - b)}{a} - d)}{c}$$

In [ ]: