Skip to content

feat: add option to predict probabilities #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion R/LearnerClassifSpatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ LearnerClassifSpatial = R6::R6Class("LearnerClassifSpatial",
initialize = function(learner) {
self$learner = assert_learner(learner)
super$initialize(
id = "classif.ranger",
id = learner$id,
param_set = learner$param_set,
predict_types = learner$predict_types,
feature_types = learner$feature_types,
properties = union(learner$properties, "missings"),
packages = learner$packages,
man = "mlr3learners::mlr_learners_classif.spatial"
)
self$predict_type = learner$predict_type
},

predict = function(task, row_ids = NULL) {
Expand All @@ -25,6 +26,13 @@ LearnerClassifSpatial = R6::R6Class("LearnerClassifSpatial",
pred$data$row_ids = seq_len(nrow(data))
pred$data$response = response
pred$data$truth = rep(NaN, nrow(data))
if (self$learner$predict_type == "prob") {
prob = matrix(NaN, nrow = nrow(data), ncol = 2)
prob[ids, 1] = pred$data$prob[, 1]
prob[ids, 2] = pred$data$prob[, 2]
attributes(prob) = attributes(pred$prob)
pred$data$prob = prob
}
pred
}
)
Expand Down
11 changes: 8 additions & 3 deletions R/predict_spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#' For vector data only `"sf"` is supported.
#' @param filename (`character(1)`)\cr
#' Path where the spatial object should be written to.
#' @param predict_type (`character(1)`)\cr
#' Type of prediction to return.
#' Accepted values are `"response"` and `"prob"`.
#'
#' @return Spatial object of class given in argument `format`.
#' @examples
Expand All @@ -31,10 +34,11 @@
#' # predict land cover classes
#' pred = predict_spatial(stack, learner, chunksize = 1L)
#' @export
predict_spatial = function(newdata, learner, chunksize = 200L, format = "terra", filename = NULL) {
predict_spatial = function(newdata, learner, chunksize = 200L, format = "terra", filename = NULL, predict_type = "response") {
task = as_task_unsupervised(newdata)
assert_multi_class(task$backend, c("DataBackendRaster", "DataBackendVector"))
assert_learner(learner)
assert_choice(predict_type, c("response", "prob"))

if (test_class(task$backend, "DataBackendRaster")) {
assert_number(chunksize)
Expand Down Expand Up @@ -63,7 +67,8 @@ predict_spatial = function(newdata, learner, chunksize = 200L, format = "terra",

stack = task$backend$stack
pred = learner$predict(task, row_ids = cells_seq:((cells_seq + cells_to_read - 1)))
terra::writeValues(x = target_raster, v = pred$response,
vals = if (predict_type == "prob") pred$prob[, learner$learner$state$train_task$positive] else pred$response
terra::writeValues(x = target_raster, v = vals,
start = terra::rowFromCell(stack, cells_seq), # start row number
nrows = terra::rowFromCell(stack, cells_to_read)) # how many rows
lg$info("Chunk %i of %i finished", n, length(bs$cells_seq))
Expand All @@ -72,7 +77,7 @@ predict_spatial = function(newdata, learner, chunksize = 200L, format = "terra",
terra::writeStop(target_raster)
lg$info("Finished raster prediction in %i seconds", as.integer(proc.time()[3] - start_time))

if (learner$task_type == "classif") {
if (learner$task_type == "classif" && predict_type == "response") {
levels = learner$learner$state$train_task$levels()[[learner$learner$state$train_task$target_names]]
value = data.table(ID = seq_along(levels), categories = levels)
target_raster = terra::categories(target_raster, value = value, index = 2)
Expand Down
7 changes: 6 additions & 1 deletion man/predict_spatial.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.