def detect_daily_work_office(user_id,day): # say should be from 1 to 5 Sections=get_section_db() office_candidate=[] home=detect_home(user_id) if home == 'N/A': return 'N/A' # print(list_first_pnt) for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }}]}): section_start_pnt=section['section_start_point'] section_end_pnt=section['section_end_point'] if Is_date(section['section_start_time'],day)==True: # parameter that the distance away from home: away_home=400 if not Is_place(section_start_pnt,home,away_home) and not Is_place(section_end_pnt,home,away_home): office_candidate.append(section['track_points'][0]) office_candidate.append(section['track_points'][-1]) elif Is_place(section_start_pnt,home,away_home) and not Is_place(section_end_pnt,home,away_home): office_candidate.append(section['track_points'][-1]) elif not Is_place(section_start_pnt,home,away_home) and Is_place(section_end_pnt,home,away_home): office_candidate.append(section['track_points'][0]) if len(office_candidate)>0: office_candidate = sorted(office_candidate, key=lambda k: parser.parse(k['time'])) # print(office_candidate) weighted_office_candidate=get_static_pnts(office_candidate) office_location=most_common(weighted_office_candidate,200) # print(len(office_candidate)) # print(len(weighted_office_candidate)) # print(calculate_appearance_rate(office_candidate,office_location)) # print(calculate_appearance_rate(weighted_office_candidate,office_location)) return office_location else: return 'N/A'
def detect_home_2(user_id): Sections = get_section_db() list_first_pnt = [] list_home_candidate = [] for section in Sections.find({ '$and': [{ 'user_id': user_id }, { 'section_start_point': { '$ne': None } }] }): list_first_pnt.append(section['track_points'][0]) list_home_candidate = get_first_daily_point(list_first_pnt) if len(list_home_candidate) == 0: return 'N/A' list_home_candidate_cood = [] for pnt in list_home_candidate: list_home_candidate_cood.append(pnt['track_location']['coordinates']) list_home_candidate_np = np.asarray(list_home_candidate_cood) minlat = np.min(list_home_candidate_np[:, 0]) minlng = np.min(list_home_candidate_np[:, 1]) list_home_candidate_np_2 = np.zeros((len(list_home_candidate_np), 2)) list_home_candidate_np_2[:, 0] = (list_home_candidate_np[:, 0] - minlat) * 89.7 list_home_candidate_np_2[:, 1] = (list_home_candidate_np[:, 1] - minlng) * 112.7 db = DBSCAN(eps=0.2, min_samples=3) db_fit = db.fit(list_home_candidate_np_2) db_labels = db_fit.labels_ new_db_labels = db_labels[db_labels != -1] if new_db_labels != []: frequency = Counter(new_db_labels) max_fre = frequency.most_common()[0][1] new_location = list_home_candidate_np[db_labels != -1] label_unique = np.unique(new_db_labels) cluster_center = np.zeros((len(label_unique), 3)) home_list = [] for label in label_unique: sub_location = new_location[new_db_labels == label] if len(sub_location) / max_fre >= 0.3: temp_center = np.mean(sub_location, axis=0) home_list.append(list(temp_center)) return home_list else: return [most_common(list_home_candidate, 200)]
def detect_home(user_id): Sections=get_section_db() list_first_pnt=[] list_home_candidate=[] for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }}]}): list_first_pnt.append(section['track_points'][0]) # print(list_first_pnt) list_home_candidate=get_first_daily_point(list_first_pnt) # print(type(list_home_candidate)) # print(type(list_home_candidate[0])) if len(list_home_candidate)==0: return 'N/A' else: home_location=most_common(list_home_candidate,500) return home_location
def json_to_records(js): """Extract records from this json object by finding the leaf nodes and taking the nodes with most common counts as the columns """ records = [] counter = json_counter(js) if counter: num_entries = common.most_common( [len(values) for values in counter.values()]) # get the fields with the correct number of entries fields = [ key for key in counter.keys() if len(counter[key]) == num_entries ] for i in range(num_entries): result = dict([(field, counter[field][i]) for field in fields]) records.append(result) return records
def detect_home_2(user_id): Sections = get_section_db() list_first_pnt = [] list_home_candidate = [] for section in Sections.find({'$and': [{'user_id': user_id}, {'section_start_point': {'$ne': None}}]}): list_first_pnt.append(section['track_points'][0]) list_home_candidate = get_first_daily_point(list_first_pnt) if len(list_home_candidate) == 0: return 'N/A' list_home_candidate_cood = [] for pnt in list_home_candidate: list_home_candidate_cood.append(pnt['track_location']['coordinates']) list_home_candidate_np = np.asarray(list_home_candidate_cood) minlat = np.min(list_home_candidate_np[:, 1]) minlng = np.min(list_home_candidate_np[:, 0]) list_home_candidate_np_2 = np.zeros((len(list_home_candidate_np), 2)) list_home_candidate_np_2[:, 1] = (list_home_candidate_np[:, 0] - minlat) * 89.7 list_home_candidate_np_2[:, 0] = (list_home_candidate_np[:, 1] - minlng) * 112.7 db = DBSCAN(eps=0.2, min_samples=3) db_fit = db.fit(list_home_candidate_np_2) db_labels = db_fit.labels_ new_db_labels = db_labels[db_labels != -1] if new_db_labels != []: frequency = Counter(new_db_labels) max_fre = frequency.most_common()[0][1] new_location = list_home_candidate_np[db_labels != -1] label_unique = np.unique(new_db_labels) cluster_center = np.zeros((len(label_unique), 3)) home_list = [] for label in label_unique: sub_location = new_location[new_db_labels == label] if len(sub_location) / max_fre >= 0.3: temp_center = np.mean(sub_location, axis=0) home_list.append(list(temp_center)) return home_list else: return [most_common(list_home_candidate, 200)]