summary_op.py 4.2 KB
Newer Older
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

"""A module for result summary ops."""

from typing import Dict, Callable
import numbers

from superbench.benchmarks.context import Enum
from superbench.common.utils import logger


class SummaryType(Enum):
    """The Enum class representing different summary ops."""

    MEAN = 'mean'
    PENCENTILE = 'percentile'
    MIN = 'min'
    MAX = 'max'
    STD = 'std'
    COUNT = 'count'


class SummaryOp:
    """SummaryOp class to maintain all summary functions."""

    functions: Dict[SummaryType, Callable] = dict()

    @classmethod
    def add_summary_func(cls, summary_type):
        """Add summary fuction.

        Args:
            summary_type (SummaryType): The type of summary function.

        Return:
            decorator (Callable): return the decorator to add the summary function.
        """
        def decorator(func):
            cls.functions[summary_type] = func
            return func

        return decorator

    @classmethod
    def get_summary_func(cls, summary_type):
        """Get summary fuction by summary_type.

        Args:
            summary_type (SummaryType): The type of summary function.

        Return:
            func (Callable): summary function, None means invalid summary type.
        """
        if summary_type in cls.functions:
            return cls.functions[summary_type]

        return None

    @staticmethod
    def _check_raw_data_df(raw_data_df):
        """Check whether raw_data_df is empty or None.

        Args:
            raw_data_df (DataFrame): raw data df
        """
        if raw_data_df is None or raw_data_df.empty:
            logger.log_and_raise(exception=Exception, msg='empty data in summary op')

    @staticmethod
    def mean(raw_data_df):
        """Mean of raw_data_df.

        Args:
            raw_data_df (DataFrame): raw data df

        Returns:
            Series: mean of raw_data_df
        """
        SummaryOp._check_raw_data_df(raw_data_df)
        return raw_data_df.mean()

    @staticmethod
    def percentile(raw_data_df, val):
        """Pencentile$(val) of raw_data_df.

        Args:
            raw_data_df (DataFrame): raw data df
            val (numbers.Number): the pencentile value, 1-99

        Returns:
            Series: mean of raw_data_df
        """
        SummaryOp._check_raw_data_df(raw_data_df)
        if not isinstance(val, numbers.Number) or val < 1 or val > 99:
            logger.log_and_raise(exception=Exception, msg='val in pencentile should be 1-99')
        return raw_data_df.quantile(val / 100)

    @staticmethod
    def min(raw_data_df):
        """The min of values for each column in raw_data_df.

        Args:
            raw_data_df (DataFrame): raw data df

        Returns:
            Series: min of raw_data_df
        """
        SummaryOp._check_raw_data_df(raw_data_df)
        return raw_data_df.min()

    @staticmethod
    def max(raw_data_df):
        """The max of values for each column in raw_data_df.

        Args:
            raw_data_df (DataFrame): raw data df

        Returns:
            Series: max of raw_data_df
        """
        SummaryOp._check_raw_data_df(raw_data_df)
        return raw_data_df.max()

    @staticmethod
    def std(raw_data_df):
        """The std of values for each column in raw_data_df.

        Args:
            raw_data_df (DataFrame): raw data df

        Returns:
            Series: std of raw_data_df
        """
        SummaryOp._check_raw_data_df(raw_data_df)
        return raw_data_df.std(axis=0, skipna=True)

    @staticmethod
    def count(raw_data_df):
        """The number of values for each column in raw_data_df.

        Args:
            raw_data_df (DataFrame): raw data df

        Returns:
            Series: count of raw_data_df
        """
        SummaryOp._check_raw_data_df(raw_data_df)
        return raw_data_df.count()


SummaryOp.add_summary_func(SummaryType.MEAN)(SummaryOp.mean)
SummaryOp.add_summary_func(SummaryType.PENCENTILE)(SummaryOp.percentile)
SummaryOp.add_summary_func(SummaryType.MIN)(SummaryOp.min)
SummaryOp.add_summary_func(SummaryType.MAX)(SummaryOp.max)
SummaryOp.add_summary_func(SummaryType.STD)(SummaryOp.std)
SummaryOp.add_summary_func(SummaryType.COUNT)(SummaryOp.count)