def savePredictionsStep(self): for i, currProb in enumerate(self.predictedProb): currSectionEntry = self.toPredictSections[i] currSection = currSectionEntry.data currProb = currProb # Insert the prediction mp = ecwm.Modeprediction() mp.trip_id = currSection.trip_id mp.section_id = currSectionEntry.get_id() mp.algorithm_id = ecwm.AlgorithmTypes.SIMPLE_RULE_ENGINE mp.predicted_mode_map = currProb mp.start_ts = currSection.start_ts mp.end_ts = currSection.end_ts self.ts.insert_data(self.user_id, "inference/prediction", mp) # There are now two predictions, but don't want to do a bunch of # refactoring, so just create the inferred # section object right here is_dict = copy.copy(currSectionEntry) del is_dict["_id"] is_dict["metadata"]["key"] = "analysis/inferred_section" is_dict["data"]["sensed_mode"] = ecwm.PredictedModeTypes[ easf.select_inferred_mode([mp])].value is_dict["data"]["cleaned_section"] = currSectionEntry.get_id() ise = ecwe.Entry(is_dict) logging.debug("Updating sensed mode for section = %s to %s" % (currSectionEntry.get_id(), ise.data.sensed_mode)) self.ts.insert(ise) # Set last_section_done after saving because otherwise if there is an error # during inference, we will not save results and never re-run self.last_section_done = self.toPredictSections[-1]
def testSelectInferredModeImbalanced(self): # dummy_inferred_mode = dim dim = ecwm.Modeprediction() dim.trip_id = "test_trip_id" dim.section_id = "test_section_id" dim.algorithm_id = ecwm.AlgorithmTypes.SEED_RANDOM_FOREST dim.sensed_mode = ecwma.MotionTypes.WALKING dim.predicted_mode_map = {"WALKING": 0.9, "BUS": 0.1} dim.start_ts = 0 dim.end_ts = 0 self.assertEqual(fc.select_inferred_mode([dim]), 'WALKING')
def testSelectInferredModeEqual(self): # dummy_inferred_mode = dim # select the lower value mode, typically = lower carbon intensity mode dim = ecwm.Modeprediction() dim.trip_id = "test_trip_id" dim.section_id = "test_section_id" dim.algorithm_id = ecwm.AlgorithmTypes.SEED_RANDOM_FOREST dim.sensed_mode = ecwma.MotionTypes.WALKING dim.predicted_mode_map = {"WALKING": 0.5, "BUS": 0.5} dim.start_ts = 0 dim.end_ts = 0 self.assertEqual(fc.select_inferred_mode([dim]), 'WALKING')
def savePredictionsStep(self): from emission.core.wrapper.user import User from emission.core.wrapper.client import Client uniqueModes = self.model.classes_ for i in range(self.predictedProb.shape[0]): currSectionEntry = self.toPredictSections[i] currSection = currSectionEntry.data currProb = self.convertPredictedProbToMap(uniqueModes, self.predictedProb[i]) # Special handling for the AIR mode # AIR is not a mode that is sensed from the phone, but it is inferred # through some heuristics in cleanAndResample instead of through the # decision tree. Ideally those heurstics should be replaced by the # inference through the decision tree, or through a separate heuristic # step. But we are out of time for a bigger refactor here. # so we say that if the sensed mode == AIR, we are going to use it # directly and ignore the inferred mode if currSection.sensed_mode == ecwma.MotionTypes.AIR_OR_HSR: currProb = {'AIR_OR_HSR': 1.0} # Insert the prediction mp = ecwm.Modeprediction() mp.trip_id = currSection.trip_id mp.section_id = currSectionEntry.get_id() mp.algorithm_id = ecwm.AlgorithmTypes.SEED_RANDOM_FOREST mp.predicted_mode_map = currProb mp.start_ts = currSection.start_ts mp.end_ts = currSection.end_ts self.ts.insert_data(self.user_id, "inference/prediction", mp) # Since there is currently only one prediction, create the inferred # section object right here is_dict = copy.copy(currSectionEntry) del is_dict["_id"] is_dict["metadata"]["key"] = "analysis/inferred_section" is_dict["data"]["sensed_mode"] = ecwm.PredictedModeTypes[ easf.select_inferred_mode([mp])].value is_dict["data"]["cleaned_section"] = currSectionEntry.get_id() ise = ecwe.Entry(is_dict) logging.debug("Updating sensed mode for section = %s to %s" % (currSectionEntry.get_id(), ise.data.sensed_mode)) self.ts.insert(ise) # Set last_section_done after saving because otherwise if there is an error # during inference, we will not save results and never re-run self.last_section_done = self.toPredictSections[-1]
def savePredictionsStep(self): from emission.core.wrapper.user import User from emission.core.wrapper.client import Client uniqueModes = self.model.classes_ for i in range(self.predictedProb.shape[0]): currSectionEntry = self.toPredictSections[i] currSection = currSectionEntry.data currProb = self.convertPredictedProbToMap(uniqueModes, self.predictedProb[i]) # Special handling for the AIR mode # AIR is not a mode that is sensed from the phone, but it is inferred # through some heuristics in cleanAndResample instead of through the # decision tree. Ideally those heurstics should be replaced by the # inference through the decision tree, or through a separate heuristic # step. But we are out of time for a bigger refactor here. # so we say that if the sensed mode == AIR, we are going to use it # directly and ignore the inferred mode if currSection.sensed_mode == ecwma.MotionTypes.AIR_OR_HSR: currProb = {'AIR_OR_HSR': 1.0} # Insert the prediction mp = ecwm.Modeprediction() mp.trip_id = currSection.trip_id mp.section_id = currSectionEntry.get_id() mp.algorithm_id = ecwm.AlgorithmTypes.SEED_RANDOM_FOREST mp.predicted_mode_map = currProb mp.start_ts = currSection.start_ts mp.end_ts = currSection.end_ts self.ts.insert_data(self.user_id, "inference/prediction", mp) # Since there is currently only one prediction, create the inferred # section object right here is_dict = copy.copy(currSectionEntry) del is_dict["_id"] is_dict["metadata"]["key"] = "analysis/inferred_section" is_dict["data"]["sensed_mode"] = ecwm.PredictedModeTypes[easf.select_inferred_mode([mp])].value is_dict["data"]["cleaned_section"] = currSectionEntry.get_id() ise = ecwe.Entry(is_dict) logging.debug("Updating sensed mode for section = %s to %s" % (currSectionEntry.get_id(), ise.data.sensed_mode)) self.ts.insert(ise)