Repository navigation
Expand file tree
/
Copy pathmain.R
More file actions
335 lines (274 loc) · 9.39 KB
/
Copy pathmain.R
File metadata and controls
335 lines (274 loc) · 9.39 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
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
library(rsample)
library(furrr)
library(sf)
library(modelr)
library(tidyverse)
library(dplyr)
library(tidymodels)
library(purrr)
library(ggplot2)
library(magrittr)
library(data.table)
library(egg)
library(grid)
library(extrafont)
library(profvis)
library(webshot)
library(gt)
library(here)
# Setup Folder & Load Functions
repo_source <<- here()
source(paste0(repo_source, "/setup.R"))
source(paste0(repo_source, "/import_bootmap.R"))
import_bootmap(repo_source)
setup_folder()
# Get data
polygons_sf <- sf::st_read(paste0(repo_source, "/data/", polygon_name))
test_df <- polygons_sf %>%
dplyr::select(all_of(c(polygonid_column, classid_column))) %>%
sf::st_drop_geometry()
# Plan multi-process
future::plan("multisession", gc = TRUE, workers = 5)
# Get model output
model_result_df <- readRDS(paste0(repo_source, "/data/", model_result))
# PRODUCE INDICES ---------------------------------------------------------
# Bootstrap polygons for different repetition number
for (rep_number in repetition_list) {
cat(paste0("Bootstrap with ", rep_number, " repetitions is started."), "\n")
# Sample indices for given repetition number
polygon_indices_bags <- furrr::future_map(
1:rep_number,
~ boot_sample(
test_data = test_df,
sample_size = nrow(test_df),
polid_column = polygonid_column,
class_column = classid_column),
.options = furrr::furrr_options(seed = 1773))
cat(paste0("Bootstrap with", rep_number, " repetitions is finished."), "\n")
# Save as object for later use
saveRDS(polygon_indices_bags,
paste0(repo_source,
"/computed_lists/polygon_indices_bags_rep", rep_number, ".rds"))
}
# PRODUCE ACCURACY BAGS --------------------------------------------------
# Simulate Confusion Matrix List
for (rep_number in repetition_list){
# Read Bootstrap Indices with Target Repetition Number
polygon_indices_bags <- readRDS(
paste0(repo_source,
"/computed_lists/polygon_indices_bags_rep", rep_number, ".rds"))
cat(paste0("Bootstrap with ", rep_number, " repetitions is started."), "\n")
# Calculate Confusion Matrix for each bag
map_accuracy_bags <- polygon_indices_bags %>%
furrr::future_map(
~ boot_confusion(
test_data = model_result_df,
boot_indices = .x,
index_column = polygonid_column,
class_column = classid_column,
pred_column = prediction_column)) %>%
furrr::future_map(
~ drop_non_square( # Convert Non square confusion matrices to NA
# Controls are added here to prevent non square
# confusion matrices, but may still result somehow.
conf_matrix = .x)
)
map_accuracy_bags <- map_accuracy_bags[!is.na(map_accuracy_bags)] # Drop NAs
cat(paste0("Bootstrap with ", rep_number, " repetitions is finished."), "\n")
saveRDS(map_accuracy_bags,
paste0(repo_source,
"/computed_lists/map_accuracy_bags_rep", rep_number, ".rds"))
}
# PRODUCE AREA ADJUSTED BAGS ---------------------------------------------------
# Calculate Simulated Adjusted Area
for (rep_number in repetition_list){
# Read Boot Indices with Target Number
map_accuracy_bags <- readRDS(
paste0(repo_source,
"/computed_lists/map_accuracy_bags_rep", rep_number, ".rds"))
cat(paste0("Bootstrap with ", rep_number, " repetitions is started."), "\n")
# Adjust area in bags
map_area_bags <- map_accuracy_bags %>%
furrr::future_map(
~ adjust_area(
conf_matrix = .x,
area_matrix = area_matrix,
type = "User"))
cat(paste0("Bootstrap with ", rep_number, " repetitions is finished."), "\n")
saveRDS(map_area_bags,
paste0(repo_source,
"/computed_lists/map_area_bags_rep", rep_number, ".rds"))
}
# COMPARE REPETITIONS -----------------------------------------------------
# Prepare an empty list to iterate
map_accuracy_bags_list <- vector("list", 6)
for (i in seq_along(repetition_list)) {
# Read Accuracy Bags
map_accuracy_bags <- readRDS(
paste0(repo_source, "/computed_lists/map_accuracy_bags_rep",
repetition_list[[i]], ".rds"))
# Prepare Data to Visualization
rep_accuracy_data <- map_accuracy_bags %>%
prepare_accuracy_vis()
# Add Repetition Indicator
rep_accuracy_data$RepetitionNumber <- repetition_list[[i]]
# Add Data to Accuracy List
map_accuracy_bags_list[[i]] <- rep_accuracy_data
}
# Bind Rows
map_accuracy_df <- map_accuracy_bags_list %>%
dplyr::bind_rows() %>%
dplyr::filter(ClassId == "C15") %>%
dplyr::mutate(ClassName := "Tomato")
# Add "n = " to the Repetition Number
map_accuracy_df %<>%
mutate(RepetitionNumber = paste0("n = ", RepetitionNumber))
map_accuracy_df$RepetitionNumber <- factor(
map_accuracy_df$RepetitionNumber,
levels = c(
"n = 50", "n = 100", "n = 200", "n = 500",
"n = 1000", "n = 1500")
)
# Create the Plot
rep_plot <- ggplot(
map_accuracy_df,
aes(x=Accuracy, fill=ClassName)) +
theme(legend.title = element_blank()) +
geom_histogram(color="black", bins = 30) +
scale_fill_manual(values = c("Tomato"="#FF0000")) +
facet_wrap(. ~ RepetitionNumber) +
theme(text = element_text(size = 22)) +
theme(legend.position = "none")
# Save Plot
ggplot2::ggsave(
filename = paste0(repo_source, "/visualizations",
"/bootmap_accuracyhistogram_differentreps_tomato.png"),
width = 40, height = 30, units = "cm",
dpi = "retina",
plot = rep_plot)
# ALLUVIAL PLOT -----------------------------------------------------------
p <- alluvial_plot(
test_df = model_result_df,
prediction_column = prediction_column,
reference_column = classid_column,
class_id = class_id,
class_decoder = class_decoder,
class_color = class_color
)
render_function_str <- build_htmlrender_str(
class_decoder = color_df$class_decoder,
class_color = color_df$class_color
)
p <- htmlwidgets::onRender(p, eval(render_function_str))
# Save Plot
htmlwidgets::saveWidget(
p, file = paste0(repo_source,
"/visualizations/bootmap_alluvialplot_test_allclasses.html")
)
# BOOTSTRAP HISTOGRAM -----------------------------------------------------
# Work on 1500 repetitions
# Read Accuracy Bags
map_accuracy_bags <- readRDS(
paste0(repo_source,
"/computed_lists/map_accuracy_bags_rep1500.rds"))
# Prepare Accuracy Bags for visualization
accuracy_data <- map_accuracy_bags %>% prepare_accuracy_vis()
# Visualize Accuracy Histogram - Multiple Classes
boot_vis_multiple(
vis_data = accuracy_data,
plot_name = "bootmap_accuracyhistogram_rep1500_allclasses.png",
plot_source = paste0(repo_source, "/visualizations"),
type = "Accuracy",
class_list = class_list,
class_decoder = class_decoder,
fill_cols = class_color
)
# Visualize Accuracy Histogram - Single Class
for (class_id in class_list) {
# Filter class from class_df
class_scope_df <- class_df %>%
dplyr::filter(class_list == class_id)
# Visualize
boot_vis_single(
vis_data = accuracy_data,
plot_name = paste0(
"bootmap_accuracyhistogram_rep1500_",
class_scope_df$class_decoder,
".png"),
plot_source = paste0(repo_source, "/visualizations"),
type = "Accuracy",
class_list = class_id,
class_decoder = class_scope_df$class_decoder,
fill_cols = class_scope_df$class_color
)
}
# Read Area Bags
map_area_bags <- readRDS(
paste0(repo_source,
"/computed_lists/map_area_bags_rep1500.rds"))
# Prepare Area Bags for visualization
area_data <- map_area_bags %>% prepare_area_vis()
# Visualize Area Histogram - Multiple Classes
boot_vis_multiple(
vis_data = area_data,
plot_name = "bootmap_areahistogram_rep1500_allclasses.png",
plot_source = paste0(repo_source, "/visualizations"),
type = "Area",
class_list = class_list,
class_decoder = class_decoder,
fill_cols = class_color
)
# Visualize Area Histogram - Single Class
for (class_id in class_list) {
# Filter class from class_df
class_scope_df <- class_df %>%
dplyr::filter(class_list == class_id)
# Visualize
boot_vis_single(
vis_data = area_data,
plot_name = paste0(
"bootmap_areahistogram_rep1500_",
class_scope_df$class_decoder, ".png"),
plot_source = paste0(repo_source, "/visualizations"),
type = "Area",
class_list = class_id,
class_decoder = class_scope_df$class_decoder,
fill_cols = class_scope_df$class_color
)
}
# BOOTSTRAP TABLE ---------------------------------------------------------
# Work on 1500 repetitions
# Read Accuracy Bags
map_accuracy_bags <- readRDS(
paste0(repo_source,
"/computed_lists/map_accuracy_bags_rep1500.rds"))
# Accuracy Table
accuracy_table <- prepare_list_table(
bag_list = map_accuracy_bags,
type = "Accuracy")
boot_vis_table(
table_data = accuracy_table,
plot_name = "bootmap_accuracytable_rep1500_allclasses.png",
plot_source = paste0(repo_source, "/visualizations"),
type = "Accuracy",
class_list = class_list,
class_decoder = class_decoder,
unit = "%")
# Read Area Bags
map_area_bags <- readRDS(
paste0(repo_source,
"/computed_lists/map_area_bags_rep1500.rds"))
# Area Table
area_table <- prepare_list_table(
bag_list = map_area_bags,
type = "Area")
# Add First Area Estimation Column
area_table$AreaEstimation <- area_matrix$area_list
boot_vis_table(
table_data = area_table,
plot_name = "bootmap_areatable_rep1500_allclasses.png",
plot_source = paste0(repo_source, "/visualizations"),
type = "Area",
class_list = class_list,
class_decoder = class_decoder,
unit = "ha")