Spaces:
Runtime error
Runtime error
Akram Sanad
commited on
Commit
·
bfa0967
1
Parent(s):
61ad012
adding pessimistic scenario
Browse files- visualize/visualize.py +164 -67
visualize/visualize.py
CHANGED
@@ -6,11 +6,13 @@ from data_pipelines.historical_weather_data import (
|
|
6 |
download_historical_weather_data,
|
7 |
aggregate_hourly_weather_data,
|
8 |
)
|
|
|
|
|
9 |
|
10 |
|
11 |
-
def concatenate_historic_forecast(historic, forecast, cols_to_keep):
|
12 |
historic["period"] = "historique"
|
13 |
-
forecast["period"] =
|
14 |
historic = historic[cols_to_keep]
|
15 |
forecast = forecast[cols_to_keep]
|
16 |
full_data = pd.concat([historic, forecast])
|
@@ -20,6 +22,7 @@ def concatenate_historic_forecast(historic, forecast, cols_to_keep):
|
|
20 |
def visualize_climate(
|
21 |
moderate: pd.DataFrame,
|
22 |
historic: pd.DataFrame,
|
|
|
23 |
x_axis="year",
|
24 |
column: str = "Precipitation (mm)",
|
25 |
cols_to_keep: List[str] = [
|
@@ -30,62 +33,146 @@ def visualize_climate(
|
|
30 |
"period",
|
31 |
],
|
32 |
):
|
33 |
-
|
34 |
-
|
35 |
|
36 |
fig = go.Figure()
|
37 |
-
# colors = {"historique": "blue", "forecast": "purple"}
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
fig.add_trace(
|
42 |
go.Scatter(
|
43 |
-
x=
|
44 |
-
y=
|
45 |
mode="lines",
|
46 |
-
name=
|
47 |
-
line=dict(color="
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
)
|
50 |
-
|
51 |
-
interpolation_df = concatenated_df[concatenated_df[x_axis] > 2023]
|
52 |
-
fig.add_trace(
|
53 |
-
go.Scatter(
|
54 |
-
x=interpolation_df[x_axis],
|
55 |
-
y=interpolation_df[column].interpolate(),
|
56 |
-
mode="lines",
|
57 |
-
line=dict(color="blue"),
|
58 |
-
showlegend=False,
|
59 |
-
),
|
60 |
-
)
|
61 |
-
|
62 |
-
fig.update_layout(
|
63 |
-
title=f"Historique et Forecast pour {column}",
|
64 |
-
xaxis_title="Date",
|
65 |
-
yaxis_title=column,
|
66 |
-
)
|
67 |
|
68 |
return fig
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
df[col_to_agg] = df.groupby("year")[col_to_agg].transform("mean")
|
73 |
return df
|
74 |
|
75 |
|
76 |
def generate_plots(
|
77 |
moderate: pd.DataFrame,
|
78 |
historic: pd.DataFrame,
|
|
|
79 |
x_axes: List[str],
|
80 |
cols_to_plot: List[str],
|
81 |
):
|
82 |
plots = []
|
83 |
for i, col in enumerate(cols_to_plot):
|
84 |
-
plots.append(visualize_climate(moderate, historic, x_axes[i], col))
|
85 |
return plots
|
86 |
|
87 |
-
|
88 |
def get_plots():
|
|
|
|
|
|
|
|
|
|
|
89 |
cols_to_keep = [
|
90 |
"Precipitation (mm)",
|
91 |
"Near Surface Air Temperature (°C)",
|
@@ -93,30 +180,11 @@ def get_plots():
|
|
93 |
"year",
|
94 |
"period",
|
95 |
]
|
96 |
-
cols_to_plot = [
|
97 |
-
"Precipitation (mm)",
|
98 |
-
"Near Surface Air Temperature (°C)",
|
99 |
-
"Surface Downwelling Shortwave Radiation (W/m²)",
|
100 |
-
]
|
101 |
x_axes = ["year"] * len(cols_to_plot)
|
102 |
-
|
103 |
latitude = 47
|
104 |
longitude = 5
|
105 |
start_year = 2000
|
106 |
end_year = 2025
|
107 |
-
cols_to_keep = [
|
108 |
-
"Precipitation (mm)",
|
109 |
-
"Near Surface Air Temperature (°C)",
|
110 |
-
"Surface Downwelling Shortwave Radiation (W/m²)",
|
111 |
-
"year",
|
112 |
-
"period",
|
113 |
-
]
|
114 |
-
cols_to_plot = [
|
115 |
-
"Precipitation (mm)",
|
116 |
-
"Near Surface Air Temperature (°C)",
|
117 |
-
"Surface Downwelling Shortwave Radiation (W/m²)",
|
118 |
-
]
|
119 |
-
x_axes = ["year"] * len(cols_to_plot)
|
120 |
|
121 |
df = download_historical_weather_data(latitude, longitude, start_year, end_year)
|
122 |
historic = aggregate_hourly_weather_data(df)
|
@@ -129,22 +197,51 @@ def get_plots():
|
|
129 |
"index": "time",
|
130 |
}
|
131 |
)
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
moderate["time"] = pd.to_datetime(moderate["time"])
|
138 |
-
moderate = moderate.sort_values(
|
139 |
-
moderate[
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
for col in cols_to_plot:
|
143 |
moderate = aggregate_yearly(moderate, col)
|
144 |
historic = aggregate_yearly(historic, col)
|
145 |
-
|
146 |
-
plots = generate_plots(
|
147 |
-
moderate, historic, x_axes, cols_to_plot
|
148 |
-
) # List of 3 plots to show
|
149 |
-
|
150 |
return plots
|
|
|
6 |
download_historical_weather_data,
|
7 |
aggregate_hourly_weather_data,
|
8 |
)
|
9 |
+
import os
|
10 |
+
from forecast import get_forecast_datasets, get_forecast_data
|
11 |
|
12 |
|
13 |
+
def concatenate_historic_forecast(historic, forecast, cols_to_keep, value_period_col = "forecast scénario modéré"):
|
14 |
historic["period"] = "historique"
|
15 |
+
forecast["period"] = value_period_col
|
16 |
historic = historic[cols_to_keep]
|
17 |
forecast = forecast[cols_to_keep]
|
18 |
full_data = pd.concat([historic, forecast])
|
|
|
22 |
def visualize_climate(
|
23 |
moderate: pd.DataFrame,
|
24 |
historic: pd.DataFrame,
|
25 |
+
pessimist: pd.DataFrame,
|
26 |
x_axis="year",
|
27 |
column: str = "Precipitation (mm)",
|
28 |
cols_to_keep: List[str] = [
|
|
|
33 |
"period",
|
34 |
],
|
35 |
):
|
36 |
+
concatenated_moderate = concatenate_historic_forecast(historic, moderate, cols_to_keep)
|
37 |
+
concatenated_moderate = concatenated_moderate.sort_values(by=x_axis) # Ensure order
|
38 |
|
39 |
fig = go.Figure()
|
|
|
40 |
|
41 |
+
if column == "Precipitation (mm)":
|
42 |
+
for condition_value in concatenated_moderate["period"].unique():
|
43 |
+
segment = concatenated_moderate[concatenated_moderate["period"] == condition_value]
|
44 |
+
avg_precipitation = segment.groupby(x_axis)[column].mean().reset_index()
|
45 |
+
|
46 |
+
fig.add_trace(
|
47 |
+
go.Bar(
|
48 |
+
x=avg_precipitation[x_axis],
|
49 |
+
y=avg_precipitation[column],
|
50 |
+
name=f"{condition_value}",
|
51 |
+
marker=dict(color="blue" if condition_value == "historique" else "purple"),
|
52 |
+
)
|
53 |
+
)
|
54 |
+
|
55 |
+
concatenated_pessimist = concatenate_historic_forecast(historic, pessimist, cols_to_keep, "forecast scénario pessimiste")
|
56 |
+
concatenated_pessimist = concatenated_pessimist.sort_values(by=x_axis)
|
57 |
+
concatenated_pessimist = concatenated_pessimist[concatenated_pessimist["period"]!="historique"]
|
58 |
+
for condition_value in concatenated_pessimist["period"].unique():
|
59 |
+
segment = concatenated_pessimist[concatenated_pessimist["period"] == condition_value]
|
60 |
+
avg_precipitation = segment.groupby(x_axis)[column].mean().reset_index()
|
61 |
+
|
62 |
+
fig.add_trace(
|
63 |
+
go.Bar(
|
64 |
+
x=avg_precipitation[x_axis],
|
65 |
+
y=avg_precipitation[column],
|
66 |
+
name=f"{condition_value}",
|
67 |
+
marker=dict(color="orange" if condition_value != "historique" else "blue"),
|
68 |
+
)
|
69 |
+
)
|
70 |
+
|
71 |
+
# Update layout for bar chart
|
72 |
+
fig.update_layout(
|
73 |
+
title=f"Moyenne de {column} par année",
|
74 |
+
xaxis_title="Année", # Set the x-axis title to Year
|
75 |
+
yaxis_title="Précipitation (mm)", # Set the y-axis title to Precipitation
|
76 |
+
barmode='group' # Group bars for different conditions
|
77 |
+
)
|
78 |
+
|
79 |
+
else:
|
80 |
+
# For other columns, continue with the line plot as before
|
81 |
+
for condition_value in concatenated_moderate["period"].unique():
|
82 |
+
segment = concatenated_moderate[concatenated_moderate["period"] == condition_value]
|
83 |
+
if condition_value == "historique":
|
84 |
+
fig.add_trace(
|
85 |
+
go.Scatter(
|
86 |
+
x=segment[x_axis], # Years on x-axis
|
87 |
+
y=segment[column], # Precipitation values on y-axis
|
88 |
+
mode="lines",
|
89 |
+
name=f"{condition_value}",
|
90 |
+
line=dict(color="blue" if condition_value == "historique" else "purple"),
|
91 |
+
showlegend=False
|
92 |
+
)
|
93 |
+
)
|
94 |
+
else:
|
95 |
+
fig.add_trace(
|
96 |
+
go.Scatter(
|
97 |
+
x=segment[x_axis], # Years on x-axis
|
98 |
+
y=segment[column], # Precipitation values on y-axis
|
99 |
+
mode="lines",
|
100 |
+
name=f"{condition_value}",
|
101 |
+
line=dict(color="blue" if condition_value == "historique" else "purple"),
|
102 |
+
)
|
103 |
+
)
|
104 |
+
|
105 |
+
# Continue with pessimistic data as in the original function...
|
106 |
+
concatenated_pessimist = concatenate_historic_forecast(historic, pessimist, cols_to_keep, "forecast scénario pessimiste")
|
107 |
+
concatenated_pessimist = concatenated_pessimist.sort_values(by=x_axis)
|
108 |
+
for condition_value in concatenated_pessimist["period"].unique():
|
109 |
+
segment = concatenated_pessimist[concatenated_pessimist["period"] == condition_value]
|
110 |
+
fig.add_trace(
|
111 |
+
go.Scatter(
|
112 |
+
x=segment[x_axis], # Years on x-axis
|
113 |
+
y=segment[column], # Precipitation values on y-axis
|
114 |
+
mode="lines",
|
115 |
+
name=f"{condition_value}",
|
116 |
+
line=dict(color="blue" if condition_value == "historique" else "orange"),
|
117 |
+
)
|
118 |
+
)
|
119 |
+
|
120 |
+
# Interpolation for the pessimistic scenario...
|
121 |
+
interpolation_pessimist = concatenated_pessimist[concatenated_pessimist[x_axis] > 2023]
|
122 |
fig.add_trace(
|
123 |
go.Scatter(
|
124 |
+
x=interpolation_pessimist[x_axis],
|
125 |
+
y=interpolation_pessimist[column].interpolate(),
|
126 |
mode="lines",
|
127 |
+
name = "forecast scénario pessimiste",
|
128 |
+
line=dict(color="orange"),
|
129 |
+
showlegend=False
|
130 |
+
|
131 |
+
),
|
132 |
+
)
|
133 |
+
interpolation_moderate = concatenated_moderate[concatenated_moderate[x_axis] > 2023]
|
134 |
+
fig.add_trace(
|
135 |
+
go.Scatter(
|
136 |
+
x=interpolation_moderate[x_axis],
|
137 |
+
y=interpolation_moderate[column].interpolate(),
|
138 |
+
mode="lines",
|
139 |
+
name = "forecast scénario modéré",
|
140 |
+
line=dict(color="purple"),
|
141 |
+
showlegend=False
|
142 |
+
|
143 |
+
),
|
144 |
+
)
|
145 |
+
fig.update_layout(
|
146 |
+
title=f"Historique et Forecast pour {column}",
|
147 |
+
xaxis_title="Year", # Set the x-axis title to Year
|
148 |
+
yaxis_title=column, # Set the y-axis title to Precipitation
|
149 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
return fig
|
152 |
|
153 |
+
def aggregate_yearly(df, col_to_agg, operation = "mean"):
|
154 |
+
df[col_to_agg] = df.groupby("year")[col_to_agg].transform(operation)
|
|
|
155 |
return df
|
156 |
|
157 |
|
158 |
def generate_plots(
|
159 |
moderate: pd.DataFrame,
|
160 |
historic: pd.DataFrame,
|
161 |
+
pessimist: pd.DataFrame,
|
162 |
x_axes: List[str],
|
163 |
cols_to_plot: List[str],
|
164 |
):
|
165 |
plots = []
|
166 |
for i, col in enumerate(cols_to_plot):
|
167 |
+
plots.append(visualize_climate(moderate, historic, pessimist, x_axes[i], col))
|
168 |
return plots
|
169 |
|
|
|
170 |
def get_plots():
|
171 |
+
cols_to_plot = [
|
172 |
+
"Precipitation (mm)",
|
173 |
+
"Near Surface Air Temperature (°C)",
|
174 |
+
"Surface Downwelling Shortwave Radiation (W/m²)",
|
175 |
+
]
|
176 |
cols_to_keep = [
|
177 |
"Precipitation (mm)",
|
178 |
"Near Surface Air Temperature (°C)",
|
|
|
180 |
"year",
|
181 |
"period",
|
182 |
]
|
|
|
|
|
|
|
|
|
|
|
183 |
x_axes = ["year"] * len(cols_to_plot)
|
|
|
184 |
latitude = 47
|
185 |
longitude = 5
|
186 |
start_year = 2000
|
187 |
end_year = 2025
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
df = download_historical_weather_data(latitude, longitude, start_year, end_year)
|
190 |
historic = aggregate_hourly_weather_data(df)
|
|
|
197 |
"index": "time",
|
198 |
}
|
199 |
)
|
200 |
+
folder_to_parse_moderate = "climate_data_moderate/"
|
201 |
+
climate_sub_folder_moderate = [
|
202 |
+
os.path.join(folder_to_parse_moderate, e)
|
203 |
+
for e in os.listdir(folder_to_parse_moderate)
|
204 |
+
if os.path.isdir(os.path.join(folder_to_parse_moderate, e))
|
205 |
+
]
|
206 |
+
climate_sub_files_moderate = [
|
207 |
+
os.path.join(e, i)
|
208 |
+
for e in climate_sub_folder_moderate
|
209 |
+
for i in os.listdir(e)
|
210 |
+
if i.endswith(".nc")
|
211 |
+
]
|
212 |
+
datasets_moderate = get_forecast_datasets(climate_sub_files_moderate)
|
213 |
+
moderate = get_forecast_data(datasets_moderate, latitude, longitude)
|
214 |
+
|
215 |
+
folder_to_parse_pessimist = "climate_data_pessimist/"
|
216 |
+
climate_sub_folder_pessimist = [
|
217 |
+
os.path.join(folder_to_parse_pessimist, e)
|
218 |
+
for e in os.listdir(folder_to_parse_pessimist)
|
219 |
+
if os.path.isdir(os.path.join(folder_to_parse_pessimist, e))
|
220 |
+
]
|
221 |
+
climate_sub_files_pessimist = [
|
222 |
+
os.path.join(e, i)
|
223 |
+
for e in climate_sub_folder_pessimist
|
224 |
+
for i in os.listdir(e)
|
225 |
+
if i.endswith(".nc")
|
226 |
+
]
|
227 |
+
datasets_pessimist = get_forecast_datasets(climate_sub_files_pessimist)
|
228 |
+
pessimist = get_forecast_data(datasets_pessimist, latitude, longitude)
|
229 |
+
moderate = moderate.rename(columns={"Precipitation (kg m-2 s-1)": "Precipitation (mm)"})#TODO : do yearly precipitations
|
230 |
moderate["time"] = pd.to_datetime(moderate["time"])
|
231 |
+
moderate = moderate.sort_values('time')
|
232 |
+
moderate['year'] = moderate["time"].dt.year
|
233 |
+
moderate["Precipitation (mm)"] = moderate["Precipitation (mm)"]*31536000
|
234 |
+
pessimist = pessimist.rename(columns={"Precipitation (kg m-2 s-1)": "Precipitation (mm)"})
|
235 |
+
pessimist["time"] = pd.to_datetime(pessimist["time"])
|
236 |
+
pessimist = pessimist.sort_values('time')
|
237 |
+
pessimist['year'] = pessimist["time"].dt.year
|
238 |
+
pessimist["Precipitation (mm)"] = pessimist["Precipitation (mm)"]*31536000
|
239 |
+
pessimist["period"] = "forecast scénario pessimiste"
|
240 |
+
historic['year'] = historic["time"].dt.year
|
241 |
+
historic["Precipitation (mm)"] = historic["Precipitation (mm)"]*8760.0
|
242 |
for col in cols_to_plot:
|
243 |
moderate = aggregate_yearly(moderate, col)
|
244 |
historic = aggregate_yearly(historic, col)
|
245 |
+
pessimist = aggregate_yearly(pessimist, col)
|
246 |
+
plots = generate_plots(moderate, historic, pessimist, x_axes, cols_to_plot)
|
|
|
|
|
|
|
247 |
return plots
|