from typing import Mapping
from typing import Union
from typing import Any
import math


def find_mAP(per_cat_metrics: Mapping[Union[str, int], Mapping[str, Any]]
             ) -> float:
    """
    Mean average precision, the mean of the average precision for each category

    Args:
        per_cat_metrics: dict, result of compute_precision_recall()

    Returns: float, mAP for this set of detection results
    """
    # minus the 'one_class' set of metrics
    num_gt_classes = len(per_cat_metrics) - 1

    mAP_from_cats = sum(
        v['average_precision']
        if k != 'one_class' and not math.isnan(v['average_precision']) else 0
        for k, v in per_cat_metrics.items()
    ) / num_gt_classes

    return mAP_from_cats