def event_pmf(event_set, result_size): count = [0] * result_size second_length_of_bins = float( watervibe_time.seconds_in_a_day) / float(result_size) start_times = event_set[0] end_times = event_set[1] number_of_events = len(start_times) for index in range(0, number_of_events): start_datetime = watervibe_time.date_for_string(start_times[index]) end_datetime = watervibe_time.date_for_string(end_times[index]) event_length = (end_datetime - start_datetime).total_seconds() start_time = start_datetime.hour * 3600 + start_datetime.minute * 60 in_bin = int(start_time / second_length_of_bins) time_in_event = 0 while time_in_event < event_length: count[in_bin] = count[in_bin] + 1 in_bin = in_bin + 1 if in_bin >= len(count): in_bin = 0 time_in_event += second_length_of_bins result = [float(x) / float(number_of_events) for x in count] return result
def reminders_available_at_next_sync(user): reminders = Reminder.objects.filter(user=user.id).order_by('-time') available_spots = users.maximum_reminders(user) next_sync_time = watervibe_time.date_for_string(user.next_sync_time) for reminder in reminders: if watervibe_time.date_for_string(reminder.time) > next_sync_time: available_spots = available_spots - 1 else: break return available_spots
def reminders_at_next_sync(user): all_reminders = user_reminders(user).order_by("-time") reminders = [] next_sync_time = date_for_string(user.next_sync_time) for reminder in all_reminders: time = date_for_string(reminder.time) if time > next_sync_time: reminders.append(reminder) else: break return reminders
def create_reminder(time, user): reminder_event_time = watervibe_time.event_time_from_date(time) if (last_reminder_for_user(user) is None): time = watervibe_time.now_in_user_timezone(user) + timedelta(minutes=1) reminder = Reminder.objects.create( time=watervibe_time.string_for_date(time), user=user) reminder.save() for event in Event.objects.filter(user=user, day_of_week=time.isoweekday()): if watervibe_time.time_is_between_period(reminder_event_time, event.start_time, event.end_time): print "%f is during event from %f to %f" % ( reminder_event_time, event.start_time, event.end_time) reminder_event_time = events.adjust_reminder_at( reminder_event_time, event) print "Adjusted to %f" % reminder_event_time hour = int(reminder_event_time) minute = float(reminder_event_time - hour) * 60 time = time.replace(hour=int(hour), minute=int(minute)) if time < watervibe_time.now_in_user_timezone( user) or time < watervibe_time.date_for_string( last_reminder_for_user(user).time): time = time + timedelta(days=1) print "Adding reminder at " + watervibe_time.string_for_date(time) reminder = Reminder.objects.create( time=watervibe_time.string_for_date(time), user=user) reminder.save() return reminder
def ounces_to_drink_in_period(user, start_date, end_date): reminders = user_reminders(user).order_by("-time") count = 0 for reminder in reminders: reminder_date = date_for_string(reminder.time) if reminder_date > start_date and reminder_date < end_date: count += 1 return count * user.drink_size
def next_reminder_for(user): reminders = users.user_reminders(user) now = watervibe_time.now_in_user_timezone(user) for reminder in reminders: reminder_date = watervibe_time.date_for_string(reminder.time) if now < reminder_date: return reminder return None
def create_reminders_for_user(user): last_reminder = last_reminder_for_user(user) if last_reminder is None: soon = watervibe_time.now_in_user_timezone(user) + timedelta( minutes=30) last_reminder = create_reminder(soon, user) last_reminder_date = watervibe_time.date_for_string(last_reminder.time) hour = last_reminder_date.hour minute = last_reminder_date.minute start_of_period = last_reminder_date end_of_period = last_reminder_date + timedelta(days=1) ounces_drunk_in_period = users.ounces_to_drink_in_period( user, start_of_period, end_of_period) required_ounces = users.ounces_in_period(user, start_of_period, end_of_period) time_between_reminders = users.maximum_time_between_reminders( user, start_of_period) last_distance = 0 print "Setting reminders every %d seconds" % time_between_reminders.total_seconds( ) while ounces_drunk_in_period < required_ounces: next_reminder_date = last_reminder_date + time_between_reminders print next_reminder_date if next_reminder_date < watervibe_time.now_in_user_timezone(user): next_reminder_date = watervibe_time.now_in_user_timezone( user) + timedelta(minutes=5) last_reminder = create_reminder(next_reminder_date, user) if (next_reminder_date > end_of_period): break last_reminder_date = watervibe_time.date_for_string(last_reminder.time) print last_reminder_date ounces_drunk_in_period = users.ounces_to_drink_in_period( user, start_of_period, end_of_period) print ounces_drunk_in_period
def weighted_average_sleep_time(user, day_of_the_week): app = importlib.import_module(user.app + "." + user.app) sleep_times, wake_times = app.sleep_times(user.app_id) parsed_times = [] for sleep_time in sleep_times: date = date_for_string(sleep_time) if date.isoweekday() == day_of_the_week: time = float(date.hour) + float(date.minute) / 60.0 parsed_times.append(time) return stats.weighted_average(parsed_times, lambda x: 1)
def end_of_period(user): return date_for_string(user.end_of_period)
def start_of_period(user): return date_for_string(user.start_of_period)
def date(reminder): return watervibe_time.date_for_string(reminder.time)