blob: 81806190a94e0bcfa4539c8830aad3112cd1152d [file] [log] [blame]
koder aka kdanilov6c491062015-04-09 22:33:13 +03001import math
2import itertools
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03003
4try:
koder aka kdanilovf86d7af2015-05-06 04:01:54 +03005 from scipy import stats
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03006 from numpy import array, linalg
7 from scipy.optimize import leastsq
8 from numpy.polynomial.chebyshev import chebfit, chebval
koder aka kdanilovf86d7af2015-05-06 04:01:54 +03009 no_numpy = False
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030010except ImportError:
11 no_numpy = True
koder aka kdanilov6c491062015-04-09 22:33:13 +030012
13
14def med_dev(vals):
15 med = sum(vals) / len(vals)
16 dev = ((sum(abs(med - i) ** 2.0 for i in vals) / len(vals)) ** 0.5)
17 return med, dev
18
19
koder aka kdanilove87ae652015-04-20 02:14:35 +030020def round_3_digit(val):
21 return round_deviation((val, val / 10.0))[0]
22
23
koder aka kdanilov6c491062015-04-09 22:33:13 +030024def round_deviation(med_dev):
25 med, dev = med_dev
26
27 if dev < 1E-7:
28 return med_dev
29
30 dev_div = 10.0 ** (math.floor(math.log10(dev)) - 1)
31 dev = int(dev / dev_div) * dev_div
32 med = int(med / dev_div) * dev_div
koder aka kdanilov7e0f7cf2015-05-01 17:24:35 +030033 return [type(med_dev[0])(med),
34 type(med_dev[1])(dev)]
koder aka kdanilov6c491062015-04-09 22:33:13 +030035
36
37def groupby_globally(data, key_func):
38 grouped = {}
39 grouped_iter = itertools.groupby(data, key_func)
40
41 for (bs, cache_tp, act, conc), curr_data_it in grouped_iter:
42 key = (bs, cache_tp, act, conc)
43 grouped.setdefault(key, []).extend(curr_data_it)
44
45 return grouped
46
47
48def approximate_curve(x, y, xnew, curved_coef):
49 """returns ynew - y values of some curve approximation"""
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030050 if no_numpy:
51 return None
52
koder aka kdanilov6c491062015-04-09 22:33:13 +030053 return chebval(xnew, chebfit(x, y, curved_coef))
54
55
56def approximate_line(x, y, xnew, relative_dist=False):
Ved-vampir03166442015-04-10 17:28:23 +030057 """ x, y - test data, xnew - dots, where we want find approximation
58 if not relative_dist distance = y - newy
59 returns ynew - y values of linear approximation"""
koder aka kdanilov66839a92015-04-11 13:22:31 +030060
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030061 if no_numpy:
62 return None
63
Ved-vampir03166442015-04-10 17:28:23 +030064 # convert to numpy.array (don't work without it)
65 ox = array(x)
66 oy = array(y)
koder aka kdanilov66839a92015-04-11 13:22:31 +030067
Ved-vampir03166442015-04-10 17:28:23 +030068 # set approximation function
koder aka kdanilov66839a92015-04-11 13:22:31 +030069 def func_line(tpl, x):
70 return tpl[0] * x + tpl[1]
71
72 def error_func_rel(tpl, x, y):
73 return 1.0 - y / func_line(tpl, x)
74
75 def error_func_abs(tpl, x, y):
76 return y - func_line(tpl, x)
77
Ved-vampir03166442015-04-10 17:28:23 +030078 # choose distance mode
koder aka kdanilov66839a92015-04-11 13:22:31 +030079 error_func = error_func_rel if relative_dist else error_func_abs
80
81 tpl_initial = tuple(linalg.solve([[ox[0], 1.0], [ox[1], 1.0]],
82 oy[:2]))
83
Ved-vampir03166442015-04-10 17:28:23 +030084 # find line
koder aka kdanilov66839a92015-04-11 13:22:31 +030085 tpl_final, success = leastsq(error_func,
86 tpl_initial[:],
87 args=(ox, oy))
88
Ved-vampir03166442015-04-10 17:28:23 +030089 # if error
90 if success not in range(1, 5):
91 raise ValueError("No line for this dots")
koder aka kdanilov66839a92015-04-11 13:22:31 +030092
Ved-vampir03166442015-04-10 17:28:23 +030093 # return new dots
koder aka kdanilov66839a92015-04-11 13:22:31 +030094 return func_line(tpl_final, array(xnew))
koder aka kdanilov6c491062015-04-09 22:33:13 +030095
96
97def difference(y, ynew):
98 """returns average and maximum relative and
Ved-vampir03166442015-04-10 17:28:23 +030099 absolute differences between y and ynew
100 result may contain None values for y = 0
101 return value - tuple:
102 [(abs dif, rel dif) * len(y)],
103 (abs average, abs max),
104 (rel average, rel max)"""
koder aka kdanilov66839a92015-04-11 13:22:31 +0300105
106 abs_dlist = []
107 rel_dlist = []
108
Ved-vampir03166442015-04-10 17:28:23 +0300109 for y1, y2 in zip(y, ynew):
110 # absolute
koder aka kdanilov66839a92015-04-11 13:22:31 +0300111 abs_dlist.append(y1 - y2)
Ved-vampir03166442015-04-10 17:28:23 +0300112
koder aka kdanilov66839a92015-04-11 13:22:31 +0300113 if y1 > 1E-6:
114 rel_dlist.append(abs(abs_dlist[-1] / y1))
115 else:
116 raise ZeroDivisionError("{0!r} is too small".format(y1))
117
118 da_avg = sum(abs_dlist) / len(abs_dlist)
119 dr_avg = sum(rel_dlist) / len(rel_dlist)
120
121 return (zip(abs_dlist, rel_dlist),
122 (da_avg, max(abs_dlist)), (dr_avg, max(rel_dlist))
123 )
koder aka kdanilov6c491062015-04-09 22:33:13 +0300124
125
126def calculate_distribution_properties(data):
127 """chi, etc"""
128
129
130def minimal_measurement_amount(data, max_diff, req_probability):
131 """
132 should returns amount of measurements to get results (avg and deviation)
133 with error less, that max_diff in at least req_probability% cases
134 """
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300135
136
137class StatProps(object):
138 def __init__(self):
139 self.average = None
140 self.mediana = None
141 self.perc_95 = None
142 self.perc_5 = None
143 self.deviation = None
144 self.confidence = None
145 self.min = None
146 self.max = None
147
148 def rounded_average_conf(self):
149 return round_deviation((self.average, self.confidence))
150
koder aka kdanilov416b87a2015-05-12 00:26:04 +0300151 def rounded_average_dev(self):
152 return round_deviation((self.average, self.deviation))
153
154 def __str__(self):
155 return "StatProps({0} ~ {1})".format(round_3_digit(self.average),
156 round_3_digit(self.deviation))
157
158 def __repr__(self):
159 return str(self)
160
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300161
162def data_property(data, confidence=0.95):
163 res = StatProps()
164 if len(data) == 0:
165 return res
166
167 data = sorted(data)
168 res.average, res.deviation = med_dev(data)
169 res.max = data[-1]
170 res.min = data[0]
171
172 ln = len(data)
173 if ln % 2 == 0:
174 res.mediana = (data[ln / 2] + data[ln / 2 - 1]) / 2
175 else:
176 res.mediana = data[ln / 2]
177
178 res.perc_95 = data[int((ln - 1) * 0.95)]
179 res.perc_5 = data[int((ln - 1) * 0.05)]
180
181 if not no_numpy and ln >= 3:
182 res.confidence = stats.sem(data) * \
183 stats.t.ppf((1 + confidence) / 2, ln - 1)
184 else:
185 res.confidence = res.deviation
186
187 return res