Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief
5"""
6import timeit
7import pandas
10def unit(x):
11 """
12 Optimizes the rendering of time.
14 .. runpython::
15 :showcode:
17 from jupytalk.benchmark.mlprediction import unit
19 print(unit(34))
20 print(unit(3.4))
21 print(unit(0.34))
22 print(unit(0.034))
23 print(unit(0.0034))
24 print(unit(0.00034))
25 print(unit(0.000034))
26 print(unit(0.0000034))
27 print(unit(0.00000034))
28 """
29 if x >= 1:
30 return "%1.2f s" % x
31 elif x >= 1e-3:
32 return "%1.2f ms" % (x * 1000)
33 elif x >= 1e-6:
34 return "%1.2f µs" % (x * 1000**2)
35 elif x >= 1e-9:
36 return "%1.2f ns" % (x * 1000**3)
37 else:
38 return "%1.2g s" % x
41def timeexec(legend, code, number=50, repeat=200, verbose=True, context=None):
42 """
43 Measures the time for a given expression.
45 @param legend name of the experiment
46 @param code code to measure (as a string)
47 @param number number of time to run the expression
48 (and then divide by this number to get an average)
49 @param repeat number of times to repeat the computation
50 of the above average
51 @param verbose print the time
52 @param globals context (usuable equal to ``globals()``)
53 @return dictionary
55 .. runpython::
56 :showcode:
58 from jupytalk.benchmark.mlprediction import timeexec
60 code = "3 * 45535266234653452"
61 print(timeexec("multiplication", code))
62 """
63 if context is None:
64 context = globals()
65 rep = timeit.repeat(code, number=number, repeat=repeat, globals=context)
66 ave = sum(rep) / (number * repeat)
67 std = (sum((x / number - ave)**2 for x in rep) / repeat)**0.5
68 fir = rep[0] / number
69 fir3 = sum(rep[:3]) / (3 * number)
70 las3 = sum(rep[-3:]) / (3 * number)
71 rep.sort()
72 mini = rep[len(rep) // 20] / number
73 maxi = rep[-len(rep) // 20] / number
74 if verbose:
75 print("Average: %s deviation %s (with %d runs) in [%s, %s]" % (
76 unit(ave), unit(std), number, unit(mini), unit(maxi)))
77 return dict(legend=legend, average=ave, deviation=std, first=fir, first3=fir3,
78 last3=las3, repeat=repeat, min5=mini, max5=maxi, code=code, run=number)
81def make_dataframe(labels, arrays):
82 """
83 Builds a dataframe from multiple arrays.
85 @param labels list of labels
86 @param arrays list of arrays (or one array)
87 @return dataframes
88 """
89 if labels is not None:
90 df = [pandas.DataFrame(data={'Label': labels})]
91 else:
92 df = []
93 if isinstance(arrays, list):
94 for i, ar in enumerate(arrays):
95 d = pandas.DataFrame(
96 data=ar, columns=["F%d_%d" % (i, j) for j in range(ar.shape[1])])
97 df.append(d)
98 else:
99 ar = arrays
100 d = pandas.DataFrame(
101 data=ar, columns=["F%d" % j for j in range(ar.shape[1])])
102 df.append(d)
103 return pandas.concat(df, axis=1)