-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.py
131 lines (116 loc) · 3.09 KB
/
analyze.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!usr/bin/env python3
import time
"""
get_daily_calories(data, date)
Accumulates the number of calories in a single date in a diet dataset.
inputs:
- data: json object
- date: string representation of date <DD/MM/YYYY>
returns:
- (int) daily number of calories
"""
def get_daily_calories(data, date):
meals = data[date]
cals = 0
for m in meals:
cals += m["cals"]
return cals
"""
get_daily_fat(data, date)
Accumulates the amount of fat in a single date in a diet dataset.
inputs:
- data: json object
- date: string representation of date <DD/MM/YYYY>
returns:
- (int) total grams of fat
"""
def get_daily_fat(data, date):
meals = data[date]
fat = 0
for m in meals:
fat += m["fat"]
return fat
"""
get_daily_calories(data, date)
Accumulates the amount of carbs in a single date in a diet dataset.
inputs:
- data: json object
- date: string representation of date <DD/MM/YYYY>
returns:
- (int) total grams of carbs
"""
def get_daily_carbs(data, date):
meals = data[date]
carbs = 0
for m in meals:
carbs += m["carbs"]
return carbs
"""
get_daily_protein(data, date)
Accumulates the amount of protein in a single date in a diet dataset.
inputs:
- data: json object
- date: string representation of date <DD/MM/YYYY>
returns:
- (int) total grams of protein
"""
def get_daily_protein(data, date):
meals = data[date]
protein = 0
for m in meals:
protein += m["protein"]
return protein
"""
get_daily_calories(data)
Accumulates the number of calories in a single dat in a diet dataset.
inputs:
- data: json object
returns:
- (int) total number of calories
"""
def get_macros(data):
new_data = merge_data(data)
cals, fat, carbs, protein, dates = [], [], [], [], []
for key in sorted(new_data):
cals.append(get_daily_calories(new_data, key))
fat.append(9 * get_daily_fat(new_data, key))
carbs.append(4 * get_daily_carbs(new_data, key))
protein.append(4 * get_daily_protein(new_data, key))
# convert date back to readable string
date = time.strftime("%m/%d/%Y", time.gmtime(key*86400))
dates.append(date)
return cals, fat, carbs, protein, dates
"""
merge_data(data)
Merges meals into a json object grouped by date
inputs:
- data: json object
returns:
- new_data: reformatted json object
"""
def merge_data(data):
new_data = {}
for row in data:
if row['date'] not in new_data:
new = []
new.append(row['meal'])
new_data[row['date']] = new
else:
new = new_data[row['date']]
new.append(row['meal'])
new_data[row['date']] = new
return new_data
"""
get_average_calories(data)
Calculates the average numbers of calories in a given range of time.
inputs:
- data: json object
returns:
- (float) average number of calories
"""
def get_average_calories(data):
new_data = merge_data(data)
cals = []
for key in sorted(new_data):
cals.append(get_daily_calories(new_data, key))
return sum(cals) / float(len(cals))