AliceTt commited on
Commit
db975c4
·
1 Parent(s): ebcc2b0

fixed dropdown

Browse files
Files changed (2) hide show
  1. app.py +7 -0
  2. summary_test.py +134 -54
app.py CHANGED
@@ -183,6 +183,13 @@ with gr.Blocks() as demo:
183
  strong {
184
  color:black !important;
185
  }
 
 
 
 
 
 
 
186
 
187
  </style>
188
  """
 
183
  strong {
184
  color:black !important;
185
  }
186
+ .options.svelte-y6qw75 {
187
+ background-color: #FFFFFF;
188
+ border-color: #000000;
189
+ }
190
+ .item.svelte-y6qw75:hover,.active.svelte-y6qw75 {
191
+ background: #c0c0c0;
192
+ }
193
 
194
  </style>
195
  """
summary_test.py CHANGED
@@ -3,7 +3,10 @@ import pandas as pd
3
  import pandas as pd
4
  import numpy as np
5
  from forecast import get_forecast_datasets, get_forecast_data
6
- from data_pipelines.historical_weather_data import download_historical_weather_data, aggregate_hourly_weather_data
 
 
 
7
  from utils.soil_utils import find_nearest_point_to_coordinates
8
  from utils.summary import get_meterological_summary, get_agricultural_yield_comparison
9
 
@@ -11,97 +14,175 @@ from utils.summary import get_meterological_summary, get_agricultural_yield_comp
11
  def get_meterological_past_data():
12
  download_historical_weather_data(latitude, longitude, start_year, end_year)
13
 
14
- def pre_process_data(scenario: str, lat: float = 47.0, lon:float = 5.0):
 
15
  start_year, end_year = 2010, 2025
16
 
17
- historical_df = aggregate_hourly_weather_data(download_historical_weather_data(latitude=lat, longitude=lon, start_year=start_year, end_year= end_year))
18
- forecast_df = get_forecast_data(scenario=scenario, longitude=lon, latitude=lat, shading_coef=0)
 
 
 
 
 
 
19
 
20
  forecast_df["time"] = pd.to_datetime(forecast_df["time"])
21
- forecast_df['year'] = forecast_df["time"].dt.year
22
- new_forecast_df = forecast_df.groupby(by="year", as_index=False).mean().reset_index()
 
 
23
  # new_forecast_df = new_forecast_df[new_forecast_df["year"] > 2025]
24
 
25
- historical_df = historical_df.reset_index().rename(columns={"index": "time"}).sort_values(by="time")
 
 
 
 
26
  historical_df["year"] = historical_df["time"].dt.year
27
- historical_df["precipitation"] = historical_df["precipitation"] / 3600 # to transform the data to kg m2 per s
 
 
28
 
29
- new_historical_df = historical_df.groupby(by="year", as_index=False).mean().reset_index()
 
 
30
  new_historical_df = new_historical_df[new_historical_df["year"] < 2024]
31
 
32
  return new_historical_df, new_forecast_df
33
 
34
- def process_all_data_for_meterological_summary(historical_data: pd.DataFrame, forecast_data: pd.DataFrame):
35
-
36
-
37
- temperature_df = pd.concat([historical_data[["year", "air_temperature_mean"]].rename(columns={"air_temperature_mean": "Near Surface Air Temperature (°C)"}),
38
- forecast_data[["year", "Near Surface Air Temperature (°C)"]]], axis=0)
39
 
40
- irradiance_df = pd.concat([historical_data[["year", "irradiance"]].rename(columns={"irradiance": "Surface Downwelling Shortwave Radiation (W/m²)"}),
41
- forecast_data[["year", "Surface Downwelling Shortwave Radiation (W/m²)"]]], axis=0)
42
-
43
- rain_df = pd.concat([historical_data[["year", "precipitation"]].rename(columns={"precipitation": "Precipitation (kg m-2 s-1)"}),
44
- forecast_data[["year", "Precipitation (kg m-2 s-1)"]]], axis=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  return temperature_df, rain_df, irradiance_df
47
 
48
- def get_yield_data(region: str = "Bourgogne-Franche-Comté", culture: str ="Blé tendre d'hiver"):
 
 
 
49
 
50
  yield_past_data = pd.read_csv("data/data_yield/data_rendement.csv")
51
  # yield_forecast_data = pd.read_csv("data/data_yield/data_rendement.csv")
52
- yield_past_data = yield_past_data[(yield_past_data["LIB_REG2"]==region) & (yield_past_data["LIB_SAA"].str.contains(culture)) ]
53
- yield_past_data = yield_past_data[["LIB_REG2", "LIB_SAA"]+ [col for col in yield_past_data.columns if 'REND' in col ]]
 
 
 
 
 
 
54
  # Transformation
55
- yield_past_data = yield_past_data.melt(id_vars=["LIB_REG2", "LIB_SAA"], var_name="year", value_name="past_yield")
 
 
56
 
57
  # Nettoyer la colonne "temps" pour enlever "REND_"
58
- yield_past_data["year"] = yield_past_data["year"].str.replace("REND_", "").astype(int)
 
 
59
 
60
  yield_forecast_data = pd.read_csv("data/data_yield/rendement_forecast.csv")
61
- yield_forecast_data = yield_forecast_data[yield_forecast_data["culture"].str.contains(culture)]
62
- return yield_past_data[["year", "past_yield"]], yield_forecast_data[["year", "yield_simple_forecast", "yield_with_shading_forecast"]]
 
 
 
 
 
 
 
63
 
64
 
65
  def get_summaries():
66
  scenario = "pessimist"
67
  lat, lon = 47.0, 5.0
68
- culture = "Blé tendre d'hiver"
69
  region = "Bourgogne-Franche-Comté"
70
 
71
  historical_df, forecast_df = pre_process_data(scenario, lat, lon)
72
 
73
- temperature_df, rain_df, irradiance_df = process_all_data_for_meterological_summary(historical_df, forecast_df)
74
-
 
 
75
  #######@
76
- meterological_summary = get_meterological_summary(scenario=scenario,
77
- temperature_df=temperature_df,
78
- irradiance_df=irradiance_df,
79
- rain_df=rain_df)
 
 
80
  print(meterological_summary)
81
 
82
- climate_data = temperature_df.merge(rain_df, on='year').merge(irradiance_df, on='year') # meteo ok
83
- closest_soil_data = find_nearest_point_to_coordinates(latitude=lat, longitude=lon) # soil ok
 
 
 
 
84
  water_deficit_data = forecast_df[["year", "Water Deficit (mm/day)"]]
85
  ############ forecast data PV ############
86
- forecast_df_pv = get_forecast_data(scenario=scenario, longitude=lon, latitude=lat, shading_coef=0.2)
 
 
87
  forecast_df_pv["time"] = pd.to_datetime(forecast_df_pv["time"])
88
- forecast_df_pv['year'] = forecast_df_pv["time"].dt.year
89
- water_deficit_data_pv = forecast_df_pv.groupby(by="year", as_index=False).mean().reset_index()[["year", "Water Deficit (mm/day)"]]
90
-
 
 
 
 
91
  # add a step to transform gps coordinates into french region to be able to filter yield data
92
- yield_past_data, yield_forecast_data = get_yield_data(region=region, culture=culture)
 
 
93
  print(yield_forecast_data.tail())
94
 
95
  # rendement (avec et sans ombrage)
96
-
97
- second_summary = get_agricultural_yield_comparison(culture=culture,
98
- region="bourgogne franche comté",
99
- water_df=water_deficit_data,
100
- water_df_pv = water_deficit_data_pv,
101
- climate_df=climate_data,
102
- soil_df=closest_soil_data,
103
- forecast_yield_df=yield_forecast_data,
104
- historical_yield_df=yield_past_data)
 
 
105
 
106
  print(yield_forecast_data.tail())
107
  print(second_summary)
@@ -155,11 +236,10 @@ def get_summaries():
155
  # })
156
  # water_deficit_data = pd.DataFrame()
157
  # climate_data = pd.DataFrame()
158
-
159
- # print(get_agricultural_yield_comparison(culture="orge",
160
  # region="bourgogne franche comté",
161
- # water_df=water_deficit_data,
162
  # climate_df=climate_data,
163
- # soil_df=closest_soil_features,
164
  # agri_yield_df=df))
165
-
 
3
  import pandas as pd
4
  import numpy as np
5
  from forecast import get_forecast_datasets, get_forecast_data
6
+ from data_pipelines.historical_weather_data import (
7
+ download_historical_weather_data,
8
+ aggregate_hourly_weather_data,
9
+ )
10
  from utils.soil_utils import find_nearest_point_to_coordinates
11
  from utils.summary import get_meterological_summary, get_agricultural_yield_comparison
12
 
 
14
  def get_meterological_past_data():
15
  download_historical_weather_data(latitude, longitude, start_year, end_year)
16
 
17
+
18
+ def pre_process_data(scenario: str, lat: float = 47.0, lon: float = 5.0):
19
  start_year, end_year = 2010, 2025
20
 
21
+ historical_df = aggregate_hourly_weather_data(
22
+ download_historical_weather_data(
23
+ latitude=lat, longitude=lon, start_year=start_year, end_year=end_year
24
+ )
25
+ )
26
+ forecast_df = get_forecast_data(
27
+ scenario=scenario, longitude=lon, latitude=lat, shading_coef=0
28
+ )
29
 
30
  forecast_df["time"] = pd.to_datetime(forecast_df["time"])
31
+ forecast_df["year"] = forecast_df["time"].dt.year
32
+ new_forecast_df = (
33
+ forecast_df.groupby(by="year", as_index=False).mean().reset_index()
34
+ )
35
  # new_forecast_df = new_forecast_df[new_forecast_df["year"] > 2025]
36
 
37
+ historical_df = (
38
+ historical_df.reset_index()
39
+ .rename(columns={"index": "time"})
40
+ .sort_values(by="time")
41
+ )
42
  historical_df["year"] = historical_df["time"].dt.year
43
+ historical_df["precipitation"] = (
44
+ historical_df["precipitation"] / 3600
45
+ ) # to transform the data to kg m2 per s
46
 
47
+ new_historical_df = (
48
+ historical_df.groupby(by="year", as_index=False).mean().reset_index()
49
+ )
50
  new_historical_df = new_historical_df[new_historical_df["year"] < 2024]
51
 
52
  return new_historical_df, new_forecast_df
53
 
 
 
 
 
 
54
 
55
+ def process_all_data_for_meterological_summary(
56
+ historical_data: pd.DataFrame, forecast_data: pd.DataFrame
57
+ ):
58
+
59
+ temperature_df = pd.concat(
60
+ [
61
+ historical_data[["year", "air_temperature_mean"]].rename(
62
+ columns={"air_temperature_mean": "Near Surface Air Temperature (°C)"}
63
+ ),
64
+ forecast_data[["year", "Near Surface Air Temperature (°C)"]],
65
+ ],
66
+ axis=0,
67
+ )
68
+
69
+ irradiance_df = pd.concat(
70
+ [
71
+ historical_data[["year", "irradiance"]].rename(
72
+ columns={"irradiance": "Surface Downwelling Shortwave Radiation (W/m²)"}
73
+ ),
74
+ forecast_data[["year", "Surface Downwelling Shortwave Radiation (W/m²)"]],
75
+ ],
76
+ axis=0,
77
+ )
78
+
79
+ rain_df = pd.concat(
80
+ [
81
+ historical_data[["year", "precipitation"]].rename(
82
+ columns={"precipitation": "Precipitation (kg m-2 s-1)"}
83
+ ),
84
+ forecast_data[["year", "Precipitation (kg m-2 s-1)"]],
85
+ ],
86
+ axis=0,
87
+ )
88
 
89
  return temperature_df, rain_df, irradiance_df
90
 
91
+
92
+ def get_yield_data(
93
+ region: str = "Bourgogne-Franche-Comté", culture: str = "Blé tendre d'hiver"
94
+ ):
95
 
96
  yield_past_data = pd.read_csv("data/data_yield/data_rendement.csv")
97
  # yield_forecast_data = pd.read_csv("data/data_yield/data_rendement.csv")
98
+ yield_past_data = yield_past_data[
99
+ (yield_past_data["LIB_REG2"] == region)
100
+ & (yield_past_data["LIB_SAA"].str.contains("Colza grain d'hiver"))
101
+ ]
102
+ yield_past_data = yield_past_data[
103
+ ["LIB_REG2", "LIB_SAA"]
104
+ + [col for col in yield_past_data.columns if "REND" in col]
105
+ ]
106
  # Transformation
107
+ yield_past_data = yield_past_data.melt(
108
+ id_vars=["LIB_REG2", "LIB_SAA"], var_name="year", value_name="past_yield"
109
+ )
110
 
111
  # Nettoyer la colonne "temps" pour enlever "REND_"
112
+ yield_past_data["year"] = (
113
+ yield_past_data["year"].str.replace("REND_", "").astype(int)
114
+ )
115
 
116
  yield_forecast_data = pd.read_csv("data/data_yield/rendement_forecast.csv")
117
+ yield_forecast_data = yield_forecast_data[
118
+ yield_forecast_data["culture"].str.contains(culture)
119
+ ]
120
+ return (
121
+ yield_past_data[["year", "past_yield"]],
122
+ yield_forecast_data[
123
+ ["year", "yield_simple_forecast", "yield_with_shading_forecast"]
124
+ ],
125
+ )
126
 
127
 
128
  def get_summaries():
129
  scenario = "pessimist"
130
  lat, lon = 47.0, 5.0
131
+ culture = "Colza d'hiver"
132
  region = "Bourgogne-Franche-Comté"
133
 
134
  historical_df, forecast_df = pre_process_data(scenario, lat, lon)
135
 
136
+ temperature_df, rain_df, irradiance_df = process_all_data_for_meterological_summary(
137
+ historical_df, forecast_df
138
+ )
139
+
140
  #######@
141
+ meterological_summary = get_meterological_summary(
142
+ scenario=scenario,
143
+ temperature_df=temperature_df,
144
+ irradiance_df=irradiance_df,
145
+ rain_df=rain_df,
146
+ )
147
  print(meterological_summary)
148
 
149
+ climate_data = temperature_df.merge(rain_df, on="year").merge(
150
+ irradiance_df, on="year"
151
+ ) # meteo ok
152
+ closest_soil_data = find_nearest_point_to_coordinates(
153
+ latitude=lat, longitude=lon
154
+ ) # soil ok
155
  water_deficit_data = forecast_df[["year", "Water Deficit (mm/day)"]]
156
  ############ forecast data PV ############
157
+ forecast_df_pv = get_forecast_data(
158
+ scenario=scenario, longitude=lon, latitude=lat, shading_coef=0.2
159
+ )
160
  forecast_df_pv["time"] = pd.to_datetime(forecast_df_pv["time"])
161
+ forecast_df_pv["year"] = forecast_df_pv["time"].dt.year
162
+ water_deficit_data_pv = (
163
+ forecast_df_pv.groupby(by="year", as_index=False)
164
+ .mean()
165
+ .reset_index()[["year", "Water Deficit (mm/day)"]]
166
+ )
167
+
168
  # add a step to transform gps coordinates into french region to be able to filter yield data
169
+ yield_past_data, yield_forecast_data = get_yield_data(
170
+ region=region, culture=culture
171
+ )
172
  print(yield_forecast_data.tail())
173
 
174
  # rendement (avec et sans ombrage)
175
+
176
+ second_summary = get_agricultural_yield_comparison(
177
+ culture=culture,
178
+ region="bourgogne franche comté",
179
+ water_df=water_deficit_data,
180
+ water_df_pv=water_deficit_data_pv,
181
+ climate_df=climate_data,
182
+ soil_df=closest_soil_data,
183
+ forecast_yield_df=yield_forecast_data,
184
+ historical_yield_df=yield_past_data,
185
+ )
186
 
187
  print(yield_forecast_data.tail())
188
  print(second_summary)
 
236
  # })
237
  # water_deficit_data = pd.DataFrame()
238
  # climate_data = pd.DataFrame()
239
+
240
+ # print(get_agricultural_yield_comparison(culture="orge",
241
  # region="bourgogne franche comté",
242
+ # water_df=water_deficit_data,
243
  # climate_df=climate_data,
244
+ # soil_df=closest_soil_features,
245
  # agri_yield_df=df))