Created
April 21, 2018 14:13
-
-
Save denysvitali/d31152022e192aaf5cf7c5d6b23cfec0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 120 | |
rm = running_mean(dates, n) | |
rm_y = running_mean(bpms, n) | |
f = interpolate.interp1d(rm, rm_y, kind='slinear', assume_sorted=False) | |
print(rm); | |
fig, ax = plt.subplots(figsize=(20,10)) | |
if False: | |
ax.plot_date(dates, | |
bpms, | |
color='#2980b9', | |
marker='o', | |
markersize=1, | |
markeredgecolor='#3498db', | |
linestyle='solid' | |
) | |
else: | |
ax.plot_date(rm, | |
f(rm), | |
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, 12))) | |
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