-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
293 lines (246 loc) · 11.8 KB
/
main.py
File metadata and controls
293 lines (246 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import matplotlib.pyplot as plt
from os import makedirs
import seaborn as sns
import pandas as pd
import numpy as np
from const import *
def analyze(dataset, column):
"""
Compute summary statistics and derived metrics for a single column.
"""
data = dataset[column]
# Year-over-year absolute change
yearly_change = np.append([np.nan], np.diff(data)).tolist()
# Year-over-year percent change
percent_change = (data.pct_change() * 100).to_list()
# Simple rolling average with a fixed window
rolling_window = 5
convolution = np.convolve(data, np.ones(rolling_window) / rolling_window, mode='valid')
rolling_avg = np.append([np.nan] * (rolling_window - 1), convolution).tolist()
# Basic summary stats
mean = np.mean(data).item()
std = np.std(data).item()
median = np.median(data).item()
# Pack everything into a DataFrame for plotting
df = pd.DataFrame({
"Year": dataset["Year"],
column: data,
"Yearly Change": yearly_change,
"Percent Change": percent_change,
"Rolling Average": rolling_avg
})
analysis = {"df": df, "mean": mean, "std": std, "median": median}
return analysis
class Plot:
"""
Helper class for generating all climate-related visualizations.
"""
def __init__(self, dataset, dataset_yearly_avg, analyses):
# Full merged dataset (monthly or raw records)
self.dataset = dataset
# Yearly-averaged dataset
self.dataset_yearly_avg = dataset_yearly_avg
# Precomputed analyses for each column
self.analyses = analyses
def plot_raw(self):
"""
Plot raw CO2 and temperature time series.
"""
fig, axs = plt.subplots(2, 1)
year = self.dataset["Year"]
# Raw CO2 levels
axs[0].plot(year, self.dataset[COLUMNS[0]].dropna(), label=COLUMNS[0], color="mediumslateblue", alpha=0.75)
axs[0].plot(year, self.dataset[COLUMNS[1]].dropna(), label=COLUMNS[1], color="midnightblue", linestyle="--")
axs[0].set_title("Carbon Dioxide Levels In Atmosphere")
axs[0].set_ylabel("Carbon Dioxide (ppm)")
axs[0].set_xlabel("Year")
axs[0].legend(fontsize=8)
axs[0].grid(alpha=0.7)
# Raw temperature series
axs[1].plot(year, self.dataset[COLUMNS[2]].dropna(), label=COLUMNS[2], color="mediumslateblue", alpha=0.75)
axs[1].plot(year, self.dataset[COLUMNS[3]].dropna(), label=COLUMNS[3], color="midnightblue", linestyle="--")
axs[1].set_title("Global Temperatures")
axs[1].set_ylabel("Temperature (celsius)")
axs[1].set_xlabel("Year")
axs[1].legend(fontsize=8)
axs[1].grid(alpha=0.7)
plt.suptitle("Climate Metrics 1958-2015")
plt.tight_layout()
# plt.show()
plt.savefig("Figures/Climate Metrics 1958-2015.png", bbox_inches="tight", dpi=300)
def plot_avg(self):
"""
Plot yearly-averaged CO2 and temperature time series.
"""
fig, axs = plt.subplots(2, 1)
year = self.dataset_yearly_avg["Year"]
# Yearly average CO2 levels
axs[0].plot(year, self.dataset_yearly_avg[COLUMNS_AVG[0]], label=COLUMNS_AVG[0], color="mediumslateblue", alpha=0.75)
axs[0].plot(year, self.dataset_yearly_avg[COLUMNS_AVG[1]], label=COLUMNS_AVG[1], color="midnightblue", linestyle="--")
axs[0].set_title("Carbon Dioxide Levels In Atmosphere")
axs[0].set_ylabel("Carbon Dioxide (ppm)")
axs[0].set_xlabel("Year")
axs[0].legend(fontsize=8)
axs[0].grid(alpha=0.7)
# Yearly average temperatures
axs[1].plot(year, self.dataset_yearly_avg[COLUMNS_AVG[2]], label=COLUMNS_AVG[2], color="mediumslateblue", alpha=0.75)
axs[1].plot(year, self.dataset_yearly_avg[COLUMNS_AVG[3]], label=COLUMNS_AVG[3], color="midnightblue", linestyle="--")
axs[1].set_title("Global Temperatures")
axs[1].set_ylabel("Temperature (celsius)")
axs[1].set_xlabel("Year")
axs[1].legend(fontsize=8)
axs[1].grid(alpha=0.7)
plt.suptitle("Yearly Average Climate Metrics 1958-2015")
plt.tight_layout()
# plt.show()
plt.savefig("Figures/Yearly Average Climate Metrics 1958-2015.png", bbox_inches="tight", dpi=300)
def plot_analyses(self):
"""
For each metric, plot:
- original vs rolling average
- yearly changes
- percent changes with a fitted trend line
- correlation matrix heatmap
"""
graphs = [
("Carbon Dioxide (ppm)", "Yearly Average Carbon Dioxide Levels Analysis"),
("Seasonally Adjusted CO2 (ppm)", "Yearly Average Seasonally Adjusted Carbon Dioxide Levels Analysis"),
("Land Temperature (celsius)", "Yearly Average Land Temperature Analysis"),
("Land and Ocean Temperature (celsius)", "Yearly Average Land and Ocean Temperature Analysis"),
]
for i, analysis in enumerate(self.analyses):
data, mean, median, std = analysis["df"], analysis["mean"], analysis["median"], analysis["std"]
# Layout: time series on top, bar chart + scatter/trend below
fig, axs = plt.subplot_mosaic([['top', 'top'], ['left', 'right']], constrained_layout=True)
year = data["Year"]
# Raw + rolling average
axs["top"].plot(year, data[COLUMNS_AVG[i]], label=COLUMNS_AVG[i], color="lightskyblue", linestyle="--")
axs["top"].plot(year, data["Rolling Average"], label="Rolling Average", color="blue", alpha=0.7)
axs["top"].set_ylabel(graphs[i][0])
axs["top"].set_xlabel("Year")
axs["top"].grid(alpha=0.7)
axs["top"].legend(fontsize=8)
# Yearly change as bars
axs["left"].bar(year, data["Yearly Change"], label="Yearly Change", color="royalblue", alpha=0.6)
axs["left"].grid(axis='y', linestyle='--', alpha=0.7)
axs["left"].set_ylabel("Yearly Change")
axs["left"].set_xlabel("Year")
axs["left"].legend(fontsize=8, loc="upper left")
# Percent change with linear trend over time
idx = np.isfinite(year) & np.isfinite(data["Percent Change"])
slope, intercept = np.polyfit(year[idx], data["Percent Change"][idx], 1).tolist()
line = slope * year + intercept
axs["right"].scatter(year, data["Percent Change"], s=20, edgecolor="none", label="Percent Change", color="mediumslateblue", alpha=0.5)
axs["right"].plot(year, line, color="blue", alpha=0.5)
axs["right"].grid(linestyle='--', alpha=0.7)
axs["right"].set_ylabel("Percent Change")
axs["right"].set_xlabel("Year")
axs["right"].legend(fontsize=8, loc="upper left")
plt.suptitle(graphs[i][1])
plt.tight_layout()
# plt.show()
plt.savefig(f"Figures/{graphs[i][1]}.png", bbox_inches="tight", dpi=300)
# Correlation matrix for this metric and its derived columns
plt.figure(figsize=(8, 6))
sns.heatmap(
data.drop("Year", axis=1).rename(columns={COLUMNS_AVG[i]: COLUMNS_ABR[i]}).corr(),
annot=True,
cmap='coolwarm',
fmt=".2f"
)
plt.title(f"{COLUMNS_ABR[i]} Correlation Matrix")
plt.tight_layout()
# plt.show()
plt.savefig(f"Figures/Yearly Average {COLUMNS[i]} Correlation Matrix.png", bbox_inches="tight", dpi=300)
def plot_co2_temp_regression(self):
"""
Fit linear regression models between yearly average CO2 concentration
and global temperature metrics, and visualize model fit and residual trends.
"""
co2_col = COLUMNS_AVG[0] # "avg. Carbon Dioxide in Atmosphere (ppm)"
temp_cols = [
COLUMNS_AVG[2], # "avg. Land Temperature (celsius)"
COLUMNS_AVG[3], # "avg. Land and Ocean Temperature (celsius)"
]
for temp_col in temp_cols:
# Select and clean data for regression
data = self.dataset_yearly_avg[[co2_col, temp_col]].dropna()
x = data[co2_col].values
y = data[temp_col].values
# Linear regression using numpy (y = slope * x + intercept)
slope, intercept = np.polyfit(x, y, 1)
y_pred = slope * x + intercept
residuals = y - y_pred
# R^2 as a simple model quality metric
ss_res = np.sum(residuals ** 2)
ss_tot = np.sum((y - np.mean(y)) ** 2)
r2 = 1 - ss_res / ss_tot
# Sort x for a clean regression line
order = np.argsort(x)
x_sorted = x[order]
y_pred_sorted = y_pred[order]
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
# Left: Model fit (scatter + regression line)
axs[0].scatter(x, y, alpha=0.6, label="Observed", color="mediumslateblue")
axs[0].plot(x_sorted, y_pred_sorted, label=f"Linear Fit (R² = {r2:.2f})", color="midnightblue")
axs[0].set_xlabel("avg. CO$_2$ in Atmosphere (ppm)")
axs[0].set_ylabel(temp_col)
axs[0].set_title(f"Linear Regression: {temp_col} vs CO$_2$")
axs[0].grid(alpha=0.7)
axs[0].legend(fontsize=8)
# Right: Residuals vs CO2 (residual trends)
axs[1].scatter(x, residuals, alpha=0.6, color="mediumslateblue")
axs[1].axhline(0, linestyle="--", linewidth=1, color="midnightblue")
axs[1].set_xlabel("avg. CO$_2$ in Atmosphere (ppm)")
axs[1].set_ylabel("Residuals (°C)")
axs[1].set_title(f"Residuals: {temp_col} vs CO$_2$")
axs[1].grid(alpha=0.7)
plt.suptitle(
f"CO$_2$ vs {temp_col}: Model Fit and Residual Trends",
y=1.02
)
plt.tight_layout()
# Make filename safe for saving
safe_temp_name = temp_col.replace(" ", "_").replace("/", "_").replace(".", "")
plt.savefig(
f"Figures/Linear Regression CO2 vs {safe_temp_name}.png",
bbox_inches="tight",
dpi=300
)
plt.close(fig)
def main():
"""
Load data, compute yearly averages, run analyses, and generate plots.
"""
makedirs("Figures", exist_ok=True)
# CO2 data
read_co2 = pd.read_csv("Data/CO2LevelsInAtmosphere.csv").dropna()
filter_co2 = read_co2.filter(["Year", "Carbon Dioxide (ppm)", "Seasonally Adjusted CO2 (ppm)"])
# Focus on 1958–2015 and compute yearly averages
co2_data = filter_co2[(filter_co2["Year"] >= 1958) & (filter_co2["Year"] <= 2015)].reset_index(drop=True)
co2_data_yearly_avg = co2_data.groupby("Year", as_index=False).mean()
# Temperature data
read_temp = pd.read_csv("Data/GlobalTemperatures.csv").dropna()
read_temp["Year"] = pd.to_datetime(read_temp["dt"]).dt.year
filter_temp = read_temp.filter((["Year", "LandAverageTemperature", "LandAndOceanAverageTemperature"]))
temp_data = filter_temp[(filter_temp["Year"] >= 1958) & (filter_temp["Year"] <= 2015)].reset_index(drop=True)
temp_data_yearly_avg = temp_data.groupby("Year", as_index=False).mean()
# Merge raw datasets
dataset = pd.merge(co2_data, temp_data, how='inner')
dataset.columns = ["Year"] + COLUMNS
# Merge yearly-averaged datasets
dataset_yearly_avg = pd.merge(co2_data_yearly_avg, temp_data_yearly_avg, how='inner')
dataset_yearly_avg.columns = ["Year"] + COLUMNS_AVG
# Precompute analyses for each yearly-average column
analyses = []
for column in COLUMNS_AVG:
analysis = analyze(dataset_yearly_avg, column)
analyses.append(analysis)
# Generate all plots
plot = Plot(dataset, dataset_yearly_avg, analyses)
plot.plot_raw()
plot.plot_avg()
plot.plot_analyses()
plot.plot_co2_temp_regression()
if __name__ == "__main__":
main()