Skip to content

Commit

Permalink
add predict method for unsuperClass objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Leutner committed Jan 7, 2019
1 parent 5b9e747 commit c9f5259
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(fortify,RasterBrick)
S3method(fortify,RasterLayer)
S3method(fortify,RasterStack)
S3method(predict,superClass)
S3method(predict,unsuperClass)
S3method(print,mapValidation)
S3method(print,superClass)
S3method(print,unsuperClass)
Expand Down
6 changes: 4 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ New:

* function `oneHotEncode`: splits a single rasterLayer into multiple layers (one per class)
with one-hot encoding, i.e. pixel value = 1 for matches, pixel value = 0 for all other classes (background).
* ggR() can now display more than one layer. Each layer can be plotted to a subplot in a multi-panel grafic.
* `ggR()` can now display more than one layer. Each layer can be plotted to a subplot in a multi-panel grafic.
* `encodeQA()`, `decodeQA()` and `classifyQA()` can now deal with the new QA format introduced with Landsat Collection data. Legacy QA designations can still be interpreted by setting the `legacy` argument.
* new `predict()` method for unsupervised classification models (`unsuperClass`).

Changes:
* all `radCor` DOS methods now work for Landsat 8 OLI
* all `radCor` DOS methods now work for Landsat 8 OLI

Fixes:
* fix `unsuperClass` for algorithms other than Hartigan-Wong (reported by Alex Ilich)
Expand Down
34 changes: 34 additions & 0 deletions R/unsuperClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,40 @@ unsuperClass <- function(img, nSamples = 10000, nClasses = 5, nStarts = 25, nIte




#' Predict a raster map based on a unsuperClass model fit.
#'
#' applies a kmeans cluster model to all pixels of a raster.
#' Useful if you want to apply a kmeans model of scene A to scene B.
#'
#' @method predict unsuperClass
#' @param object unsuperClass object
#' @param img Raster object. Layernames must correspond to layernames used to train the superClass model, i.e. layernames in the original raster image.
#' @param ... Further arguments passed to writeRaster.
#' @export
#' @examples
#' ## Load training data
#' data(rlogo)
#'
#' ## Perform unsupervised classification
#' uc <- unsuperClass(rlogo, nClasses = 10)
#'
#' ## Apply the model to another raster
#' map <- predict(uc, rlogo)
predict.unsuperClass <- function(object, img, ...){
stopifnot(inherits(object, c("RStoolbox", "unsuperClass")))
model <- object$model
wrArgs <- list(...)
out <- .paraRasterFun(img, rasterFun=raster::calc, args = list(fun=function(x, kmeans=force(model)){
if(!is.matrix(x)) x <- as.matrix(x)
predKmeansCpp(x, centers=kmeans$centers)}, forcefun=TRUE), wrArgs = wrArgs)

return(out)
}




#' @method print unsuperClass
#' @export
print.unsuperClass <- function(x, ...){
Expand Down
29 changes: 29 additions & 0 deletions man/predict.unsuperClass.Rd

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

13 changes: 12 additions & 1 deletion tests/testthat/test-unsuperClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ test_that("kmeans fail detection", {
expect_warning(unsuperClass(lsat, nSamples = ncell(lsat), nStarts = 1, nClasses = 20), "doesn't converge properly")
})


## Predict S3 method
test_that("predict.unSuperClass", {
skip_on_cran()
uc <- unsuperClass(lsat, nSamples = ncell(lsat), nClasses = 2)
expect_s4_class(pred <- predict(uc, lsat), "RasterLayer")
expect_equal(unique(uc$map - pred), 0)
tmpFile <- tempfile(fileext = ".grd")
pred <- predict(uc, lsat, filename = tmpFile )
expect_false(inMemory(pred))
expect_equal(filename(pred), tmpFile)
file.remove(tmpFile, gsub("grd", "gri", tmpFile))
})

0 comments on commit c9f5259

Please sign in to comment.