Coverage for src / graphs / rating_graph.py: 0%

46 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-01 10:28 +0000

1import numpy as np 

2import pandas as pd 

3import plotly.express as px 

4import plotly.graph_objects as go 

5from plotly.subplots import make_subplots 

6 

7from src.static import static_values_enum 

8from src.static.static_values_enum import RatingLevel, Format 

9 

10 

11def create_rating_graph(df, theme): 

12 df = df.copy() # Create a copy of the DataFrame 

13 df['date'] = pd.to_datetime(df['created_date']).dt.strftime('%Y-%m-%d %H:%M:%S') 

14 fig = px.scatter( 

15 df, 

16 x='date', 

17 y='rating', 

18 color='account', 

19 color_discrete_sequence=px.colors.qualitative.Set1, 

20 template=theme, 

21 height=800, 

22 ) 

23 fig.update_layout( 

24 xaxis={'type': 'category', 'categoryorder': 'category ascending'}, 

25 ) 

26 

27 # Start from 1 skip Novice 

28 for i in np.arange(1, len(static_values_enum.league_ratings)): 

29 y = static_values_enum.league_ratings[i] 

30 color = static_values_enum.league_colors[i] 

31 league_name = RatingLevel(i).name 

32 

33 fig.add_hline(y=y, 

34 line_width=1, 

35 line_dash="dash", 

36 annotation_text=league_name, 

37 annotation_position="top left", 

38 line_color=color) 

39 return fig 

40 

41 

42def get_scatter_trace(df, name, show_legend=False): 

43 color = { 

44 'win_pct': px.colors.qualitative.Plotly[0], 

45 'battles': px.colors.qualitative.Plotly[1], 

46 'win': px.colors.qualitative.Plotly[2], 

47 'loss': px.colors.qualitative.Plotly[3], 

48 } 

49 

50 df = df.copy() # Create a copy of the DataFrame 

51 df['date'] = pd.to_datetime(df['created_date']).dt.strftime('%Y-%m-%d') 

52 return go.Scatter(x=df.date, 

53 y=df[name], 

54 mode='lines+markers', 

55 name=name, 

56 legendgroup=name, 

57 line=dict( 

58 color=color[name], 

59 ), 

60 showlegend=show_legend 

61 ) 

62 

63 

64def plot_daily_stats_battle(daily_df, theme): 

65 daily_df['win_pct'] = daily_df.apply(lambda row: (row.win / row.battles * 100), axis=1) 

66 

67 wild_daily_df = daily_df.loc[daily_df['format'] == Format.wild.value] 

68 wild_daily_df = wild_daily_df.sort_values('created_date') 

69 modern_daily_df = daily_df.loc[daily_df['format'] == Format.modern.value] 

70 modern_daily_df = modern_daily_df.sort_values('created_date') 

71 

72 fig = make_subplots( 

73 specs=[[{"secondary_y": True}, {"secondary_y": True}]], 

74 subplot_titles=("Modern", "Wild"), 

75 horizontal_spacing=0.1, 

76 rows=1, 

77 cols=2, 

78 ) 

79 

80 # Get all columns except 'created_date' and 'format' 

81 columns_to_plot = modern_daily_df.columns.difference(['created_date', 'format']) 

82 for column in columns_to_plot: 

83 if column == 'win_pct': 

84 secondary_y = True 

85 else: 

86 secondary_y = False 

87 fig.add_trace( 

88 get_scatter_trace(modern_daily_df, column, show_legend=False), 

89 secondary_y=secondary_y, 

90 row=1, 

91 col=1 

92 ) 

93 

94 columns_to_plot = wild_daily_df.columns.difference(['created_date', 'format']) 

95 for column in columns_to_plot: 

96 if column == 'win_pct': 

97 secondary_y = True 

98 else: 

99 secondary_y = False 

100 fig.add_trace( 

101 get_scatter_trace(wild_daily_df, column, show_legend=True), 

102 secondary_y=secondary_y, 

103 row=1, 

104 col=2 

105 ) 

106 

107 fig.update_xaxes(showgrid=True, gridwidth=0.5) 

108 fig.update_yaxes(showgrid=True, gridwidth=0.5) 

109 

110 fig.update_layout( 

111 xaxis={'type': 'category', 'categoryorder': 'category ascending'}, 

112 xaxis2={'type': 'category', 'categoryorder': 'category ascending'}, 

113 template=theme, 

114 title_text="Daily battle stats", 

115 margin=dict(l=10, r=10), 

116 legend=dict( 

117 orientation="h", 

118 yanchor="bottom", 

119 y=1.15, 

120 xanchor="right", 

121 x=1 

122 ), 

123 yaxis1=dict( 

124 showgrid=False, 

125 range=[0, daily_df.battles.max() + 20], 

126 title="battles", 

127 ), 

128 yaxis2=dict( 

129 showgrid=False, 

130 range=[0, 100], 

131 title='win (%)'), 

132 

133 yaxis3=dict( 

134 showgrid=False, 

135 range=[0, daily_df.battles.max() + 20], 

136 title="battles", 

137 ), 

138 yaxis4=dict( 

139 showgrid=False, 

140 range=[0, 100], 

141 title='win (%)'), 

142 ) 

143 return fig