In [1]:
import re, requests, StringIO
from dateutil import tz
In [2]:
def exec_ipynb(url):
import json, re, urllib2
nb = (urllib2.urlopen(url) if re.match(r'https?:', url) else open(url)).read()
exec '\n'.join([''.join(cell['input']) for cell in json.loads(nb)['worksheets'][0]['cells'] if cell['cell_type'] == 'code']) in globals()
exec_ipynb('Fluxtream-Library.ipynb')
fluxtream_login()
Out[2]:
In [9]:
import datetime, exifread, glob, json, os, pprint, subprocess
from dateutil import tz
# Keep track of where you save the files, and enter in the next section
import httplib, urllib, time, base64, string, datetime, json, csv, calendar
from dateutil import tz
from dateutil import parser
# Replace non alnum chars with _
def sanitize_channel_name(name):
return re.sub(r'\W+', '_', name).strip('_')
def epoch_time(dt):
epoch = datetime.datetime(1970, 1, 1, tzinfo=tz.tzutc())
return (dt - epoch).total_seconds()
# Returns 2D array of data suitable for posting to Fluxtream API
def sleepyhead_csv_to_channel_map(filename, timezone):
# Sleepyhead channel map maps channel name to a 2D array of data
# of the form [[unixtime0, value0], [unixtime1, value1],...]
channel_map = {}
reader = csv.reader(open(filename,'rb'), delimiter=',')
# skip header
header = reader.next()
rowcount = 0;
data = []
for row in reader:
# In sleepyhead output, the zeroth column is the datetime of the start of sleep, the second column is the event type
# and the third column is a value
datetime_str= row[0]
event_name = row[2]
value = float(row[3])
dt = datetime.datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone)
if event_name in channel_map:
channel_map[event_name].append([epoch_time(dt), value])
else:
channel_map[event_name]=[[epoch_time(dt), value]]
#print "%s (%d): %s %f [%d]" % (dt, epoch_time(dt), event_name, value, len(channel_map[event_name]))
return channel_map
def sleepyhead_channel_map_to_fluxtream(device_name,channel_map):
for event_name in channel_map:
ch_name = sanitize_channel_name(event_name)
fluxtream_upload(device_name, [ch_name], channel_map[event_name])
print "Uploaded %d samples to %s" %(len(channel_map[event_name]), ch_name)
In [10]:
device_name = 'CPAP'
timezone = tz.gettz('America/New York')
csv_path = '/Users/anne/education/bodytrack/data/cpap/sleepyhead/SleepyHead_Username_Details_2015-03-25_2015-03-31.csv'
print 'Reading CSV from %s...' % csv_path
channel_map = sleepyhead_csv_to_channel_map(csv_path,timezone)
sleepyhead_channel_map_to_fluxtream(device_name,channel_map)
In [ ]: