def grouped_to_summary(time_grouped_df, key_to_fill_fn, summary_fn): ret_list = [] # When we group by a time range, the key is the end of the range for key, section_group_df in time_grouped_df: curr_msts = ecwms.ModeStatTimeSummary() key_to_fill_fn(key, section_group_df, curr_msts) curr_msts.nUsers = len(section_group_df.user_id.unique()) mode_grouped_df = section_group_df.groupby('sensed_mode') mode_results = summary_fn(mode_grouped_df) for mode, result in mode_results.items(): if eac.get_section_key_for_analysis_results() == "analysis/inferred_section": curr_msts[ecwmp.PredictedModeTypes(mode).name] = result else: curr_msts[ecwm.MotionTypes(mode).name] = result ret_list.append(curr_msts) return ret_list
def convertPredictedProbToMap(self, uniqueModes, predictedProbArr): currProbMap = {} uniqueModesInt = [int(um) for um in uniqueModes] logging.debug("predictedProbArr has %s non-zero elements" % np.count_nonzero(predictedProbArr)) logging.debug("uniqueModes are %s " % uniqueModesInt) logging.debug("predictedProbArr = %s" % predictedProbArr) for (j, oldMode) in enumerate(uniqueModesInt): if predictedProbArr[j] != 0: uniqueMode = self.seed_modes_mapping[str(oldMode)] modeName = ecwm.PredictedModeTypes(uniqueMode).name logging.debug("Setting probability of mode %s (%s) to %s" % (uniqueMode, modeName, predictedProbArr[j])) currProbMap[modeName] = predictedProbArr[j] # logging.debug("after setting value = %s" % currProbMap[modeName]) # logging.debug("after setting map = %s" % currProbMap) # logging.debug("Returning map %s" % currProbMap) return currProbMap
def select_inferred_mode(prediction_list): # We currently only support a single prediction assert(len(prediction_list) == 1) curr_prediction = prediction_list[0] assert(curr_prediction.algorithm_id == ecwm.AlgorithmTypes.SEED_RANDOM_FOREST) prediction_map = curr_prediction["predicted_mode_map"] max_value = max(prediction_map.values()) logging.debug("max confidence in prediction map = %s" % max_value) keys_for_max_value = [k for (k, v) in prediction_map.items() if v == max_value] logging.debug("max keys in prediction map = %s" % keys_for_max_value) if len(keys_for_max_value) == 1: return keys_for_max_value[0] else: classes_for_max_value = [ecwm.PredictedModeTypes[key].value for key in keys_for_max_value] logging.debug("classes for max_value = %s" % classes_for_max_value) min_class = min(classes_for_max_value) logging.debug("min_class = %s" % min_class) return ecwm.PredictedModeTypes(min_class).name
def grouped_to_summary(time_grouped_df, key_to_fill_fn, summary_fn): ret_list = [] # When we group by a time range, the key is the end of the range for key, section_group_df in time_grouped_df: curr_msts = ecwms.ModeStatTimeSummary() key = fix_int64_key_if_needed(key) key_to_fill_fn(key, section_group_df, curr_msts) curr_msts.nUsers = len(section_group_df.user_id.unique()) mode_grouped_df = section_group_df.groupby('sensed_mode') mode_results = summary_fn(mode_grouped_df) for mode, result in mode_results.items(): if eac.get_section_key_for_analysis_results() == "analysis/inferred_section": curr_msts[ecwmp.PredictedModeTypes(mode).name] = result else: curr_msts[ecwm.MotionTypes(mode).name] = result ret_list.append(curr_msts) # import bson.json_util as bju # logging.debug("After appending %s, ret_list = %s" % (curr_msts, ret_list)) # for k in curr_msts.keys(): # print("Serializing key = %s" % k) # logging.debug("Serializing key %s = %s" % # (k, bju.dumps(curr_msts[k]))) return ret_list