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

118 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-03 19:06 +0000

1import pandas as pd 

2import plotly.express as px 

3import plotly.graph_objects as go 

4from plotly.subplots import make_subplots 

5 

6from src.static.static_values_enum import Leagues 

7 

8 

9def plot_season_stats_battle(season_df, theme): 

10 season_df = season_df.sort_values(by=['season']) 

11 season_df = season_df.dropna(subset=['rating']) 

12 season_df['win_pct'] = season_df.apply(lambda row: (row.wins / row.battles * 100), axis=1) 

13 

14 fig = make_subplots(specs=[[{'secondary_y': True}]]) 

15 trace3 = go.Scatter(x=season_df.season, 

16 y=season_df.win_pct, 

17 mode='lines+markers', 

18 name='win percentage') 

19 trace4 = go.Scatter(x=season_df.season, 

20 y=season_df.battles, 

21 mode='lines+markers', 

22 name='battles') 

23 trace5 = go.Scatter(x=season_df.season, 

24 y=season_df.wins, 

25 mode='lines+markers', 

26 name='wins') 

27 fig.add_trace(trace3, secondary_y=True) 

28 fig.add_trace(trace4) 

29 fig.add_trace(trace5) 

30 fig.update_xaxes(showgrid=True, gridwidth=1) # , gridcolor=GRID_COLOR) 

31 fig.update_yaxes(showgrid=True, gridwidth=1) # , gridcolor=GRID_COLOR) 

32 

33 fig.update_layout( 

34 template=theme, 

35 # paper_bgcolor=PAPER_BGCOLOR, 

36 # plot_bgcolor=PLOT_BGCOLOR, 

37 # font=TEXT_FONT, 

38 legend=dict( 

39 orientation='h', 

40 yanchor='bottom', 

41 y=1.02, 

42 xanchor='right', 

43 x=1 

44 ), 

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

46 xaxis=dict( 

47 tickvals=season_df.season, 

48 ), 

49 yaxis1=dict( 

50 # zerolinecolor=GRID_COLOR, 

51 showgrid=False, 

52 range=[0, season_df.battles.max() + 20], 

53 title='battles', 

54 ), 

55 yaxis2=dict( 

56 # zerolinecolor=GRID_COLOR, 

57 showgrid=False, 

58 overlaying='y', 

59 side='right', 

60 anchor='x2', 

61 range=[0, 100], 

62 title='win (%)'), 

63 ) 

64 return fig 

65 

66 

67def plot_season_stats_rating(season_df, theme): 

68 season_df = season_df.sort_values(by=['season']) 

69 season_df = season_df.dropna(subset=['rating']) 

70 season_df = season_df.astype({'league': 'int'}) 

71 season_ratings = [0, 400, 700, 1000, 1300, 1600, 1900, 2200, 2500, 2800, 3100, 3400, 3700, 4200, 4700, 5100] 

72 season_df['end_league_rating'] = season_df.apply(lambda row: season_ratings[row.league], axis=1) 

73 

74 fig = make_subplots(specs=[[{'secondary_y': True}]]) 

75 trace1 = go.Scatter(x=season_df.season, 

76 y=season_df.rating, 

77 mode='lines+markers', 

78 name='end rating', 

79 line=dict(color='firebrick', width=2), 

80 ) 

81 trace2 = go.Bar(x=season_df.season, 

82 y=season_df.rating, 

83 showlegend=False, 

84 name='not displayed ony to create secondary axis', 

85 opacity=0, 

86 ) 

87 

88 trace3 = go.Bar(x=season_df.season, 

89 y=season_df.end_league_rating, 

90 name='end league', 

91 offset=-0.3, 

92 width=0.6, 

93 marker=dict( 

94 color='rgb(8,48,107)', 

95 line_color='rgb(8,48,107)', 

96 line_width=1), 

97 opacity=1, 

98 ) 

99 

100 fig.add_trace(trace1, secondary_y=True) 

101 fig.add_trace(trace2) 

102 fig.add_trace(trace3) 

103 

104 fig.update_layout( 

105 template=theme, 

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

107 legend=dict( 

108 orientation='h', 

109 yanchor='bottom', 

110 y=1.02, 

111 xanchor='right', 

112 x=1 

113 ), 

114 xaxis=dict( 

115 tickvals=season_df.season, 

116 ), 

117 

118 yaxis2=dict( 

119 showgrid=True, 

120 title='rating', 

121 gridwidth=1, 

122 nticks=25, 

123 range=[0, season_df.rating.max() * 1.05] 

124 ), 

125 yaxis=dict( 

126 zeroline=False, 

127 showgrid=False, 

128 title='league', 

129 range=[0, season_df.rating.max() * 1.05], 

130 tickvals=[0, 400, 700, 1000, 1300, 1600, 1900, 2200, 2500, 2800, 3100, 3400, 3700, 4200, 4700, 9999], 

131 ticktext=[Leagues(0).name, 

132 Leagues(1).name, 

133 Leagues(2).name, 

134 Leagues(3).name, 

135 Leagues(4).name, 

136 Leagues(5).name, 

137 Leagues(6).name, 

138 Leagues(7).name, 

139 Leagues(8).name, 

140 Leagues(9).name, 

141 Leagues(10).name, 

142 Leagues(11).name, 

143 Leagues(12).name, 

144 Leagues(13).name, 

145 Leagues(14).name, 

146 Leagues(15).name], 

147 ), 

148 ) 

149 

150 return fig 

151 

152 

153def get_season_ids_range(df_array): 

154 all_season_ids = set() 

155 for df in df_array: 

156 if 'season_id' in df: 

157 all_season_ids.update(df.season_id.unique()) 

158 min_season_id = min(all_season_ids) 

159 max_season_id = max(all_season_ids) 

160 return set(range(min_season_id, max_season_id + 1)) 

161 

162 

163def plot_season_stats_earnings(season_df_sps, 

164 season_df_dec, 

165 season_df_merits, 

166 season_df_unclaimed_sps, 

167 season_df_glint, 

168 theme): 

169 # Data consistency 

170 columns_dec = [ 

171 'reward', 

172 'quest_rewards', 

173 'season_rewards', 

174 'rental_payment', 

175 'earn_rental_payment', 

176 'cost_rental_payment', 

177 'rental_payment_fees', 

178 'market_rental', 

179 'rental_refund', 

180 'tournament_prize', 

181 'enter_tournament', 

182 'modern_leaderboard_prizes', 

183 'wild_leaderboard_prizes'] 

184 columns_sps = [ 

185 'claim_staking_rewards', 

186 'claim_staking_rewards_staking_rewards', 

187 'claim_staking_rewards_validator_rewards', 

188 'validate_block', 

189 'token_award', 

190 'tournament_prize', 

191 'token_transfer_multi', 

192 'enter_tournament'] 

193 columns_merits = [ 

194 'quest_rewards', 

195 'season_rewards', 

196 'brawl_prize'] 

197 columns_glint = [ 

198 'ranked_rewards', 

199 'season_rewards', 

200 ] 

201 

202 if not season_df_dec.empty: 

203 season_df_dec = season_df_dec.copy().sort_values(by=['season_id']).fillna(0) 

204 season_df_dec['total'] = season_df_dec.filter(columns_dec).sum(axis=1, numeric_only=True) 

205 else: 

206 season_df_dec['season_id'] = [] 

207 season_df_dec['total'] = [] 

208 

209 season_df_sps_combined = pd.DataFrame() 

210 season_df_sps_combined['season_id'] = [] 

211 season_df_sps_combined['total'] = [] 

212 if not season_df_sps.empty: 

213 season_df_sps_combined = season_df_sps.copy().sort_values(by=['season_id']).fillna(0) 

214 season_df_sps_combined['total'] = season_df_sps.filter(columns_sps).sum(axis=1, numeric_only=True) 

215 

216 if not season_df_unclaimed_sps.empty: 

217 season_df_sps_combined['total_sps'] = season_df_sps_combined['total'] 

218 season_df_unclaimed_sps = season_df_unclaimed_sps.copy().sort_values(by=['season_id']).fillna(0) 

219 season_df_unclaimed_sps['total_unclaimed_sps'] = (season_df_unclaimed_sps.drop(['season_id'], axis=1) 

220 .sum(axis=1, numeric_only=True)) 

221 season_df_sps_combined = season_df_sps_combined.merge(season_df_unclaimed_sps, on=['season_id', 'player']) 

222 season_df_sps_combined['total'] = season_df_sps_combined.total_sps + season_df_sps_combined.total_unclaimed_sps 

223 

224 if not season_df_merits.empty: 

225 season_df_merits = season_df_merits.copy().sort_values(by=['season_id']).fillna(0) 

226 season_df_merits['total'] = season_df_merits.filter(columns_merits).sum(axis=1, numeric_only=True) 

227 else: 

228 season_df_merits['season_id'] = [] 

229 season_df_merits['total'] = [] 

230 

231 if not season_df_glint.empty: 

232 season_df_glint = season_df_glint.copy().sort_values(by=['season_id']).fillna(0) 

233 season_df_glint['total'] = season_df_glint.filter(columns_glint).sum(axis=1, numeric_only=True) 

234 else: 

235 season_df_glint['season_id'] = [] 

236 season_df_glint['total'] = [] 

237 

238 trace1 = go.Scatter(x=season_df_dec.season_id, 

239 y=season_df_dec.total, 

240 mode='lines+markers', 

241 name='DEC total (earnings - payments)', 

242 line=dict(color='royalblue')) 

243 

244 trace2 = go.Scatter(x=season_df_merits.season_id, 

245 y=season_df_merits.total, 

246 mode='lines+markers', 

247 name='MERITS total (earnings)', 

248 line=dict(color='red', width=2)) 

249 

250 trace3 = go.Scatter(x=season_df_sps_combined.season_id, 

251 y=season_df_sps_combined.total, 

252 mode='lines+markers', 

253 name='SPS total (earnings - payments)', 

254 line=dict(color='lightgreen', width=2)) 

255 

256 # fill glint dataframe with other season_id. 

257 season_range = get_season_ids_range([season_df_dec, season_df_merits, season_df_sps_combined, season_df_glint]) 

258 season_df_glint = season_df_glint.set_index('season_id').reindex(season_range, fill_value=0).reset_index() 

259 season_df_glint = season_df_glint.copy().sort_values(by=['season_id']).fillna(0) 

260 

261 trace4 = go.Scatter(x=season_df_glint.season_id, 

262 y=season_df_glint.total, 

263 mode='lines+markers', 

264 name='GLINT (ranked + season rewards) ', 

265 line=dict(color='steelblue', width=2)) 

266 

267 titles = [] 

268 traces = [] 

269 tick_values_arr = [] 

270 if season_df_dec.total.sum() != 0: 

271 titles.append('DEC') 

272 traces.append(trace1) 

273 tick_values_arr.append(season_df_dec.season_id) 

274 if season_df_merits.total.sum() != 0: 

275 titles.append('MERITS') 

276 traces.append(trace2) 

277 tick_values_arr.append(season_df_merits.season_id) 

278 if season_df_sps_combined.total.sum() != 0: 

279 titles.append('SPS') 

280 traces.append(trace3) 

281 tick_values_arr.append(season_df_sps_combined.season_id) 

282 if season_df_glint.total.sum() != 0: 

283 titles.append('GLINT') 

284 traces.append(trace4) 

285 tick_values_arr.append(season_df_glint.season_id) 

286 

287 fig = make_subplots(rows=len(titles), cols=1, row_heights=[800] * len(titles)) 

288 

289 for i, trace in enumerate(traces): 

290 fig.add_trace(trace, row=i + 1, col=1) 

291 

292 for i, tick_values in enumerate(tick_values_arr): 

293 xaxis_name = f"xaxis{i + 1}" if i > 0 else "xaxis" 

294 yaxis_name = f"yaxis{i + 1}" if i > 0 else "yaxis" 

295 fig.update_layout( 

296 { 

297 xaxis_name: dict( 

298 showgrid=True, 

299 gridwidth=1, 

300 tickvals=tick_values, 

301 ), 

302 yaxis_name: dict( 

303 title=titles[i], 

304 side='right', 

305 ) 

306 } 

307 ) 

308 

309 fig.update_layout( 

310 template=theme, 

311 height=480 * len(traces), # px 

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

313 legend=dict( 

314 x=0, 

315 y=1, 

316 font=dict( 

317 family='Courier', 

318 size=12, 

319 color='black' 

320 ), 

321 bgcolor='LightSteelBlue', 

322 bordercolor='Black', 

323 borderwidth=2 

324 ), 

325 ) 

326 

327 return fig 

328 

329 

330def plot_season_stats_earnings_all(season_df, 

331 title, 

332 theme, 

333 skip_zero=True): 

334 if skip_zero: 

335 season_df = season_df.loc[:, (season_df.sum(axis=0) != 0.0)] 

336 

337 # Sort the columns alphabetically 

338 season_df = season_df.reindex(sorted(season_df.columns), axis=1) 

339 

340 markers = False if len(season_df) > 1 else True 

341 fig = px.line(season_df, 

342 x='season_id', 

343 y=season_df.columns, 

344 markers=markers, 

345 title=title) 

346 

347 fig.update_layout( 

348 template=theme, 

349 xaxis=dict( 

350 showgrid=True, 

351 gridwidth=1, 

352 tickvals=season_df.season_id, 

353 ), 

354 

355 ) 

356 return fig