diff --git a/lib/inat_inferrer.py b/lib/inat_inferrer.py index dfcb608..afbefa5 100644 --- a/lib/inat_inferrer.py +++ b/lib/inat_inferrer.py @@ -272,6 +272,14 @@ def combine_results(self, raw_vision_scores, raw_geo_scores, filter_taxon, debug # multipliedby the normalized geo score leaf_scores["combined_score"] = leaf_scores["normalized_vision_score"] * \ leaf_scores["normalized_geo_score"] + + sum_of_root_node_aggregated_combined_scores = leaf_scores["combined_score"].sum() + if sum_of_root_node_aggregated_combined_scores > 0: + leaf_scores["normalized_combined_score"] = leaf_scores[ + "combined_score"] / sum_of_root_node_aggregated_combined_scores + else: + leaf_scores["normalized_combined_score"] = 0 + if debug: print("Score Combining Time: %0.2fms" % ((time.time() - start_time) * 1000.)) leaf_scores.reset_index(drop=True, inplace=True) diff --git a/lib/inat_vision_api_responses.py b/lib/inat_vision_api_responses.py index c06133e..5cf292c 100644 --- a/lib/inat_vision_api_responses.py +++ b/lib/inat_vision_api_responses.py @@ -32,7 +32,7 @@ def object_response(leaf_scores, inferrer): common_ancestor = inferrer.common_ancestor_from_leaf_scores(leaf_scores, debug=True) if common_ancestor is not None: common_ancestor_frame = pd.DataFrame([common_ancestor]) - common_ancestor_frame = InatVisionAPIResponses.update_aggregated_scores_scaling( + common_ancestor_frame = InatVisionAPIResponses.update_common_ancestor_scores_scaling( common_ancestor_frame ) common_ancestor = InatVisionAPIResponses.array_response_common_ancestor_columns( @@ -135,7 +135,7 @@ def limit_leaf_scores_for_response(leaf_scores): @staticmethod def update_leaf_scores_scaling(leaf_scores): score_columns = [ - "combined_score", + "normalized_combined_score", "geo_score", "normalized_vision_score", "geo_threshold" @@ -145,6 +145,16 @@ def update_leaf_scores_scaling(leaf_scores): ].multiply(100, axis="index") return leaf_scores + @staticmethod + def update_common_ancestor_scores_scaling(aggregated_scores): + score_columns = [ + "normalized_aggregated_combined_score" + ] + aggregated_scores[score_columns] = aggregated_scores[ + score_columns + ].multiply(100, axis="index") + return aggregated_scores + @staticmethod def update_aggregated_scores_scaling(aggregated_scores): score_columns = [ @@ -162,7 +172,7 @@ def update_aggregated_scores_scaling(aggregated_scores): @staticmethod def array_response_columns(leaf_scores): columns_to_return = [ - "combined_score", + "normalized_combined_score", "geo_score", "taxon_id", "name", @@ -171,25 +181,26 @@ def array_response_columns(leaf_scores): ] column_mapping = { "taxon_id": "id", - "normalized_vision_score": "vision_score" + "normalized_vision_score": "vision_score", + "normalized_combined_score": "combined_score" } return leaf_scores[columns_to_return].rename(columns=column_mapping) @staticmethod def array_response_common_ancestor_columns(common_ancestor_dataframe): columns_to_return = [ - "aggregated_combined_score", + "normalized_aggregated_combined_score", "aggregated_geo_score", "taxon_id", "name", - "normalized_aggregated_vision_score", + "aggregated_vision_score", "aggregated_geo_threshold" ] column_mapping = { - "aggregated_combined_score": "combined_score", + "normalized_aggregated_combined_score": "combined_score", "aggregated_geo_score": "geo_score", "taxon_id": "id", - "normalized_aggregated_vision_score": "vision_score", + "aggregated_vision_score": "vision_score", "aggregated_geo_threshold": "geo_threshold" } return common_ancestor_dataframe[columns_to_return].rename(columns=column_mapping)