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 _times(self): """Fill in start and end times and dates.""" # just in case a calibration timestamp happens to occur before or after the first or last CGM reading of the day if len(self.calibrations) != 0 and len(self.readings) != 0: self.start_time, self.date = util_time.get_start_time( self.calibrations[0]['timestamp'], self.readings[0]['timestamp']) self.end_time = util_time.get_end_time( self.calibrations[-1]['timestamp'], self.readings[-1]['timestamp'])[0] elif len(self.readings) != 0: self.start_time = util_time.parse_timestamp( self.readings[0]['timestamp']) self.date = self.start_time.date() self.end_time = util_time.parse_timestamp( self.readings[-1]['timestamp']) else: self.start_time = util_time.parse_timestamp( self.calibrations[0]['timestamp']) self.date = self.start_time.date() print print str( self.date) + " has (a) calibration(s) but no CGM readings." print self.end_time = util_time.parse_timestamp( self.calibrations[0]['timestamp'])
def _times(self): """Fill in start and end times and dates.""" # just in case a calibration timestamp happens to occur before or after the first or last CGM reading of the day if len(self.calibrations) != 0 and len(self.readings) != 0: self.start_time, self.date = util_time.get_start_time(self.calibrations[0]['timestamp'], self.readings[0]['timestamp']) self.end_time = util_time.get_end_time(self.calibrations[-1]['timestamp'], self.readings[-1]['timestamp'])[0] elif len(self.readings) != 0: self.start_time = util_time.parse_timestamp(self.readings[0]['timestamp']) self.date = self.start_time.date() self.end_time = util_time.parse_timestamp(self.readings[-1]['timestamp']) else: self.start_time = util_time.parse_timestamp(self.calibrations[0]['timestamp']) self.date = self.start_time.date() print print str(self.date) + " has (a) calibration(s) but no CGM readings." print self.end_time = util_time.parse_timestamp(self.calibrations[0]['timestamp'])
def _split_by_month(self): """Split data into monthly batches.""" last_month = self.dates[0].month month = DexcomMonth() for day in self.dates: current_month = day.month current_day = self.days[day] if current_month != last_month: # crunch data on just concluded month self.months[util_time.parse_timestamp( month.readings[0]['timestamp'])] = month self._parse_continuous(month) month._times() month.calculate_GVI_and_PGS() self._crunch_all(month) # update last month last_month = current_month # initialize new month month = DexcomMonth() month.calibrations.extend(current_day.calibrations) month.readings.extend(current_day.readings) month.just_readings.extend(current_day.just_readings) elif current_month == last_month: month.calibrations.extend(current_day.calibrations) month.readings.extend(current_day.readings) month.just_readings.extend(current_day.just_readings) else: self.months[util_time.parse_timestamp( month.readings[0]['timestamp'])] = month self._parse_continuous(month) month._times() month.calculate_GVI_and_PGS() self._crunch_all(month)
def _split_by_week(self): """Split data into weekly batches.""" last_week = self.dates[0].isocalendar()[1] week = DexcomWeek() for day in self.dates: current_week = day.isocalendar()[1] current_day = self.days[day] if current_week != last_week: # crunch data on just concluded week self.weeks[util_time.parse_timestamp( week.readings[0]['timestamp'])] = week self._parse_continuous(week) week._times() week.calculate_GVI_and_PGS() self._crunch_all(week) # update last week last_week = current_week # initialize new week week = DexcomWeek() week.calibrations.extend(current_day.calibrations) week.readings.extend(current_day.readings) week.just_readings.extend(current_day.just_readings) elif current_week == last_week: week.calibrations.extend(current_day.calibrations) week.readings.extend(current_day.readings) week.just_readings.extend(current_day.just_readings) else: self.weeks[util_time.parse_timestamp( week.readings[0]['timestamp'])] = week self._parse_continuous(week) week._times() week.calculate_GVI_and_PGS() self._crunch_all(week)
def _split_by_month(self): """Split data into monthly batches.""" last_month = self.dates[0].month month = DexcomMonth() for day in self.dates: current_month = day.month current_day = self.days[day] if current_month != last_month: # crunch data on just concluded month self.months[util_time.parse_timestamp(month.readings[0]['timestamp'])] = month self._parse_continuous(month) month._times() month.calculate_GVI_and_PGS() self._crunch_all(month) # update last month last_month = current_month # initialize new month month = DexcomMonth() month.calibrations.extend(current_day.calibrations) month.readings.extend(current_day.readings) month.just_readings.extend(current_day.just_readings) elif current_month == last_month: month.calibrations.extend(current_day.calibrations) month.readings.extend(current_day.readings) month.just_readings.extend(current_day.just_readings) else: self.months[util_time.parse_timestamp(month.readings[0]['timestamp'])] = month self._parse_continuous(month) month._times() month.calculate_GVI_and_PGS() self._crunch_all(month)
def _split_by_week(self): """Split data into weekly batches.""" last_week = self.dates[0].isocalendar()[1] week = DexcomWeek() for day in self.dates: current_week = day.isocalendar()[1] current_day = self.days[day] if current_week != last_week: # crunch data on just concluded week self.weeks[util_time.parse_timestamp(week.readings[0]['timestamp'])] = week self._parse_continuous(week) week._times() week.calculate_GVI_and_PGS() self._crunch_all(week) # update last week last_week = current_week # initialize new week week = DexcomWeek() week.calibrations.extend(current_day.calibrations) week.readings.extend(current_day.readings) week.just_readings.extend(current_day.just_readings) elif current_week == last_week: week.calibrations.extend(current_day.calibrations) week.readings.extend(current_day.readings) week.just_readings.extend(current_day.just_readings) else: self.weeks[util_time.parse_timestamp(week.readings[0]['timestamp'])] = week self._parse_continuous(week) week._times() week.calculate_GVI_and_PGS() self._crunch_all(week)
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