def testSegmentationPointsSmoothedHighConfidenceMotion(self): ts = esta.TimeSeries.get_time_series(self.androidUUID) tq = estt.TimeQuery("metadata.write_ts", 1440695152.989, 1440699266.669) shcmsm = shcm.SmoothedHighConfidenceMotion(60, 100, [ecwm.MotionTypes.TILTING, ecwm.MotionTypes.UNKNOWN, ecwm.MotionTypes.STILL]) segmentation_points = shcmsm.segment_into_sections(ts, 0, tq) for (start, end, motion) in segmentation_points: logging.info("section is from %s (%f) -> %s (%f) using mode %s" % (start.fmt_time, start.ts, end.fmt_time, end.ts, motion)) self.assertIsNotNone(segmentation_points) self.assertEqual(len(segmentation_points), 2) self.assertEqual([start.ts for (start, end, motion) in segmentation_points], [1440695873.453, 1440698306.892]) self.assertEqual([end.ts for (start, end, motion) in segmentation_points], [1440698066.704, 1440699234.834])
def segment_trip_into_sections(user_id, trip_entry, trip_source): ts = esta.TimeSeries.get_time_series(user_id) time_query = esda.get_time_query_for_trip_like(esda.RAW_TRIP_KEY, trip_entry.get_id()) distance_from_place = _get_distance_from_start_place_to_end(trip_entry) if (trip_source == "DwellSegmentationTimeFilter"): import emission.analysis.intake.segmentation.section_segmentation_methods.smoothed_high_confidence_motion as shcm shcmsm = shcm.SmoothedHighConfidenceMotion(60, 100, [ 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, 50, [ 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, distance_from_place, 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_entry = 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. ts = esta.TimeSeries.get_time_series(user_id) get_loc_for_ts = lambda time: ecwl.Location( ts.get_entry_at_ts("background/filtered_location", "data.ts", time)[ "data"]) trip_start_loc = get_loc_for_ts(trip_entry.data.start_ts) trip_end_loc = get_loc_for_ts(trip_entry.data.end_ts) 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)) 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)) section = ecwc.Section() section.trip_id = trip_entry.get_id() if prev_section_entry 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) # We create the entry after filling in the section so that we know # that the data is included properly section_entry = ecwe.Entry.create_entry(user_id, esda.RAW_SECTION_KEY, section, create_id=True) if prev_section_entry 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 = ecws.Stop() stop.trip_id = trip_entry.get_id() stop_entry = ecwe.Entry.create_entry(user_id, esda.RAW_STOP_KEY, stop, create_id=True) logging.debug("stop = %s, stop_entry = %s" % (stop, stop_entry)) stitch_together(prev_section_entry, stop_entry, section_entry) ts.insert(stop_entry) ts.update(prev_section_entry) # After we go through the loop, we will be left with the last section, # which does not have an ending stop. We insert that too. ts.insert(section_entry) prev_section_entry = section_entry
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