def testAirOverrideHack(self):
    self.testPredictedProb()
    self.pipeline.toPredictSections[1]["data"]["sensed_mode"] = ecwma.MotionTypes.AIR_OR_HSR.value
    self.pipeline.toPredictSections[3]["data"]["sensed_mode"] = ecwma.MotionTypes.AIR_OR_HSR.value
    self.pipeline.savePredictionsStep()

    for i, section in enumerate(self.pipeline.toPredictSections):
        predicted_mode = esds.get_inferred_mode_entry(self.testUUID, section.get_id())
        self.assertIsNotNone(predicted_mode)

        ise = esds.cleaned2inferred_section(self.testUUID, section.get_id())
        self.assertIsNotNone(ise)

        if i == 0:
            self.assertEqual(predicted_mode.data["predicted_mode_map"],
                {'WALKING': 0.7, 'BUS': 0.3})
            self.assertEqual(ise.data.sensed_mode, ecwm.PredictedModeTypes.WALKING)

        if i == 1:
            self.assertEqual(predicted_mode.data["predicted_mode_map"],
                {'AIR_OR_HSR': 1.0})
            self.assertEqual(ise.data.sensed_mode, ecwm.PredictedModeTypes.AIR_OR_HSR)

        if i == 2:
            self.assertEqual(predicted_mode.data["predicted_mode_map"],
                {'WALKING': 0.2, 'BUS': 0.8})
            self.assertEqual(ise.data.sensed_mode, ecwm.PredictedModeTypes.BUS)

        if i == 3:
            self.assertEqual(predicted_mode.data["predicted_mode_map"],
                {'AIR_OR_HSR': 1.0})
            self.assertEqual(ise.data.sensed_mode, ecwm.PredictedModeTypes.AIR_OR_HSR)
Beispiel #2
0
    def testAirOverrideHack(self):
        self.testPredictedProb()
        self.pipeline.toPredictSections[1]["data"][
            "sensed_mode"] = ecwma.MotionTypes.AIR_OR_HSR.value
        self.pipeline.toPredictSections[3]["data"][
            "sensed_mode"] = ecwma.MotionTypes.AIR_OR_HSR.value
        self.pipeline.savePredictionsStep()

        for i, section in enumerate(self.pipeline.toPredictSections):
            predicted_mode = esds.get_inferred_mode_entry(
                self.testUUID, section.get_id())
            self.assertIsNotNone(predicted_mode)

            ise = esds.cleaned2inferred_section(self.testUUID,
                                                section.get_id())
            self.assertIsNotNone(ise)

            if i == 0:
                self.assertEqual(predicted_mode.data["predicted_mode_map"], {
                    'WALKING': 0.7,
                    'BUS': 0.3
                })
                self.assertEqual(ise.data.sensed_mode,
                                 ecwm.PredictedModeTypes.WALKING)

            if i == 1:
                self.assertEqual(predicted_mode.data["predicted_mode_map"],
                                 {'AIR_OR_HSR': 1.0})
                self.assertEqual(ise.data.sensed_mode,
                                 ecwm.PredictedModeTypes.AIR_OR_HSR)

            if i == 2:
                self.assertEqual(predicted_mode.data["predicted_mode_map"], {
                    'WALKING': 0.2,
                    'BUS': 0.8
                })
                self.assertEqual(ise.data.sensed_mode,
                                 ecwm.PredictedModeTypes.BUS)

            if i == 3:
                self.assertEqual(predicted_mode.data["predicted_mode_map"],
                                 {'AIR_OR_HSR': 1.0})
                self.assertEqual(ise.data.sensed_mode,
                                 ecwm.PredictedModeTypes.AIR_OR_HSR)
def section_to_geojson(section, tl):
    """
    This is the trickiest part of the visualization.
    The section is basically a collection of points with a line through them.
    So the representation is a feature in which one feature which is the line, and one feature collection which is the set of point features.
    :param section: the section to be converted
    :return: a feature collection which is the geojson version of the section
    """

    ts = esta.TimeSeries.get_time_series(section.user_id)
    entry_it = ts.find_entries(["analysis/recreated_location"],
                               esda.get_time_query_for_trip_like(
                                   "analysis/cleaned_section",
                                   section.get_id()))

    # TODO: Decide whether we want to use Rewrite to use dataframes throughout instead of python arrays.
    # dataframes insert nans. We could use fillna to fill with default values, but if we are not actually
    # using dataframe features here, it is unclear how much that would help.
    feature_array = []
    section_location_entries = [ecwe.Entry(entry) for entry in entry_it]
    if len(section_location_entries) != 0:
        logging.debug("first element in section_location_array = %s" % section_location_entries[0])

        if not ecc.compare_rounded_arrays(section.data.end_loc.coordinates,
                                      section_location_entries[-1].data.loc.coordinates,
                                      digits=4):
            logging.info("section_location_array[-1].data.loc %s != section.data.end_loc %s even after df.ts fix, filling gap" % \
                    (section_location_entries[-1].data.loc, section.data.end_loc))
            assert(False)
            last_loc_doc = ts.get_entry_at_ts("background/filtered_location", "data.ts", section.data.end_ts)
            if last_loc_doc is None:
                logging.warning("can't find entry to patch gap, leaving gap")
            else:
                last_loc_entry = ecwe.Entry(last_loc_doc)
                logging.debug("Adding new entry %s to fill the end point gap between %s and %s"
                   % (last_loc_entry.data.loc, section_location_entries[-1].data.loc,
                        section.data.end_loc))
                section_location_entries.append(last_loc_entry)

    points_line_feature = point_array_to_line(section_location_entries)
    points_line_feature.id = str(section.get_id())
    points_line_feature.properties.update(copy.copy(section.data))
    # Update works on dicts, convert back to a section object to make the modes
    # work properly
    points_line_feature.properties = ecwcs.Cleanedsection(points_line_feature.properties)

    points_line_feature.properties["feature_type"] = "section"

    if eac.get_section_key_for_analysis_results() == esda.INFERRED_SECTION_KEY:
        ise = esds.cleaned2inferred_section(section.user_id, section.get_id())
        if ise is not None:
            logging.debug("mapped cleaned section %s -> inferred section %s" % 
                (section.get_id(), ise.get_id()))
            logging.debug("changing mode from %s -> %s" % 
                (points_line_feature.properties.sensed_mode, ise.data.sensed_mode))
            points_line_feature.properties["sensed_mode"] = str(ise.data.sensed_mode)
        else:
            points_line_feature.properties["sensed_mode"] = str(points_line_feature.properties.sensed_mode)
    else:
        points_line_feature.properties["sensed_mode"] = str(points_line_feature.properties.sensed_mode)
    
    _del_non_derializable(points_line_feature.properties, ["start_loc", "end_loc"])

    # feature_array.append(gj.FeatureCollection(points_feature_array))
    feature_array.append(points_line_feature)

    return gj.FeatureCollection(feature_array)
def section_to_geojson(section, tl):
    """
    This is the trickiest part of the visualization.
    The section is basically a collection of points with a line through them.
    So the representation is a feature in which one feature which is the line, and one feature collection which is the set of point features.
    :param section: the section to be converted
    :return: a feature collection which is the geojson version of the section
    """

    ts = esta.TimeSeries.get_time_series(section.user_id)
    entry_it = ts.find_entries(["analysis/recreated_location"],
                               esda.get_time_query_for_trip_like(
                                   "analysis/cleaned_section",
                                   section.get_id()))

    # TODO: Decide whether we want to use Rewrite to use dataframes throughout instead of python arrays.
    # dataframes insert nans. We could use fillna to fill with default values, but if we are not actually
    # using dataframe features here, it is unclear how much that would help.
    feature_array = []
    section_location_entries = [ecwe.Entry(entry) for entry in entry_it]
    if len(section_location_entries) != 0:
        logging.debug("first element in section_location_array = %s" %
                      section_location_entries[0])

        if not ecc.compare_rounded_arrays(
                section.data.end_loc.coordinates,
                section_location_entries[-1].data.loc.coordinates,
                digits=4):
            logging.info("section_location_array[-1].data.loc %s != section.data.end_loc %s even after df.ts fix, filling gap" % \
                    (section_location_entries[-1].data.loc, section.data.end_loc))
            assert (False)
            last_loc_doc = ts.get_entry_at_ts("background/filtered_location",
                                              "data.ts", section.data.end_ts)
            if last_loc_doc is None:
                logging.warning("can't find entry to patch gap, leaving gap")
            else:
                last_loc_entry = ecwe.Entry(last_loc_doc)
                logging.debug(
                    "Adding new entry %s to fill the end point gap between %s and %s"
                    % (last_loc_entry.data.loc,
                       section_location_entries[-1].data.loc,
                       section.data.end_loc))
                section_location_entries.append(last_loc_entry)

    points_line_feature = point_array_to_line(section_location_entries)
    points_line_feature.id = str(section.get_id())
    points_line_feature.properties.update(copy.copy(section.data))
    # Update works on dicts, convert back to a section object to make the modes
    # work properly
    points_line_feature.properties = ecwcs.Cleanedsection(
        points_line_feature.properties)

    points_line_feature.properties["feature_type"] = "section"

    if eac.get_section_key_for_analysis_results() == esda.INFERRED_SECTION_KEY:
        ise = esds.cleaned2inferred_section(section.user_id, section.get_id())
        if ise is not None:
            logging.debug("mapped cleaned section %s -> inferred section %s" %
                          (section.get_id(), ise.get_id()))
            logging.debug("changing mode from %s -> %s" %
                          (points_line_feature.properties.sensed_mode,
                           ise.data.sensed_mode))
            points_line_feature.properties["sensed_mode"] = str(
                ise.data.sensed_mode)
        else:
            points_line_feature.properties["sensed_mode"] = str(
                points_line_feature.properties.sensed_mode)
    else:
        points_line_feature.properties["sensed_mode"] = str(
            points_line_feature.properties.sensed_mode)

    _del_non_derializable(points_line_feature.properties,
                          ["start_loc", "end_loc"])

    # feature_array.append(gj.FeatureCollection(points_feature_array))
    feature_array.append(points_line_feature)

    return gj.FeatureCollection(feature_array)