def _parse_continuous(self, unit): """Parse continuous segments of blood glucose readings for arbitrary (non-day) time batches (week, month, year).""" last_time = util_time.parse_timestamp(unit.readings[0]['timestamp']) segment = [] for r in unit.readings: # update current time current_time = util_time.parse_timestamp(r['timestamp']) # triggered all but first run of loop if last_time < current_time: delta = current_time - last_time if util_time.compare_timedelta_minutes(delta, 6, '>'): unit.continuous = False unit.continuous_segments.append(segment) segment = [r] else: segment.append(r) # only triggered during first run of loop elif last_time == current_time: segment.append(r) # update last time last_time = current_time if len(segment) > 1: unit.continuous_segments.append(segment)
def _split_by_day(self): """Split data into daily batches.""" current_date = self.start_date calibs = [] # loop through all calibrations and batch them by date for c in self.calibrations: if util_time.parse_timestamp( c['timestamp']).date() == current_date: calibs.append(c) else: # need to check if calibrations are empty...it happens if calibs != []: # T&E because DexcomDay object may not have been created yet for current_date try: self.days[current_date].calibrations = calibs except KeyError as k1: self.days[current_date] = DexcomDay() self.days[current_date].calibrations = calibs # reset calibs for next run of loop calibs = [] # update current_date current_date = util_time.increment_date(current_date) if util_time.parse_timestamp( c['timestamp']).date() == current_date: calibs.append(c) # add final day's data if calibs != []: try: self.days[current_date].calibrations = calibs except KeyError as k2: self.days[current_date] = DexcomDay() self.days[current_date].calibrations = calibs # reset current_date to beginning current_date = self.start_date # store last most recent timestamp in order to check for continuity between meter readings last_time = util_time.parse_timestamp(self.readings[0]['timestamp']) dates = [] readings = [] # stores a *continuous* series of BG readings segment = [] for r in self.readings: if util_time.parse_timestamp( r['timestamp']).date() == current_date: readings.append(r) # update current_time current_time = util_time.parse_timestamp(r['timestamp']) # only interested in continuity if the last_time and current_time are of the same date if last_time.date() == current_time.date(): # last_time will always be less than current_time, except for first run of loop, when they will be equal if last_time < current_time: delta = current_time - last_time # delta of 5 doesn't work since some BG readings are 5:01 apart if util_time.compare_timedelta_minutes(delta, 6, '>'): # T&E because DexcomDay object may not have been created yet for current_date # in particular, if date has no calibrations, this could arise try: self.days[current_date].continuous = False except KeyError as k3: self.days[current_date] = DexcomDay() self.days[current_date].continuous = False # store and reinitalize segment when delta is greater than 6 self.days[current_date].continuous_segments.append( segment) segment = [r] # if delta falls within acceptable range for continuity, just add reading to segment else: segment.append(r) # only triggered during first run of loop elif last_time == current_time: segment.append(r) # triggered at the beginning of each new day else: segment.append(r) # update last_time last_time = current_time else: # readings could be empty if no data for a particular day if readings != []: # T&E because DexcomDay object may not have been created yet for current_date # in particular, if date has no calibrations, this could arise try: self.days[current_date].readings = readings except KeyError as k4: self.days[current_date] = DexcomDay() self.days[current_date].readings = readings if not self.days[current_date].continuous: self.days[current_date].continuous_segments.append( segment) else: self.days[current_date].continuous_segments.append( readings) # reset readings and segment for next run of loop readings = [] segment = [] dates.append(current_date) # udpate current_date current_date = util_time.increment_date(current_date) if util_time.parse_timestamp( r['timestamp']).date() == current_date: readings.append(r) # add final day's data dates.append(current_date) if readings != []: try: self.days[current_date].readings = readings except KeyError as k5: self.days[current_date] = DexcomDay() self.days[current_date].readings = readings if not self.days[current_date].continuous: self.days[current_date].continuous_segments.append(segment) else: self.days[current_date].continuous_segments.append(readings) # must remain here because of chicken/egg problem with calling DexcomDay._times() for date in dates: try: d = self.days[date] d._times() except KeyError as k6: pass
def _split_by_day(self): """Split data into daily batches.""" current_date = self.start_date calibs = [] # loop through all calibrations and batch them by date for c in self.calibrations: if util_time.parse_timestamp(c['timestamp']).date() == current_date: calibs.append(c) else: # need to check if calibrations are empty...it happens if calibs != []: # T&E because DexcomDay object may not have been created yet for current_date try: self.days[current_date].calibrations = calibs except KeyError as k1: self.days[current_date] = DexcomDay() self.days[current_date].calibrations = calibs # reset calibs for next run of loop calibs = [] # update current_date current_date = util_time.increment_date(current_date) if util_time.parse_timestamp(c['timestamp']).date() == current_date: calibs.append(c) # add final day's data if calibs != []: try: self.days[current_date].calibrations = calibs except KeyError as k2: self.days[current_date] = DexcomDay() self.days[current_date].calibrations = calibs # reset current_date to beginning current_date = self.start_date # store last most recent timestamp in order to check for continuity between meter readings last_time = util_time.parse_timestamp(self.readings[0]['timestamp']) dates = [] readings = [] # stores a *continuous* series of BG readings segment = [] for r in self.readings: if util_time.parse_timestamp(r['timestamp']).date() == current_date: readings.append(r) # update current_time current_time = util_time.parse_timestamp(r['timestamp']) # only interested in continuity if the last_time and current_time are of the same date if last_time.date() == current_time.date(): # last_time will always be less than current_time, except for first run of loop, when they will be equal if last_time < current_time: delta = current_time - last_time # delta of 5 doesn't work since some BG readings are 5:01 apart if util_time.compare_timedelta_minutes(delta, 6, '>'): # T&E because DexcomDay object may not have been created yet for current_date # in particular, if date has no calibrations, this could arise try: self.days[current_date].continuous = False except KeyError as k3: self.days[current_date] = DexcomDay() self.days[current_date].continuous = False # store and reinitalize segment when delta is greater than 6 self.days[current_date].continuous_segments.append(segment) segment = [r] # if delta falls within acceptable range for continuity, just add reading to segment else: segment.append(r) # only triggered during first run of loop elif last_time == current_time: segment.append(r) # triggered at the beginning of each new day else: segment.append(r) # update last_time last_time = current_time else: # readings could be empty if no data for a particular day if readings != []: # T&E because DexcomDay object may not have been created yet for current_date # in particular, if date has no calibrations, this could arise try: self.days[current_date].readings = readings except KeyError as k4: self.days[current_date] = DexcomDay() self.days[current_date].readings = readings if not self.days[current_date].continuous: self.days[current_date].continuous_segments.append(segment) else: self.days[current_date].continuous_segments.append(readings) # reset readings and segment for next run of loop readings = [] segment = [] dates.append(current_date) # udpate current_date current_date = util_time.increment_date(current_date) if util_time.parse_timestamp(r['timestamp']).date() == current_date: readings.append(r) # add final day's data dates.append(current_date) if readings != []: try: self.days[current_date].readings = readings except KeyError as k5: self.days[current_date] = DexcomDay() self.days[current_date].readings = readings if not self.days[current_date].continuous: self.days[current_date].continuous_segments.append(segment) else: self.days[current_date].continuous_segments.append(readings) # must remain here because of chicken/egg problem with calling DexcomDay._times() for date in dates: try: d = self.days[date] d._times() except KeyError as k6: pass