def turn_into_new_trip(self, user_id): print("new trip") ts = esta.TimeSeries.get_time_series(user_id) trip = ecwrt.Rawtrip() sections = [] our_json = self.get_json() mode_list = set ( ) if "plan" not in our_json: print("While querying alternatives from %s to %s" % (self.start_point, self.end_point)) print("query URL is %s" % self.make_url()) print("Response %s does not have a plan " % our_json) raise PathNotFoundException(our_json['debugOutput']) trip.start_loc = gj.Point( (float(our_json["plan"]["from"]["lat"]), float(our_json["plan"]["from"]["lon"])) ) trip.end_loc = gj.Point( (float(our_json["plan"]["to"]["lat"]), float(our_json["plan"]["to"]["lon"])) ) trip.start_local_dt = ecsdlq.get_local_date(otp_time_to_ours( our_json['plan']['itineraries'][0]["startTime"]).timestamp, "UTC") trip.end_local_dt = ecsdlq.get_local_date(otp_time_to_ours( our_json['plan']['itineraries'][0]["endTime"]).timestamp, "UTC") trip_id = ts.insert(ecwe.Entry.create_entry(user_id, "segmentation/raw_trip", trip)) for leg in our_json["plan"]["itineraries"][0]['legs']: section = ecws.Section() section.trip_id = trip_id section.start_local_dt = ecsdlq.get_local_date(otp_time_to_ours( leg["startTime"]).timestamp, "UTC") section.end_local_dt = ecsdlq.get_local_date(otp_time_to_ours( leg["endTime"]).timestamp, "UTC") section.distance = float(leg["distance"]) section.start_loc = gj.Point( (float(leg["from"]["lat"]), float(leg["from"]["lon"])) ) section.end_loc = gj.Point( (float(leg["to"]["lat"]), float(leg["to"]["lon"])) ) ts.insert_data(user_id, "segmentation/raw_section", section)
def create_places_and_trips(user_id, segmentation_points, segmentation_method_name): # new segments, need to deal with them # First, retrieve the last place so that we can stitch it to the newly created trip. # Again, there are easy and hard. In the easy case, the trip was # continuous, was stopped when the trip end was detected, and there is # no gap between the start of the trip and the last place. But there # can be other issues caused by gaps in tracking. A more detailed # description of dealing with gaps in tracking can be found in the wiki. # Let us first deal with the easy case. # restart_events_df = get_restart_events(ts, time_query) ts = esta.TimeSeries.get_time_series(user_id) last_place_entry = esdp.get_last_place_entry(esda.RAW_PLACE_KEY, user_id) if last_place_entry is None: last_place = start_new_chain(user_id) last_place.source = segmentation_method_name last_place_entry = ecwe.Entry.create_entry(user_id, "segmentation/raw_place", last_place, create_id = True) else: last_place = last_place_entry.data # if is_easy_case(restart_events_df): # Theoretically, we can do some sanity checks here to make sure # that we are fairly close to the last point. Maybe mark some kind # of confidence level based on that? logging.debug("segmentation_point_list has length %s" % len(segmentation_points)) for (start_loc_doc, end_loc_doc) in segmentation_points: logging.debug("start_loc_doc = %s, end_loc_doc = %s" % (start_loc_doc, end_loc_doc)) get_loc_for_row = lambda row: ts.df_row_to_entry("background/filtered_location", row).data start_loc = get_loc_for_row(start_loc_doc) end_loc = get_loc_for_row(end_loc_doc) logging.debug("start_loc = %s, end_loc = %s" % (start_loc, end_loc)) # Stitch together the last place and the current trip curr_trip = ecwrt.Rawtrip() curr_trip.source = segmentation_method_name curr_trip_entry = ecwe.Entry.create_entry(user_id, "segmentation/raw_trip", curr_trip, create_id = True) new_place = ecwrp.Rawplace() new_place.source = segmentation_method_name new_place_entry = ecwe.Entry.create_entry(user_id, "segmentation/raw_place", new_place, create_id = True) stitch_together_start(last_place_entry, curr_trip_entry, start_loc) stitch_together_end(new_place_entry, curr_trip_entry, end_loc) ts.insert(curr_trip_entry) # last_place is a copy of the data in this entry. So after we fix it # the way we want, we need to assign it back to the entry, otherwise # it will be lost ts.update(last_place_entry) last_place_entry = new_place_entry # The last last_place hasn't been stitched together yet, but we # need to save it so that it can be the last_place for the next run ts.insert(last_place_entry)
def testSegmentationWrapperWithManualTrip(self): ts = esta.TimeSeries.get_time_series(self.androidUUID) test_trip = ecwrt.Rawtrip() test_trip.start_ts = 1440695152.989 test_trip.start_fmt_time = "2015-08-27 10:05:52.989000-07:00" test_trip.start_local_dt = { 'year': 2015, 'month': 8, 'day': 27, 'hour': 10, 'minute': 5, 'second': 52, 'timezone': "America/Los_Angeles" } test_trip.start_loc = { "type": "Point", "coordinates": [-122.4029569, 37.6162024] } test_trip.end_ts = 1440699266.669 test_trip.end_fmt_time = "2015-08-27 11:14:26.669000-07:00" test_trip.end_local_dt = { 'year': 2015, 'month': 8, 'day': 27, 'hour': 11, 'minute': 14, 'second': 26, 'timezone': "America/Los_Angeles" } test_trip.end_loc = { "type": "Point", "coordinates": [-122.2603947, 37.875023] } test_trip_id = ts.insert( ecwe.Entry.create_entry(self.androidUUID, "segmentation/raw_trip", test_trip)) eaiss.segment_trip_into_sections(self.androidUUID, test_trip_id, "DwellSegmentationTimeFilter") created_stops_entries = esdt.get_raw_stops_for_trip( self.androidUUID, test_trip_id) created_sections_entries = esdt.get_raw_sections_for_trip( self.androidUUID, test_trip_id) created_stops = [entry.data for entry in created_stops_entries] created_sections = [entry.data for entry in created_sections_entries] tq_stop = estt.TimeQuery("data.enter_ts", 1440658800, 1440745200) queried_stops = esda.get_objects(esda.RAW_STOP_KEY, self.androidUUID, tq_stop) tq_section = estt.TimeQuery("data.start_ts", 1440658800, 1440745200) queried_sections = esda.get_objects(esda.RAW_SECTION_KEY, self.androidUUID, tq_section) for i, stop in enumerate(created_stops): logging.info("Retrieved stop %s: %s -> %s" % (i, stop.enter_fmt_time, stop.exit_fmt_time)) for i, section in enumerate(created_sections): logging.info("Retrieved section %s: %s -> %s" % (i, section.start_fmt_time, section.end_fmt_time)) self.assertEqual(len(created_stops), 1) self.assertEqual(created_stops[0].enter_ts, 1440698066.704) self.assertEqual(created_stops[0].exit_ts, 1440698306.892) self.assertEqual(created_stops[0].exit_loc, created_sections[1].start_loc) self.assertEqual(created_stops[0].ending_section, created_sections_entries[0].get_id()) self.assertEqual(created_stops[0].starting_section, created_sections_entries[1].get_id()) self.assertEqual(len(created_sections), 2) logging.info("Checking fields for section %s" % created_sections[0]) self.assertEqual(created_sections[0].start_ts, 1440695152.989) self.assertEqual(created_sections[0].end_ts, 1440698066.704) self.assertIsNone(created_sections[0].start_stop) self.assertEqual(created_sections[0].end_stop, created_stops_entries[0].get_id()) logging.info("Checking fields for section %s" % created_sections[1]) self.assertEqual(created_sections[1].start_ts, 1440698306.892) self.assertEqual(created_sections[1].end_ts, 1440699266.669) self.assertEqual(created_sections[1].start_stop, created_stops_entries[0].get_id()) self.assertIsNone(created_sections[1].end_stop) self.assertEqual(created_sections, queried_sections) self.assertEqual(created_stops, queried_stops)
def create_places_and_trips(user_id, segmentation_points, segmentation_method_name): # new segments, need to deal with them # First, retrieve the last place so that we can stitch it to the newly created trip. # Again, there are easy and hard. In the easy case, the trip was # continuous, was stopped when the trip end was detected, and there is # no gap between the start of the trip and the last place. But there # can be other issues caused by gaps in tracking. A more detailed # description of dealing with gaps in tracking can be found in the wiki. # Let us first deal with the easy case. # restart_events_df = get_restart_events(ts, time_query) ts = esta.TimeSeries.get_time_series(user_id) last_place_entry = esdp.get_last_place_entry(esda.RAW_PLACE_KEY, user_id) if last_place_entry is None: last_place = start_new_chain(user_id) last_place.source = segmentation_method_name last_place_entry = ecwe.Entry.create_entry(user_id, "segmentation/raw_place", last_place, create_id=True) else: last_place = last_place_entry.data # if is_easy_case(restart_events_df): # Theoretically, we can do some sanity checks here to make sure # that we are fairly close to the last point. Maybe mark some kind # of confidence level based on that? logging.debug("segmentation_point_list has length %s" % len(segmentation_points)) for (start_loc_doc, end_loc_doc) in segmentation_points: logging.debug("start_loc_doc = %s, end_loc_doc = %s" % (start_loc_doc, end_loc_doc)) get_loc_for_row = lambda row: ts.df_row_to_entry( "background/filtered_location", row).data start_loc = get_loc_for_row(start_loc_doc) end_loc = get_loc_for_row(end_loc_doc) logging.debug("start_loc = %s, end_loc = %s" % (start_loc, end_loc)) # Stitch together the last place and the current trip curr_trip = ecwrt.Rawtrip() curr_trip.source = segmentation_method_name curr_trip_entry = ecwe.Entry.create_entry(user_id, "segmentation/raw_trip", curr_trip, create_id=True) new_place = ecwrp.Rawplace() new_place.source = segmentation_method_name new_place_entry = ecwe.Entry.create_entry(user_id, "segmentation/raw_place", new_place, create_id=True) if found_untracked_period(ts, last_place_entry.data, start_loc): # Fill in the gap in the chain with an untracked period curr_untracked = ecwut.Untrackedtime() curr_untracked.source = segmentation_method_name curr_untracked_entry = ecwe.Entry.create_entry( user_id, "segmentation/raw_untracked", curr_untracked, create_id=True) restarted_place = ecwrp.Rawplace() restarted_place.source = segmentation_method_name restarted_place_entry = ecwe.Entry.create_entry( user_id, "segmentation/raw_place", restarted_place, create_id=True) untracked_start_loc = ecwe.Entry( ts.get_entry_at_ts("background/filtered_location", "data.ts", last_place_entry.data.enter_ts)).data untracked_start_loc[ "ts"] = untracked_start_loc.ts + epq.END_FUZZ_AVOID_LTE _link_and_save(ts, last_place_entry, curr_untracked_entry, restarted_place_entry, untracked_start_loc, start_loc) logging.debug("Created untracked period %s from %s to %s" % (curr_untracked_entry.get_id(), curr_untracked_entry.data.start_ts, curr_untracked_entry.data.end_ts)) logging.debug("Resetting last_place_entry from %s to %s" % (last_place_entry, restarted_place_entry)) last_place_entry = restarted_place_entry _link_and_save(ts, last_place_entry, curr_trip_entry, new_place_entry, start_loc, end_loc) last_place_entry = new_place_entry # The last last_place hasn't been stitched together yet, but we # need to save it so that it can be the last_place for the next run ts.insert(last_place_entry)