Spaces:
Runtime error
Runtime error
Hugo Massonnat commited on
Commit ·
d93b3a4
1
Parent(s): f0b11dd
change yield plot function for plots with no shading
Browse files- compute_yield.py +35 -59
compute_yield.py
CHANGED
|
@@ -131,7 +131,7 @@ def plot_yield(
|
|
| 131 |
culture: str = "Colza d'hiver",
|
| 132 |
region: str = "Bourgogne-Franche-Comté",
|
| 133 |
scenario: str = "pessimist",
|
| 134 |
-
shading_coef: float = 0.,
|
| 135 |
) -> plt.Figure:
|
| 136 |
monthly_forecast = compute_yield_forecast(
|
| 137 |
latitude=latitude,
|
|
@@ -141,33 +141,38 @@ def plot_yield(
|
|
| 141 |
shading_coef=0.,
|
| 142 |
)
|
| 143 |
|
| 144 |
-
monthly_forecast_with_shading = compute_yield_forecast(
|
| 145 |
-
latitude=latitude,
|
| 146 |
-
longitude=longitude,
|
| 147 |
-
culture=culture,
|
| 148 |
-
scenario=scenario,
|
| 149 |
-
shading_coef=shading_coef,
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
yield_forecast = get_annual_yield(monthly_forecast)
|
| 153 |
-
yield_forecast_with_shading = get_annual_yield(monthly_forecast_with_shading)
|
| 154 |
-
|
| 155 |
n_years = 10
|
| 156 |
-
years = 2025 + np.arange(len(
|
|
|
|
| 157 |
aggregated_forecasts = yield_forecast.rolling(n_years).sum()[years % n_years == 0]
|
| 158 |
-
aggregated_forecasts_with_shading = yield_forecast_with_shading.rolling(n_years).sum()[years % n_years == 0]
|
| 159 |
|
| 160 |
width = 3 # the width of the bars
|
| 161 |
fig, ax = plt.subplots(layout='constrained')
|
| 162 |
-
aggregated_years = years[years % n_years == 0]
|
| 163 |
_ = ax.bar(aggregated_years, aggregated_forecasts, width, label="No shading")
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
ax.legend()
|
|
|
|
|
|
|
| 166 |
ax.set_ylim(150)
|
| 167 |
|
| 168 |
return fig
|
| 169 |
|
| 170 |
if __name__ == '__main__':
|
|
|
|
|
|
|
| 171 |
cultures = ["Colza d'hiver", "Blé tendre d'hiver", "Orge d'hiver"]
|
| 172 |
dfs = []
|
| 173 |
for culture in cultures:
|
|
@@ -180,14 +185,6 @@ if __name__ == '__main__':
|
|
| 180 |
scenario=scenario,
|
| 181 |
shading_coef=0.,
|
| 182 |
)
|
| 183 |
-
# print(monthly_forecast.head())
|
| 184 |
-
|
| 185 |
-
yield_forecast = get_annual_yield(monthly_forecast)
|
| 186 |
-
yield_forecast_df = yield_forecast.reset_index()
|
| 187 |
-
yield_forecast_df.columns = ["time", "yield_simple_forecast"]
|
| 188 |
-
yield_forecast_df["year"] = yield_forecast_df["time"].dt.year
|
| 189 |
-
|
| 190 |
-
print(yield_forecast_df.head())
|
| 191 |
|
| 192 |
monthly_forecast_with_shading = compute_yield_forecast(
|
| 193 |
latitude=47,
|
|
@@ -196,43 +193,22 @@ if __name__ == '__main__':
|
|
| 196 |
scenario=scenario,
|
| 197 |
shading_coef=shading_coef,
|
| 198 |
)
|
| 199 |
-
# print(monthly_forecast_with_shading.head())
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
years = 2025 + np.arange(len(yield_forecast_with_shading))
|
| 204 |
-
aggregated_forecasts = yield_forecast.rolling(n_years).sum()[years % n_years == 0]
|
| 205 |
-
aggregated_forecasts_with_shading = yield_forecast_with_shading.rolling(n_years).sum()[years % n_years == 0]
|
| 206 |
-
# plt.plot(yield_forecast.rolling(n_years).sum(), label="No shading")
|
| 207 |
-
# plt.plot(yield_forecast_with_shading.rolling(n_years).sum(), label="20% Shading")
|
| 208 |
-
# plt.bar(years[years % n_years == 0], aggregated_forecasts, label="No shading")
|
| 209 |
-
# plt.bar(years[years % n_years == 0], aggregated_forecasts_with_shading, label="20% Shading")
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
aggregated_years = years[years % n_years == 0]
|
| 215 |
-
rects = ax.bar(aggregated_years, aggregated_forecasts, width, label="No shading")
|
| 216 |
-
rects2 = ax.bar(aggregated_years + width, aggregated_forecasts_with_shading, width, label="20% shading")
|
| 217 |
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
plt.show()
|
| 221 |
-
=======
|
| 222 |
-
yield_forecast_with_shading = get_annual_yield(monthly_forecast_with_shading)
|
| 223 |
-
yield_forecast_with_shading_df = yield_forecast_with_shading.reset_index()
|
| 224 |
-
yield_forecast_with_shading_df.columns = ["time", "yield_with_shading_forecast"]
|
| 225 |
-
yield_forecast_with_shading_df["year"] = yield_forecast_with_shading_df["time"].dt.year
|
| 226 |
-
final_df = pd.merge(yield_forecast_df[["year", "yield_simple_forecast"]], yield_forecast_with_shading_df[["year", "yield_with_shading_forecast"]], on="year")
|
| 227 |
-
final_df["culture"] = culture
|
| 228 |
-
dfs.append(final_df)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
result = pd.concat(dfs, axis=0)
|
| 232 |
-
result.to_csv("data/data_yield/rendement_forecast.csv", index=False)
|
| 233 |
-
|
| 234 |
-
# plt.plot(yield_forecast.rolling(5).mean(), label="No shading")
|
| 235 |
-
# plt.plot(yield_forecast_with_shading.rolling(5).mean(), label="20% Shading")
|
| 236 |
-
# plt.legend()
|
| 237 |
-
# plt.show()
|
| 238 |
-
>>>>>>> Stashed changes
|
|
|
|
| 131 |
culture: str = "Colza d'hiver",
|
| 132 |
region: str = "Bourgogne-Franche-Comté",
|
| 133 |
scenario: str = "pessimist",
|
| 134 |
+
shading_coef: float = 0.2,
|
| 135 |
) -> plt.Figure:
|
| 136 |
monthly_forecast = compute_yield_forecast(
|
| 137 |
latitude=latitude,
|
|
|
|
| 141 |
shading_coef=0.,
|
| 142 |
)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
yield_forecast = get_annual_yield(monthly_forecast)
|
|
|
|
|
|
|
| 145 |
n_years = 10
|
| 146 |
+
years = 2025 + np.arange(len(yield_forecast))
|
| 147 |
+
aggregated_years = years[years % n_years == 0]
|
| 148 |
aggregated_forecasts = yield_forecast.rolling(n_years).sum()[years % n_years == 0]
|
|
|
|
| 149 |
|
| 150 |
width = 3 # the width of the bars
|
| 151 |
fig, ax = plt.subplots(layout='constrained')
|
|
|
|
| 152 |
_ = ax.bar(aggregated_years, aggregated_forecasts, width, label="No shading")
|
| 153 |
+
|
| 154 |
+
if shading_coef > 0:
|
| 155 |
+
monthly_forecast_with_shading = compute_yield_forecast(
|
| 156 |
+
latitude=latitude,
|
| 157 |
+
longitude=longitude,
|
| 158 |
+
culture=culture,
|
| 159 |
+
scenario=scenario,
|
| 160 |
+
shading_coef=shading_coef,
|
| 161 |
+
)
|
| 162 |
+
yield_forecast_with_shading = get_annual_yield(monthly_forecast_with_shading)
|
| 163 |
+
aggregated_forecasts_with_shading = yield_forecast_with_shading.rolling(n_years).sum()[years % n_years == 0]
|
| 164 |
+
_ = ax.bar(aggregated_years + width, aggregated_forecasts_with_shading, width, label="20% shading")
|
| 165 |
+
|
| 166 |
ax.legend()
|
| 167 |
+
ax.set_xlabel("Année")
|
| 168 |
+
ax.set_ylabel(f"Production agricole de {culture} estimée (quintal / ha)")
|
| 169 |
ax.set_ylim(150)
|
| 170 |
|
| 171 |
return fig
|
| 172 |
|
| 173 |
if __name__ == '__main__':
|
| 174 |
+
latitude = 47
|
| 175 |
+
longitude = 5
|
| 176 |
cultures = ["Colza d'hiver", "Blé tendre d'hiver", "Orge d'hiver"]
|
| 177 |
dfs = []
|
| 178 |
for culture in cultures:
|
|
|
|
| 185 |
scenario=scenario,
|
| 186 |
shading_coef=0.,
|
| 187 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
monthly_forecast_with_shading = compute_yield_forecast(
|
| 190 |
latitude=47,
|
|
|
|
| 193 |
scenario=scenario,
|
| 194 |
shading_coef=shading_coef,
|
| 195 |
)
|
|
|
|
| 196 |
|
| 197 |
+
fig = plot_yield(latitude, longitude, culture, scenario="pessimist", shading_coef=shading_coef)
|
| 198 |
+
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
# yield_forecast = get_annual_yield(monthly_forecast)
|
| 201 |
+
# yield_forecast_df = yield_forecast.reset_index()
|
| 202 |
+
# yield_forecast_df.columns = ["time", "yield_simple_forecast"]
|
| 203 |
+
# yield_forecast_df["year"] = yield_forecast_df["time"].dt.year
|
| 204 |
+
# yield_forecast_with_shading = get_annual_yield(monthly_forecast_with_shading)
|
| 205 |
+
# yield_forecast_with_shading_df = yield_forecast_with_shading.reset_index()
|
| 206 |
+
# yield_forecast_with_shading_df.columns = ["time", "yield_with_shading_forecast"]
|
| 207 |
+
# yield_forecast_with_shading_df["year"] = yield_forecast_with_shading_df["time"].dt.year
|
| 208 |
+
# final_df = pd.merge(yield_forecast_df[["year", "yield_simple_forecast"]], yield_forecast_with_shading_df[["year", "yield_with_shading_forecast"]], on="year")
|
| 209 |
+
# final_df["culture"] = culture
|
| 210 |
+
# dfs.append(final_df)
|
| 211 |
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
+
# result = pd.concat(dfs, axis=0)
|
| 214 |
+
# result.to_csv("data/data_yield/rendement_forecast.csv", index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|