File size: 1,538 Bytes
28c256d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from typing import Any, Dict


def get_metric_value(indicator: str, metrics: Dict) -> Any:
    """Get the metric value specified by an indicator, which can be either a
    metric name or a full name with evaluator prefix.

    Args:
        indicator (str): The metric indicator, which can be the metric name
            (e.g. 'AP') or the full name with prefix (e.g. 'COCO/AP')
        metrics (dict): The evaluation results output by the evaluator

    Returns:
        Any: The specified metric value
    """

    if '/' in indicator:
        # The indicator is a full name
        if indicator in metrics:
            return metrics[indicator]
        else:
            raise ValueError(
                f'The indicator "{indicator}" can not match any metric in '
                f'{list(metrics.keys())}')
    else:
        # The indicator is metric name without prefix
        matched = [k for k in metrics.keys() if k.split('/')[-1] == indicator]

        if not matched:
            raise ValueError(
                f'The indicator {indicator} can not match any metric in '
                f'{list(metrics.keys())}')
        elif len(matched) > 1:
            raise ValueError(f'The indicator "{indicator}" matches multiple '
                             f'metrics {matched}')
        else:
            return metrics[matched[0]]