def seed_bmdata(): ''' hacked way to get our static data into the db ''' # if there is no body media data in the db bm_data = BodyMediaData.objects() if bm_data: return 'already seeded' # attach to a specific player player = Player.objects(name='matt')[0] # in 'static' dir we have an excel file with Body Media data rows data_path = 'static/data/v2.csv' with open(data_path, 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',') ''' iterate over the rows and inject body media entries assuming this column order in the excel file: timestamp, lying down, sleep duration, calories in, calories out, average MET, sed, mod, vigorous ''' count = 0 for row in reader: if count == 0: count += 1 continue bmdata = BodyMediaData( timestamp = datetime.datetime.fromtimestamp(time.mktime( time.strptime(row[0], '%Y%m%d.000000'))) , lying_down = float(row[1]) , sleep_duration = float(row[2]) , caloric_intake = float(row[3]) , caloric_output = float(row[4]) , average_met = float(row[5]) , sedentary_activity_duration = float(row[6]) , moderate_activity_duration = float(row[7]) , vigorous_activity_duration = float(row[8]) , player=player ) bmdata.save() return 'ok'
def destroy_seed(): objects = [] objects.extend(Question.objects()) objects.extend(Player.objects()) objects.extend(Answer.objects()) objects.extend(BodyMediaData.objects()) for o in objects: o.delete() return 'ouch'
def seed_bmdata(): ''' hacked way to get our static data into the db ''' # if there is no body media data in the db bm_data = BodyMediaData.objects() if bm_data: return 'already seeded' # attach to a specific player player = Player.objects(name='matt')[0] # in 'static' dir we have an excel file with Body Media data rows data_path = 'static/data/v2.csv' with open(data_path, 'rb') as csvfile: reader = csv.reader(csvfile, delimiter=',') ''' iterate over the rows and inject body media entries assuming this column order in the excel file: timestamp, lying down, sleep duration, calories in, calories out, average MET, sed, mod, vigorous ''' count = 0 for row in reader: if count == 0: count += 1 continue bmdata = BodyMediaData(timestamp=datetime.datetime.fromtimestamp( time.mktime(time.strptime(row[0], '%Y%m%d.000000'))), lying_down=float(row[1]), sleep_duration=float(row[2]), caloric_intake=float(row[3]), caloric_output=float(row[4]), average_met=float(row[5]), sedentary_activity_duration=float(row[6]), moderate_activity_duration=float(row[7]), vigorous_activity_duration=float(row[8]), player=player) bmdata.save() return 'ok'
def player_data(name, date): ''' date is of the format 20120910 or YYmd ''' players = Player.objects(name=name) if not players: abort(404) player = players[0] requested_date = datetime.datetime.fromtimestamp( time.mktime(time.strptime(date, '%Y%m%d'))) data = None # what if 2010?? while not data: data = BodyMediaData.objects(player=player, timestamp=requested_date) requested_date = requested_date - datetime.timedelta(days=1) data = data[0] todays_result = compute(player, requested_date, data) # total-score computation total_score = 0 for i in [0, 1, 2]: requested_date = requested_date - datetime.timedelta(days=i) r = compute(player, requested_date, data) if i == 0: total_score += 0.6 * ( 10 * r['physical_activity'] + r['question_responses'] + 10 * r['net_calories'] + 10 * r['time_slept']) elif i == 1: total_score += 0.3 * ( 10 * r['physical_activity'] + r['question_responses'] + 10 * r['net_calories'] + 10 * r['time_slept']) elif i == 2: total_score += 0.1 * ( 10 * r['physical_activity'] + r['question_responses'] + 10 * r['net_calories'] + 10 * r['time_slept']) todays_result['total_score'] = int(total_score) return jsonify(todays_result)
def player_data(name, date): ''' date is of the format 20120910 or YYmd ''' players = Player.objects(name=name) if not players: abort(404) player = players[0] requested_date = datetime.datetime.fromtimestamp(time.mktime( time.strptime(date, '%Y%m%d'))) data = None # what if 2010?? while not data: data = BodyMediaData.objects(player=player, timestamp=requested_date) requested_date = requested_date - datetime.timedelta(days=1) data = data[0] todays_result = compute(player, requested_date, data) # total-score computation total_score = 0 for i in [0,1,2]: requested_date = requested_date - datetime.timedelta(days=i) r = compute(player, requested_date, data) if i == 0: total_score += 0.6*(10*r['physical_activity'] + r['question_responses'] + 10*r['net_calories'] + 10*r['time_slept']) elif i == 1: total_score += 0.3*(10*r['physical_activity'] + r['question_responses'] + 10*r['net_calories'] + 10*r['time_slept']) elif i == 2: total_score += 0.1*(10*r['physical_activity'] + r['question_responses'] + 10*r['net_calories'] + 10*r['time_slept']) todays_result['total_score'] = int(total_score) return jsonify(todays_result)
def compute(player, requested_date, data): ''' compute all parameters.. ''' # calculatron ''' net caloric intake ''' net = data.caloric_intake - data.caloric_output if net >= 100: net_calories = 3 elif net >= -100 and net < 100: net_calories = 2 else: net_calories = 1 ''' activity calculator ''' caloric_target = 2475 # parameterize later age = 39 if player.gender == 'male': BMR = 66 + (6.23 * player.weight) + (12.7 * player.height) - (6.8 * age) else: BMR = 655 + (4.35 * player.weight) + (4.7 * player.height) - (4.7 * age) baseline = BMR * 1.2 if data.caloric_output >= caloric_target + 200: physical_activity = 3 elif data.caloric_output >= baseline + 350 and data.caloric_output < caloric_target + 200: physical_activity = 2 else: physical_activity = 1 ''' sleep calculator ''' sleep_goal = 60*8 # parameterize one day, lalala # try to get the previous day previous_day = requested_date - datetime.timedelta(days=1) previous_day_data = BodyMediaData.objects(player=player , timestamp=previous_day) if not previous_day_data: # 100% weighted to current day percent_of_goal = data.lying_down / sleep_goal sleep_efficiency = data.sleep_duration / data.lying_down if percent_of_goal > 1: percent_score = 3 elif percent_of_goal > 0.9: percent_score = 2 else: percent_score = 1 if sleep_efficiency >= 0.8: efficiency_score = 3 elif sleep_efficiency > 0.6: efficiency_score = 2 else: efficiency_score = 1 if efficiency_score == 3 and percent_score == 3: time_slept = 3 elif efficiency_score == 2 or percent_score == 2: time_slept = 2 elif efficiency_score == 1 or percent_score == 1: time_slept = 1 else: previous_day_data = previous_day_data[0] # 25% yesterday, 75% today percent_of_goal = data.lying_down / sleep_goal percent_of_goal_yesterday = previous_day_data.lying_down / sleep_goal sleep_efficiency = data.sleep_duration / data.lying_down sleep_efficiency_yesterday = previous_day_data.sleep_duration / data.lying_down if percent_of_goal*0.75 + percent_of_goal_yesterday*0.25 > 1: percent_score = 3 if percent_of_goal*0.75 + percent_of_goal_yesterday*0.25 > 0.9: percent_score = 2 else: percent_score = 1 if sleep_efficiency*0.75 + sleep_efficiency_yesterday*0.25 >= 0.8: efficiency_score = 3 if sleep_efficiency*0.75 + sleep_efficiency_yesterday*0.25 >= 0.6: efficiency_score = 2 else: efficiency_score = 1 if efficiency_score == 3 and percent_score == 3: time_slept = 3 elif efficiency_score == 2 or percent_score == 2: time_slept = 2 elif efficiency_score == 1 or percent_score == 1: time_slept = 1 ''' question score ''' yesterday = requested_date - datetime.timedelta(days=1) question_responses = Answer.objects(player=player , timestamp__gte=yesterday, timestamp__lte=requested_date).count() if question_responses > 10: question_responses = 10 return { 'time_slept': time_slept , 'net_calories': net_calories , 'physical_activity': physical_activity , 'question_responses': question_responses , 'requested_date': requested_date.strftime('%d/%m/%Y') }
def compute(player, requested_date, data): ''' compute all parameters.. ''' # calculatron ''' net caloric intake ''' net = data.caloric_intake - data.caloric_output if net >= 100: net_calories = 3 elif net >= -100 and net < 100: net_calories = 2 else: net_calories = 1 ''' activity calculator ''' caloric_target = 2475 # parameterize later age = 39 if player.gender == 'male': BMR = 66 + (6.23 * player.weight) + (12.7 * player.height) - (6.8 * age) else: BMR = 655 + (4.35 * player.weight) + (4.7 * player.height) - (4.7 * age) baseline = BMR * 1.2 if data.caloric_output >= caloric_target + 200: physical_activity = 3 elif data.caloric_output >= baseline + 350 and data.caloric_output < caloric_target + 200: physical_activity = 2 else: physical_activity = 1 ''' sleep calculator ''' sleep_goal = 60 * 8 # parameterize one day, lalala # try to get the previous day previous_day = requested_date - datetime.timedelta(days=1) previous_day_data = BodyMediaData.objects(player=player, timestamp=previous_day) if not previous_day_data: # 100% weighted to current day percent_of_goal = data.lying_down / sleep_goal sleep_efficiency = data.sleep_duration / data.lying_down if percent_of_goal > 1: percent_score = 3 elif percent_of_goal > 0.9: percent_score = 2 else: percent_score = 1 if sleep_efficiency >= 0.8: efficiency_score = 3 elif sleep_efficiency > 0.6: efficiency_score = 2 else: efficiency_score = 1 if efficiency_score == 3 and percent_score == 3: time_slept = 3 elif efficiency_score == 2 or percent_score == 2: time_slept = 2 elif efficiency_score == 1 or percent_score == 1: time_slept = 1 else: previous_day_data = previous_day_data[0] # 25% yesterday, 75% today percent_of_goal = data.lying_down / sleep_goal percent_of_goal_yesterday = previous_day_data.lying_down / sleep_goal sleep_efficiency = data.sleep_duration / data.lying_down sleep_efficiency_yesterday = previous_day_data.sleep_duration / data.lying_down if percent_of_goal * 0.75 + percent_of_goal_yesterday * 0.25 > 1: percent_score = 3 if percent_of_goal * 0.75 + percent_of_goal_yesterday * 0.25 > 0.9: percent_score = 2 else: percent_score = 1 if sleep_efficiency * 0.75 + sleep_efficiency_yesterday * 0.25 >= 0.8: efficiency_score = 3 if sleep_efficiency * 0.75 + sleep_efficiency_yesterday * 0.25 >= 0.6: efficiency_score = 2 else: efficiency_score = 1 if efficiency_score == 3 and percent_score == 3: time_slept = 3 elif efficiency_score == 2 or percent_score == 2: time_slept = 2 elif efficiency_score == 1 or percent_score == 1: time_slept = 1 ''' question score ''' yesterday = requested_date - datetime.timedelta(days=1) question_responses = Answer.objects(player=player, timestamp__gte=yesterday, timestamp__lte=requested_date).count() if question_responses > 10: question_responses = 10 return { 'time_slept': time_slept, 'net_calories': net_calories, 'physical_activity': physical_activity, 'question_responses': question_responses, 'requested_date': requested_date.strftime('%d/%m/%Y') }