-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleep_wake_heatmap.py
55 lines (43 loc) · 1.63 KB
/
sleep_wake_heatmap.py
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
import plotly as py
import plotly.graph_objs as go
from datetime import datetime, time, timedelta
import utils.names as names
from utils.csvparser import parse
from utils.exporter import export
from utils.args import args
# load data from csv into an OrderedDict
raw_data = parse(args['data_files'], args['start_date'], args['end_date'])
dates = list(raw_data.keys())
# populating the grid
w = (dates[-1] - dates[0]).days + 2
start_time = 3
t0 = datetime.combine(dates[0], time(hour=start_time))
timeline = [0] * 24 * w
def datetime_to_index(dt):
return round((dt - t0).total_seconds() / 3600)
for date, rests in raw_data.items():
for r in rests:
rest, wake, is_nap = r
st = datetime_to_index(rest)
en = datetime_to_index(wake)
timeline[st:en] = [1] * (en - st)
grid = [[timeline[j + 23 - i] for j in range(0, len(timeline), 24)] for i in range(0, 24)]
# array of y-axis labels
hours = [time(hour=i) for i in range(0, 24)]
hours = hours[start_time:] + hours[:start_time]
hours = list(reversed(hours))
# creating and exporting the graph
trace = go.Heatmap(z=grid,
x=dates,
y=hours,
xgap=10,
ygap=10,
showscale=False,
hoverinfo='x+y+text+name',
colorscale=[[0, '#f7f7f7'], [1, '#ebf3c2']])
data = go.Data([trace])
layout = go.Layout(title=names.graph_title('Sleep Heatmap', dates),
xaxis={'fixedrange': 'True', 'ticklen': 0},
yaxis={'title': 'Time of Day', 'ticklen': 0})
figure = go.Figure(data=data, layout=layout)
export(figure, __file__, dates)