35
IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research [email protected] https://data-ken.org Sunnyvale, California, USA PyData Seattle July 6, 2017

IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research [email protected] Sunnyvale, California, USA

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

IOT, PYTHON, AND ML:From Chips and Bits to Data Science

Jeff FischerData-Ken [email protected]://data-ken.orgSunnyvale, California, USA

PyData Seattle July 6, 2017

Page 2: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Agenda

¨ Project overview¨ Hardware¨ Data capture¨ Data analysis¨ Player¨ Parting thoughts

© 2016, 2017 Jeff Fischer

Page 3: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Why Python for IoT?

¨ High-level, easy to prototype ideas and explore options¨ Runs on embedded devices

¨ Python data analysis ecosystem

© 2016, 2017 Jeff Fischer

Array and matrix processing High level data analysis tools Numerical analysis routines Machine learning

Raspberry Pi

• Linux “workstation”• Can run CPython and full

data science stack• Not battery friendly

ESP8266

• System-on-a-chip with 32-bit CPU, WiFi, I/O

• Low power consumption• Only 96K data memory!• MicroPython to the

rescure

Page 4: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Project Motivation

¨ First thought about smart thermostat, but too dangerous¨ Lighting is “safe”¨ If out of town for the weekend, don’t want to leave the house dark¨ Timers are flakey and predictable¨ Would like a self-contained solution¨ “Wouldn’t it be cool to use machine learning?”

© 2016, 2017 Jeff Fischer

Page 5: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Lighting Replay Application

Data Capture

Lux Sensors

ESP8266 remote nodes + Raspberry Pi

Analysis and MachineLearning

Offline analysis using Jupyter, Pandas, HMMlearn

Captured sensordata

Smart Lights

PlayerApplication

Simple script using HMMlearn and Phue to control Philips Hue lights

HMM state machines

© 2016, 2017 Jeff Fischer

Page 6: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Hardware

Page 7: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

ESP8266

© 2016, 2017 Jeff Fischer

TSL2591lux sensorbreakoutboard

Lithium IonPolymerBattery3.7v 350mAh

MicroUSB toUSB cable

½ Size breadboard

Adafruit Feather HUZZAHESP8266 breakout board

Page 8: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

ESP8266: Wiring Diagram

© 2016, 2017 Jeff Fischer

SDA

SCL

GND

3V

Page 9: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Raspberry Pi

© 2016, 2017 Jeff Fischer

Raspberry Pi 2

Breakout cable“Pi Cobbler Plus”

Solderless Breadboards

Resistor

LEDTSL2591lux sensorbreakoutboard

Page 10: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Raspberry Pi: Wiring Diagram

© 2016, 2017 Jeff Fischer

ResistorLED

Anode(long lead)

Cathode(short lead)

10k

GND

3.3V

SDASCL

GPIO 0

Page 11: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Data Capture

Page 12: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Lighting Replay Application: Capture

LuxSensor ESP8266

Front Bedroom Sensor Node

LuxSensor ESP8266

Back Bedroom Sensor Node

Raspberry Pi(Dining Room)

MQTTBroker

Data Capture

App

LuxSensor

FlatFiles

© 2016, 2017 Jeff Fischer

MQTT

MQTT

Page 13: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Event-driven IoT Code Can Be Ugly

def sample_and_process(sensor, mqtt_writer, xducer, completion_cb, error_cb):try:

sample = sensor.sample()except StopIteration:

final_event = xducer.complete()if final_event:

mqtt_writer.send(final_event,lambda: mqtt_writer.disconnect(lambda: completion_cb(False), error_cb), error_cb)

else:mqtt_writer.disconnect(lambda: completion_cb(False), error_cb)

returnexcept Exception as e:

error_cb(e)mqtt_writer.disconnect(lambda: pass, error_cb)return

event = SensorEvent(sensor_id=sensor.sensor_id, ts=time.time(), val=sample)csv_writer(event)median_event = xducer.step(event)if median_event:

mqtt_writer.send(median_event,lambda: completion_cb(True), error_cb)

else:completion_cb(True)

def loop():def completion_cb(more):

if more:event_loop.call_later(0.5, loop)

else:print("all done, no more callbacks to schedule")event_loop.stop()

def error_cb(e):print("Got error: %s" % e)event_loop.stop()

event_loop.call_soon(lambda: sample_and_process(sensor, mqtt_writer, transducer, completion_cb, error_cb))

Problems

1. Callback hell

2. Connecting of event streams intermixed with handling of runtime situations: normal flow, error, and end-of-stream conditions.

3. Low-level scheduling

4. async/await helps, but not much© 2016, 2017 Jeff Fischer

Page 14: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

My Solution: ThingFlow

¨ What is ThingFlow?¤ A Domain Specific Language for IoT event processing¤ Runs on Python3 and MicroPython

¨ Co-creator¤ Rupak Majumdar, Scientific Director at Max Planck Institute for Software Systems

¨ Why did we create ThingFlow?¤ IoT event processing code can be very convoluted¤ No standardization of sensors, adapters, and transformations¤ Different frameworks for microcontrollers, edge processing, analytics

© 2016, 2017 Jeff Fischer

Page 15: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Simple ThingFlow Example

o Periodically sample a light sensoro Write the sensed value to a local fileo Every 5 samples, send the moving average to MQTT Broker

© 2016, 2017 Jeff Fischer

LuxSensor

Writeto

File

EventScheduler

Sendto

MQTT

MovingAvg

Graphical Representation

sensor.connect(file_writer(’file’))sensor.transduce(MovingAvg(5)).connect(mqtt_writer)scheduler.schedule_periodic(sensor, 5)

Code

Page 16: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

ESP8266 ThingFlow Code

© 2016, 2017 Jeff Fischer

from thingflow import Scheduler, SensorAsOutputThingfrom tsl2591 import Tsl2591from mqtt_writer import MQTTWriterfrom wifi import wifi_connectimport os

# Params to setWIFI_SID= …WIFI_PW= …SENSOR_ID="front-room"BROKER='192.168.11.153'

wifi_connect(WIFI_SID, WIFI_PW)sensor = SensorAsOutputThing(Tsl2591())writer = MQTTWriter(SENSOR_ID, BROKER, 1883,

'remote-sensors')sched = Scheduler()sched.schedule_sensor(sensor, SENSOR_ID, 60, writer)sched.run_forever()

https://github.com/jfischer/micropython-tsl2591

Sample at 60 second intervals

The MQTT writer is connected tothe lux sensor.

See https://github.com/mpi-sws-rse/thingflow-examples/blob/master/lighting_replay_app/capture/esp8266_main.py

Page 17: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Raspberry Pi Code

© 2016, 2017 Jeff Fischer

LuxSensor

MQTTAdapter

Mapto

UTF8

ParseJSON

Mapto

eventsDispatch

CSV FileWriter

(front room)

CSV FileWriter

(back room)

CSV FileWriter

(dining room)

https://github.com/mpi-sws-rse/thingflow-examples/blob/master/lighting_replay_app/capture/sensor_capture.py

Page 18: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Data Analysis

Page 19: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Lighting Replay Application: Analysis

Raspberry Pi(Dining Room)

FlatFiles

HMMdefinitions

Laptop

Jupyter Notebook

filecopy

© 2016, 2017 Jeff Fischer

Page 20: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Preprocessing the Data(ThingFlow running in a Jupyter Notebook)

© 2016, 2017 Jeff Fischer

CSV FileReader

Fill inmissingtimes

SlidingMean

Roundvalues

OutputEventCount

CaptureNaN

Indexes

Pandas Writer

(raw series)

Pandas Writer

(smoothed series)

reader.fill_in_missing_times()\.passthrough(raw_series_writer)\.transduce(SensorSlidingMeanPassNaNs(5)).select(round_event_val).passthrough(smoothed_series_writer)\.passthrough(capture_nan_indexes).output_count()

Page 21: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Data Processing: Raw Data

© 2016, 2017 Jeff Fischer

Front room, last day

Datagaps

Page 22: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Data Processing: Smoothed Data

© 2016, 2017 Jeff Fischer

Front room, last day

Page 23: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Data Processing: K-Means Clustering

© 2016, 2017 Jeff Fischer

Front room, last day

Page 24: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Data Processing: Mapping to on-off values

© 2016, 2017 Jeff Fischer

Front room, last day

Page 25: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Applying “Machine Learning”

¨ Apply a supervised learning to create predictions for the light¤ Regression => predict light value¤ Classification => Light “on” or “off”¤ Features = time of day; time relative to sunrise, sunset; history

¨ Challenges¤ Transitions more important than individual samples (200 vs. 25,000)¤ Different class sizes: light is mostly off¤ Really a random process

¨ Solution: Hidden Markov Models© 2016, 2017 Jeff Fischer

Page 26: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Hidden Markov Models (HMMs)

¨ Markov process

¤ State machine with probability associated with each outgoing transition

¤ Probabilities determined only by the current state, not on history

¨ Hidden Markov Model¤ The states are not visible to the observer, only the outputs

(“emissions”).

¨ In a machine learning context:

¤ (Sequence of emissions, # states) => inferred HMM

¨ The hmmlearn library will do this for us.¤ https://github.com/hmmlearn/hmmlearn

¨ But, no way to account for time of day, etc.© 2016, 2017 Jeff Fischer

Example Markov process(from Wikipedia)

Page 27: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Slicing Data into Time-based “Zones”

© 2016, 2017 Jeff Fischer

Sunrise

30 Minutesbeforesunset

Max(sunset+60m, 9:30 pm)

0 1 2 3 0

Page 28: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

HMM Training and Prediction Process

Training1. Build a list of sample subsequences for each zone

2. Guess a number of states (e.g. 5)

3. For each zone, create an HMM and call fit() with the subsequences

Prediction

For each zone of a given day:n Run the associated HMM to generate N samples for an N minute zone durationn Associated a computed timestamp with each sample

© 2016, 2017 Jeff Fischer

Page 29: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

HMM Predicted Data

© 2016, 2017 Jeff Fischer

Front room, one week predicted data

Front room, one day predicted data

Page 30: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Replaying the Lights

Page 31: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Lighting Replay Application: Replay

Front RoomSmart Light

Raspberry Pi(Dining Room)

HMMdefinitions

PlayerScript

Back RoomSmart Light

PhilipsHueBridge

WiFiRouterandSwitch

ZigBee

HTTP

© 2016, 2017 Jeff Fischer

Page 32: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Logic of the Replay Script

¨ Use phue library to control lights¨ Reuse time zone logic and HMMs from analysis¨ Pseudo-code:

Initial testing of lightswhile True:

compute predicted values for rest of dayorganize predictions into a time-sorted list of on/off eventsfor each event:

sleep until event timesend control message for event

wait until next day

© 2016, 2017 Jeff Fischerhttps://github.com/mpi-sws-rse/thingflow-examples/blob/master/lighting_replay_app/player/lux_player.py

Page 33: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Parting Thoughts

Page 34: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Lessons Learned

¨ End-to-end projects great for learning

¨ Machine learning involves trial-and-error

¨ Visualization is key

¨ Python ecosystem is great for both runtime IoT and offline analytics

© 2016, 2017 Jeff Fischer

Page 35: IOT, PYTHON, AND ML - Data-Ken · 2018-08-21 · IOT, PYTHON, AND ML: From Chips and Bits to Data Science Jeff Fischer Data-Ken Research jeff@data-ken.org Sunnyvale, California, USA

Thank You

Contact MeEmail: [email protected]: @fischer_jeffWebsite and blog: https://data-ken.org

More InformationThingFlow: https://thingflow.io

Examples (including lighting replay app): https://github.com/mpi-sws-rse/thingflow-examples

Hardware tutorial: http://micropython-iot-hackathon.readthedocs.io/en/latest/