-
Notifications
You must be signed in to change notification settings - Fork 6
/
modelmap_processing.R
367 lines (318 loc) · 15 KB
/
modelmap_processing.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
#' Run model for wetland prediction
#'
#' @param qdatafn training data filename.
#' @param model.type model type to use (string). One of "RF", "QRF", or "CF".
#' @param model.folder folder where the output folder will be created.
#' @param unique.rowname unique identifier column name in the training data.
#' @param predList character vector of predictor names that match the column
#' names in the training data.
#' @param predFactor character vector of predictor names that are factors
#' (categorical), or FALSE if there are no categorical predictors.
#' @param response.name response variable (string); column name in the training
#' data.
#' @param response.type response type (string). One of "binary", "categorical",
#' or "continuous".
#' @param seed number (integer) used to initialize randomization.
#' @param response.target optional character vector used to subset training
#' data to include only those rows that have a value in \code{response.name}
#' column that matches those in \code{response.target}.
#' @param aoi.col optional string to specify the column name in the training
#' data which identifies seperate AOIs. The values in this column must not
#' contain the character "-". Setting this parameter means a seperate model
#' object will be output each using a sub-set of training points defined by
#' the values in this column. All sub-sets will be run unless the
#' \code{aoi.target} parameter is specified. To run the model using all
#' training points, use \code{aoi.col = NULL} (default).
#' @param aoi.target optional character vector to identify which AOI to run
#' the model on. If \code{aoi.target = NULL} (default), and \code{aoi.col}
#' has been specified, the model will be run on each AOI in turn. \code{aoi.
#' target} will be appended on to the output folder name created for each AOI.
#' @param MODELfn optional filename to use for saved model output. If
#' \code{MODELfn = NULL} (default), a unique name is created automatically
#' using the date and time the model is run. If \code{aoi.col} is specified,
#' each unique value in this field is appended to \code{MODELfn} for each
#' model run.
#' @param na.action optional string to specify action to take if there are NA
#' values in the predictor data. Defaults to \code{na.roughfix}.
#' @param ... any other parameters to pass to ModelMap::model.build.
#' @return list containing model object from ModelMap::model.build for each AOI;
#' list elements are named by the output model folder string (including AOI
#' target name if aoi.target is provided).
wetland_model <- function(qdatafn,
model.type,
model.folder,
unique.rowname,
predList,
predFactor,
response.name,
response.type,
seed,
response.target = NULL,
aoi.col = NULL,
aoi.target = NULL,
MODELfn = NULL,
na.action = "na.roughfix",
...
) {
qdata <- read.csv(qdatafn, stringsAsFactors = FALSE)
# Subset training data based on response.target, or use the full dataset
if (!is.null(response.target)) {
qdata <- qdata[qdata[[response.name]] %in% response.target,]
}
# Generate unique model run name if MODELfn param is NULL
if (is.null(MODELfn)) {
MODELfn <- format(Sys.time(), "%Y%m%d-%H%M%S")
}
# Don't use aoi.target if aoi.col is not specified
if (is.null(aoi.col)) {
aoi.target <- NULL
} else {
# If aoi.col is specified but aoi.target is NULL, find all unique values in
# aoi.col
if (is.null(aoi.target)) {
aoi.target <- unique(qdata[[aoi.col]])
}
}
# Prepend aoi.target to model run name
if (!is.null(aoi.target)) {
MODELfn <- paste(MODELfn, aoi.target, sep = "-")
}
# Create return list based on number of AOIs
out.list <- vector(mode = "list", length = length(MODELfn))
names(out.list) <- MODELfn
# For each AOI...
for (i in 1:length(MODELfn)) {
# Create new folder for model output
dir.create(model.folder.out <- file.path(model.folder, MODELfn[i]),
showWarnings = TRUE)
# Subset training data based on AOI column, or use the full dataset
if (!is.null(aoi.col)) {
qdata.aoi <- qdata[qdata[[aoi.col]] == aoi.target[i],]
} else {
qdata.aoi <- qdata
}
# Run the model
#---------------
model.obj <- ModelMap::model.build(model.type = model.type,
qdata.trainfn = qdata.aoi,
folder = model.folder.out,
unique.rowname = unique.rowname,
MODELfn = MODELfn[i],
predList = predList,
predFactor = predFactor,
response.name = response.name,
response.type = response.type,
na.action = na.action,
seed = seed,
...)
# TO DO:
# Copy model params to log - read {MODELfn}_model_building_arguments.txt and
# write param values to log.csv, so they can be re-used as input to subsequent
# model runs. Optional - append to existing log file to keep a running record
# of model runs.
# ...
# Model Diagnostics
#-------------------
# TO DO:
# Set par for better pdf layout; set back to original values after.
# ...
model.pred <- ModelMap::model.diagnostics(model.obj = model.obj,
qdata.trainfn = qdata.aoi,
folder = model.folder.out,
MODELfn = MODELfn[i],
unique.rowname = unique.rowname,
# Model Validation Arguments
prediction.type = "OOB",
device.type = "pdf",
cex = 1.2)
# TO DO:
# Save diagnostic values (section commented out below) to an output csv?
# Which values to save?
# Write diagnostic values to same log.csv as model params. Append to existing
# file so that multiple model runs can be recorded and compared.
# ...
# # Look at confusion matrix text output, read into R
# # For categorical models, this file contains the observed category for each
# # location, category predicted by majoirty vote, as well as one column for each
# # category observed in the data, giving the proportion of trees that voted for
# # that category.
# CMX.CSV <- read.table(file.path(model.folder.out, paste0(MODELfn, "_pred_cmx.csv")),
# header = FALSE,
# sep = ",",
# stringsAsFactors = FALSE)
#
# PRED <- read.table(file.path(model.folder.out, paste0(MODELfn, "_pred.csv")),
# header = TRUE,
# sep = ",",
# stringsAsFactors = TRUE)
#
# # To calculate conf. matrix, we use observed and predicted columns. Read.table()
# # function converts columns containing strings to factors.
# # Because there are many categories present in the observed data, to get a
# # symetric confusion matrix it is important to make sure all levels are presents
# # in both factors. These lines are needed for numeric categories, redundant for
# # character categories.
# PRED$pred <- as.factor(PRED$pred)
# PRED$obs <- as.factor(PRED$obs)
#
# # Adjust levels so all values are included in both observed and predicted.
# LEVELS <- unique(c(levels(PRED$pred), levels(PRED$obs)))
# PRED$pred <- factor(PRED$pred, levels = LEVELS)
# PRED$obs <- factor(PRED$obs, levels = LEVELS)
#
# # Calculate confusion matrix
# # Calculate the errors of Omission and Comission
# CMX <- table(predicted = PRED$pred, observed = PRED$obs)
# CMX.diag <- diag(CMX)
# CMX.OMISSION <- 1 - (CMX.diag / apply(CMX, 2, sum))
# CMX.COMISSION <- 1 - (CMX.diag / apply(CMX, 1, sum))
#
# # Calculate PCC (Percent correctly classified)
# CMX.PCC <- sum(CMX.diag) / sum(CMX)
#
# # Calculate Kappa
# CMX.KAPPA <- PresenceAbsence::Kappa(CMX)
#
# # Calculate MAUC. Calculated from the category specific predicitons (% of trees
# # that voted for each category)
# VOTE <- HandTill2001::multcap(response = PRED$obs,
# predicted = as.matrix(PRED[, -c(1, 2, 3)]))
# MAUC <- HandTill2001::auc(VOTE)
out.list[[MODELfn[i]]] <- model.obj
}
return(out.list)
}
#' Wetland map production
#'
#' @param model.out list object returned from \code{wetland_model}. List
#' contains model object(s) from ModelMap::model.build for each AOI;
#' list elements are named by the output model folder string (including AOI
#' target name if aoi.target is provided).
#' @param model.folder folder where the output from \code{wetland_model} was
#' created. Same as \code{model.folder} input to \code{wetland_model}.
#' @param rastLUTfn filename of a .csv or dataframe of a rastLUT.
#' @param aoi optional SpatialPolygon object that was used to intersect with
#' input points. This parameter is required if \code{model.out} was generated
#' using an AOI.
#' @param aoi.col optional string to specify the column name in the \code{aoi}
#' data which identifies seperate AOIs. Required if \code{aoi} is provided.
#' @param na.action optional string to specify action to take if there are NA
#' values in the prediction data. Defaults to \code{na.omit}.
#' @param ... any other parameters to pass to ModelMap::model.mapmake.
wetland_map <- function (model.out,
model.folder,
rastLUTfn,
aoi = NULL,
aoi.col = NULL,
na.action = "na.omit",
...) {
# Loop through all model objects in model.out
for (i in 1:length(model.out)) {
if (is.null(aoi) & length(model.out) > 1) {
# Model was run with an AOI, but no AOI file provided
stop("Your model output contains more than 1 model object, but no AOI
object has been provided. Please set the aoi parameter.")
} else if (!is.null(aoi) & is.null(aoi.col)) {
stop("The aoi.col parameter is required if aoi parameter is not NULL.")
} else if (!is.null(aoi)) {
# Extract the AOI target name from the model name
aoi.target <- tail(strsplit(names(model.out)[i], "-")[[1]], n = 1)
# Check that the aoi.target exists in aoi object's aoi.col field
if (!aoi.target %in% aoi[[aoi.col,]]) {
stop(paste0("The AOI used for this model run (",
aoi.target,
") does not appear to exist in the AOI object provided."))
} else {
# TO DO:
# Clip rasters (from rastLUT) to AOI and save in temp folder
# ...
# TO DO:
# Generate temp rastLUT using AOI rasters
# ...
}
}
MODELfn <- names(model.out)[i]
model.folder.out <- file.path(model.folder, MODELfn)
# model.mapmake() creates an ascii text file and an imagine .img file of
# predictions for each map pixel.
model.mapmake(model.obj = model.out[i],
folder = model.folder.out,
MODELfn = MODELfn,
rastLUTfn = rastLUTfn,
na.action = na.action,
...)
# Read rastLUTfn if not a dataframe
if (!is.data.frame(rastLUTfn)) {
rastLUT <- read.csv("../../testdata/raster_stack/rastLUT.csv",
header = FALSE,
stringsAsFactors = FALSE)
} else {
rastLUT <- rastLUTfn
}
# Read predList from rastLUT
predList <- rastLUT[, 2]
# Create a RasterStack from rastLUT rasters if AOI is used. This will be used
# to clip rasters to AOIs
if (!is.null(aoi)) {
rs <- stack_rasters(unique(rastLUT[ ,1]), aligned = TRUE)
# Remove any layers/bands not in the rastLUT
rs <- raster::subset(rs, rastLUT[ ,2])
}
# Loop through all model objects in model.out
for (i in 1:length(model.out)) {
# Model was run with an AOI, but no AOI provided
if (is.null(aoi) & length(model.out) > 1) {
stop("Your model output contains more than 1 model object, but no AOI
object has been provided. Please set the aoi parameter.")
# AOI provided, but no AOI column specified
} else if (!is.null(aoi) & is.null(aoi.col)) {
stop("The aoi.col parameter is required if aoi parameter is not NULL.")
# AOI has been provided...so clip the rasters
} else if (!is.null(aoi)) {
# Extract the AOI target name from the model name
aoi.target <- tail(strsplit(names(model.out)[i], "-")[[1]], n = 1)
# Check that the aoi.target exists in aoi object's aoi.col field
if (!aoi.target %in% aoi[[aoi.col,]]) {
stop(paste0("The AOI used for this model run (", aoi.target,
") does not exist in the AOI object provided."))
} else {
# Clip rasters (from rastLUT) to AOI and save in temp folder
# Extract AOI target poly
aoi.target.shp <- aoi[aoi[[aoi.col]] == aoi.target,]
# Clip rasters to poly
rs.crop <- raster::crop(rs, extent(aoi.target.shp))
rs.mask <- raster::mask(rs.crop, aoi.target.shp)
# Write clipped rasters to temp files
model.folder.tmp <- file.path(model.folder, paste0("tmp_", aoi.target))
dir.create(model.folder.tmp, showWarnings = FALSE)
rs.files <- paste0(file.path(model.folder.tmp, names(rs.mask)), ".img")
raster::writeRaster(rs.mask,
filename = rs.files,
bylayer = TRUE,
overwrite = TRUE,
format = "HFA")
# Generate temp rastLUT
# All temp rasters are single band
rastLUT <- data.frame(rs.files, predList, 1, stringsAsFactors = FALSE)
}
}
MODELfn <- names(model.out)[i]
model.folder.out <- file.path(model.folder, MODELfn)
# Make map raster
#-------------------
# model.mapmake() creates an ascii text file and an imagine .img file of
# predictions for each map pixel.
ModelMap::model.mapmake(model.obj = model.out[[i]],
folder = model.folder.out,
MODELfn = MODELfn,
rastLUTfn = rastLUT,
na.action = na.action,
...)
# Clean up any temp folders
unlink(file.path(model.folder, "tmp_*"), recursive = TRUE)
}
# TO DO:
# Setup mapcodes
# Produce map
# ...
}