How to use touch_factor method in Airtest

Best Python code snippet using Airtest

ios.py

Source:ios.py Github

copy

Full Screen

...245 # 当前版本: wda 4.1.4 获取到的/window/size,在ipad+桌面横屏下拿到的是 height * height,需要修正246 if self.is_pad and self.home_interface():247 self._size['window_width'] = int(width * self._touch_factor)248 @property249 def touch_factor(self):250 if not self._touch_factor:251 self._display_info()252 return self._touch_factor253 @touch_factor.setter254 def touch_factor(self, factor):255 """256 touch_factor is used to convert click coordinates: mobile phone real coordinates = touch_factor * screen coordinates257 In general, no manual settings are required258 touch_factor用于换算点击坐标:手机真实坐标 = touch_factor * 屏幕坐标259 默认计算方式是: self.display_info['window_height'] / self.display_info['height']260 但在部分特殊型号手机上可能不准确,例如iOS14.4的7P,默认值为 1/3,但部分7P点击位置不准确,可自行设置为:self.touch_factor = 1 / 3.3261 (一般情况下,无需手动设置!)262 Examples:263 >>> device = connect_device("iOS:///")264 >>> device.touch((100, 100)) # wrong position265 >>> print(device.touch_factor)266 0.333333267 >>> device.touch_factor = 1 / 3.3 # default is 1/3268 >>> device.touch((100, 100))...

Full Screen

Full Screen

Index.py

Source:Index.py Github

copy

Full Screen

1from regression import shotRate2import numpy as np3from math import exp, factorial4import operator5import datetime6def matchEstimates(matchData, coefs, probs):7 expectedshots = []8 total_team_touches = []9 for teamId, value1 in matchData.iteritems():10 contributions=[]11 for key in matchData.keys():12 if key != teamId:13 opponent_teamId = key14 won_contest_sum = 015 yellow_card_opp_sum = 016 red_card_opp_sum = 017 accurate_pass_sum = 018 total_tackle_opp_sum = 019 touches_sum = 020 aerial_won_sum = 021 for player_name, value2 in matchData[teamId]["Player_stats"].iteritems():22 for key in matchData[teamId]["Player_stats"][player_name]["Match_stats"].keys():23 if key == "won_contest":24 won_contest_sum += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["won_contest"])25 if key == "accurate_pass":26 accurate_pass_sum += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["accurate_pass"])27 if key == "touches":28 touches_sum += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["touches"])29 if key == "aerial_won": 30 aerial_won_sum += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["aerial_won"]) 31 for player_name, value2 in matchData[opponent_teamId]["Player_stats"].iteritems():32 for key in matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"].keys():33 if key == "total_tackle":34 total_tackle_opp_sum += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["total_tackle"])35 if key == "yellow_card":36 yellow_card_opp_sum += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["yellow_card"])37 if key == "red_card":38 red_card_opp_sum += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["red_card"])39 if key == "second_yellow":40 red_card_opp_sum += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["second_yellow"])41 42 contributions.append(won_contest_sum)43 contributions.append(accurate_pass_sum)44 contributions.append(total_tackle_opp_sum)45 contributions.append(aerial_won_sum)46 contributions.append(yellow_card_opp_sum)47 contributions.append(red_card_opp_sum)48 total_team_touches.append(touches_sum)49 regression_pred = coefs[0] + coefs[1]*contributions[0] + coefs[2]*contributions[1] + coefs[3]*contributions[2] \50 + coefs[4]*contributions[3] + coefs[5]*contributions[4] + coefs[6]*contributions[5]51 expectedshots.append(regression_pred)52 expectedshots = np.array(expectedshots)53 lambda_ = probs[0]*expectedshots54 estimates = [lambda_, expectedshots, total_team_touches]55 return estimates56def goalProbabilities(lambda_):57 goal_probabilities = dict()58 sum_prob = 059 points_home = 060 points_away = 061 for i in range(5):62 for j in range(5):63 a = str(j) + "-" + str(i)64 prob = exp(-(lambda_[0] + lambda_[1]))*pow(lambda_[0], j)*pow(lambda_[1], i)/(factorial(j)*factorial(i))65 if j > i:66 points_home += 3*prob67 if j < i:68 points_away += 3*prob69 if j ==i:70 points_away += prob71 points_home += prob72 sum_prob += prob73 goal_probabilities.update({a : prob})74 goal_probabilities = sorted(goal_probabilities.items(), key=operator.itemgetter(1))75 print points_home, points_away76def playerPoints(matchData, estimates, coefs, probs):77 lambda_ = estimates[0]78 expectedshots = estimates[1]79 total_team_touches = estimates[2]80 total_team_touches_home = total_team_touches[0]81 total_team_touches_away = total_team_touches[1]82 derivative_home = 083 derivative_away = 084 cross_positions_1 = ["ML", "MR", "AML", "AMR"]85 cross_positions_2 = ["FWL", "FWR", "DR", "DL", "DMR", "DML"]86 87 for i in range(5):88 for j in range(5):89 a = str(j) + "-" + str(i)90 prob = exp(-(lambda_[0] + lambda_[1]))*pow(lambda_[0], j)*pow(lambda_[1], i)/(factorial(j)*factorial(i))91 if j > i:92 derivative_home += prob*(j/lambda_[0] - 1)93 if j < i:94 derivative_away += prob*(i/lambda_[1] - 1)95 exp_pts = dict() 96 # count to keep track of home or away team97 for teamId, value1 in matchData.iteritems():98 clean_sheet_away = 099 clean_sheet_home = 0100 for key in matchData.keys():101 if key != teamId:102 opponent_teamId = key103 if "goals" in matchData[teamId]["aggregate_stats"].keys():104 home_team_goal = float(matchData[teamId]["aggregate_stats"]["goals"])105 else:106 home_team_goal = 0107 clean_sheet_away = 1108 if "goals" in matchData[opponent_teamId]["aggregate_stats"].keys():109 away_team_goal = float(matchData[opponent_teamId]["aggregate_stats"]["goals"])110 else:111 away_team_goal = 0112 clean_sheet_home = 1113 114 if "total_scoring_att" in matchData[teamId]["aggregate_stats"].keys():115 home_team_shots = float(matchData[teamId]["aggregate_stats"]["total_scoring_att"])116 else:117 home_team_shots = 0118 if "total_scoring_att" in matchData[opponent_teamId]["aggregate_stats"].keys():119 away_team_shots = float(matchData[opponent_teamId]["aggregate_stats"]["total_scoring_att"])120 else:121 away_team_shots = 0122 if "total_tackle" in matchData[teamId]["aggregate_stats"].keys():123 home_team_tackle = float(matchData[teamId]["aggregate_stats"]["total_tackle"])124 else:125 home_team_tackle = 0126 if "total_tackle" in matchData[opponent_teamId]["aggregate_stats"].keys():127 away_team_tackle = float(matchData[opponent_teamId]["aggregate_stats"]["total_tackle"])128 else:129 away_team_tackle = 0130 131 if home_team_goal >= away_team_goal:132 if home_team_goal == away_team_goal:133 game_result_home = 1134 game_result_away = 1135 else:136 game_result_away = 0137 game_result_home = 3138 else:139 game_result_away = 3140 game_result_home = 0141 142 for player_name, value2 in matchData[teamId]["Player_stats"].iteritems():143 player_id = str(matchData[teamId]["Player_stats"][player_name]["player_details"]["player_id"])144 date = datetime.datetime.strptime(matchData[teamId]["team_details"]["date"], "%d/%m/%Y").strftime("%m/%d/%Y")145 won_contest = 0146 yellow_card_opp = 0147 red_card_opp = 0148 accurate_pass = 0149 total_tackle_opp = 0150 touches = 0151 aerial_won = 0152 saves = 0153 goals = 0154 assists = 0155 shots = 0156 cross_factor = 0157 player_position = int(matchData[teamId]["Player_stats"][player_name]["player_details"]["player_position_value"])158 player_position_info = str(matchData[teamId]["Player_stats"][player_name]["player_details"]["player_position_info"])159 if player_position < 5:160 game_time = 0.85/11161 if player_position == 1:162 defensive_actions = 0.3163 if player_position == 2:164 defensive_actions = 0.14165 if player_position == 3 and str(matchData[teamId]["Player_stats"][player_name]["player_details"]["player_position_info"]) == "DMC":166 defensive_actions = 0.14 167 else:168 game_time = 0.15/7169 if player_position_info in cross_positions_1:170 cross_factor = 0.30171 elif player_position_info in cross_positions_2:172 cross_factor = 0.20173 elif player_position_info == "GK" or player_position_info == "Sub":174 cross_factor = 0175 else :176 cross_factor = 0.05177 for key in matchData[teamId]["Player_stats"][player_name]["Match_stats"].keys():178 if key == "won_contest":179 won_contest += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["won_contest"])180 if key == "accurate_pass":181 accurate_pass += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["accurate_pass"])182 if key == "touches":183 touches += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["touches"])184 if key == "aerial_won": 185 aerial_won += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["aerial_won"])186 if key == "saves":187 saves += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["saves"])188 if key == "total_tackle":189 total_tackle_opp += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["total_tackle"])190 if key == "yellow_card":191 yellow_card_opp += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["yellow_card"])192 if key == "red_card":193 red_card_opp += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["red_card"])194 if key == "second_yellow":195 red_card_opp += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["second_yellow"])196 if key == "goals":197 goals += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["goals"])198 if key == "goal_assist":199 assists += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["goal_assist"])200 if key == "total_scoring_att":201 shots += float(matchData[teamId]["Player_stats"][player_name]["Match_stats"]["total_scoring_att"])202 touch_factor = touches/total_team_touches_home203 if home_team_goal != 0:204 goal_factor = goals/home_team_goal205 assist_factor = assists/home_team_goal206 else :207 goal_factor = 0208 assist_factor = 0209 if home_team_shots != 0:210 shot_factor = shots/home_team_shots211 else :212 shot_factor = 0213 if home_team_tackle != 0:214 tackle_factor = total_tackle_opp/home_team_tackle215 else:216 tackle_factor = 0217 contributions = np.array([won_contest, accurate_pass, touch_factor, aerial_won, saves, total_tackle_opp, yellow_card_opp, red_card_opp, goal_factor, assist_factor, shot_factor, cross_factor, tackle_factor])218 exp_pt_1 = 2*derivative_home*probs[0]*(coefs[1]*won_contest + coefs[2]*accurate_pass + coefs[4]*aerial_won) \219 - derivative_away*probs[0]*(coefs[3]*total_tackle_opp + coefs[5]*yellow_card_opp + coefs[6]*red_card_opp) \220 + derivative_away*probs[1]*saves/(expectedshots[1]*probs[2])*1.5221 exp_pt_2 = game_result_home*touch_factor222 exp_pt_3 = 1.37*game_time223 exp_pt_4 = 1.02*goals224 exp_pt_5 = 1.02*assists225 exp_pt_6 = 2.73*defensive_actions*clean_sheet_home226 exp_pt = exp_pt_1 + exp_pt_2*0.25 + exp_pt_3*0.15 + exp_pt_4*0.05 + exp_pt_5*0.05 + exp_pt_6*0.13227 exp_pts.update({player_id : [player_name, teamId, player_position, contributions, [exp_pt], date]})228 for player_name, value2 in matchData[opponent_teamId]["Player_stats"].iteritems():229 player_id = str(matchData[opponent_teamId]["Player_stats"][player_name]["player_details"]["player_id"])230 date = datetime.datetime.strptime(matchData[opponent_teamId]["team_details"]["date"], "%d/%m/%Y").strftime("%m/%d/%Y")231 won_contest = 0232 yellow_card_opp = 0233 red_card_opp = 0234 accurate_pass = 0235 total_tackle_opp = 0236 touches = 0237 aerial_won = 0238 saves = 0239 goals = 0240 assists = 0241 shots = 0242 player_position = int(matchData[opponent_teamId]["Player_stats"][player_name]["player_details"]["player_position_value"])243 player_position_info = str(matchData[opponent_teamId]["Player_stats"][player_name]["player_details"]["player_position_info"])244 if player_position < 5:245 game_time = 0.85/11246 if player_position == 1:247 defensive_actions = 0.3248 if player_position == 2:249 defensive_actions = 0.13250 if player_position == 3 and str(matchData[opponent_teamId]["Player_stats"][player_name]["player_details"]["player_position_info"]) == "DMC":251 defensive_actions = 0.13252 else:253 game_time = 0.15/7254 if player_position_info in cross_positions_1:255 cross_factor = 0.30256 elif player_position_info in cross_positions_2:257 cross_factor = 0.20258 elif player_position_info == "GK" or player_position_info == "Sub":259 cross_factor = 0260 else :261 cross_factor = 0.05262 for key in matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"].keys():263 if key == "won_contest":264 won_contest += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["won_contest"])265 if key == "accurate_pass":266 accurate_pass += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["accurate_pass"])267 if key == "touches":268 touches += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["touches"])269 if key == "aerial_won": 270 aerial_won += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["aerial_won"])271 if key == "saves":272 saves += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["saves"])273 if key == "total_tackle":274 total_tackle_opp += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["total_tackle"])275 if key == "yellow_card":276 yellow_card_opp += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["yellow_card"])277 if key == "red_card":278 red_card_opp += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["red_card"])279 if key == "second_yellow":280 red_card_opp += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["second_yellow"])281 if key == "goals":282 goals += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["goals"])283 if key == "goal_assist":284 assists += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["goal_assist"])285 if key == "total_scoring_att":286 shots += float(matchData[opponent_teamId]["Player_stats"][player_name]["Match_stats"]["total_scoring_att"]) 287 288 touch_factor = touches/total_team_touches_away289 if away_team_goal != 0:290 goal_factor = goals/away_team_goal291 assist_factor = assists/away_team_goal292 else :293 goal_factor = 0294 assist_factor = 0295 if away_team_shots != 0:296 shot_factor = shots/away_team_shots297 else :298 shot_factor = 0299 if away_team_tackle != 0:300 tackle_factor = total_tackle_opp/away_team_tackle301 else:302 tackle_factor = 0303 304 contributions = np.array([won_contest, accurate_pass, touch_factor, aerial_won, saves, total_tackle_opp, yellow_card_opp, red_card_opp, goal_factor, assist_factor, shot_factor, cross_factor, tackle_factor])305 exp_pt_1 = 2*derivative_away*probs[0]*(coefs[1]*won_contest + coefs[2]*accurate_pass + coefs[4]*aerial_won) \306 - derivative_home*probs[0]*(coefs[3]*total_tackle_opp + coefs[5]*yellow_card_opp + coefs[6]*red_card_opp) \307 + derivative_home*saves*probs[1]/(expectedshots[0]*probs[2])*1.5308 exp_pt_2 = game_result_away*touch_factor309 exp_pt_3 = 1.37*game_time310 exp_pt_4 = 1.02*goals311 exp_pt_5 = 1.02*assists312 exp_pt_6 = 2.73*defensive_actions*clean_sheet_away313 exp_pt = exp_pt_1 + exp_pt_2*0.25 + exp_pt_3*0.15 + exp_pt_4*0.05 + exp_pt_5*0.05 + exp_pt_6*0.13314 exp_pts.update({player_id : [player_name, opponent_teamId, player_position, contributions,[exp_pt], date]})315 break316 return exp_pts317def dict_update(dict1, matchUpdate):318 for key, value in matchUpdate.iteritems():319 if key in dict1.keys():320 dict1[key].append(matchUpdate[key][4][0])321 else:322 dict1[key] = matchUpdate[key][4]323def position_update(dict1, matchUpdate):324 # position vector is [others, postion1, position2, position3, position4, position5] 325 for key, value in matchUpdate.iteritems():326 position = int(matchUpdate[key][2])327 if key in dict1.keys():328 dict1[key][position] += 1329 else:330 dict1[key] = []331 for i in range(6):332 if i == position:333 dict1[key].append(1)334 else:335 dict1[key].append(0)336def contributions_update(dict1, matchUpdate):337 # contributions vector is [won_contest, accurate_pass, touch_factor, aerial_won, saves, total_tackle_opp, yellow_card_opp, red_card_opp, goals, assists]338 for key, value in matchUpdate.iteritems():339 contributions = matchUpdate[key][3]340 if key in dict1.keys():341 dict1[key] = dict1[key] + contributions342 else:343 dict1[key] = contributions344def player_name_update(dict1, matchUpdate):345 for key, value in matchUpdate.iteritems():346 player_name = matchUpdate[key][0]347 player_team = matchUpdate[key][1]348 if key in dict1.keys():349 if player_team in dict1[key][1].keys():350 dict1[key][1][player_team] += 1351 else:352 dict1[key][1][player_team] = 1353 else:354 dict1[key]=[]355 dict1[key].append(player_name)356 dict1[key].append({player_team: 1})357 358def summary_update(dict_1, matchUpdate):359 for key, value in matchUpdate.iteritems():360 contributions = matchUpdate[key][3]361 position = matchUpdate[key][2]362 pts = matchUpdate[key][4][0]363 date = matchUpdate[key][5]364 updated_array = np.append(contributions, position)365 updated_array = np.append(updated_array, pts)366 if key in dict_1.keys():367 dict_1[key][date] = updated_array368 369 else:370 dict_1[key] = {}371 dict_1[key][date] = updated_array372 373 374def roster_update(dict1, matchUpdate):375 for player_id, value in matchUpdate.iteritems():376 team_id = value[1]377 if team_id in dict1.keys():378 if player_id in dict1[team_id].keys():379 if value[2] < 5:380 dict1[team_id][player_id][0] += 1/38.0381 else:382 dict1[team_id][player_id][1] += 1/38.0383 else:384 dict1[team_id][player_id] = [0, 0]385 if value[2] < 5:386 dict1[team_id][player_id][0] += 1/38.0387 else:388 dict1[team_id][player_id][1] += 1/38.0389 else:390 dict1[team_id] = dict()391 dict1[team_id][player_id] = [0, 0]392 if value[2] < 5:393 dict1[team_id][player_id][0] += 1/38.0394 else:395 dict1[team_id][player_id][1] += 1/38.0396 397def aggregate_to_mean_contribution(dict1, aggregate_dict):398 for player_id, player_data in aggregate_dict.iteritems():399 beta = 0.9400 num_starts = 0401 num_subs = 0402 sorteddates = aggregate_dict[player_id].keys()403 sorteddates.sort()404 for date in sorteddates:405 match_data = aggregate_dict[player_id][date]406 contribution = match_data[:-2]407 if match_data[-2] < 5:408 if num_starts == 0:409 player_start = contribution410 num_starts += 1411 else:412 player_start = beta*player_start + (1-beta)*contribution413 num_starts += 1414 else:415 if num_subs == 0:416 player_sub = contribution417 num_subs += 1418 else:419 player_sub = beta*player_sub + (1-beta)*contribution420 num_subs += 1421 if num_starts > 0:422 player_start = player_start423 else:424 player_start = 0*player_sub425 if num_subs > 0:426 player_sub = player_sub427 else:428 player_sub = 0*player_start429 430 dict1[player_id] = [player_start, player_sub] 431def match_contributions(dict1, mean_contribution_dict, team_roster_dict):432 for team_id, home_players in team_roster_dict.iteritems():433 count_home = 0434 for player, appearances in home_players.iteritems():435 if count_home > 0:436 home_contribution = home_contribution + mean_contribution_dict[player][0][:-5]*appearances[0] \437 + mean_contribution_dict[player][1][:-5]*appearances[1]438 else:439 home_contribution = mean_contribution_dict[player][0][:-5]*appearances[0] \440 + mean_contribution_dict[player][1][:-5]*appearances[1]441 442 count_home += 1443 dict1[team_id] = home_contribution444def game_result(home_team_id, away_team_id, teamMeanContribution_dict, coefs, probs):445 home_team_contribution = teamMeanContribution_dict[home_team_id]446 away_team_contribution = teamMeanContribution_dict[away_team_id]447 448 expectedshots_home = coefs[0] + coefs[1]*home_team_contribution[0] + coefs[2]*home_team_contribution[1] + \449 coefs[3]*away_team_contribution[5] + coefs[4]*home_team_contribution[3] + coefs[5]*away_team_contribution[6] + \450 coefs[6]*away_team_contribution[7]451 expectedshots_away = coefs[0] + coefs[1]*away_team_contribution[0] + coefs[2]*away_team_contribution[1] + \452 coefs[3]*home_team_contribution[5] + coefs[4]*away_team_contribution[3] + coefs[5]*home_team_contribution[6] + \453 coefs[6]*home_team_contribution[7]454 expectedshots = np.array([expectedshots_home, expectedshots_away])455 expectedshots = [1.2, 1]*expectedshots456 lambda_ = probs[0]*expectedshots457 home_win_prob = 0458 draw_prob = 0459 away_win_prob = 0460 461 for i in range(10):462 for j in range(10):463 prob = exp(-(lambda_[0] + lambda_[1]))*pow(lambda_[0], j)*pow(lambda_[1], i)/(factorial(j)*factorial(i))464 if j > i:465 home_win_prob += prob466 if j < i:467 away_win_prob += prob468 if j == i:469 draw_prob += prob470 sum_prob = home_win_prob + away_win_prob + draw_prob471 home_win_prob = home_win_prob/sum_prob472 away_win_prob = away_win_prob/sum_prob473 draw_prob = draw_prob/sum_prob...

Full Screen

Full Screen

playerRating.py

Source:playerRating.py Github

copy

Full Screen

1import json2import numpy as np3from regression import *4from shotEffectiveness import estimateProbabilities5from Index import *6from collections import OrderedDict7import operator8import unicodedata9file_pointer = open("/Users/shubhampawar/epl_data/xml_stats/season16-17/season_stats.json", "rb")10data = json.load(file_pointer, object_pairs_hook=OrderedDict)11# previous two seasons data and current season data12file_pointer = open("/Users/shubhampawar/epl_data/xml_stats/season17-18/season_stats.json", "rb")13data.update(json.load(file_pointer, object_pairs_hook=OrderedDict))14# file_pointer = open("/Users/shubhampawar/epl_data/xml_stats/season15-16/season_stats.json", "rb")15# data.update(json.load(file_pointer, object_pairs_hook=OrderedDict))16# file_pointer = open("/Users/shubhampawar/epl_data/xml_stats/season14-15/season_stats.json", "rb")17# data.update(json.load(file_pointer, object_pairs_hook=OrderedDict))18total_shots = []19won_contest = []20yellow_card_opp = []21red_card_opp = []22accurate_pass = []23total_tackle_opp= []24touches = []25aerial_won = []26shots = 027goals = 028onTarget = 029saves = 030blocks = 031aggregate_stats_dict = dict()32player_stats_dict = dict()33for matchId, value1 in data.iteritems():34 for teamId, value2 in data[matchId].iteritems():35 for key in data[matchId].keys():36 if key != teamId:37 opponent_teamId = key 38 for key in data[matchId][teamId]["aggregate_stats"].keys():39 if key in aggregate_stats_dict.keys():40 aggregate_stats_dict[key] +=141 else:42 aggregate_stats_dict.update({key: 1})43 total_shots.append(int(data[matchId][teamId]["aggregate_stats"]["total_scoring_att"]))44 45 if "total_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys():46 shots += float(data[matchId][teamId]["aggregate_stats"]["total_scoring_att"])47 if "goals" in data[matchId][teamId]["aggregate_stats"].keys():48 goals += float(data[matchId][teamId]["aggregate_stats"]["goals"])49 if ("ontarget_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()) and ("blocked_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()):50 onTarget += float(data[matchId][teamId]["aggregate_stats"]["ontarget_scoring_att"]) + float(data[matchId][teamId]["aggregate_stats"]["blocked_scoring_att"])51 if ("ontarget_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()) and not("blocked_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()):52 onTarget += float(data[matchId][teamId]["aggregate_stats"]["ontarget_scoring_att"])53 if not("ontarget_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()) and ("blocked_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()):54 onTarget += float(data[matchId][teamId]["aggregate_stats"]["blocked_scoring_att"])55 if ("ontarget_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()) and ("goals" in data[matchId][teamId]["aggregate_stats"].keys()):56 saves += float(data[matchId][teamId]["aggregate_stats"]["ontarget_scoring_att"]) - float(data[matchId][teamId]["aggregate_stats"]["goals"])57 if ("ontarget_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys()) and not("goals" in data[matchId][teamId]["aggregate_stats"].keys()):58 saves += float(data[matchId][teamId]["aggregate_stats"]["ontarget_scoring_att"])59 if "blocked_scoring_att" in data[matchId][teamId]["aggregate_stats"].keys():60 blocks += float(data[matchId][teamId]["aggregate_stats"]["blocked_scoring_att"])61 won_contest_sum = 062 yellow_card_opp_sum = 063 red_card_opp_sum = 064 accurate_pass_sum = 065 total_tackle_opp_sum = 066 touches_sum = 067 aerial_won_sum = 068 for player_name, value3 in data[matchId][teamId]["Player_stats"].iteritems():69 for key in data[matchId][teamId]["Player_stats"][player_name]["Match_stats"].keys():70 # if key in player_stats_dict.keys():71 # player_stats_dict[key] +=172 # else:73 # player_stats_dict.update({key: 1}) 74 if key == "won_contest":75 won_contest_sum += float(data[matchId][teamId]["Player_stats"][player_name]["Match_stats"]["won_contest"])76 if key == "accurate_pass":77 accurate_pass_sum += float(data[matchId][teamId]["Player_stats"][player_name]["Match_stats"]["accurate_pass"])78 if key == "touches":79 touches_sum += float(data[matchId][teamId]["Player_stats"][player_name]["Match_stats"]["touches"])80 if key == "aerial_won": 81 aerial_won_sum += float(data[matchId][teamId]["Player_stats"][player_name]["Match_stats"]["aerial_won"]) 82 83 won_contest.append(won_contest_sum)84 accurate_pass.append(accurate_pass_sum)85 touches.append(touches_sum)86 aerial_won.append(aerial_won_sum)87 for player_name, value3 in data[matchId][opponent_teamId]["Player_stats"].iteritems():88 for key in data[matchId][opponent_teamId]["Player_stats"][player_name]["Match_stats"].keys():89 if key == "total_tackle":90 total_tackle_opp_sum += float(data[matchId][opponent_teamId]["Player_stats"][player_name]["Match_stats"]["total_tackle"])91 if key == "yellow_card":92 yellow_card_opp_sum += float(data[matchId][opponent_teamId]["Player_stats"][player_name]["Match_stats"]["yellow_card"])93 if key == "red_card":94 red_card_opp_sum += float(data[matchId][opponent_teamId]["Player_stats"][player_name]["Match_stats"]["red_card"])95 if key == "second_yellow":96 red_card_opp_sum += float(data[matchId][opponent_teamId]["Player_stats"][player_name]["Match_stats"]["second_yellow"])97 98 total_tackle_opp.append(total_tackle_opp_sum) 99 yellow_card_opp.append(yellow_card_opp_sum)100 red_card_opp.append(red_card_opp_sum)101shots_data = np.transpose(np.array([total_shots, won_contest, accurate_pass, total_tackle_opp, aerial_won, yellow_card_opp, red_card_opp]))102#print shots_data.shape103coefficients = shotRegression(shots_data)104#print total_shots[0], won_contest[0], accurate_pass[0], total_tackle[0], touches[0], aerial_won[0], yellow_card_opp[0], red_card_opp[0]105probabilities = estimateProbabilities(shots, goals, onTarget, saves, blocks)106playerRatings = dict()107playerPosition = dict()108playerIdNameData = dict()109playerMatchSummary = dict()110playerMeanContributions = dict()111teamMeanContribution = dict()112teamRoster = dict()113for matchId, value in data.iteritems():114 estimates = matchEstimates(data[matchId], coefficients, probabilities) 115 #lambda_ = estimates[0]116 #goalProbabilities(lambda_)117 playerData = playerPoints(data[matchId], estimates, coefficients, probabilities)118 position_update(playerPosition, playerData)119 summary_update(playerMatchSummary, playerData)120 player_name_update(playerIdNameData, playerData)121 roster_update(teamRoster, playerData)122 aggregate_to_mean_contribution(playerMeanContributions, playerMatchSummary)123 dict_update(playerRatings, playerData)124match_contributions(teamMeanContribution, playerMeanContributions, teamRoster)125#result = game_result("13", "16", teamMeanContribution, coefficients, probabilities)126#print result127 128#playerRatings = sorted(playerRatings.items(), key=operator.itemgetter(1))129#print playerRatings130ratings_file = "playerRatings.csv"131positions_file = "playerPositions.csv"132contributions_file = "playerContributions.csv"133player_name_file = "playerNameId.csv"134team_roster = "teamRoster.csv"135mean_starts_contributions_file = "playerStartsMeanContributions.csv"136mean_subs_contributions_file = "playerSubsMeanContributions.csv"137team_mean_contributions_file = "teamMeanContributions.csv"138csvfiles = [ratings_file, positions_file, contributions_file, player_name_file]139fp = open(ratings_file, 'wb')140fp.write("Player_id, Ratings\n")141for player, ratings in playerRatings.iteritems():142 ratings_string = ""143 for rating in ratings:144 ratings_string += "," + str(rating)145 fp.write(str(player) + ratings_string + "\n")146fp.close()147fp = open(positions_file, 'wb')148fp.write("Player_id, Others, Goalkeeper, Defender, Midfielder, Forward, Substitute\n")149for player, positions in playerPosition.iteritems():150 positions_string = ""151 for position in positions:152 positions_string += "," + str(position)153 fp.write(str(player) + positions_string + "\n")154fp.close()155''' fp = open(contributions_file, 'wb')156# contributions vector is [won_contest, accurate_pass, touch_factor, aerial_won, saves, total_tackle_opp, yellow_card_opp, red_card_opp, goals, assists]157fp.write("Player_id, won_contest, accurate_pass, touch_factor, aerial_won, saves, total_tackle_opp, yellow_card_opp, red_card_opp, goals, assists\n")158for player, contributions in playerContributions.iteritems():159 contributions_string = ""160 for contribution in contributions:161 contributions_string += "," + str(contribution)162 fp.write(str(player) + contributions_string + "\n")163fp.close() '''164fp = open(player_name_file, 'wb')165fp.write("Player_id, Player_name, Player_team1, Appearances1, Player_team2, Appearances2\n")166for player, playerInfo in playerIdNameData.iteritems():167 player_name = unicodedata.normalize("NFKD", playerInfo[0]).encode('ascii', 'ignore')168 player_team_info_string = ""169 for team, appearances in playerInfo[1].iteritems():170 player_team_info_string += "," + str(team) + "," + str(appearances)171 fp.write(str(player) + "," + player_name + player_team_info_string + "\n")172fp.close()173fp = open(contributions_file, 'wb')174fp.write("Player_id, Date, won_contest, accurate_pass, touch_factor, aerial_won, saves, tackles, yellow_card, red_card, goal_factor, assist_factor, shot_factor, cross_factor, tackle_factor, Position, Rating\n")175for player, matchData in playerMatchSummary.iteritems():176 for date, contributions in matchData.iteritems():177 contributions_string_ = ""178 for contribution in contributions:179 contributions_string_ += "," + str(contribution)180 fp.write(str(player) + "," + str(date) + contributions_string_ + "\n") 181fp.close()182fp = open(team_roster, 'wb')183fp.write("Team_id, Player_id, Start_prob, Sub_prob\n")184for team, roster in teamRoster.iteritems():185 for player_id, data in teamRoster[team].iteritems():186 fp.write(str(team) + "," + str(player_id) + "," + str(data[0]) + "," + str(data[1]) + "\n")187fp.close()188fstart = open(mean_starts_contributions_file, 'wb')189fsub = open(mean_subs_contributions_file, 'wb')190fstart.write("Player_id, won_contest, accurate_pass, touch_factor, aerial_won, saves, tackles, yellow_card, red_card, goal_factor, assist_factor, shot_factor, cross_factor, tackle_factor\n")191fsub.write("Player_id, won_contest, accurate_pass, touch_factor, aerial_won, saves, tackles, yellow_card, red_card, goal_factor, assist_factor, shot_factor, cross_factor, tackle_factor\n")192for player, mean_data in playerMeanContributions.iteritems():193 mean_start_contribution_string = ""194 mean_sub_contribution_string = ""195 for contribution in mean_data[0]:196 mean_start_contribution_string += "," + str(contribution)197 for contribution in mean_data[1]:198 mean_sub_contribution_string += "," + str(contribution)199 fstart.write(str(player) + mean_start_contribution_string + "\n")200 fsub.write(str(player) + mean_sub_contribution_string + "\n")201fstart.close()202fsub.close()203fp = open(team_mean_contributions_file, 'wb')204fp.write("Team_id, won_contest, accurate_pass, touch_factor, aerial_won, saves, total_tackle_opp, yellow_card_opp, red_card_opp\n")205for team, contributions in teamMeanContribution.iteritems():206 contributions_string = ""207 for contribution in contributions:208 contributions_string += "," + str(contribution)209 fp.write(str(team) + contributions_string + "\n")210fp.close()211fp = open("info_file.csv", "wb")212fp.write("Constant, Won_contest, accurate_pass, tackle_opp, aerial_won, yellow_card, red_card\n")213count = 0214for coefs in coefficients:215 if count == 0:216 fp.write(str(coefs))217 count = 1218 continue219 fp.write("," + str(coefs))220fp.write("\n")221fp.write("P(G|S), P(onTarget|shot), P(saveOrBlock|onTarget)\n")222count = 0223for prob in probabilities:224 if count == 0:225 fp.write(str(prob))226 count = 1227 continue228 fp.write("," + str(prob))229fp.write("\n")...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful