This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_predict.R
266 lines (209 loc) · 10.1 KB
/
cluster_predict.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
#####
## BEGIN: Predict for gridded covariates
## Let's parallelize the process using the snow package:
## NOTE: The 00.1, alpha, script used the new parallel package
## but it seems like this is a simpler way given that we have
## to write out our results one block at a time...
## NOTE also: If you've changed the predictor set then you
## need to change the column renaming in the cluster_predict()
## function and the subset of the RasterLayer objects in the
## raster brick that gets created before the process is started...
cluster_predict <- function(prediction_raster,
quant_output=FALSE,
minblocks=NULL,
nmb = 50) {
tStart <- Sys.time()
if (is.null(minblocks)) {
minblocks <- wpGetBlocksNeeded(prediction_raster,
cores=bsgm.cluster_workers,
n=nmb)
}
loginfo(paste0("Covariate_stack will be divided to ",minblocks,' blocks'))
cl <- getCluster()
on.exit( returnCluster() )
nodes <- length(cl)
blocks <- blockSize(prediction_raster, minblocks=minblocks)
clusterEvalQ(cl, {
require(raster)
require(randomForest)
})
if (quant_output) {
clusterExport(cl, c("bsgmfit_final", "bsgmfit_quant", "covariate_stack"))
} else {
clusterExport(cl, c("bsgmfit_final", "covariate_stack"))
}
clusterExport(cl, "blocks", envir=environment())
clusterExport(cl, "quant_output", envir=environment())
#################################################################################
#################################################################################
## Define the function that will be run on each cluster to do the predictions:
#
call_predictions <- function (i) {
row_data <- data.frame( getValues(covariate_stack,
row=blocks$row[i],
nrows=blocks$nrows[i]) )
## Convert field names to something more manageable and
## that matches our bsgmfit variable list:
## Full covariate stack:
#
names(row_data) <- c(names(bsgmfit_final$forest$xlevels),"region_mask")
## Detect if we have any NA or Inf values, and that the values are
## covered by our census administrative units:
#
na_present <- apply(is.na(row_data), 1, any)
inf_present <- apply(row_data == -Inf | row_data == Inf, 1, any)
region_mask_vec <- (is.na(row_data$region_mask))
## Use the first if you want to mask out water pixels, this can greatly
## speed up predictions over areas with a lot of water, however, you
## run the risk of having no predictions in the resulting dataset
## if you have a census block small enough that it might only have
## water cover (GeoCover/GlobCover is what determines the water mask):
#
roi_subset <- (!na_present & !inf_present & !region_mask_vec)
## Create a set of predictions based on our covariates:
#
predictions <- numeric(length=length(row_data[,1]))
predictions[] <- NA
if (quant_output) {
predictions <- data.frame("rf_pred"=predictions,
"rf_sd"=predictions,
"rf_05"=predictions,
"rf_50"=predictions,
"rf_95"=predictions)
}else{
predictions <- data.frame("rf_pred"=predictions,
"rf_sd"=predictions)
}
## I f we have data where NAs or Inf values are not present then we
## predict for those cells (where we subset our data according to the
## roi_subset and remove the census zone and water mask columns (length(row_data) - 2):
#
if (sum(roi_subset) > 0) {
## Use the below to get probabilities:
prediction_set <- predict(bsgmfit_final,
newdata=row_data[roi_subset,1:(length(row_data)-1)],
predict.all=TRUE,
type = "prob")
## Retrieve the probability of transition predictions from the prediction set:
## Enable below for the proportion of votes, i.e. the probability of transition:
predictions$rf_pred[roi_subset] <- prediction_set$aggregate[,2]
## Use the below to get the binary classification response:
# prediction_set <- predict(bsgmfit_final,
# newdata=row_data[roi_subset,1:(length(row_data)-2)],
# predict.all=TRUE,
# type = "response")
#
## Convert the individual tree predictions to numeric:
class(prediction_set$aggregate) <- "numeric"
predictions$rf_sd[roi_subset] <- apply(prediction_set$individual, MARGIN=1, sd)
if (quant_output) {
prediction_set <- predict(bsgmfit_quant,
newdata=row_data[roi_subset,1:(length(row_data)-1)],
quantiles=c(0.05, 0.50, 0.95))
predictions$rf_05[roi_subset] <- prediction_set[,1]
predictions$rf_50[roi_subset] <- prediction_set[,2]
predictions$rf_95[roi_subset] <- prediction_set[,3]
}
}
return(predictions)
}
#
##
#################################################################################
#################################################################################
## Start all nodes on a prediction:
for (i in 1:nodes) {
sendCall(cl[[i]], call_predictions, i, tag=i)
}
## Start the raster writer object so we can store our results as they
## come back from our cluster:
#
prediction_raster <- writeStart(prediction_raster,
filename=paste0(bsgm.output.path.countries,
bsgm.predict.density.rf.pred),
format="GTiff",
datatype="FLT4S",
overwrite=TRUE,
options=c("COMPRESS=LZW"))
sd_raster <- prediction_raster
sd_raster <- writeStart(sd_raster,
filename=paste0(bsgm.output.path.countries, bsgm.predict.density.rf.sd),
format="GTiff",
datatype="FLT4S",
overwrite=TRUE,
options=c("COMPRESS=LZW"))
if (quant_output) {
prediction_raster_05 <- prediction_raster
prediction_raster_05 <- writeStart(prediction_raster_05,
filename=paste0(bsgm.output.path.countries, bsgm.predict.density.rf.pred_05),
format="GTiff",
datatype="FLT4S",
overwrite=TRUE,
options=c("COMPRESS=LZW"))
prediction_raster_50 <- prediction_raster
prediction_raster_50 <- writeStart(prediction_raster_50,
filename=paste0(bsgm.output.path.countries, bsgm.predict.density.rf.pred_50),
format="GTiff",
datatype="FLT4S",
overwrite=TRUE,
options=c("COMPRESS=LZW"))
prediction_raster_95 <- prediction_raster
prediction_raster_95 <- writeStart(prediction_raster_95,
filename=paste0(bsgm.output.path.countries, bsgm.predict.density.rf.pred_95),
format="GTiff",
datatype="FLT4S",
overwrite=TRUE,
options=c("COMPRESS=LZW"))
}
########################################################################
##
## Create our primary cluster processing loop, recalling that we already
## have clusters running:
#
for (i in 1:blocks$n) {
## Receive results from a node:
predictions <- recvOneData(cl)
## Check if there was an error:
if (!predictions$value$success) {
stop("ERROR: Cluster barfed...\n\n", predictions)
}
## Which block are we processing:
block <- predictions$value$tag
prediction_raster <- writeValues(prediction_raster,
predictions$value$value$rf_pred,
blocks$row[block])
sd_raster <- writeValues(sd_raster,
predictions$value$value$rf_sd,
blocks$row[block])
if (quant_output) {
prediction_raster_05 <- writeValues(prediction_raster_05,
predictions$value$value$rf_05,
blocks$row[block])
prediction_raster_50 <- writeValues(prediction_raster_50,
predictions$value$value$rf_50,
blocks$row[block])
prediction_raster_95 <- writeValues(prediction_raster_95,
predictions$value$value$rf_95,
blocks$row[block])
}
## Check to see if we are at the end of our block list:
ni <- nodes + i
if (ni <= blocks$n) {
sendCall(cl[[predictions$node]], call_predictions, ni, tag=ni)
}
tEnd <- Sys.time()
wpProgressMessage(i,
max=blocks$n,
label= paste0("received block ",ni,
" Processing Time: ",
wpTimeDiff(tStart,tEnd)))
}
prediction_raster <- writeStop(prediction_raster)
sd_raster <- writeStop(sd_raster)
if (quant_output) {
prediction_raster_05 <- writeStop(prediction_raster_05)
prediction_raster_50 <- writeStop(prediction_raster_50)
prediction_raster_95 <- writeStop(prediction_raster_95)
}
return(prediction_raster)
}