def export(file_path, contents): # appending .csv file if not file_path.endswith(file_extention): file_path += file_extention row_timestamp = "timestamp" row_entries = "entries" row_exits = "exits" row_current_population = "current_population" file = open(file_path, 'w') field_names = [ row_timestamp, row_entries, row_exits, row_current_population ] writer = csv.DictWriter(file, fieldnames=field_names) file.write("Starting time: " + __convert_time_full(contents[0][0]) + "\n") file.write("Ending time: " + __convert_time_full(contents[-1][0]) + " (plus monitoring duration of " + str(60 / ConfigHelper.getHourSegments()) + " mins)\n") file.write("\n") file.write(row_timestamp + ", " + row_entries + ", " + row_exits + ", " + row_current_population + "\n") for c in contents: time = __convert_time(c[0]) writer.writerow({ row_timestamp: time, row_entries: c[1], row_exits: c[2], row_current_population: c[3] }) file.close()
def makeExportData(): # this is a 2d array of the rows to be exported csv_data = [] conn = BehaviorDatabaseHelper.connect() all_actions = BehaviorDatabaseHelper.getAllActionsSortedbyTime(conn) # number of seconds for each span of observation time_bracket = 3600 / ConfigHelper.getHourSegments() time_end = all_actions[0].time current_entries = 0 current_exits = 0 current_population = 0 filtered_actions = all_actions # __filterActions(all_actions) for action in filtered_actions: # this is guaranteed to hit on the first packet # check if a new bracket needs to be created if action.time > time_end: # update current_population = current_population + current_entries - current_exits # each time stamp will be the start time # and the next observation will be when it ends csv_data += [(time_end, current_entries, current_exits, current_population)] # reset counters time_end = time_end + time_bracket current_entries = 0 current_exits = 0 # entry if action.action == 1: current_entries += 1 # exit else: current_exits += 1 return csv_data[1:]
def makeTimeSlots(): # returns an array of the size of time slots that should be created slotsPerHour = ConfigHelper.getHourSegments() backTrackHours = ConfigHelper.getNumBackTrackHours() return [0] * slotsPerHour * backTrackHours