Completed
Pull Request — master (#78)
by
unknown
01:17
created

Stats.hd15iqr()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
1
from __future__ import division
2
from __future__ import print_function
3
4
import operator
5
import statistics
6
from bisect import bisect_left
7
from bisect import bisect_right
8
9
from .utils import cached_property
10
from .utils import funcname
11
from .utils import get_cprofile_functions
12
13
14
class Stats(object):
15
    fields = (
16
        "min", "max", "mean", "stddev", "rounds", "median", "iqr", "q1", "q3", "iqr_outliers", "stddev_outliers",
17
        "outliers", "ld15iqr", "hd15iqr", "ops",
18
    )
19
20
    def __init__(self):
21
        self.data = []
22
23
    def __bool__(self):
24
        return bool(self.data)
25
26
    def __nonzero__(self):
27
        return bool(self.data)
28
29
    def as_dict(self):
30
        return dict(
31
            (field, getattr(self, field))
32
            for field in self.fields
33
        )
34
35
    def update(self, duration):
36
        self.data.append(duration)
37
38
    @cached_property
39
    def sorted_data(self):
40
        return sorted(self.data)
41
42
    @cached_property
43
    def total(self):
44
        return sum(self.data)
45
46
    @cached_property
47
    def min(self):
48
        return min(self.data)
49
50
    @cached_property
51
    def max(self):
52
        return max(self.data)
53
54
    @cached_property
55
    def mean(self):
56
        return statistics.mean(self.data)
57
58
    @cached_property
59
    def stddev(self):
60
        if len(self.data) > 1:
61
            return statistics.stdev(self.data)
62
        else:
63
            return 0
64
65
    @property
66
    def stddev_outliers(self):
67
        """
68
        Count of StdDev outliers: what's beyond (Mean - StdDev, Mean - StdDev)
69
        """
70
        count = 0
71
        q0 = self.mean - self.stddev
72
        q4 = self.mean + self.stddev
73
        for val in self.data:
74
            if val < q0 or val > q4:
75
                count += 1
76
        return count
77
78
    @cached_property
79
    def rounds(self):
80
        return len(self.data)
81
82
    @cached_property
83
    def median(self):
84
        return statistics.median(self.data)
85
86
    @cached_property
87
    def ld15iqr(self):
88
        """
89
        Tukey-style Lowest Datum within 1.5 IQR under Q1.
90
        """
91
        if len(self.data) == 1:
92
            return self.data[0]
93
        else:
94
            return self.sorted_data[bisect_left(self.sorted_data, self.q1 - 1.5 * self.iqr)]
95
96
    @cached_property
97
    def hd15iqr(self):
98
        """
99
        Tukey-style Highest Datum within 1.5 IQR over Q3.
100
        """
101
        if len(self.data) == 1:
102
            return self.data[0]
103
        else:
104
            pos = bisect_right(self.sorted_data, self.q3 + 1.5 * self.iqr)
105
            if pos == len(self.data):
106
                return self.sorted_data[-1]
107
            else:
108
                return self.sorted_data[pos]
109
110 View Code Duplication
    @cached_property
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
111
    def q1(self):
112
        rounds = self.rounds
113
        data = self.sorted_data
114
115
        # See: https://en.wikipedia.org/wiki/Quartile#Computing_methods
116
        if rounds == 1:
117
            return data[0]
118
        elif rounds % 2:  # Method 3
119
            n, q = rounds // 4, rounds % 4
120
            if q == 1:
121
                return 0.25 * data[n - 1] + 0.75 * data[n]
122
            else:
123
                return 0.75 * data[n] + 0.25 * data[n + 1]
124
        else:  # Method 2
125
            return statistics.median(data[:rounds // 2])
126
127 View Code Duplication
    @cached_property
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
128
    def q3(self):
129
        rounds = self.rounds
130
        data = self.sorted_data
131
132
        # See: https://en.wikipedia.org/wiki/Quartile#Computing_methods
133
        if rounds == 1:
134
            return data[0]
135
        elif rounds % 2:  # Method 3
136
            n, q = rounds // 4, rounds % 4
137
            if q == 1:
138
                return 0.75 * data[3 * n] + 0.25 * data[3 * n + 1]
139
            else:
140
                return 0.25 * data[3 * n + 1] + 0.75 * data[3 * n + 2]
141
        else:  # Method 2
142
            return statistics.median(data[rounds // 2:])
143
144
    @cached_property
145
    def iqr(self):
146
        return self.q3 - self.q1
147
148
    @property
149
    def iqr_outliers(self):
150
        """
151
        Count of Tukey outliers: what's beyond (Q1 - 1.5IQR, Q3 + 1.5IQR)
152
        """
153
        count = 0
154
        q0 = self.q1 - 1.5 * self.iqr
155
        q4 = self.q3 + 1.5 * self.iqr
156
        for val in self.data:
157
            if val < q0 or val > q4:
158
                count += 1
159
        return count
160
161
    @cached_property
162
    def outliers(self):
163
        return "%s;%s" % (self.stddev_outliers, self.iqr_outliers)
164
165
    @cached_property
166
    def ops(self):
167
        s = sum(self.data)
168
        if s:
169
            return len(self.data) / s
170
        return 0
171
172
173
class Metadata(object):
174
    def __init__(self, fixture, iterations, options):
175
        self.name = fixture.name
176
        self.fullname = fixture.fullname
177
        self.group = fixture.group
178
        self.param = fixture.param
179
        self.params = fixture.params
180
        self.extra_info = fixture.extra_info
181
        self.cprofile_stats = fixture.cprofile_stats
182
183
        self.iterations = iterations
184
        self.stats = Stats()
185
        self.options = options
186
        self.fixture = fixture
187
188
    def __bool__(self):
189
        return bool(self.stats)
190
191
    def __nonzero__(self):
192
        return bool(self.stats)
193
194
    def get(self, key, default=None):
195
        try:
196
            return getattr(self.stats, key)
197
        except AttributeError:
198
            return getattr(self, key, default)
199
200
    def __getitem__(self, key):
201
        try:
202
            return getattr(self.stats, key)
203
        except AttributeError:
204
            return getattr(self, key)
205
206
    @property
207
    def has_error(self):
208
        return self.fixture.has_error
209
210
    def as_dict(self, include_data=True, flat=False, stats=True, cprofile=None):
211
        result = {
212
            "group": self.group,
213
            "name": self.name,
214
            "fullname": self.fullname,
215
            "params": self.params,
216
            "param": self.param,
217
            "extra_info": self.extra_info,
218
            "options": dict(
219
                (k, funcname(v) if callable(v) else v) for k, v in self.options.items()
220
            )
221
        }
222
        if self.cprofile_stats:
223
            cprofile_list = result["cprofile"] = []
224
            cprofile_functions = get_cprofile_functions(self.cprofile_stats)
225
            stats_columns = ["cumtime", "tottime", "ncalls", "ncalls_recursion",
226
                             "tottime_per", "cumtime_per", "function_name"]
227
            # move column first
228
            if cprofile is not None:
229
                stats_columns.remove(cprofile)
230
                stats_columns.insert(0, cprofile)
231
            for column in stats_columns:
232
                cprofile_functions.sort(key=operator.itemgetter(column), reverse=True)
233
                for cprofile_function in cprofile_functions[:25]:
234
                    if cprofile_function not in cprofile_list:
235
                        cprofile_list.append(cprofile_function)
236
                # if we want only one column or we already have all available functions
237
                if cprofile is None or len(cprofile_functions) == len(cprofile_list):
238
                    break
239
        if stats:
240
            stats = self.stats.as_dict()
241
            if include_data:
242
                stats["data"] = self.stats.data
243
            stats["iterations"] = self.iterations
244
            if flat:
245
                result.update(stats)
246
            else:
247
                result["stats"] = stats
248
        return result
249
250
    def update(self, duration):
251
        self.stats.update(duration / self.iterations)
252