-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphStationsVsSource.py
More file actions
283 lines (233 loc) · 8.54 KB
/
Copy pathGraphStationsVsSource.py
File metadata and controls
283 lines (233 loc) · 8.54 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
from matplotlib import pyplot as plt
from matplotlib.ticker import PercentFormatter
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import sys
import os
binSize = 2.5 # mm
# Attempts to create a folder
def attemptCreateFolder(path):
try:
os.mkdir(path)
except:
pass
# Returns bin colors for scatter plot
def createHistogram(a, b, bins=90):
histogram, xBins, yBins = np.histogram2d(a, b, bins=bins)
xIdx = np.clip(np.digitize(a, xBins), 0, histogram.shape[0]-1)
yIdx = np.clip(np.digitize(b, yBins), 0, histogram.shape[1]-1)
return histogram[xIdx, yIdx]
# Gets at index using iloc for Pandas objects and regular indexing for others
def safeGetAtIndex(x, index):
if isinstance(x, pd.Series) or isinstance(x, pd.DataFrame):
return x.iloc[index]
else:
return x[index]
# Returns bias and stddev of bias
def getBias(x, y):
totalDiff = 0.0
biases = []
for i in range(0, len(x)):
bias = safeGetAtIndex(y, i) - safeGetAtIndex(x, i)
totalDiff += bias
biases.append(bias)
return totalDiff / len(x), np.std(biases)
# Returns coefficient (m), intercept (b), and r^2 value
def getLinearRegression(x, y):
x = np.array(x).reshape(-1, 1)
y = np.array(y)
model = LinearRegression().fit(x, y)
r2 = model.score(x, y)
return model.coef_[0], model.intercept_, r2
# Return RMSE
def getRMSE(x, y):
return np.sqrt(np.mean((y - x) ** 2))
# Returns mean absolute error
def getMAE(x, y):
return np.mean(np.abs(y - x))
# Returns mean absolute percentage error
def getMAPE(x, y):
return np.mean(np.abs((y - x) / y)) * 100
# Returns mean bias percentage error
def getMBPE(x, y):
return np.mean((y - x) / x) * 100
# Makes a graph with a scatter plot and a histogram
# Returns r2, bias, rmse, mae, mape, mbpe stats
def graphSiteVsSource(siteData, sourceName, sourceData, siteName, frequency, threshold, outputName):
dataMax = max(max(siteData), max(sourceData))
plt.figure()
# Create histogram
plt.subplot(1, 2, 1)
plt.grid()
plt.gca().set_axisbelow(True)
plt.gca().set_aspect("equal")
plt.xlim(0, dataMax)
plt.ylim(0, dataMax)
colors = createHistogram(siteData, sourceData)
plt.scatter(siteData, sourceData, c=colors, alpha=0.5, s=1.5, cmap="jet")
plt.gca().set_xlabel(f"{siteName} Precipitation (mm)")
plt.gca().set_ylabel(f"{sourceName} Precipitation (mm)")
# y = x line
plt.axline(
[0, 0],
[dataMax, dataMax],
color="black",
alpha=0.5,
linewidth=0.5
)
# Create histogram
plt.subplot(1, 2, 2)
plt.grid()
plt.gca().set_axisbelow(True)
bins = np.arange(0, dataMax + binSize, binSize)
plt.hist(
siteData,
alpha=0.5,
label="Sites",
bins=bins,
weights=np.ones(len(siteData)) / len(siteData) # make percentage
)
plt.hist(
sourceData,
alpha=0.5,
label=sourceName,
bins=bins,
weights=np.ones(len(sourceData)) / len(sourceData)
)
plt.gca().set_xlabel("Precipitation (mm)")
plt.gca().set_ylabel("Frequency (%)")
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.ylim(0, 1)
plt.legend(loc="upper right")
# Get stats
# Site is always observation
m, b, r2 = getLinearRegression(siteData, sourceData)
bias, _ = getBias(siteData, sourceData)
rmse = getRMSE(siteData, sourceData)
mae = getMAE(siteData, sourceData)
mape = getMAPE(siteData, sourceData)
mbpe = getMBPE(siteData, sourceData)
# Figure title
title = [
f"{frequency} Hour Total {siteName} vs {sourceName}",
f"{len(siteData)} Observations ({threshold}mm threshold)",
f"y ~ {round(m, 3)}x + {round(b, 3)} (R^2: {round(r2, 3)})",
f"RMSE: {round(rmse, 3)}",
f"Mean Bias: {round(bias, 3)}, Mean Bias Percent Error: {round(mbpe, 3)}%",
f"Mean Absolute Error: {round(mae, 3)}, Mean Absolute Percent Error: {round(mape, 3)}%"
]
plt.suptitle("\n".join(title))
# Format
plt.gcf().set_size_inches(13, 7)
plt.tight_layout()
plt.savefig(outputName, dpi=300, bbox_inches="tight")
print(f"Saved as {outputName}")
plt.close()
return r2, bias, rmse, mae, mape, mbpe
# Source = StageIV or MRMS data
# Sites = ASOS, UCONN, SBU, etc
def graph(sourceFilename, sourceName, siteFilenames, frequency, threshold):
# Open files
source = pd.read_csv(sourceFilename, index_col="Time", parse_dates=True)
print(f"Read source file {sourceFilename} ({sourceName}) with {len(source.columns)} sites")
sites = []
for name in siteFilenames:
site = pd.read_csv(name, index_col="Time", parse_dates=True)
sites.append(site)
print(f"Read {name} with {len(site.columns)} sites")
# Init output
attemptCreateFolder("graphs")
attemptCreateFolder(f"graphs/SiteVs{sourceName}_{frequency}hr")
print("Attempted to create output folders")
# Save error metrics
metrics = {
"site": [],
"r2": [],
"bias": [],
"rmse": [],
"mae": [],
"mape": [],
"mbpe": []
}
# Accumulate data for overall graph
allData = []
allSource = []
# Create plots for each station
for station in source.columns:
# Find what other file this data is in
data = pd.DataFrame(columns=[station])
numFiles = 0
for site in sites:
if station in site.columns:
data = site[[station]] if len(data) == 0 else pd.concat([data, site[[station]]])
data = data[~data.index.duplicated(keep="first")]
numFiles += 1
if numFiles == 0:
print(f"Could not find any other file to match site {station}!")
continue
print(f"Used {numFiles} files for site {station}, {len(data)} rows")
# Remove missing data and data below threshold
dataFiltered = data[(data[station] != -9999) & (data[station] >= threshold)]
sourceFiltered = source[(source[station] != -9999) & (source[station] >= threshold)]
# Drop duplicates
dataFiltered = dataFiltered[~dataFiltered.index.duplicated(keep="first")]
sourceFiltered = sourceFiltered[~sourceFiltered.index.duplicated(keep="first")]
# Find matching days
common = dataFiltered.index.intersection(sourceFiltered.index)
dataFiltered = dataFiltered.loc[common]
sourceFiltered = sourceFiltered.loc[common]
dataFiltered = dataFiltered[station]
sourceFiltered = sourceFiltered[station]
# Accumulate data
allData.extend(dataFiltered)
allSource.extend(sourceFiltered)
# Create single station graph and get metrics
r2, bias, rmse, mae, mape, mbpe = graphSiteVsSource(
dataFiltered,
sourceName,
sourceFiltered,
station,
frequency,
threshold,
f"graphs/SiteVs{sourceName}_{frequency}hr/{station}_vs_{sourceName}.jpg"
)
# Save metrics
metrics["site"].append(station)
metrics["r2"].append(r2)
metrics["bias"].append(bias)
metrics["rmse"].append(rmse)
metrics["mae"].append(mae)
metrics["mape"].append(mape)
metrics["mbpe"].append(mbpe)
# Create all site graph
allData = np.array(allData)
allSource = np.array(allSource)
graphSiteVsSource(
allData,
sourceName,
allSource,
"Sites",
frequency,
threshold,
f"graphs/AllSites_vs_{sourceName}_{frequency}hr.jpg"
)
# Save metrics
metrics = pd.DataFrame(metrics)
metrics.set_index("site", inplace=True)
metricsFilename = f"Metrics_SitesVs{sourceName}_{frequency}hr.csv"
metrics.to_csv(metricsFilename)
print(f"Saved as {metricsFilename}")
# Run
if __name__ == "__main__":
graph(
sys.argv[1], # Processed/StageIV_1hr.csv or Processed/MRMS_1hr.csv or ...
"MRMS_Multi", # MRMS or StageIV
sys.argv[2:], # Processed/CombinedASOS.csv, Processed/CombinedSBU.csv...
1, # hour frequency (1, 6, or 24). Used in graph title
0.25, # minimum mm of precipitation to include in graph
)
# Execute from command line:
#python3 Scripts/graphing/GraphStationsVsSource.py Processed/MRMS/MRMS_1hr_Multi.csv Processed/CombinedASOS_FixedRanges.csv Processed/CombinedSBU_v2.csv Processed/CombinedSBU_BNL_v2.csv Processed/CombinedUCONN_2122_v2.csv Processed/CombinedUCONN_2223_v2_truncated.csv Processed/CombinedUCONN_2324_v2_truncated.csv
#python3 Scripts/graphing/GraphStationsVsSource.py Processed/MRMS/MRMS_6hr_Multi.csv Processed/aggregated/ASOS_6h.csv Processed/aggregated/SBU_6h.csv Processed/aggregated/SBU_BNL_6h.csv Processed/aggregated/UCONN_2122_6h.csv Processed/aggregated/UCONN_2223_6h.csv Processed/aggregated/UCONN_2324_6h.csv
#python3 Scripts/graphing/GraphStationsVsSource.py Processed/MRMS/MRMS_24hr_Multi.csv Processed/aggregated/ASOS_24h.csv Processed/aggregated/SBU_24h.csv Processed/aggregated/SBU_BNL_24h.csv Processed/aggregated/UCONN_2122_24h.csv Processed/aggregated/UCONN_2223_24h.csv Processed/aggregated/UCONN_2324_24h.csv