Best Python code snippet using autotest_python
regression.py
Source:regression.py
...141 def getAvgPercent(self, avgs_dict):142 return self._process_files(avgs_dict, self._get_augment_rate)143 def getSD(self):144 return self._process_files(self.files_dict, self._get_list_sd)145 def getSDRate(self, sds_dict):146 return self._process_files(sds_dict, self._get_rate)147 def getTtestPvalue(self, fs_dict1, fs_dict2, paired=None):148 """149 scipy lib is used to compute p-value of Ttest150 scipy: http://www.scipy.org/151 t-test: http://en.wikipedia.org/wiki/Student's_t-test152 """153 try:154 from scipy import stats155 import numpy as np156 except ImportError:157 print "No python scipy/numpy library installed!"158 return None159 ret = []160 s1 = self._process_files(fs_dict1, self._get_list_self, merge=False)161 s2 = self._process_files(fs_dict2, self._get_list_self, merge=False)162 # s*[line][col] contians items (line*col) of all sample files163 for line in range(len(s1)):164 tmp = []165 if type(s1[line]) != list:166 tmp = s1[line]167 else:168 if len(s1[line][0]) < 2:169 continue170 for col in range(len(s1[line])):171 avg1 = self._get_list_avg(s1[line][col])172 avg2 = self._get_list_avg(s2[line][col])173 sample1 = np.array(s1[line][col])174 sample2 = np.array(s2[line][col])175 warnings.simplefilter("ignore", RuntimeWarning)176 if (paired):177 (t, p) = stats.ttest_rel(sample1, sample2)178 else:179 (t, p) = stats.ttest_ind(sample1, sample2)180 flag = "+"181 if float(avg1) > float(avg2):182 flag = "-"183 tmp.append(flag + "%.3f" % (1 - p))184 tmp = "|".join(tmp)185 ret.append(tmp)186 return ret187 def _get_rate(self, data):188 """ num2 / num1 * 100 """189 result = "0.0"190 if len(data) == 2 and float(data[0]) != 0:191 result = float(data[1]) / float(data[0]) * 100192 if result < 1:193 result = "%.2f%%" % result194 else:195 result = "%.0f%%" % result196 return result197 def _get_augment_rate(self, data):198 """ (num2 - num1) / num1 * 100 """199 result = "+0.0"200 if len(data) == 2 and float(data[0]) != 0:201 result = "%+.3f%%" % ((float(data[1]) - float(data[0])) /202 float(data[0]) * 100)203 return result204 def _get_list_sd(self, data):205 """206 sumX = x1 + x2 + ... + xn207 avgX = sumX / n208 sumSquareX = x1^2 + ... + xn^2209 SD = sqrt([sumSquareX - (n * (avgX ^ 2))] / (n - 1))210 """211 sum = sqsum = 0212 n = len(data)213 for i in data:214 sum += float(i)215 sqsum += float(i) ** 2216 avg = sum / n217 if avg == 0 or n == 1 or sqsum - (n * avg ** 2) <= 0:218 return "0.0"219 return "%.3f" % (((sqsum - (n * avg ** 2)) / (n - 1)) ** 0.5)220 def _get_list_avg(self, data):221 """ Compute the average of list entries """222 sum = 0223 for i in data:224 sum += float(i)225 if is_int(str(data[0])):226 return "%d" % (sum / len(data))227 return "%.2f" % (sum / len(data))228 def _get_list_self(self, data):229 """ Use this to convert sample dicts """230 return data231 def _process_lines(self, files_dict, row, func, avg_update, merge):232 """ Use unified function to process same lines of different samples """233 lines = []234 ret = []235 for i in range(len(files_dict)):236 lines.append(files_dict[i][row].split("|"))237 for col in range(len(lines[0])):238 data_list = []239 for i in range(len(lines)):240 tmp = lines[i][col].strip()241 if is_int(tmp):242 data_list.append(int(tmp))243 else:244 data_list.append(float(tmp))245 ret.append(func(data_list))246 if avg_update:247 for i in avg_update.split('|'):248 line = i.split(',')249 ret[int(line[0])] = "%.2f" % (float(ret[int(line[1])]) /250 float(ret[int(line[2])]))251 if merge:252 return "|".join(ret)253 return ret254 def _process_files(self, files_dict, func, avg_update=None, merge=True):255 """256 Process dicts of sample files with assigned function,257 func has one list augment.258 """259 ret_lines = []260 for i in range(len(files_dict[0])):261 if re.findall("[a-zA-Z]", files_dict[0][i]):262 ret_lines.append(files_dict[0][i].strip())263 else:264 line = self._process_lines(files_dict, i, func, avg_update,265 merge)266 ret_lines.append(line)267 return ret_lines268def display(lists, rates, allpvalues, f, ignore_col, sum="Augment Rate",269 prefix0=None, prefix1=None, prefix2=None, prefix3=None):270 """271 Display lists data to standard format272 param lists: row data lists273 param rates: augment rates lists274 param f: result output file275 param ignore_col: do not display some columns276 param sum: compare result summary277 param prefix0: output prefix in head lines278 param prefix1: output prefix in Avg/SD lines279 param prefix2: output prefix in Diff Avg/P-value lines280 param prefix3: output prefix in total Sign line281 """282 def str_ignore(str, split=False):283 str = str.split("|")284 for i in range(ignore_col):285 str[i] = " "286 if split:287 return "|".join(str[ignore_col:])288 return "|".join(str)289 def tee_line(content, file, n=None):290 fd = open(file, "a")291 print content292 str = ""293 str += "<TR ALIGN=CENTER>"294 content = content.split("|")295 for i in range(len(content)):296 if n and i >= 2 and i < ignore_col + 2:297 str += "<TD ROWSPAN=%d WIDTH=1%% >%s</TD>" % (n, content[i])298 else:299 str += "<TD WIDTH=1%% >%s</TD>" % content[i]300 str += "</TR>"301 fd.write(str + "\n")302 fd.close()303 for l in range(len(lists[0])):304 if not re.findall("[a-zA-Z]", lists[0][l]):305 break306 tee("<TABLE BORDER=1 CELLSPACING=1 CELLPADDING=1 width=10%><TBODY>",307 f)308 tee("<h3>== %s " % sum + "==</h3>", f)309 category = 0310 for i in range(len(lists[0])):311 for n in range(len(lists)):312 is_diff = False313 for j in range(len(lists)):314 if lists[0][i] != lists[j][i]:315 is_diff = True316 if len(lists) == 1 and not re.findall("[a-zA-Z]", lists[j][i]):317 is_diff = True318 pfix = prefix1[0]319 if len(prefix1) != 1:320 pfix = prefix1[n]321 if is_diff:322 if n == 0:323 tee_line(pfix + lists[n][i], f, n=len(lists) + len(rates))324 else:325 tee_line(pfix + str_ignore(lists[n][i], True), f)326 if not is_diff and n == 0:327 if '|' in lists[n][i]:328 tee_line(prefix0 + lists[n][i], f)329 elif "Category:" in lists[n][i]:330 if category != 0 and prefix3:331 if len(allpvalues[category - 1]) > 0:332 tee_line(prefix3 + str_ignore(333 allpvalues[category - 1][0]), f)334 tee("</TBODY></TABLE>", f)335 tee("<br>", f)336 tee("<TABLE BORDER=1 CELLSPACING=1 CELLPADDING=1 "337 "width=10%><TBODY>", f)338 category += 1339 tee("<TH colspan=3 >%s</TH>" % lists[n][i], f)340 else:341 tee("<TH colspan=3 >%s</TH>" % lists[n][i], f)342 for n in range(len(rates)):343 if lists[0][i] != rates[n][i] and not re.findall("[a-zA-Z]",344 rates[n][i]):345 tee_line(prefix2[n] + str_ignore(rates[n][i], True), f)346 if prefix3 and len(allpvalues[-1]) > 0:347 tee_line(prefix3 + str_ignore(allpvalues[category - 1][0]), f)348 tee("</TBODY></TABLE>", f)349def analyze(test, type, arg1, arg2, configfile):350 """ Compute averages/p-vales of two samples, print results nicely """351 config = ConfigParser.ConfigParser()352 config.read(configfile)353 ignore_col = int(config.get(test, "ignore_col"))354 avg_update = config.get(test, "avg_update")355 desc = config.get(test, "desc")356 def get_list(dir):357 result_file_pattern = config.get(test, "result_file_pattern")358 cmd = 'find %s|grep "%s.*/%s"' % (dir, test, result_file_pattern)359 print cmd360 return commands.getoutput(cmd)361 if type == 'file':362 arg1 = get_list(arg1)363 arg2 = get_list(arg2)364 commands.getoutput("rm -f %s.*html" % test)365 s1 = Sample(type, arg1)366 avg1 = s1.getAvg(avg_update=avg_update)367 sd1 = s1.getSD()368 s2 = Sample(type, arg2)369 avg2 = s2.getAvg(avg_update=avg_update)370 sd2 = s2.getSD()371 sd1 = s1.getSDRate([avg1, sd1])372 sd2 = s1.getSDRate([avg2, sd2])373 avgs_rate = s1.getAvgPercent([avg1, avg2])374 navg1 = []375 navg2 = []376 allpvalues = []377 tmp1 = []378 tmp2 = []379 for i in range(len(avg1)):380 if not re.findall("[a-zA-Z]", avg1[i]):381 tmp1.append([avg1[i]])382 tmp2.append([avg2[i]])383 elif 'Category' in avg1[i] and i != 0:384 navg1.append(tmp1)385 navg2.append(tmp2)386 tmp1 = []...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!