Skip to content

Instantly share code, notes, and snippets.

@tomthe
Created October 11, 2016 13:49
Show Gist options
  • Save tomthe/dd50618cd4209aeafbc057d0bf01624c to your computer and use it in GitHub Desktop.
Save tomthe/dd50618cd4209aeafbc057d0bf01624c to your computer and use it in GitHub Desktop.
uses bokeh to plot Data from the Mi-Band aquired by https://github.com/Freeyourgadget/Gadgetbridge
# uses bokeh to plot Data from the Mi-Band aquired by https://github.com/Freeyourgadget/Gadgetbridge
# licence: GPL
import sqlite3
import logging
from datetime import date, datetime
duration = 66
conn = sqlite3.connect('Gadgetbridge')
c = conn.cursor()
xdata=[]
xdates=[]
ydata=[]
ysteps=[]
xsteps=[]
yhr2=[]
sqltxt ="""select HEART_RATE, datetime(timestamp, 'unixepoch') as datum, timestamp
from MI_BAND_ACTIVITY_SAMPLE
where HEART_RATE between 20 and 200
and date(datetime(timestamp+3600*6, 'unixepoch'))=date('now', '-' || ? || ' days')
order by HEART_RATE
LIMIT 1
OFFSET 20"""
print("okay: ", sqltxt)
for i in xrange(0,duration,1):
try:
c.execute(sqltxt,(i,))
result = c.fetchone()
print(i,"result: ", result)
xdata.append(result[1])
xdates.append(datetime.fromtimestamp(result[2]))
ydata.append(result[0])
except Exception as e:
logging.exception("message")
##############
sqltxt ="""select HEART_RATE, datetime(timestamp, 'unixepoch') as datum, timestamp
from MI_BAND_ACTIVITY_SAMPLE
where HEART_RATE between 20 and 200
and date(datetime(timestamp+3600*6, 'unixepoch'))=date('now', '-' || ? || ' days')
order by HEART_RATE
LIMIT 1
OFFSET 60"""
print("okay: ", sqltxt)
for i in xrange(0,duration,1):
try:
c.execute(sqltxt,(i,))
result = c.fetchone()
print(i,"result_hr2: ", result)
yhr2.append(result[0])
except Exception as e:
logging.exception("message")
###################
sqltxt="""select SUM(STEPS) as daysteps, datetime(ROUND(AVG(timestamp)), 'unixepoch') as datum, timestamp
from MI_BAND_ACTIVITY_SAMPLE
where (timestamp > strftime('%s','now','-' || ? ||' days'))
group by timestamp/(3600*24)
"""
for row in c.execute(sqltxt,(str(duration),)):
print row
ysteps.append((row[0]))
xsteps.append(datetime.fromtimestamp(row[2]))
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import gridplot
# output to static HTML file
output_file("lines2.html")
print ("plotting to lines2.html")
# create a new plot with a title and axis labels
#p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
# create a new plot with a a datetime axis type
tools='pan,wheel_zoom,reset,save,hover'
phr = figure(title="Resting Heart Rate", y_axis_label='HeartRate', x_axis_type='datetime',height=300, width=1200, tools=tools)
psteps = figure(title="Steps per day",x_axis_type='datetime', x_axis_label='date', x_range=phr.x_range,y_axis_type="log",height=300,width=1200)
# add a line renderer with legend and line thickness
phr.line(xdates, ydata, legend="Resting Heart Rate Offset 20", line_width=2, color="orange")
psteps.line(xsteps,ysteps,legend="steps")
phr.line(xdates, yhr2, legend="Resting Heart Rate Offset 20", line_width=2, color="red")
pall = gridplot([[phr,],[psteps,]])
print "done"
show(pall)
Copy link

ghost commented Oct 11, 2016

thanks. I was scratching my head how to separate days and concluded it would be best to separate them by local noon.. so your code would work nicely at noon.
Trying to install bokeh.plotting on my droid in termux now..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment