def print_closenesses(match_OPTA, formation, home_or_away_string, half=0): pass_map = onet.get_all_pass_destinations(match_OPTA, team=home_or_away_string, exclude_subs=False, half=half) player_times = formation.team_object.get_on_pitch_periods() graphs = centrality.get_directed_graphs(pass_map, player_times, formation.team_object, role_grouped=True) closeness = centrality.current_flow_closeness_directed( graphs['scaled_graph'], start_node=1 # goalkeeper role ) print("Match {}.{}:\t {}".format( match_id, half, [r_id for r_id, _ in sorted(closeness.items(), key=lambda t: -t[1])]))
def plot_closeness_vs_betweenness(match_OPTA, team_id, match_id=None, half=0): """ For a single team in a single match, closeness on the x-axis and betweenness on the y-axis """ team_object = match_OPTA.team_map.get(team_id) if team_object is None: print("Team not in match") return home_or_away = "home" if team_object == match_OPTA.hometeam else "away" player_times = team_object.get_on_pitch_periods() # role_grouped = True # for period in [0, 1, 2]: pass_map = onet.get_all_pass_destinations(match_OPTA, team=home_or_away, exclude_subs=False, half=half) graphs = centrality.get_directed_graphs( pass_map, player_times, team_object, role_grouped=True, period=half ) betweenness = centrality.current_flow_betweenness_directed( graphs['scaled_graph'], start_node=1 ) closeness = centrality.current_flow_closeness_directed( graphs['scaled_graph'], start_node=1 ) players = graphs['scaled_graph'].nodes() x = [closeness[p] for p in players] y = [betweenness[p] for p in players] fig, ax = plt.subplots() ax.set_xlabel("closeness") ax.set_ylabel("betweenness") title = "Match {} Team {}".format(match_id, team_id) if half != 0: title = "{} - Half {}".format(title, half) ax.set_title(title) ax.set_xlim([0, 3]) ax.set_ylim([0, 0.8]) colors = [] for i, p_id in enumerate(players): if p_id in offense: colors.append('#28b463') elif p_id in midfield: colors.append('#f4d03f') elif p_id in defense: colors.append('#ec7063') else: colors.append('#85c1e9') ax.scatter(x, y, c=colors) for i, p_id in enumerate(players): ax.annotate(p_id, (x[i], y[i])) plt.show()
def plot_cross_half_centrality(match, team_id, metric="closeness"): """ Plot one centrality measure of two matches against each other on opposing axes. Args: matches (matchOPTA): match to evaluate centrality measures team_id (int): ID for the team in question Kwargs: metric (string): the metric used for the match-to-match comparison """ allowed_metrics = { "closeness": centrality.current_flow_closeness_directed, "betweenness": centrality.current_flow_betweenness_directed, "pagerank": onet.get_pagerank } metric_function = allowed_metrics.get(metric) if metric_function is None: raise("Unallowed metric") metric_results = {} players = set() for half in [0,1]: team_object = match.team_map.get(team_id) if team_object is None: raise("Team not in match") home_or_away = "home" if team_object == match.hometeam else "away" player_times = team_object.get_on_pitch_periods() pass_map = onet.get_all_pass_destinations(match, team=home_or_away, exclude_subs=False, half=half) graphs = centrality.get_directed_graphs( pass_map, player_times, team_object, role_grouped=True, period=half ) players = players.union(set(graphs['scaled_graph'].nodes())) if metric != "pagerank": metric_results[half] = metric_function( graphs['scaled_graph'], start_node=1 ) else: metric_results[half] = metric_function( match_OPTA, team=home_or_away, half=half, role_grouped=True ) print(metric_results[half]) players = list(players) x = [metric_results[0].get(p) if p in metric_results[0].keys() else 0 for p in players] y = [metric_results[1].get(p) if p in metric_results[1].keys() else 0 for p in players] fig, ax = plt.subplots() ax.set_xlabel("First Half {}".format(metric)) ax.set_ylabel("Second Half {}".format(metric)) ax.set_title("Team {}".format(team_id)) if metric == "closeness": ax.set_xlim([0, 3]) ax.set_ylim([0, 3]) elif metric == "betweenness": ax.set_xlim([0, 0.8]) ax.set_ylim([0, 0.8]) elif metric == "pagerank": ax.set_xlim([0.085, 0.098]) ax.set_ylim([0.085, 0.098]) # draw center line line_x_vals = [ min(set(x).union(set(y))), max(set(x).union(set(y))) ] line_y_vals = line_x_vals plt.plot(line_x_vals, line_y_vals, '--', color='r') colors = [] for i, p_id in enumerate(players): if p_id in offense: colors.append('#28b463') elif p_id in midfield: colors.append('#f4d03f') elif p_id in defense: colors.append('#ec7063') else: colors.append('#85c1e9') ax.scatter(x, y, c=colors) for i, p_id in enumerate(players): ax.annotate(p_id, (x[i], y[i])) plt.show()
formation = formations[match_id] print("Against {} Score {}-{}".format( opposing_team_object.team_id, match_OPTA.homegoals if home_or_away_string == "home" else match_OPTA.awaygoals, match_OPTA.awaygoals if home_or_away_string == "home" else match_OPTA.homegoals)) for half in [0, 1, 2]: pass_map = onet.get_all_pass_destinations( match_OPTA, team=home_or_away_string, exclude_subs=False, half=half) player_times = team_object.get_on_pitch_periods() graphs = centrality.get_directed_graphs(pass_map, player_times, team_object, role_grouped=True) betweenness = centrality.current_flow_betweenness_directed( graphs['scaled_graph'], start_node=1 # goalkeeper role ) print("Match {}.{}, Opposing Formation {}:\t {}".format( match_id, half, formation.opp_formation_1 if half == 1 else formation.opp_formation_2, [ r_id for r_id, _ in sorted(betweenness.items(), key=lambda t: -t[1]) ])) print()