def findActiveTimesTask(): profiles = Profile.objects.all() time_averages = [0] * 24 num_users = 0 for profile in profiles: num_users += 1 # token = socialhealth_tasks.getToken(profile, "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profile, "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") user_time_averages = activeTimesComputation(internalDataStore) for i in range(len(time_averages)): time_averages[i] += user_time_averages[i] #print num_users #print time_averages for i in range(len(time_averages)): time_averages[i] = time_averages[i] // num_users for profile in profiles: # token = socialhealth_tasks.getToken(profile, "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profile, "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") internalDataStore.saveAnswer("activeTimes", time_averages)
def leaderboardComputationTask(): profiles = Profile.objects.all() # profiles = [] # profiles.append(Profile.objects.get(uuid="341cc5cd-0f42-45f1-9f66-273ac3ed8b2e")) unsorted_dict = {} for profile in profiles: # token = socialhealth_tasks.getToken(profile, "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profile, "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") values = aggregateLeaderboardComputation(internalDataStore, "activityStats", leaderboardComputation, False) unsorted_dict[profile.uuid] = LeaderboardRanking({ "average_activity_rate": values[0]["average_activity_rate"], "max_high_activity_rate": values[0]["max_high_activity_rate"], "min_low_activity_rate": values[0]["min_low_activity_rate"]}) #sorted_dict = sorted(unsorted_dict.values(), key=attrgetter('average_activity_rate')) sorted_dict = sorted(unsorted_dict, key = lambda uuid: unsorted_dict[uuid].average_activity_rate, reverse=False) average_activity_rates_list = [] for uuid in sorted_dict: average_activity_rates_list.append(unsorted_dict[uuid].get_average_activity_rate()) for uuid in sorted_dict: profile = Profile.objects.get(uuid=uuid) # token = socialhealth_tasks.getToken(profile, "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profile, "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") percentileValue = calculatePercentile(average_activity_rates_list, unsorted_dict[uuid].get_average_activity_rate()) user_activity_list = [] user_activity_dict = { "average_activity_rate": unsorted_dict[uuid].get_average_activity_rate(), "max_high_activity_rate": unsorted_dict[uuid].get_max_high_activity_rate(), "min_low_activity_rate": unsorted_dict[uuid].get_min_low_activity_rate(), "rank": {"own": len(sorted_dict) - sorted_dict.index(uuid), "total": len(sorted_dict), "percentile": percentileValue} } user_activity_list.append(user_activity_dict) internalDataStore.saveAnswer("activityStats", user_activity_list)
def findActiveLocationsTask(): profiles = Profile.objects.all() location_frequencies = {} for profile in profiles: try: optin_object = Optin.objects.get(datastore_owner = profile, app_id = "Living Lab", lab_id = "MIT-FIT") except Optin.DoesNotExist: optin_object = None if optin_object: if optin_object.data_aggregation == 0: continue #print profile.id internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") values = activeLocationsComputation(internalDataStore) for value in values: location_value = tuple((round(value[0],4), round(value[1],4))) if location_value in location_frequencies: location_frequencies[location_value] = location_frequencies[location_value] + 1 else: location_frequencies[location_value] = 1 location_frequencies_list = [] for key in location_frequencies: location_value = { "lat": key[0], "lng": key[1], "count": location_frequencies[key]} location_frequencies_list.append(location_value) for profile in profiles: internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") internalDataStore.saveAnswer("activeLocations", location_frequencies_list)
def recentProbeCounts(): profiles = Profile.objects.all() startTime = getStartTime(1, False) for profile in profiles: ids = getInternalDataStore(profile, "", "Living Lab", "") probes = ["ActivityProbe", "SimpleLocationProbe", "CallLogProbe", "SmsProbe", "WifiProbe", "BluetoothProbe"] answer = {} for probe in probes: data = ids.getData(probe, startTime, None) answer[probe] = data.count() ids.saveAnswer("RecentProbeCounts", answer)
def testGetData(): profiles = Profile.objects.all() #print profiles[17].uuid # token = socialhealth_tasks.getToken(profiles[17], "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profiles[17], "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") probes = ["LocationProbe", "ActivityProbe", "SmsProbe", "CallLogProbe", "BluetoothProbe", "WifiProbe", "ScreenProbe"] startTime = 1403136000 endTime = 1403222400 internalDataStore.getData(probes[1], startTime, endTime)
def populateEventsForUsers(): profiles = Profile.objects.all() for profile in profiles: # token = socialhealth_tasks.getToken(profile, "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profile, "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") events = [] random_numbers = random.sample(range(25), 7) for random_number in random_numbers: events.append(data[random_number]) #print events internalDataStore.saveAnswer("mitfitEventRegistrations", events)
def recentProbeCounts(): profiles = Profile.objects.all() startTime = getStartTime(1, False) for profile in profiles: ids = getInternalDataStore(profile, "", "Living Lab", "") probes = [ "ActivityProbe", "SimpleLocationProbe", "CallLogProbe", "SmsProbe", "WifiProbe", "BluetoothProbe" ] answer = {} for probe in probes: data = ids.getData(probe, startTime, None) answer[probe] = data.count() ids.saveAnswer("RecentProbeCounts", answer)
def recommendEvents(): profiles = Profile.objects.all() eventRegistrations = {} userRegistrations = {} for profile in profiles: #print profile.uuid # token = socialhealth_tasks.getToken(profile, "app-uuid") # internalDataStore = socialhealth_tasks.getInternalDataStore(profile, "Living Lab", "Social Health Tracker", token) internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") eventRegistrations, userEventRegistrations = eventRecommendationComputation(internalDataStore, eventRegistrations, profile.uuid) userRegistrations[profile.uuid] = userEventRegistrations #print eventRegistrations eventSet = set() jaccardCoefficientDict = {} for event1 in eventRegistrations.keys(): for event2 in eventRegistrations.keys(): if event1 != event2: usersEvent1 = eventRegistrations[event1] usersEvent2 = eventRegistrations[event2] intersectUsers = list(set(usersEvent1) & set(usersEvent2)) unionUsers = list(set(usersEvent1) | set(usersEvent2)) jaccardCoefficientKey = (event1, event2) eventSet.add(event1) eventSet.add(event2) if len(unionUsers) > 0: jaccardCoefficientDict[jaccardCoefficientKey] = len(intersectUsers)/len(unionUsers) else: jaccardCoefficientDict[jaccardCoefficientKey] = 0 #print jaccardCoefficientDict for profile in profiles: #print profile.uuid recommendedEvents = {} for userRegisteredEvent in userRegistrations[profile.uuid]: for event in eventSet: if userRegisteredEvent != event: if event in recommendedEvents: if jaccardCoefficientDict[(userRegisteredEvent, event)] > recommendedEvents[event]: recommendedEvents[event] = jaccardCoefficientDict[(userRegisteredEvent, event)] else: recommendedEvents[event] = jaccardCoefficientDict[(userRegisteredEvent, event)] #print recommendedEvents sortedRecommendedEvents = sorted(recommendedEvents.items(), key = lambda recommendedEvent: recommendedEvent[1], reverse=True)
def recentProbeDataScores(): profiles = Profile.objects.all() for profile in profiles: startTime = socialhealth_tasks.getStartTime(6, True) currentTime = time.time() timeRanges = [(start, start + 3600) for start in range(int(startTime), int(currentTime), 3600)] probeAnswerKeys = {'recentActivityProbeByHour': 'ActivityProbe', 'recentSmsProbeByHour': 'SmsProbe', 'recentCallLogProbeByHour': 'CallLogProbe', 'recentBluetoothProbeByHour': 'BluetoothProbe', 'recentWifiProbeByHour': 'WifiProbe', 'recentSimpleLocationProbeByHour': 'LocationProbe', 'recentRunningApplicationsProbeByHour': 'RunningApplicationsProbe', 'recentHardwareInfoProbeByHour': 'HardwareInfoProbe', 'recentAppUsageProbeByHour': 'AppUsageProbe'} internalDataStore = getInternalDataStore(profile, "Living Lab", "MIT-FIT", "") #for testing, currently use the following user #if profile.uuid == "341cc5cd-0f42-45f1-9f66-273ac3ed8b2e": for probeAnswerKey, probe in probeAnswerKeys.iteritems(): probeLevels = aggregateForUser(probe, internalDataStore, probeAnswerKey, timeRanges, probeForTimeRange, False)
def computeAllFingerprints(): for profile in Profile.objects.all(): print profile.uuid ids = getInternalDataStore(profile, "Living Lab", "Auth", "") computeFingerprint(ids) print "... done computing wifi auth fingerprints"
def computeAllFingerprints(): for profile in Profile.objects.all(): print profile.uuid ids = getInternalDataStore(profile, "Living Lab", "Auth", "") computeFingerprint(ids)