你睡眠环境怎么样?Jawbone Up 数据,R 实现
当前位置:以往代写 > 其他教程 >你睡眠环境怎么样?Jawbone Up 数据,R 实现
2019-06-14

你睡眠环境怎么样?Jawbone Up 数据,R 实现

你睡眠环境怎么样?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
2
3
4
5
6
7
8
9
10
11

account
= {
“email” :
[email protected]” ,
“passwd” :
“yourpassword”
}

 
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)
 

返回的JSON数据示譬喻下:

}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

{

    “meta”:
{
        “code”:
200,

        “message”:
“OK”,

        “user_xid”:
“RGaCBFg9CsDYVvm2xxxxxx”,

        “time”:
1374161411
    },

    “data”:
{
        “earliest”:
20130605,

        “data”:
[
            [

                20130605,

                {

                    “e_protein”:
null,

                    “weight”:
57.0,

                    “m_distance”:
270.0,

                    “s_awakenings”:
null,

                    “height”:
1.58,

                    “m_lcat”:
88.0,

                    “n_awakenings”:
null,

                    “s_quality”:
null,

                    “n_deep”:
null,

                    “m_steps”:
367.0,

                    “e_cholesterol”:
null,

                    “s_light”:
null,

                    “s_deep”:
null,

                    “s_bedtime”:
null,

                    “n_bedtime”:
null,

                    “m_workout_time”:
0.0,

                    “e_calcium”:
null,

                    “e_sat_fat”:
null,

                    “n_duration”:
null,

                    “n_light”:
null,

                    “e_calories”:
null,

                    “pal”:
null,

                    “m_lcit”:
0.0,

                    “s_awake_time”:
null,

                    “e_unsat_fat”:
null,

                    “m_calories”:
12.3110000491,

                    “bmr”:
1319.38485068,

                    “m_total_calories”:
1331.69585073,

                    “e_sugar”:
null,

                    “e_sodium”:
null,

                    “s_awake”:
null,

                    “s_asleep_time”:
null,

                    “s_duration”:
null,

                    “gender”:
1.0,

                    “age”:
37.0136986301,

                    “e_carbs”:
null,

                    “e_fiber”:
null,

                    “n_quality”:
null,

                    “n_awake_time”:
null,

                    “n_awake”:
null,

                    “n_asleep_time”:
null,

                    “m_active_time”:
201.0

                }
            ]

        ],

        “links”:
{

            “next”:
“/nudge/api/v.1.34/users/RGaCBFg9CsDYVvm2kchbcw/trends/?end_date=20110724&range=d&range_duration=730&bucket_size=d”
        }

    }
}

 


数据预处理惩罚

#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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

def
list2string(list
):

    ret
= “”
    for
l in
list:

        ret
= “%s,%s”
% (ret
, l)
    return
“%s\n” %
ret[1:]

 
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”
)

 

#p#分页标题#e#

处理惩罚后的数据文件样本为:

1
2
3
4
5
6
7

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,

    关键字:

在线提交作业