Skip to content
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

sits_distances_from_data optimization (read the comments!) #22

Merged
merged 20 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b797696
bug fixes in sits_foreach() and sits_distances()
rolfsimoes Sep 11, 2017
96313f8
Merge branch 'master' of https://github.com/rolfsimoes/sits
rolfsimoes Sep 12, 2017
8fdf21b
Merge origin master
rolfsimoes Sep 12, 2017
5286f26
Merge branch 'dev'
rolfsimoes Sep 12, 2017
f0601f0
Merge branch 'dev' of https://github.com/rolfsimoes/sits into dev
rolfsimoes Sep 15, 2017
0316817
Merge branch 'dev' of https://github.com/rolfsimoes/sits into dev
rolfsimoes Sep 16, 2017
a263850
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Sep 18, 2017
8de7b60
Add example PRODES accuracy experiments
rolfsimoes Sep 18, 2017
e5a43d2
Merge branch 'master' of https://github.com/rolfsimoes/sits
rolfsimoes Sep 18, 2017
057e796
Merge branch 'dev'
rolfsimoes Sep 18, 2017
648d0a7
PRODES Machine Learning comparison and results.
rolfsimoes Sep 18, 2017
d146fec
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Sep 19, 2017
38ed25b
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Sep 21, 2017
42e2f66
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Sep 22, 2017
cafdc00
Merge upstream master
rolfsimoes Sep 25, 2017
2d92983
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Sep 25, 2017
cc91785
Fix ML function
rolfsimoes Sep 27, 2017
b99afb8
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Sep 27, 2017
1578f28
Merge branch 'master' of https://github.com/e-sensing/sits into dev
rolfsimoes Oct 2, 2017
fbb27b6
Introduces two new functions:
rolfsimoes Oct 4, 2017
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
Prev Previous commit
Next Next commit
Merge branch 'master' of https://github.com/e-sensing/sits into dev
Conflicts:
	R/sits_assessment.R
  • Loading branch information
rolfsimoes committed Sep 20, 2017
commit d146fecbc1b8c3e86c8fea751ffb91f9187d6db7
17 changes: 11 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: sits
Type: Package
Title: Satellite Image Time Series Analysis
Version: 0.9.2
Date: 2017-09-02
Version: 0.9.17
Date: 2017-09-17
Authors@R: c(person('Gilberto', 'Camara', role = c('aut', 'cre'), email = '[email protected]'),
person('Rolf', 'Simoes', role = c('aut'), email = '[email protected]'),
person('Victor', 'Maus', role = c('aut'), email = '[email protected]'),
Expand All @@ -25,8 +25,6 @@ BugReports: https://github.com/e-sensing/sits/issues
License: GPL-2 | file LICENSE
LazyData: true
Imports:
wtss,
dtwSat,
magrittr,
caret,
dendextend,
Expand Down Expand Up @@ -58,6 +56,7 @@ Imports:
reshape2,
R.utils,
scales,
sf,
signal,
snow,
sp,
Expand All @@ -68,7 +67,9 @@ Imports:
utils,
wtsps,
TSdist,
zoo
zoo,
wtss,
dtwSat
LinkingTo:
Rcpp
Remotes:
Expand All @@ -83,10 +84,11 @@ Collate:
'sits_TWDTW_plot.R'
'sits_WTSPS.R'
'sits_WTSS.R'
'sits_assessment.R'
'sits_accuracy.R'
'sits_classification.R'
'sits_cluster.R'
'sits_colors.R'
'sits_confusion_matrix.R'
'sits_dendrogram.R'
'sits_distances.R'
'sits_export.R'
Expand All @@ -101,10 +103,13 @@ Collate:
'sits_patterns.R'
'sits_pipe.R'
'sits_plot.R'
'sits_projections.R'
'sits_raster.R'
'sits_space_time_operations.R'
'sits_stack_proc.R'
'sits_tibble.R'
'sits_tibble_utils.R'
'sits_validate.R'
Suggests: knitr,
rmarkdown,
testthat
14 changes: 11 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

export("%>%")
export("sits_cluster_names<-")
export(sits_STRaster)
export(sits_TS_distances)
export(sits_TWDTW_classify)
export(sits_TWDTW_distances)
export(sits_TWDTW_matches)
export(sits_XY_inside_raster)
export(sits_accuracy)
export(sits_accuracy_area)
export(sits_accuracy_classif)
export(sits_algorithmWTSPS)
export(sits_align)
export(sits_apply)
export(sits_bands)
export(sits_break_ts)
export(sits_cancelProcess_WTSPS)
export(sits_classify)
export(sits_classify_raster)
export(sits_cloud_filter)
export(sits_cluster)
export(sits_cluster_cleaner)
Expand Down Expand Up @@ -42,11 +46,12 @@ export(sits_formula_smooth)
export(sits_fromCSV)
export(sits_fromGZ)
export(sits_fromJSON)
export(sits_fromLatLong)
export(sits_fromRaster)
export(sits_fromSHP)
export(sits_fromTable)
export(sits_fromWTSS)
export(sits_fromZOO)
export(sits_fromlatlong)
export(sits_fromtable)
export(sits_gam)
export(sits_gbm)
export(sits_getcovWTSS)
Expand All @@ -63,8 +68,10 @@ export(sits_kfold_validate)
export(sits_kohonen)
export(sits_labels)
export(sits_labels_list)
export(sits_latlong_to_proj)
export(sits_lda)
export(sits_linear_interp)
export(sits_match_dates)
export(sits_max_colors)
export(sits_merge)
export(sits_missing_values)
Expand All @@ -83,7 +90,6 @@ export(sits_plot_patterns)
export(sits_predict)
export(sits_prune)
export(sits_qda)
export(sits_raster)
export(sits_relabel)
export(sits_rename)
export(sits_rfor)
Expand All @@ -110,6 +116,8 @@ export(sits_toXLSX)
export(sits_toZOO)
export(sits_train)
export(sits_transmute)
export(sits_ts_fromRasterCSV)
export(sits_ts_fromRasterXY)
export(sits_values)
export(sits_whittaker)
import(dtwSat)
Expand Down
163 changes: 163 additions & 0 deletions R/sits_accuracy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#' @title Evaluates the accuracy of classification
#' @name sits_accuracy
#' @author Rolf Simoes, \email{rolf.simoes@@inpe.br}
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#
#' @description Evaluates the accuracy of classification stored in two vectors.
#' Returns a confusion matrix used by the "caret" package
#'
#' @param conf.tb A tibble containing pairs of reference and predicted values
#' @param conv.lst A list conversion list of labels. If NULL no conversion is done.
#' @param pred_sans_ext (Boolean) remove all label extension (i.e. every string after last '.' character) from predictors before compute assesment.
#' @return caret_assess a confusion matrix assessment produced by the caret package
#'
#' @export
sits_accuracy <- function(conf.tb, conv.lst = NULL, pred_sans_ext = FALSE){


# recover predicted and reference vectors from input
pred.vec <- conf.tb$predicted
ref.vec <- conf.tb$reference

# remove predicted labels' extensions
if (pred_sans_ext)
pred.vec <- tools::file_path_sans_ext(pred.vec)

# convert class names
if (!purrr::is_null(conv.lst)) {
names_ref <- dplyr::pull (dplyr::distinct (conf.tb, reference))
ensurer::ensure_that(names_ref,
all(. %in% names(conv.lst)),
err_desc = "sits_accuracy: conversion list does not contain all reference labels")
pred.vec <- as.character(conv.lst[pred.vec])
ref.vec <- as.character(conv.lst[ref.vec])
}

# call caret package to the classification statistics
caret_assess <- caret::confusionMatrix(pred.vec, ref.vec)

# print the result
.print_confusion_matrix (caret_assess)

# return invisible
return (invisible(caret_assess))
}
#' @title Area-weighted post-classification accuracy assessment of classified maps
#' @name sits_accuracy_area
#' @author Victor Maus, \email{vwmaus1@@gmail.com}
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#'
#' @description To use this function, the input table should be a set of results containing
#' both the label assigned by the user and the classification result.
#' Accuracy assessment set us a confusion matrix to determine the accuracy of your classified result.
#' This function uses an area-weighted technique proposed by Olofsson et al. to
#' produce accuracy estimates that are more reliable
#'
#' We plan to do an improved version of this function that includes a Raster R object
#' with the classified map and a vector with the labels of the classified map
#' (Gilberto-Rolf-05-Jun-2017)
#'
#' This function calls \code{\link[dtwSat]{twdtwAssess}} from \pkg{dtwSat}.
#' \code{\link[dtwSat]{twdtwAssess}} performs an accuracy assessment of the classified, including
#' Overall Accuracy, User's Accuracy, Produce's Accuracy, error matrix (confusion matrix),
#' and estimated area according to [1-2].
#'
#' @references
#' [1] Olofsson, P., Foody, G.M., Stehman, S.V., Woodcock, C.E. (2013).
#' Making better use of accuracy data in land change studies: Estimating
#' accuracy and area and quantifying uncertainty using stratified estimation.
#' Remote Sensing of Environment, 129, pp.122-131.
#'
#' @references
#' [2] Olofsson, P., Foody G.M., Herold M., Stehman, S.V., Woodcock, C.E., Wulder, M.A. (2014)
#' Good practices for estimating area and assessing accuracy of land change. Remote Sensing of
#' Environment, 148, pp. 42-57.
#'
#' @param results.tb a sits table with a set of lat/long/time locations with known and trusted labels and
#' with the result of classification method
#' @param area a list with the area of each label
#' @param conf.int specifies the confidence level (0-1).
#' @param rm.nosample if sum of columns and sum of rows of the error matrix are zero
#' then remove class. Default is TRUE.
#' @export
sits_accuracy_area <- function (results.tb, area, conf.int = 0.95, rm.nosample = FALSE){

# Get reference classes
references <- results.tb$label

# Get mapped classes
# mapped <- dplyr::bind_rows(results.tb$distances) %>%
# dplyr::select(dplyr::matches("classification")) %>% unlist

# create a vector to store the result of the predictions
mapped <- results.tb$class
# Get all labels
classes <- unique(c(references, mapped))

# Create error matrix
error_matrix <- table(factor(mapped, levels = classes, labels = classes),
factor(references, levels = classes, labels = classes))

# Get area - TO IMPROVE USING THE METADATA FROM SATELLITE PRODUCTS
if(missing(area))
area <- rowSums(error_matrix)

# Compute accuracy metrics using dtwSat::twdtwAssess
assessment <- dtwSat::twdtwAssess (error_matrix,
area = area,
conf.int = conf.int,
rm.nosample = rm.nosample )

return (assessment)

}

#' @title Evaluates the accuracy of a labelled set of data
#' @name sits_accuracy_classif
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#
#' @description Tests the accuracy of a classification model by comparing an input data set
#' that has been obtained independently to a the predicted values of the model.
#' This function can be used to test the accuracy of a classification model against a
#' data set that is obtained independently. The quality of the accuracy assessment
#' depends critically of the quality of the input data set, which should be be part of the
#' data set used for training the model.
#' This function should be used when the patterns are not directly derived from the samples.
#' It provides an initial assessment of the validity of using this set of pattern
#' to classify an area whose samples are given.
#' This function returns the Overall Accuracy, User's Accuracy,
#' Producer's Accuracy, error matrix (confusion matrix), and Kappa values.
#'
#' @param data.tb A sits tibble containing a set of samples with known and trusted labels
#' @param patterns.tb A sits tibble containing a set of patterns (independent of input data)
#' @param ml_model A model trained by \code{\link[sits]{sits_train}}
#' @param dist_method Method to compute distances (e.g., sits_TWDTW_distances)
#' @param interval Period between two classifications
#' @param ... Other parameters to be passed to the distance function
#' @return assess Assessment of validation
#' @export
sits_accuracy_classif <- function (data.tb,
patterns.tb,
ml_model,
dist_method = sits_TWDTW_distances(),
interval = "12 month") {

# does the input data exist?
.sits_test_tibble (data.tb)
.sits_test_tibble (patterns.tb)
ensurer::ensure_that (data.tb, !("NoClass" %in% sits_labels(.)),
err_desc = "sits_test_patterns: please provide a labelled set of time series")

# classify data
class.tb <- sits_classify (data.tb, patterns.tb, ml_model, dist_method, interval = interval)
# retrieve the reference labels
class.tb <- dplyr::mutate (class.tb, reference = label)

# calculate the accuracy assessment
assess <- sits_accuracy(class.tb, pred_sans_ext = TRUE)

return (invisible (assess))
}



Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.