Skip to content

Instantly share code, notes, and snippets.

@denysvitali
Created April 21, 2018 14:02
Show Gist options
  • Select an option

  • Save denysvitali/d0f6b878acede4f501bb2c61961203d8 to your computer and use it in GitHub Desktop.

Select an option

Save denysvitali/d0f6b878acede4f501bb2c61961203d8 to your computer and use it in GitHub Desktop.
import json
from pprint import pprint
import time
from datetime import date,datetime,timedelta
from numpy import arange
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange, date2num
from matplotlib.markers import MarkerStyle
import numpy as np
import plotly.plotly as py
from scipy import interpolate
from scipy.interpolate import spline
from operator import itemgetter
data = json.load(open('hr.json'))
times = []
bpms = []
tps = [];
k = 0
for i in data["insertedDataPoint"]:
time=datetime.fromtimestamp(round(int(i["startTimeNanos"])/10e8))
value=i["value"][0]["fpVal"]
tps.append((time, value));
tps.sort(key=itemgetter(0))
for i in tps:
times.append(i[0])
bpms.append(i[1])
dates = date2num(times)
def running_mean(x, N):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
n = 15
rm = running_mean(dates, n)
rm_y = running_mean(bpms, n)
print(rm);
fig, ax = plt.subplots(figsize=(20,10))
if False:
ax.plot_date(dates,
bpms,
marker='o',
markersize=1,
markeredgecolor='#2c3e50',
linestyle='solid'
)
ax.plot_date(rm,
rm_y,
color='#d35400',
marker='o',
markersize=1,
markeredgecolor='#e67e22',
linestyle='solid',
)
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(arange(0, 25, n)))
ax.xaxis.set_major_formatter(DateFormatter('%d.%m'))
ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
plt.title('Hearth rate over time');
plt.gcf().autofmt_xdate()
plt.savefig('plot.png')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment