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

57 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 

4 

5 

6def plot_land_all(land_df, theme): 

7 land_df.sort_values('created_date', inplace=True) 

8 

9 fig = px.bar(land_df, 

10 x='created_date', 

11 y='received_amount', 

12 color='resource_symbol', 

13 barmode='group', 

14 title='Amount harvest', 

15 labels={ 

16 "received_amount": "Received amount (log)", 

17 "created_date": "Harvest date", 

18 "resource_symbol": "Resource" 

19 }) 

20 fig.update_layout( 

21 template=theme, 

22 xaxis=dict( 

23 title="Date", 

24 showgrid=True, 

25 gridwidth=1, 

26 ), 

27 yaxis=dict( 

28 title="amount (log)", 

29 type="log", 

30 exponentformat="none", 

31 dtick=1, 

32 ), 

33 ) 

34 return fig 

35 

36 

37def plot_land_pools(df, theme): 

38 df.sort_values('date', inplace=True) 

39 

40 tokens = df['token'].unique() 

41 figures = {} 

42 

43 color_value = '#1f77b4' 

44 color_resource = '#ff7f0e' 

45 color_dec = '#2ca02c' 

46 

47 for token in tokens: 

48 df_token = df[df['token'] == token] 

49 

50 # Calculate max values for y-axis ranges 

51 max_value = df_token['value'].max() * 1.1 

52 max_my_resource_quantity = df_token['my_resource_quantity'].max() * 1.1 

53 max_my_dec_quantity = df_token['my_dec_quantity'].max() * 1.1 

54 

55 fig = go.Figure() 

56 

57 fig.add_trace( 

58 go.Scatter(x=df_token['date'], 

59 y=df_token['value'], 

60 mode='lines+markers', 

61 line=dict(color=color_value), 

62 marker=dict(color=color_value), 

63 name=f'{token} Value', 

64 yaxis='y1') 

65 ) 

66 fig.add_trace( 

67 go.Scatter(x=df_token['date'], 

68 y=df_token['my_resource_quantity'], 

69 mode='lines+markers', 

70 line=dict(color=color_resource), 

71 marker=dict(color=color_resource), 

72 name=f'{token} Resource Quantity', 

73 yaxis='y2') 

74 ) 

75 fig.add_trace( 

76 go.Scatter(x=df_token['date'], 

77 y=df_token['my_dec_quantity'], 

78 mode='lines+markers', 

79 line=dict(color=color_dec), 

80 marker=dict(color=color_dec), 

81 name=f'{token} DEC Quantity', 

82 yaxis='y3') 

83 ) 

84 

85 fig.update_layout( 

86 template=theme, 

87 title=f'Pool Information for {token}', 

88 xaxis=dict(title='Date'), 

89 yaxis=dict( 

90 title='Value ($)', 

91 titlefont=dict(color=color_value), 

92 tickfont=dict(color=color_value), 

93 side='right', 

94 range=[0, max_value], 

95 showgrid=True, 

96 zeroline=True 

97 ), 

98 yaxis2=dict( 

99 title='Resource Quantity', 

100 titlefont=dict(color=color_resource), 

101 tickfont=dict(color=color_resource), 

102 anchor="free", 

103 overlaying="y", 

104 side="left", 

105 position=0, 

106 range=[0, max_my_resource_quantity], 

107 showgrid=False, 

108 zeroline=True 

109 ), 

110 yaxis3=dict( 

111 title='DEC Quantity', 

112 titlefont=dict(color=color_dec), 

113 tickfont=dict(color=color_dec), 

114 anchor="free", 

115 overlaying="y", 

116 side="left", 

117 position=0.1, 

118 range=[0, max_my_dec_quantity], 

119 showgrid=False, 

120 zeroline=True 

121 ), 

122 legend=dict(x=1.1, y=1, traceorder='grouped'), 

123 hovermode='x unified' 

124 ) 

125 

126 figures[token] = fig 

127 

128 return figures # Return a dictionary of figures 

129 

130 

131def plot_cumsum(land_df, theme): 

132 land_df = land_df.pivot(index='created_date', columns='resource_symbol', 

133 values=['received_amount', 'grain_eaten', 'grain_rewards_eaten', 'resource_amount', 

134 'tax_amount']) 

135 land_df = land_df.fillna(0) 

136 

137 result_df = pd.DataFrame() 

138 

139 if ('received_amount', 'GRAIN') in land_df.columns: 

140 land_df['GRAIN_earned'] = (land_df[('received_amount', 'GRAIN')] 

141 + land_df[('grain_rewards_eaten', 'GRAIN')] 

142 - land_df[('grain_eaten', 'GRAIN')]) 

143 if ('received_amount', 'SPS') in land_df.columns: 

144 land_df['GRAIN_earned'] = land_df['GRAIN_earned'] - land_df[('grain_eaten', 'SPS')] 

145 if ('received_amount', 'RESEARCH') in land_df.columns: 

146 land_df['GRAIN_earned'] = land_df['GRAIN_earned'] - land_df[('grain_eaten', 'RESEARCH')] 

147 

148 result_df['GRAIN'] = land_df['GRAIN_earned'].cumsum() 

149 

150 if ('received_amount', 'RESEARCH') in land_df.columns: 

151 land_df['RESEARCH_earned'] = land_df[('received_amount', 'RESEARCH')] 

152 result_df['RESEARCH'] = land_df['RESEARCH_earned'].cumsum() 

153 

154 if ('received_amount', 'SPS') in land_df.columns: 

155 land_df['SPS_earned'] = land_df[('received_amount', 'SPS')] 

156 result_df['SPS'] = land_df['SPS_earned'].cumsum() 

157 

158 # Plot with Plotly Express 

159 fig = px.line(result_df, 

160 x=result_df.index, 

161 y=result_df.columns, 

162 title='Cumulative sum of the resources', 

163 labels={'variable': 'Resource'}) 

164 

165 fig.update_layout( 

166 template=theme, 

167 xaxis=dict( 

168 title="Date", 

169 showgrid=True, 

170 gridwidth=1, 

171 ), 

172 yaxis=dict( 

173 title="amount (log)", 

174 type="log", 

175 exponentformat="none", 

176 dtick=1, 

177 ), 

178 ) 

179 return fig 

180 

181 

182def plot_tax_cumsum(land_df, theme): 

183 land_df.columns = land_df.columns.str.replace('_received_tax', '') 

184 

185 numeric_columns = land_df.select_dtypes(include='number').columns 

186 land_df_cumsum = land_df[numeric_columns].cumsum() 

187 

188 fig = px.bar(land_df, 

189 x='created_date', 

190 y=numeric_columns, 

191 barmode='group', 

192 title='Received taxes including cumulative sum', 

193 labels={'variable': 'Resource'}) 

194 

195 for col in numeric_columns: 

196 mode = 'lines' if land_df_cumsum[col].size > 1 else 'markers' 

197 fig.add_scatter(x=land_df.created_date, 

198 y=land_df_cumsum[col], 

199 mode=mode, 

200 name=f'{col} Cumulative Sum') 

201 

202 fig.update_layout( 

203 template=theme, 

204 xaxis=dict( 

205 title="Date", 

206 showgrid=True, 

207 gridwidth=1, 

208 ), 

209 yaxis=dict( 

210 title="amount (log)", 

211 type="log", 

212 exponentformat="none", 

213 dtick=1, 

214 ), 

215 ) 

216 return fig