-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiagnostics.R
420 lines (348 loc) · 15.3 KB
/
diagnostics.R
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Diagnostics and post processing of superwell outputs
#
# Hassan Niazi, [email protected]
# Jan 2023, JGCRI, PNNL
library(tidyverse)
library(GGally)
library(ggpubr)
library(plotly)
library(psych)
#rm(list = ls())
# General types of plots:
# single variables plots for all key variables (static and dynamic)
# two variable plots for key variables (static and dynamic)
# multi-variable matrix plots (static only)
# load data ########
country <- "United States"
output_filename <- paste0(gsub(" ", "_", tolower(country)), ".csv")
output_dir <- "outputs/"
read.csv(paste0(output_dir, output_filename)) -> outdata_all
#read.csv("outputs/united_states.csv") -> outdata_all
#read.csv("outputs/2023-04-14_pakistan.csv") -> outdata_all
#read.csv("outputs/2023-04-0_united_states.csv") -> outdata_all
outdata_all %>% select(!c(gcam_basin_name, country_name, continent)) -> outdata
# new
ggplot() +
# geom_histogram(aes(x = irrigated_depth)) +
geom_point(aes(x = outdata$cumulative_vol_produced_allwells, y = outdata$unit_cost), shape = ".", alpha = 1) +
# stat_ecdf(aes(x = irrigated_depth), geom = "step") +
labs(x = "Cumulative Vol Produced All Wells", y = "Unit Cost") +
theme_bw()
# old
ggplot() +
# geom_histogram(aes(x = irrigated_depth)) +
geom_point(aes(x = outdata$total_volume_produced, y = outdata$unit_cost), shape = ".", alpha = 1) +
# stat_ecdf(aes(x = irrigated_depth), geom = "step") +
labs(x = "Cumulative Vol Produced All Wells", y = "Unit Cost") +
theme_bw()
# plot irrigated depth
outdata %>% mutate(irrigated_depth = volume_produced_perwell/areal_extent) -> irr_depth
ggplot() +
# geom_histogram(aes(x = irr_depth$irrigated_depth)) +
geom_point(aes(x = irr_depth$irrigated_depth, y = 1:nrow(irr_depth))) +
# stat_ecdf(aes(x = irr_depth$irrigated_depth), geom = "step") +
labs(x = "Irrigated Depth", y = "Count") +
theme_bw()
max(irr_depth$irrigated_depth)
plot(irr_depth$irrigated_depth)
outdata %>% filter(number_of_wells > 5000) %>% arrange(number_of_wells) %>% mutate(Nwells = "Nwells_HI") -> outdata_Nwells_hi
outdata %>% filter(number_of_wells < 10) %>% arrange(number_of_wells) %>% mutate(Nwells = "Nwells_LO") -> outdata_Nwells_lo
outdata_Nwells <- bind_rows(outdata_Nwells_hi, outdata_Nwells_lo)
dfhistplot <- outdata_Nwells_hi
ggplot(dfhistplot) +
geom_histogram(aes(x = dfhistplot$number_of_wells)) +
geom_point(aes(y = 1:nrow(dfhistplot), x = dfhistplot$number_of_wells)) +
labs(x = "Number of Wells", y = "Count") +
theme_bw()
dfplot_Nwells <- outdata_Nwells
headers_all <- colnames(dfplot_Nwells)
dpNwells <- ggplot(dfplot_Nwells) + theme_bw() + theme(axis.title.x = element_blank())
outplots_pts_Nwells <- list()
for (head in headers_all) {
outplots_pts_Nwells[[head]] <- dpNwells +
geom_point(aes(x = 1:nrow(dfplot_Nwells), y = .data[[head]], colour = Nwells)) + #), shape = ".", alpha = 1)
scale_color_manual(values = c("darkred","yellow2")) +
labs(y = head) +
theme(legend.position="none")
# uncomment to save all individual plots
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_", head, ".png"))
}
ggarrange(plotlist = outplots_pts_Nwells, ncol = 9, nrow = 5)
#ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_pts_Nwells_all_v3.png"), width = 16, height = 9, units = "in")
# plot all cdfs
outdata_all %>% select(!c(gcam_basin_name, country_name, continent, depletion_limit, gcam_basin_id, energy_cost_rate, unit_cost_per_km3)) -> outdata_hist
dfplot_histcdf <- outdata_hist
headers_histcdf <- colnames(dfplot_histcdf)
dp_histcdf <- ggplot(dfplot_histcdf) + theme_bw() + theme(axis.title.x = element_blank())
outplots_pts_histcdf <- list()
outplots_bars_histcdf <- list()
outplots_cdf_histcdf <- list()
for (head in headers_histcdf) {
outplots_pts_histcdf[[head]] <- dp_histcdf +
geom_point(aes(x = 1:nrow(dfplot_histcdf), y = .data[[head]])) +
labs(y = head)
outplots_bars_histcdf[[head]] <- dp_histcdf +
geom_histogram(aes(x = .data[[head]])) +
labs(y = head)
#stat_ecdf(aes(x = dfhistplot$number_of_wells), geom = "step") +
outplots_cdf_histcdf[[head]] <- dp_histcdf +
stat_ecdf(aes(x = .data[[head]]), geom = "step") +
labs(y = head)
# uncomment to save all individual histograms
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_hist_", head, ".png"))
}
# ggarrange(plotlist = outplots_pts_histcdf, ncol = 10, nrow = 4)
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_histcdf_pts_v0.png"), width = 16, height = 9, units = "in")
ggarrange(plotlist = outplots_bars_histcdf, ncol = 10, nrow = 4)
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_histcdf_bars_v1.png"), width = 16, height = 9, units = "in")
ggarrange(plotlist = outplots_cdf_histcdf, ncol = 10, nrow = 4)
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_histcdf_cdf_v1.png"), width = 16, height = 9, units = "in")
# STATIC PLOTS ########
## plot all headers ########
dfplot <- outdata
headers_all <- colnames(dfplot)
dp <- ggplot(dfplot) + theme_bw() + theme(axis.title.x = element_blank())
outplots_pts_all <- list()
outplots_bars_all <- list()
for (head in headers_all) {
outplots_pts_all[[head]] <- dp +
geom_point(aes(x = 1:nrow(dfplot), y = .data[[head]])) +
labs(y = head)
# uncomment to save all individual plots
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_", head, ".png"))
}
ggarrange(plotlist = outplots_pts_all, ncol = 9, nrow = 5)
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_pts_allv2.png"), width = 16, height = 9, units = "in")
## plot meaningful variables only ########
headers <-
c("iteration",
"year_number",
"area",
"aqfr_sat_thickness",
"thickness",
"total_head",
"storativity",
"hydraulic_conductivity",
"well_id",
"radius_of_influence",
#"drawdown_roi", # minor rounding variation
#"radial_extent", # same as radius of influence
#"areal_extent", # quadratic of radial extent
"number_of_wells",
"total_volume_produced",
"volume_produced",
"available_volume",
#"continent",
#"country_name",
#"gcam_basin_id", # TODO: arrange the input data so outputs are more organized/ordinal
#"gcam_basin_name",
"depletion_limit", # controlled by depletion_limit but is it controlling depth -> appears so
"drawdown",
#"depletion_limit",
"depth_to_piez_surface",
#"well_installation_cost",
"annual_capital_cost",
#"maintenance_cost",
"nonenergy_cost", #
"cost_of_energy",
#"energy_cost_rate",
#"electric_energy", # useful for energy cost section, but not for diagnostics -> function of total_head and well_yield
"unit_cost"
)
outplots_pts <- list()
outplots_bars <- list()
for (head in headers) {
outplots_pts[[head]] <- dp +
geom_point(aes(x = 1:nrow(dfplot), y = .data[[head]])) +
labs(y = head)
outplots_bars[[head]] <- dp +
geom_histogram(aes(x = .data[[head]])) +
labs(y = head)
# uncomment to save all individual histograms
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_hist_", head, ".png"))
}
ggarrange(plotlist = outplots_pts, ncol = 7, nrow = 3)
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_pts_v2.png"), width = 16, height = 9, units = "in")
ggarrange(plotlist = outplots_bars, ncol = 7, nrow = 3)
# ggsave(filename = paste0("outputs/outplots-diag/", tolower(country), "_bars.png"), width = 16, height = 9, units = "in")
## matrix of variables pairs ########
pairs_static <- ggpairs(outdata[headers]) + theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
png(filename = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel.png"), width = 2133, height = 1200, units = "px")
#pdf(file = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel.pdf"), width = 16, height = 9)
pairs.panels(outdata[headers],
method = "pearson", # correlation method
hist.col = "dodgerblue",
rug = FALSE,
#show.points= FALSE,
#jiggle = TRUE,
smooth = TRUE,
scale = TRUE,
density = TRUE, # show density plots
ellipses = TRUE # show correlation ellipses
)
dev.off()
# pretty pair panel (smoothed), but confusing
png(filename = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel_sm.png"), width = 2133, height = 1200, units = "px")
#pdf(file = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel_sm.pdf"), width = 16, height = 9)
pairs.panels(outdata[headers],
method = "pearson", # correlation method
hist.col = "gold",
rug = FALSE,
show.points= FALSE,
smoother = T,
smooth = TRUE,
scale = TRUE,
density = TRUE, # show density plots
ellipses = TRUE # show correlation ellipses
)
dev.off()
# screened variables for pair plots
headers_pairs <-
c(#"iteration",
#"year_number",
"area",
"aqfr_sat_thickness",
#"thickness",
"total_head",
"storativity",
"hydraulic_conductivity",
#"well_id",
"radius_of_influence",
#"drawdown_roi", # minor rounding variation
#"radial_extent", # same as radius of influence
#"areal_extent", # quadratic of radial extent
"number_of_wells",
#"total_volume_produced",
"volume_produced",
"available_volume",
#"continent",
#"country_name",
#"gcam_basin_id", # TODO: arrange the input data so outputs are more organized/ordinal
#"gcam_basin_name",
"exploitable_groundwater", # controlled by depletion_limit but is it controlling depth -> appears so
"drawdown",
#"depletion_limit",
"depth_to_piez_surface",
#"well_installation_cost", # why is it varying? Should be somehow linked to well depth?
#"annual_capital_cost",
#"maintenance_cost",
"Total_nonenergy_cost",
"cost_of_energy",
#"energy_cost_rate",
#"electric_energy", # useful for energy cost section, but not for diagnostics -> function of total_head and well_yield
"unit_cost"
)
pairs_static_screened <- ggpairs(outdata[headers_pairs]) + theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
png(filename = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel_scr.png"), width = 2133, height = 1200, units = "px")
#pdf(file = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel_scr.pdf"), width = 16, height = 9)
pairs.panels(outdata[headers_pairs],
method = "pearson", # correlation method
hist.col = "dodgerblue",
rug = FALSE,
#show.points= FALSE,
#jiggle = TRUE,
smooth = TRUE,
scale = TRUE,
density = TRUE, # show density plots
ellipses = TRUE # show correlation ellipses
)
dev.off()
# pretty pair panel (smoothed), but confusing
png(filename = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel_sm_scr.png"), width = 2133, height = 1200, units = "px")
#pdf(file = paste0("outputs/outplots-diag/", tolower(country), "_pairs_panel_sm_scr.pdf"), width = 16, height = 9)
pairs.panels(outdata[headers_pairs],
method = "pearson", # correlation method
hist.col = "gold",
rug = FALSE,
show.points= FALSE,
smoother = TRUE,
smooth = TRUE,
scale = TRUE,
density = TRUE, # show density plots
ellipses = TRUE # show correlation ellipses
)
dev.off()
# DYNAMIC PLOTS ########
## one-variable single plot ########
plot_ly(outdata) %>%
add_trace(x = ~1:nrow(outdata),
y = ~outdata$volume_produced,
color = ~as.factor(outdata$gcam_basin_id),
text = ~outdata$well_id,
type = "scatter",
mode = "markers")
oneVarDynamic <- function(data, var) {
plot_ly(data) %>%
add_trace(x = 1:nrow(data),
y = enquo(var),
color = as.factor(data$gcam_basin_id),
text = data$well_id,
type = "scatter",
mode = "markers")
}
oneVarDynamic(outdata, drawdown)
oneVarDynamic(outdata, volume_produced)
oneVarDynamic(outdata, radius_of_influence)
oneVarDynamic(outdata, number_of_wells)
oneVarDynamic(outdata, Total_nonenergy_cost)
## two-variable single plot interactive ########
plot_ly(outdata) %>%
add_trace(x = ~outdata$radius_of_influence,
y = ~outdata$volume_produced,
color = ~as.factor(outdata$gcam_basin_id),
text = ~outdata$well_id,
type = "scatter",
mode = "markers")
twoVarDynamic <- function(data, xvar , yvar, colorvar) {
# set flexible coloring variable, but set basin ID as default
if (missing(colorvar)) {
colorvar = as.factor(data$gcam_basin_id)
} else { #TODO: faulty, colorvar does not get passed as a column header
colorvar = as.factor(data$colorvar)
}
# plot
ply2var <- plot_ly(data) %>%
add_trace(x = enquo(xvar),
y = enquo(yvar),
color = colorvar,
text = data$well_id,
type = "scatter",
mode = "markers")
return(ply2var)
}
twoVarDynamic(outdata, radius_of_influence, drawdown)
twoVarDynamic(outdata, radius_of_influence, number_of_wells)
twoVarDynamic(outdata, drawdown, depth_to_piez_surface)
twoVarDynamic(outdata, area, year_number)
twoVarDynamic(outdata, area, available_volume)
twoVarDynamic(outdata, year_number, well_id)
twoVarDynamic(outdata, available_volume, year_number)
twoVarDynamic(outdata, radius_of_influence, volume_produced)
twoVarDynamic(outdata, volume_produced, total_volume_produced)
## matrix of variable pairs: interactive ########
for (head in headers_all) {
outplots_pts_all_ply[[head]] <-
plot_ly(outdata) %>%
add_trace(x = ~1:nrow(outdata),
y = ~.data[[head]], # or just use the variable name
type = "scatter",
mode = "markers"
)
}
outplots_pts_all_intr <- plot_ly(outdata) %>%
add_trace(x = ~outdata$radius_of_influence,
y = ~outdata$volume_produced,
color = ~as.factor(outdata$gcam_basin_id),
type = "scatter",
mode = "markers")
# meaningful variables only: interactive
pairs_dynamic <- ggplotly(pairs_static_screened)
## Model comparison metrics
read.csv("outputs/united_states-maxdep1-scen1.csv") -> outdatamip
outdatamip %>% group_by(well_id) %>% mutate(areaPerWell = area/sum(number_of_wells)) -> areaPerWell
# ARCHIVE ######################################################################
#headers <- c("iteration", "year_number", "area", "radius_of_influence", "drawdown_roi", "areal_extent", "total_head", "aqfr_sat_thickness", "storativity", "thickness", "unit_cost", "hydraulic_conductivity", "radial_extent", "number_of_wells", "volume_produced", "total_volume_produced", "available_volume", "continent", "well_id", "country_name", "gcam_basin_id", "gcam_basin_name", "exploitable_groundwater", "well_installation_cost", "annual_capital_cost", "Total_nonenergy_cost", "maintenance_cost", "cost_of_energy", "energy_cost_rate", "electric_energy", "drawdown", "depletion_limit", "depth_to_piez_surface")