def turn_into_new_trip(self, user_id): print "new trip" trip = ecsdtq.create_new_trip(user_id) 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 = otp_time_to_ours(our_json['plan']['itineraries'][0]["startTime"]) trip.end_local_dt = otp_time_to_ours(our_json['plan']['itineraries'][0]["endTime"]) ecsdtq.save_trip(trip) for leg in our_json["plan"]["itineraries"][0]['legs']: section = ecsdsq.create_new_section(user_id, trip["_id"]) section.start_local_dt = otp_time_to_ours(leg["startTime"]) section.end_local_dt = otp_time_to_ours(leg["endTime"]) 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"])) ) ecsdsq.save_section(section)
def testQuerySectionsForTrip(self): new_trip = self.create_fake_trip() new_section = esds.create_new_section(self.testUserId, new_trip.get_id()) new_section.start_ts = 5 new_section.end_ts = 6 esds.save_section(new_section) ret_sections = esdt.get_sections_for_trip(self.testUserId, new_trip.get_id()) self.assertEqual(ret_sections, [new_section])
def testSaveSection(self): new_section = esds.create_new_section(self.testUserId, self.test_trip_id) new_section.start_ts = 5 new_section.end_ts = 6 esds.save_section(new_section) self.assertEqual(edb.get_section_new_db().find({"end_ts": 6}).count(), 1) self.assertEqual(edb.get_section_new_db().find_one({"end_ts": 6})["_id"], new_section.get_id()) self.assertEqual(edb.get_section_new_db().find_one({"end_ts": 6})["user_id"], self.testUserId) self.assertEqual(edb.get_section_new_db().find_one({"end_ts": 6})["trip_id"], self.test_trip_id)
def segment_trip_into_sections(user_id, trip_id): ts = esta.TimeSeries.get_time_series(user_id) trip = esdt.get_trip(trip_id) time_query = esdt.get_time_query_for_trip(trip_id) import emission.analysis.intake.segmentation.section_segmentation_methods.smoothed_high_confidence_motion as shcm shcmsm = shcm.SmoothedHighConfidenceMotion(60, [ecwm.MotionTypes.TILTING, ecwm.MotionTypes.UNKNOWN, ecwm.MotionTypes.STILL, ecwm.MotionTypes.NONE, # iOS only ecwm.MotionTypes.STOPPED_WHILE_IN_VEHICLE]) # iOS only segmentation_points = shcmsm.segment_into_sections(ts, time_query) # Since we are segmenting an existing trip into sections, we do not need to worry about linking with # a prior place, since it will be linked through the trip object. # So this is much simpler than the trip case. # Again, since this is segmenting a trip, we can just start with a section prev_section = None # TODO: Should we link the locations to the trips this way, or by using a foreign key? # If we want to use a foreign key, then we need to include the object id in the data df as well so that we can # set it properly. trip_start_loc = ecwl.Location(ts.get_entry_at_ts("background/filtered_location", "data.ts", trip.start_ts)["data"]) trip_end_loc = ecwl.Location(ts.get_entry_at_ts("background/filtered_location", "data.ts", trip.end_ts)["data"]) logging.debug("trip_start_loc = %s, trip_end_loc = %s" % (trip_start_loc, trip_end_loc)) for (i, (start_loc_doc, end_loc_doc, sensed_mode)) in enumerate(segmentation_points): logging.debug("start_loc_doc = %s, end_loc_doc = %s" % (start_loc_doc, end_loc_doc)) start_loc = ecwl.Location(start_loc_doc) end_loc = ecwl.Location(end_loc_doc) logging.debug("start_loc = %s, end_loc = %s" % (start_loc, end_loc)) section = esds.create_new_section(user_id, trip_id) if prev_section is None: # This is the first point, so we want to start from the start of the trip, not the start of this segment start_loc = trip_start_loc if i == len(segmentation_points) - 1: # This is the last point, so we want to end at the end of the trip, not at the end of this segment # Particularly in this case, if we don't do this, then the trip end may overshoot the section end end_loc = trip_end_loc fill_section(section, start_loc, end_loc, sensed_mode) if prev_section is not None: # If this is not the first section, create a stop to link the two sections together # The expectation is prev_section -> stop -> curr_section stop = esdst.create_new_stop(user_id, trip_id) stitch_together(prev_section, stop, section) esdst.save_stop(stop) esds.save_section(prev_section) # Because we have now linked it to the stop, we need to save it again esds.save_section(section) prev_section = section
def testQuerySections(self): new_section = esds.create_new_section(self.testUserId, self.test_trip_id) new_section.start_ts = 5 new_section.end_ts = 6 esds.save_section(new_section) ret_arr_one = esds.get_sections_for_trip(self.testUserId, self.test_trip_id) self.assertEqual(len(ret_arr_one), 1) self.assertEqual(ret_arr_one, [new_section]) ret_arr_list = esds.get_sections_for_trip_list(self.testUserId, [self.test_trip_id]) self.assertEqual(ret_arr_one, ret_arr_list) ret_arr_time = esds.get_sections(self.testUserId, enua.UserCache.TimeQuery("start_ts", 4, 6)) self.assertEqual(ret_arr_list, ret_arr_time)
def testQuerySections(self): new_section = esds.create_new_section(self.testUserId, self.test_trip_id) new_section.start_ts = 5 new_section.end_ts = 6 esds.save_section(new_section) ret_arr_one = esds.get_sections_for_trip(self.testUserId, self.test_trip_id) self.assertEqual(len(ret_arr_one), 1) self.assertEqual(ret_arr_one, [new_section]) ret_arr_list = esds.get_sections_for_trip_list(self.testUserId, [self.test_trip_id]) self.assertEqual(ret_arr_one, ret_arr_list) ret_arr_time = esds.get_sections( self.testUserId, enua.UserCache.TimeQuery("start_ts", 4, 6)) self.assertEqual(ret_arr_list, ret_arr_time)
def testSaveSection(self): new_section = esds.create_new_section(self.testUserId, self.test_trip_id) new_section.start_ts = 5 new_section.end_ts = 6 esds.save_section(new_section) self.assertEqual(edb.get_section_new_db().find({ "end_ts": 6 }).count(), 1) self.assertEqual( edb.get_section_new_db().find_one({"end_ts": 6})["_id"], new_section.get_id()) self.assertEqual( edb.get_section_new_db().find_one({"end_ts": 6})["user_id"], self.testUserId) self.assertEqual( edb.get_section_new_db().find_one({"end_ts": 6})["trip_id"], self.test_trip_id)
def segment_trip_into_sections(user_id, trip_id, trip_source): ts = esta.TimeSeries.get_time_series(user_id) trip = esdt.get_trip(trip_id) time_query = esdt.get_time_query_for_trip(trip_id) if (trip_source == "DwellSegmentationTimeFilter"): import emission.analysis.intake.segmentation.section_segmentation_methods.smoothed_high_confidence_motion as shcm shcmsm = shcm.SmoothedHighConfidenceMotion(60, [ ecwm.MotionTypes.TILTING, ecwm.MotionTypes.UNKNOWN, ecwm.MotionTypes.STILL ]) else: assert (trip_source == "DwellSegmentationDistFilter") import emission.analysis.intake.segmentation.section_segmentation_methods.smoothed_high_confidence_with_visit_transitions as shcmvt shcmsm = shcmvt.SmoothedHighConfidenceMotionWithVisitTransitions( 49, [ ecwm.MotionTypes.TILTING, ecwm.MotionTypes.UNKNOWN, ecwm.MotionTypes.STILL, ecwm.MotionTypes.NONE, # iOS only ecwm.MotionTypes.STOPPED_WHILE_IN_VEHICLE ]) # iOS only segmentation_points = shcmsm.segment_into_sections(ts, time_query) # Since we are segmenting an existing trip into sections, we do not need to worry about linking with # a prior place, since it will be linked through the trip object. # So this is much simpler than the trip case. # Again, since this is segmenting a trip, we can just start with a section prev_section = None # TODO: Should we link the locations to the trips this way, or by using a foreign key? # If we want to use a foreign key, then we need to include the object id in the data df as well so that we can # set it properly. trip_start_loc = ecwl.Location( ts.get_entry_at_ts("background/filtered_location", "data.ts", trip.start_ts)["data"]) trip_end_loc = ecwl.Location( ts.get_entry_at_ts("background/filtered_location", "data.ts", trip.end_ts)["data"]) logging.debug("trip_start_loc = %s, trip_end_loc = %s" % (trip_start_loc, trip_end_loc)) for (i, (start_loc_doc, end_loc_doc, sensed_mode)) in enumerate(segmentation_points): logging.debug("start_loc_doc = %s, end_loc_doc = %s" % (start_loc_doc, end_loc_doc)) start_loc = ecwl.Location(start_loc_doc) end_loc = ecwl.Location(end_loc_doc) logging.debug("start_loc = %s, end_loc = %s" % (start_loc, end_loc)) section = esds.create_new_section(user_id, trip_id) if prev_section is None: # This is the first point, so we want to start from the start of the trip, not the start of this segment start_loc = trip_start_loc if i == len(segmentation_points) - 1: # This is the last point, so we want to end at the end of the trip, not at the end of this segment # Particularly in this case, if we don't do this, then the trip end may overshoot the section end end_loc = trip_end_loc fill_section(section, start_loc, end_loc, sensed_mode) if prev_section is not None: # If this is not the first section, create a stop to link the two sections together # The expectation is prev_section -> stop -> curr_section stop = esdst.create_new_stop(user_id, trip_id) stitch_together(prev_section, stop, section) esdst.save_stop(stop) esds.save_section( prev_section ) # Because we have now linked it to the stop, we need to save it again esds.save_section(section) prev_section = section
def testCreateNew(self): new_section = esds.create_new_section(self.testUserId, self.test_trip_id) self.assertIsNotNone(new_section.get_id()) self.assertEqual(new_section.user_id, self.testUserId) self.assertEqual(new_section.trip_id, self.test_trip_id)