diff --git a/R/mesma.R b/R/mesma.R index 756da55..e68ba48 100644 --- a/R/mesma.R +++ b/R/mesma.R @@ -96,18 +96,20 @@ mesma <- function(img, em, method = "NNLS", iterate = 400, tolerance = 0.0000000 if(length(em[,1]) < 2){ stop("'em' must contain at least two endmembers (number of rows in 'em').") } - em_check <- em[colnames(em)[colnames(em) != "class"]] + em_check <- em[,which(colnames(em) != "class")] if(length(em_check[1,]) != nlyr(img)){ stop("'em' and 'img' have different numbers of spectral features (number of columns in 'em', excluding column 'class'). Both need to represent the same number of spectral bands for equal spectral resolutions/ranges.") } # MESMA or SMA? + em <- as.data.frame(em) # safer for ow but generally unnecessary as we go back to matrix before nnls if(!is.null(em$class)){ classes <- unique(em$class) # check n_models n_em_cl <- min(sapply(classes, function(x) nrow(em[em$class == x,]), USE.NAMES = F)) if(n_em_cl < n_models){ warning(paste0("'n_models' cannot be larger than the minimum number of provided endmembers per class. Setting 'n_models' to ", n_em_cl, ".")) + n_models <- n_em_cl } .vMessage(paste0("Found column 'class' in 'em', creating endmember combinations using 'n_models = ", n_models, "'...")) diff --git a/man/mesma.Rd b/man/mesma.Rd index 1133b9c..8318cd4 100644 --- a/man/mesma.Rd +++ b/man/mesma.Rd @@ -19,7 +19,7 @@ mesma( \arguments{ \item{img}{SpatRaster. Remote sensing imagery (usually hyperspectral).} -\item{em}{Matrix or data.frame with spectral endmembers. Columns represent the spectral bands (i.e. columns correspond to number of bands in \code{img}). Rows represent either a single endmember of a class (SMA) or multiple endmember of a class (MESMA), if a column with name \code{class} is present, containing the class name each endmember belongs to, e.g. "water" or "land". See details below. Number of rows needs to be > 1.} +\item{em}{Matrix or data.frame with spectral endmembers. Columns represent the spectral bands (i.e. columns correspond to number of bands in \code{img}). Rows represent either a single endmember per class (SMA) or multiple endmembers per class (MESMA), if a column with name \code{class} is present, containing the class name each endmember belongs to, e.g. "water" or "land". See details below. Number of rows needs to be > 1.} \item{method}{Character. Select an unmixing method. Currently, only "NNLS" is implemented. Default is "NNLS". \itemize{ @@ -83,7 +83,7 @@ em <- rbind( ) # unmix the lsat image -probs <- mesma(img = lsat, em = em, trunc_prob = F) +probs <- mesma(img = lsat, em = em) plot(probs) }