Akram Sanad commited on
Commit
61e4fc2
·
1 Parent(s): af358f2

visualize.py file added

Browse files
data/data_climate_test/final_climate_data.csv ADDED
The diff for this file is too large to render. See raw diff
 
visualize.ipynb CHANGED
@@ -2738,7 +2738,7 @@
2738
  ],
2739
  "source": [
2740
  "for plot in plots:\n",
2741
- " plot.show()"
2742
  ]
2743
  },
2744
  {
 
2738
  ],
2739
  "source": [
2740
  "for plot in plots:\n",
2741
+ " plot.show() "
2742
  ]
2743
  },
2744
  {
visualize/visualize.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd # stress hydrique and rendement, besoin en eau
2
+ import plotly.graph_objects as go
3
+ from typing import List
4
+ import plotly.express as px
5
+ from fuzzywuzzy import process
6
+ from data_pipelines.historical_weather_data import (
7
+ download_historical_weather_data,
8
+ aggregate_hourly_weather_data,
9
+ )
10
+
11
+ def concatenate_historic_forecast(historic, forecast, cols_to_keep):
12
+ historic["period"] = "historique"
13
+ forecast["period"] = "futur"
14
+ historic = historic[cols_to_keep]
15
+ forecast = forecast[cols_to_keep]
16
+ full_data = pd.concat([historic, forecast])
17
+ return full_data
18
+
19
+
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] = [
26
+ "Precipitation (mm)",
27
+ "Near Surface Air Temperature (°C)",
28
+ "Surface Downwelling Shortwave Radiation (W/m²)",
29
+ "year",
30
+ "period",
31
+ ],
32
+ ):
33
+ concatenated_df = concatenate_historic_forecast(historic, moderate, cols_to_keep)
34
+ concatenated_df = concatenated_df.sort_values(by=x_axis) # Ensure order
35
+
36
+ fig = go.Figure()
37
+ # colors = {"historique": "blue", "forecast": "purple"}
38
+
39
+ for condition_value in concatenated_df["period"].unique():
40
+ segment = concatenated_df[concatenated_df["period"] == condition_value]
41
+ fig.add_trace(
42
+ go.Scatter(
43
+ x=segment[x_axis],
44
+ y=segment[column],
45
+ mode="lines",
46
+ name=f"{condition_value}",
47
+ line=dict(color="blue" if condition_value == "futur" else "purple"),
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
+ def aggregate_yearly(df, col_to_agg):
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
+ if __name__ == "__main__":
88
+ cols_to_keep = [
89
+ "Precipitation (mm)",
90
+ "Near Surface Air Temperature (°C)",
91
+ "Surface Downwelling Shortwave Radiation (W/m²)",
92
+ "year",
93
+ "period",
94
+ ]
95
+ cols_to_plot = [
96
+ "Precipitation (mm)",
97
+ "Near Surface Air Temperature (°C)",
98
+ "Surface Downwelling Shortwave Radiation (W/m²)",
99
+ ]
100
+ x_axes = ["year"] * len(cols_to_plot)
101
+
102
+ latitude = 47
103
+ longitude = 5
104
+ start_year = 2000
105
+ end_year = 2025
106
+ cols_to_keep = [
107
+ "Precipitation (mm)",
108
+ "Near Surface Air Temperature (°C)",
109
+ "Surface Downwelling Shortwave Radiation (W/m²)",
110
+ "year",
111
+ "period",
112
+ ]
113
+ cols_to_plot = [
114
+ "Precipitation (mm)",
115
+ "Near Surface Air Temperature (°C)",
116
+ "Surface Downwelling Shortwave Radiation (W/m²)",
117
+ ]
118
+ x_axes = ["year"] * len(cols_to_plot)
119
+
120
+ df = download_historical_weather_data(latitude, longitude, start_year, end_year)
121
+ historic = aggregate_hourly_weather_data(df)
122
+ historic = historic.reset_index()
123
+ historic = historic.rename(
124
+ columns={
125
+ "precipitation": "Precipitation (mm)",
126
+ "air_temperature_mean": "Near Surface Air Temperature (°C)",
127
+ "irradiance": "Surface Downwelling Shortwave Radiation (W/m²)",
128
+ "index": "time",
129
+ }
130
+ )
131
+ historic["time"] = pd.to_datetime(historic["time"])
132
+ historic = historic.sort_values("time")
133
+ historic = historic[historic["time"] < "2025-01-01"]
134
+ climate_data_path = "data/data_climate_test/final_climate_data.csv" # will use a similar func to download_historical_weather_data
135
+ moderate = pd.read_csv(climate_data_path)
136
+ moderate["time"] = pd.to_datetime(moderate["time"])
137
+ moderate = moderate.sort_values("time")
138
+ moderate["year"] = moderate["time"].dt.year
139
+ historic["year"] = historic["time"].dt.year
140
+ moderate["Precipitation (mm)"] = moderate["Precipitation (mm)"] * 3600
141
+ for col in cols_to_plot:
142
+ moderate = aggregate_yearly(moderate, col)
143
+ historic = aggregate_yearly(historic, col)
144
+
145
+
146
+ plots = generate_plots(moderate, historic, x_axes, cols_to_plot) # List of 3 plots to show