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

standardize/style code formatting #117

Merged
merged 4 commits into from
May 25, 2020
Merged
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
209 changes: 112 additions & 97 deletions hyperSpec/R/Arith.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,74 +76,78 @@
##' flu / flu$c


setMethod ("Arith", signature (e1 = "hyperSpec", e2 = "hyperSpec"),
function (e1, e2){
validObject (e1)
validObject (e2)
setMethod(
"Arith", signature(e1 = "hyperSpec", e2 = "hyperSpec"),
function(e1, e2) {
validObject(e1)
validObject(e2)

e1 <- .expand (e1, dim (e2) [c (1, 3)])
e2 <- .expand (e2, dim (e1) [c (1, 3)])
e1 <- .expand(e1, dim(e2) [c(1, 3)])
e2 <- .expand(e2, dim(e1) [c(1, 3)])

e1 [[]] <- callGeneric (e1[[]], e2[[]])
e1
}
)
e1 [[]] <- callGeneric(e1[[]], e2[[]])
e1
}
)

.arithx <- function (e1, e2){
validObject (e1)
.arithx <- function(e1, e2) {
validObject(e1)

if (missing (e2)){
e1 [[]] <- callGeneric (e1 [[]])
if (missing(e2)) {
e1 [[]] <- callGeneric(e1 [[]])
e1
} else {
e2 <- as.matrix (e2)
e2 <- as.matrix(e2)

## called /only/ with e1 hyperSpec but e2 matrix-like
e1 <- .expand (e1, dim (e2))
e2 <- .expand (e2, dim (e1) [c (1, 3)])
e1 <- .expand(e1, dim(e2))
e2 <- .expand(e2, dim(e1) [c(1, 3)])

e1 [[]] <- callGeneric (e1 [[]], e2)
e1 [[]] <- callGeneric(e1 [[]], e2)
e1
}
}
##' @rdname Arith
setMethod ("Arith", signature (e1 = "hyperSpec", e2 = "numeric"), .arithx)
setMethod("Arith", signature(e1 = "hyperSpec", e2 = "numeric"), .arithx)
##' @rdname Arith
setMethod ("Arith", signature (e1 = "hyperSpec", e2 = "matrix"), .arithx)
setMethod("Arith", signature(e1 = "hyperSpec", e2 = "matrix"), .arithx)
##' @rdname Arith
setMethod ("Arith", signature (e1 = "hyperSpec", e2 = "missing"), .arithx)
setMethod("Arith", signature(e1 = "hyperSpec", e2 = "missing"), .arithx)

.arithy <- function (e1, e2){
e1 <- as.matrix (e1)
validObject (e2)
.arithy <- function(e1, e2) {
e1 <- as.matrix(e1)
validObject(e2)

## called /only/ with e2 hyperSpec but e1 matrix-like
e1 <- .expand (e1, dim (e2) [c (1, 3)])
e2 <- .expand (e2, dim (e1))
e1 <- .expand(e1, dim(e2) [c(1, 3)])
e2 <- .expand(e2, dim(e1))

e2 [[]] <- callGeneric (e1, e2 [[]])
e2 [[]] <- callGeneric(e1, e2 [[]])
e2
}
##' @rdname Arith
setMethod ("Arith", signature (e1 = "numeric", e2 = "hyperSpec"), .arithy)
setMethod("Arith", signature(e1 = "numeric", e2 = "hyperSpec"), .arithy)
##' @rdname Arith
setMethod ("Arith", signature (e1 = "matrix", e2 = "hyperSpec"), .arithy)
setMethod("Arith", signature(e1 = "matrix", e2 = "hyperSpec"), .arithy)

##' @param m matrix
##' @param target.dim target size to expand the vector to for the sweep-shortcuts
##' @noRd
.expand <- function (m, target.dim) {
m.dim = dim (m)
.expand <- function(m, target.dim) {
m.dim <- dim(m)

if (m.dim [1] == 1L & target.dim [1] > 1L)
m <- m [rep (1, target.dim [1]),, drop = FALSE]
if (m.dim [1] == 1L & target.dim [1] > 1L) {
m <- m [rep(1, target.dim [1]), , drop = FALSE]
}

if (is (m, "hyperSpec")) {
if (m.dim [3] == 1L & target.dim [2] > 1L)
m <- m [,, rep (1, target.dim [2]), wl.index = TRUE]
if (is(m, "hyperSpec")) {
if (m.dim [3] == 1L & target.dim [2] > 1L) {
m <- m [, , rep(1, target.dim [2]), wl.index = TRUE]
}
} else {
if (m.dim [2] == 1L & target.dim [2] > 1L)
m <- m [, rep (1, target.dim [2]), drop = FALSE]
if (m.dim [2] == 1L & target.dim [2] > 1L) {
m <- m [, rep(1, target.dim [2]), drop = FALSE]
}
}

m
Expand All @@ -156,79 +160,90 @@ setMethod ("Arith", signature (e1 = "matrix", e2 = "hyperSpec"), .arithy)
##' %*%,hyperSpec,matrix-method
##' @export
##' @seealso \code{\link[base]{matmult}} for matrix multiplications with \code{\%*\%}.
setMethod ("%*%", signature (x = "hyperSpec", y = "hyperSpec"),
function (x, y){
validObject (x)
validObject (y)

if (ncol(y) > 1)
warning(paste("Dropping column(s) of y:", paste(colnames(y$..),
collapse = ", ")))

x@data$spc <- x@data$spc %*% y@data$spc
.wl (x) <- y@wavelength
x@label$.wavelength = y@label$.wavelength

x
}
)
setMethod(
"%*%", signature(x = "hyperSpec", y = "hyperSpec"),
function(x, y) {
validObject(x)
validObject(y)

if (ncol(y) > 1) {
warning(paste("Dropping column(s) of y:", paste(colnames(y$..),
collapse = ", "
)))
}

x@data$spc <- x@data$spc %*% y@data$spc
.wl(x) <- y@wavelength
x@label$.wavelength <- y@label$.wavelength

x
}
)

##' @rdname Arith
setMethod ("%*%", signature (x = "hyperSpec", y = "matrix"),
function (x, y){
validObject (x)
x@data$spc <- x@data$spc %*% y
.wl (x) <- seq_len (ncol (y))
x@label$.wavelength = NA
x
}
)
setMethod(
"%*%", signature(x = "hyperSpec", y = "matrix"),
function(x, y) {
validObject(x)
x@data$spc <- x@data$spc %*% y
.wl(x) <- seq_len(ncol(y))
x@label$.wavelength <- NA
x
}
)

##' @rdname Arith
setMethod ("%*%", signature (x = "matrix", y = "hyperSpec"),
function (x, y){
validObject (y)

if (ncol(y) > 1)
warning(paste("Dropping column(s) of y:", paste(colnames(y$..),
collapse = ", ")))
y <- new ("hyperSpec",
wavelength = y@wavelength,
spc = x %*% y@data$spc,
log = y@log
)

y
}
)

.test (.arithx) <- function (){
context ("Arith")
setMethod(
"%*%", signature(x = "matrix", y = "hyperSpec"),
function(x, y) {
validObject(y)

if (ncol(y) > 1) {
warning(paste("Dropping column(s) of y:", paste(colnames(y$..),
collapse = ", "
)))
}
y <- new("hyperSpec",
wavelength = y@wavelength,
spc = x %*% y@data$spc,
log = y@log
)

y
}
)

.test(.arithx) <- function() {
context("Arith")

test_that("binary -", {
expect_equal (as.matrix (flu - flu),
matrix (0, nrow = nrow (flu), ncol = nwl (flu), dimnames = dimnames (flu [[]])))

expect_equal (as.matrix (flu - flu [1]), as.matrix (sweep (flu, 2, flu [1], `-`)))

expect_equal (as.matrix (flu - flu [,, 450]), as.matrix (sweep (flu, 1, flu [,, 450], `-`)))
expect_equal(
as.matrix(flu - flu),
matrix(0, nrow = nrow(flu), ncol = nwl(flu), dimnames = dimnames(flu [[]]))
)

expect_equal(as.matrix(flu - flu [1]), as.matrix(sweep(flu, 2, flu [1], `-`)))

expect_equal(as.matrix(flu - flu [, , 450]), as.matrix(sweep(flu, 1, flu [, , 450], `-`)))
})

test_that("binary /", {
expect_equal (as.matrix (flu / flu),
matrix (1, nrow = nrow (flu), ncol = nwl (flu), dimnames = dimnames (flu [[]])))

expect_equal (as.matrix (flu / flu [1]), as.matrix (sweep (flu, 2, flu [1], `/`)))

expect_equal (as.matrix (flu / flu [,, 450]), as.matrix (sweep (flu, 1, flu [,, 450], `/`)))
expect_equal(
as.matrix(flu / flu),
matrix(1, nrow = nrow(flu), ncol = nwl(flu), dimnames = dimnames(flu [[]]))
)

expect_equal(as.matrix(flu / flu [1]), as.matrix(sweep(flu, 2, flu [1], `/`)))

expect_equal(as.matrix(flu / flu [, , 450]), as.matrix(sweep(flu, 1, flu [, , 450], `/`)))
})

test_that("binary + with scalar", {
expect_equal (as.matrix (flu + 1), as.matrix (flu) + 1)
expect_equal (as.matrix (1 + flu), as.matrix (flu) + 1)
expect_equal(as.matrix(flu + 1), as.matrix(flu) + 1)
expect_equal(as.matrix(1 + flu), as.matrix(flu) + 1)
})

test_that("unary -", {
expect_equal (as.matrix (-flu), - as.matrix (flu))
expect_equal(as.matrix(-flu), -as.matrix(flu))
})
}
59 changes: 29 additions & 30 deletions hyperSpec/R/Compare.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
##' The comparison operators \code{>}, \code{<}, \code{>=}, \code{<=},
##' \code{==}, and \code{!=} for \code{hyperSpec} objects.
##'
##'
##' \code{all.equal} checks the equality of two hyperSpec objects.
##'
##'
##' The comparison operators \code{>}, \code{<}, \code{>=}, \code{<=},
##' \code{==}, and \code{!=} work on the spectra matrix of the \code{hyperSpec}
##' object. They have their usual meaning (see \code{\link[base]{Comparison}}).
##' The operators work also with one \code{hyperSpec} object and a numeric
##' (scalar) object or a matrices of the same size as the spectra matrix of the
##' \code{hyperSpec} object.
##'
##'
##' With numeric vectors \code{\link[hyperSpec]{sweep}} might be more
##' appropriate.
##'
##'
##' If you want to calculate on the \code{data.frame} \code{hyperSpec@@data},
##' you have to do this directly on \code{hyperSpec@@data}.
##'
Expand All @@ -29,56 +29,55 @@
##' @param e1,e2 Either two \code{hyperSpec} objects or one \code{hyperSpec}
##' object and matrix of same size as \code{hyperSpec[[]]} or a scalar
##' (numeric of length 1).
##'
##'
##' As \code{hyperSpec} objects must have numeric spectra matrices, the
##' resulting matrix of the comparison is returned directly.
##' @return a logical matrix for the comparison operators.
##' @seealso \code{\link[hyperSpec]{sweep-methods}} for calculations involving
##' a vector and the spectral matrix.
##'
##'
##' \code{\link[methods]{S4groupGeneric}} for group generic methods.
##'
##'
##' \code{\link[base]{Comparison}} for the base comparison functions.
##'
##'
##' \code{\link[hyperSpec]{Arith}} for arithmetic operators,
##' \code{\link[hyperSpec]{Math}} for mathematical group generic functions
##' (groups Math and Math2) working on \code{hyperSpec} objects.
##' @keywords methods arith
##' @export
##' @examples
##'
##'
##' flu [,,445 ~ 450] > 300
##'
##'
##' all (flu == flu[[]])
##'
##'

setMethod ("Compare", signature (e1 = "hyperSpec", e2 = "hyperSpec"),
function (e1, e2){
validObject (e1)
validObject (e2)
setMethod(
"Compare", signature(e1 = "hyperSpec", e2 = "hyperSpec"),
function(e1, e2) {
validObject(e1)
validObject(e2)

callGeneric (e1[[]], e2[[]])
}
)
callGeneric(e1[[]], e2[[]])
}
)

.compx <- function (e1, e2){
validObject (e1)
callGeneric (e1 [[]], e2)
.compx <- function(e1, e2) {
validObject(e1)
callGeneric(e1 [[]], e2)
}

.compy <- function (e1, e2){
validObject (e2)
callGeneric (e1, e2 [[]])
.compy <- function(e1, e2) {
validObject(e2)
callGeneric(e1, e2 [[]])
}

##' @rdname Comparison
setMethod ("Compare", signature (e1 = "hyperSpec", e2 = "numeric"), .compx)
setMethod("Compare", signature(e1 = "hyperSpec", e2 = "numeric"), .compx)
##' @rdname Comparison
setMethod ("Compare", signature (e1 = "hyperSpec", e2 = "matrix"), .compx)
setMethod("Compare", signature(e1 = "hyperSpec", e2 = "matrix"), .compx)

##' @rdname Comparison
setMethod ("Compare", signature (e1 = "numeric", e2 = "hyperSpec"), .compy)
setMethod("Compare", signature(e1 = "numeric", e2 = "hyperSpec"), .compy)
##' @rdname Comparison
setMethod ("Compare", signature (e1 = "matrix", e2 = "hyperSpec"), .compy)


setMethod("Compare", signature(e1 = "matrix", e2 = "hyperSpec"), .compy)
16 changes: 8 additions & 8 deletions hyperSpec/R/DollarNames.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
##' @param pattern pattern to look for
##' @return the name of the extra data slot
##' @importFrom utils .DollarNames
.DollarNames.hyperSpec <- function (x, pattern = "")
grep (pattern, colnames (x@data), value = TRUE)
.DollarNames.hyperSpec <- function(x, pattern = "") {
grep(pattern, colnames(x@data), value = TRUE)
}

.test (.DollarNames.hyperSpec) <- function(){
context (".DollarNames")
.test(.DollarNames.hyperSpec) <- function() {
context(".DollarNames")

test_that("expansion on missing pattern", {
expect_equal(.DollarNames (flu), colnames (flu))
expect_equal(.DollarNames(flu), colnames(flu))
})

test_that("expansion on missing pattern", {
expect_equal(.DollarNames (flu, "f"), "filename")
expect_equal(.DollarNames (flu, "c"), c ("spc", "c"))
expect_equal(.DollarNames(flu, "f"), "filename")
expect_equal(.DollarNames(flu, "c"), c("spc", "c"))
})

}
Loading