def update_dropdown_team_options(season, competition): if season and competition: df = filter_competition(df_players, competition) df = filter_season(df, season) teams = sorted(df["team_url"].unique()) return get_dash_dropdown_options(teams, teams) else: return []
def update_datatable_raw(competition, season, team, player_url): df = df_players if competition: df = filter_competition(df, competition) if season: df = filter_season(df, season) if team: df = filter_team_url(df, team) if player_url: df = filter_player_url(df, player_url) return df[VISIBLE_COLUMNS].to_dict("records")
def update_dropdown_team_options(team, competition, season): if season and competition and team: df = filter_competition(df_players, competition) df = filter_season(df, season) df = filter_team_url(df, team) player_urls, player_names = (df[[ "player_url", "player_name" ]].drop_duplicates().sort_values("player_name").values.transpose()) return get_dash_dropdown_options(player_urls, player_names) else: return []
def main(): player_url_to_name = helpers.get_map_from_url_to_name(df_players, "player") team_url_to_name = helpers.get_map_from_url_to_name(df_players, "team") st.title("Goal Difference Project") st.write("Explore datasets from big 5 leagues.") st.header("gd90 calculation") st.markdown(""" (Sum of the goal differences (GD) during each of the appearances) / (total playing time) x 90 """) # st.write("As formula:") # st.latex( # r""" # \text{gd90} = \frac{\sum{\text{GD}}}{\sum{\text{min'}} \cdot 90} # """ # ) # The `gd90` value in plots below is calculated for each player as 90 * SUM(goal difference while player is on field) / SUM(minutes played) st.header("Analysis for selected season, player and team") VISIBLE_COLUMNS = [ "competition", "year", "matchday", "team_name", "player_name", "goal_difference", "duration", ] competitions = sorted(df_players["competition"].unique()) competition = st.sidebar.selectbox("Select Competition", competitions) df_competition = data.filter_competition(df_players, competition) seasons = sorted(df_competition["year"].unique()) season = st.sidebar.selectbox("Select Season", seasons) df_season = data.filter_season(df_competition, season) teams = sorted(df_season.sort_values("team_name")["team_url"].unique()) team = st.sidebar.selectbox( "Select Team", teams, format_func=team_url_to_name.get, ) df_team = data.filter_team_url(df_season, team) min_appearances = st.sidebar.number_input( "Filter players with less than input appearances", min_value=0, max_value=34, value=5, step=1, ) fig_season = visualization.scatter_players_for_season( df_players, df_matches, competition, season, x_column="gd90", y_column="full_games", min_appearances=min_appearances, team=team, ) st.plotly_chart(fig_season) fig_team = visualization.scatter_players_for_team( df_players=df_players, df_matches=df_matches, team=team, competition=competition, season=season, x_column="gd90", y_column="full_games", min_appearances=min_appearances, ) st.plotly_chart(fig_team) fig_team_bar = visualization.bar_players_for_team( df_players=df_players, df_matches=df_matches, team=team, competition=competition, season=season, weight_column="full_games", min_appearances=min_appearances, ) st.plotly_chart(fig_team_bar) players = df_team.sort_values("player_name")["player_url"].unique() player = st.selectbox("Select Player", players, format_func=player_url_to_name.get) df_player = data.filter_player_url(df_team, player) default_columns = [ "matchday", "goal_difference", "duration", ] selected_columns = st.multiselect("Columns to display", options=VISIBLE_COLUMNS, default=default_columns) st.write(df_player[selected_columns].reset_index(drop=True))
def bar_players_for_team( df_players, df_matches, competition, season, team, column="gd90", weight_column="full_games", min_appearances: int = 5, ): df_players = filter_competition(df_players, competition) df_players = filter_season(df_players, season) df_players = filter_team_url(df_players, team) team_name = helpers.get_map_from_url_to_name(df_players, "team")[team] df_players = get_players_goal_differences(df_players) df_players = filter_appearances(df_players, min_appearances) df_players = df_players.sort_values(column) values = df_players[column] players = df_players.index.get_level_values(2) width_weights = df_players[weight_column] # get mean value for team df_matches = filter_competition(df_matches, competition) df_matches = filter_season(df_matches, season) team_goal_difference = goal_difference_for_team(df_matches, team) trace = go.Bar( x=players, y=df_players[column], width=0.05 + 0.7 * ((width_weights / width_weights.max())), orientation="v", hoverinfo="text", hovertext=[ f"<b>{player}</b><br>{column}={value:.1f}<br>{weight_column}={weight:.1f}" for player, value, weight in zip(players, values, width_weights) ], marker={"color": "mediumvioletred", "line": {"width": 0}}, ) layout = get_default_layout(f"{team_name} {season}", "Player", column) annotations = [ go.layout.Annotation( x=-0.1, y=0.95, xref="paper", xanchor="left", yanchor="middle", yref="paper", text="Bar width scaled with minutes played", font=ANNOTATION_TEXT_FONT, ) ] shapes = [ go.layout.Shape( type="line", xref="paper", x0=0, y0=team_goal_difference, x1=1, y1=team_goal_difference, line=dict(color="mediumvioletred", width=4, dash="dash",), ) ] layout.update(annotations=annotations, shapes=shapes) layout.xaxis.update(showgrid=False) layout.yaxis.update(showgrid=True) data = [trace] fig = go.Figure(data=data, layout=layout) return fig
def scatter_players_for_team( df_players, df_matches, competition, season, team, x_column="gd90", y_column="full_games", min_appearances: int = 5, ): column = "gd90" df_players = filter_competition(df_players, competition) df_players = filter_season(df_players, season) df_players = filter_team_url(df_players, team) team_name = helpers.get_map_from_url_to_name(df_players, "team")[team] df_players = get_players_goal_differences(df_players) df_players = filter_appearances(df_players, min_appearances) # display(df.head()) # get mean value for team df_matches = filter_competition(df_matches, competition) df_matches = filter_season(df_matches, season) team_goal_difference = goal_difference_for_team(df_matches, team) # df_team = df_grouped[df_grouped.index.get_level_values(0) == team] player_names = df_players.index.get_level_values(2) trace = go.Scatter( x=df_players[x_column], y=df_players[y_column], mode="markers+text", name="Players", text=player_names, textposition="top center", marker={"color": "mediumvioletred", "size": 12, "symbol": "diamond",}, textfont=PLAYER_TEXT_FONT, ) # get mean value for team df_matches = filter_competition(df_matches, competition) df_matches = filter_season(df_matches, season) team_goal_difference = goal_difference_for_team(df_matches, team) # for goal difference of team shapes = [ go.layout.Shape( type="line", yref="paper", x0=team_goal_difference, y0=0, x1=team_goal_difference, y1=1, line=dict(color="mediumvioletred", width=3, dash="dash",), ) ] layout = get_default_layout(f"{team_name} {season}", x_column, y_column) layout.yaxis.update(rangemode="tozero") layout.update(shapes=shapes) fig = go.Figure([trace], layout=layout) return fig
def scatter_players_for_season( df_players, df_matches, competition: str, season: str, x_column: str, y_column: str, min_appearances: int = 5, team: "str" = None, ): df_players = filter_competition(df_players, competition) df_players = filter_season(df_players, season) df_grouped = get_players_goal_differences(df_players) df_grouped = filter_appearances(df_grouped, min_appearances) teams = df_grouped.index.get_level_values(0) players = df_grouped.index.get_level_values(2) data = [] if team is not None: team_name = helpers.get_map_from_url_to_name(df_players, "team")[team] trace_team = get_scatter_for_df( df_grouped.loc[[team]], x_column, y_column, name=team_name, marker_color="mediumvioletred", marker_symbol="diamond", ) data.append(trace_team) df_grouped = df_grouped.drop(index=team) trace_all = get_scatter_for_df( df_grouped, x_column, y_column, name="All / other teams" ) data.append(trace_all) layout = get_default_layout(f"{competition} {season}", x_column, y_column) layout.yaxis.update(rangemode="tozero") if team is not None: # get mean value for team df_matches = filter_competition(df_matches, competition) df_matches = filter_season(df_matches, season) team_goal_difference = goal_difference_for_team(df_matches, team) # for goal difference of team shapes = [ go.layout.Shape( type="line", yref="paper", x0=team_goal_difference, y0=0, x1=team_goal_difference, y1=1, line=dict(color="mediumvioletred", width=4, dash="dash",), ) ] layout.update(shapes=shapes) # fig.update_xaxes(ticks="outside", tickwidth=2, tickcolor='crimson', ticklen=10) # fig.update_yaxes(ticks="outside", tickwidth=2, tickcolor='crimson', ticklen=10, col=1) fig = go.Figure(data, layout=layout) return fig