你睡眠环境怎么样?Jawbone Up 数据,R 实现
身边许多伴侣都对 Jawbone Up 对睡眠监控的环境很感乐趣,一般会顺带问问:
“你睡眠环境怎么样?”。
“好吧,俺计较一下再汇报你。”
下面的文中涉及到如何用 R 画基于日历的热度图,以及如何将多个图举办归并的内容。
数据获取
需要利用的数据是逐日的睡眠环境数据。
要获取这个数据有两个步伐,最简朴的步伐是从 Jawbone.com 网站下载小我私家的数据文件。详情拜见小我私家数据官方下载 。
尚有一种步伐是操作
非官方API 中的 user/%userXid%/trends
要领获取按日统计数据。
下面的代码回收 Python 实现,用于从处事器端获取 Trends 数据。
device = DeviceJawboneUp ()
# login
device.get_users_login(account[“email”], account[“passwd”])
# Trends
daytrends = device.get_users_trends( endDate = None , inRange = “d” , bucketSize = “d” )
device.saveJsonData( filename = “/users_trends-day.json” , data = daytrends)
1 |
account device.get_users_login(account[“email”], # Trends device.saveJsonData( |
返回的JSON数据示譬喻下:
}
}
}
1 |
{ “meta”: “message”: “user_xid”: “time”: “data”: “data”: 20130605, { “e_protein”: “weight”: “m_distance”: “s_awakenings”: “height”: “m_lcat”: “n_awakenings”: “s_quality”: “n_deep”: “m_steps”: “e_cholesterol”: “s_light”: “s_deep”: “s_bedtime”: “n_bedtime”: “m_workout_time”: “e_calcium”: “e_sat_fat”: “n_duration”: “n_light”: “e_calories”: “pal”: “m_lcit”: “s_awake_time”: “e_unsat_fat”: “m_calories”: “bmr”: “m_total_calories”: “e_sugar”: “e_sodium”: “s_awake”: “s_asleep_time”: “s_duration”: “gender”: “age”: “e_carbs”: “e_fiber”: “n_quality”: “n_awake_time”: “n_awake”: “n_asleep_time”: “m_active_time”: } ], “links”: “next”: }
|
数据预处理惩罚
#p#分页标题#e#
下面的代码,回收 Python 实现了将 Trends 数据转换成平面CSV文件。
def parseJawboneTrends(self , path , filename ):
fn = “%s%s.json” % ( path , filename )
ofn = “%s.csv” % ( filename )
input = open( fn , ‘r’ )
str = input.read()
v = json.loads(str)
output = open( ofn , ‘w’ )
datas = v[“data”][“data”]
tmps = datas[0][1]
headerKeys = sorted(tmps , key=lambda tmps:tmps[0] )
header = “datestr,%s” % self.list2string(headerKeys)
print header
output.write( header )
for d in datas:
tmps = d[1]
stmp = “”
for key in headerKeys:
stmp = “%s,%s” % ( stmp , tmps[ key ])
str = “%s,%s\n” % (d[0] , stmp[1:])
print str
output.write( str )
input.close()
output.close()
if __name__ == “__main__”:
datapath = “../data/jawboneup/”
parseJawboneTrends( path = datapath , filename = “users_trends-day” )
1 |
def ret ret fn str datas tmps header output.write( for stmp stmp str output.write( input.close() datapath
|
#p#分页标题#e#
处理惩罚后的数据文件样本为:
1 |
datestr,age,bmr,e_protein,e_calories,e_cholesterol,e_sat_fat,e_calcium,e_unsat_fat,e_sugar,e_sodium,e_carbs,e_fiber,gender,height,m_distance,m_lcat,m_workout_time,m_lcit,m_active_time,m_calories,m_total_calories,m_steps,n_awakenings,n_deep,n_bedtime,n_duration,n_light,n_quality,n_awake_time,n_awake,n_asleep_time,pal,s_awakenings,s_quality,s_light,s_deep,s_bedtime,s_awake_time,s_awake,s_asleep_time,s_duration,weight 20130605,37.0136986301,1319.38485068,None,None,None,None,None,None,None,None,None,None,1.0,1.58,270.0,88.0,0.0,0.0,201.0,12.3110000491,1331.69585073,367.0,None,None,None,None,None,None,None,None,None,None, |