-
Notifications
You must be signed in to change notification settings - Fork 3
/
apply.R
231 lines (198 loc) · 6.56 KB
/
apply.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Functions ------------------------------------------------------------------
.na.if.different <- function(x) {
if (length(unique(x)) > 1) NA else x[1]
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.apply_workhorse <- function(data, MARGIN, FUN, ...) {
if (length(data$spc) == 0) {
stop("empty spectra matrix.")
}
spc <- apply(data[, "spc", drop = FALSE], MARGIN, FUN, ...)
if (MARGIN == 1) {
if (is.null(spc)) {
spc <- matrix(ncol = 0, nrow = nrow(data))
} else if (is.vector(spc)) {
dim(spc) <- c(length(spc), 1)
} else if (is.matrix(spc)) {
spc <- t(spc)
}
data$spc <- I(spc)
} else if (MARGIN == 2) {
if (is.null(spc)) {
return(data[0, ])
}
if (is.null(dim(spc))) {
dim(spc) <- c(1, ncol(data$spc))
}
if (all(dim(spc) == dim(data$spc))) {
data$spc <- spc
} else {
nrow <- nrow(spc)
cols <- colnames(data)
cols <- which(cols != "spc")
if (length(cols) > 0) {
data[1, cols] <- lapply(data[, cols, drop = FALSE], .na.if.different)
}
data <- data[rep(1, nrow), , drop = FALSE]
data$spc <- I(spc)
rownames(data) <- rownames(spc)
}
}
data$spc <- unclass(data$spc)
data
}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.apply <- function(X, MARGIN, FUN, ..., label.wl = NULL,
label.spc = NULL, new.wavelength = NULL, simplify) {
validObject(X)
if (missing(MARGIN)) { # apply for functions that the complete spectra matrix
## is easier: tmp <- apply (x, , FUN, ...)
## does:
## tmp <- x
## tmp[[]] <- FUN (x[[]], ...)
X@data$spc <- do.call(FUN, list(X@data$spc, ...))
} else if (all(MARGIN == 1:2)) { # apply for functions that take scalar arguments.
tmp <- apply(X@data$spc, MARGIN = MARGIN, FUN, ...)
tmp <- as.numeric(tmp) # otherwise surprises will be waiting
dim(tmp) <- dim(X@data$spc)
X@data$spc <- tmp
} else {
## the usual: for each row / for each column
X@data <- .apply_workhorse(X@data, MARGIN = MARGIN, FUN = FUN, ...)
if (all(MARGIN == 1)) {
## if the number of data points per spectrum is changed, the wavelength
## vector needs to be adapted, too
if (ncol(X@data$spc) != length(X@wavelength)) {
## only internal functions here: the validation will fail until the wavelength axis is
## adjusted
if (!is.null(new.wavelength)) { # vector with new wavelength is given
if (is.numeric(new.wavelength)) { # either directly,
.wl(X) <- new.wavelength
} else {
dots <- list(...)
.wl(X) <- dots[[new.wavelength]] # or as name of the argument that becomes the new
# wavelength vector
}
} else if (ncol(X@data$spc) != length(X@wavelength)) {
wl <- as.numeric(colnames(X@data$spc)) # if not given, try to make from colnames of the
# spectra matrix
if (length(wl) != ncol(X@data$spc) || any(is.na(wl))) {
wl <- seq_len(ncol(X@data$spc))
} # or just number sequentially
.wl(X) <- wl
}
}
}
}
if (!is.null(label.wl)) {
X@label$.wavelength <- label.wl
}
if (!is.null(label.spc)) {
X@label$spc <- label.spc
}
validObject(X)
X
}
#' Compute summary statistics for the spectra of a `hyperSpec` object
#'
#' [apply()] gives the functionality of [base::apply()] for `hyperSpec` objects.
#'
#' The generic functions of group [methods::Math()] are not defined
#' for `hyperSpec` objects. Instead, `apply` can be used. For
#' functions like `log()` that work on scalars, `MARGIN = 1:2` gives
#' the appropriate behavior.
#'
#' `spcapply` does the same as `apply` with `MARGIN = 1`, but
#' additionally allows to set a new wavelength axis and adjust the labels.
#'
#' `wlapply` does the same as `apply` with `MARGIN = 2`, but
#' additionally allows to set a new wavelength axis and adjust the labels.
#'
#' @name apply
#' @rdname apply
#' @aliases apply
#' apply,hyperSpec-method
#' @docType methods
#'
#' @param X,spc a `hyperSpec` object
#' @param MARGIN The subscript which the function will be applied over.
#'
#' - `1` indicates rows (`FUN` is applied to each spectrum),
#' - `2` indicates columns (`FUN` is applied to each wavelength),
#' - `1:2` indicates that `FUN` should be applied to each single
#' element of the spectra matrix. Note that many basic mathematical
#' functions are already defined for hyperSpec objects (see
#' [Math()]).
#'
#' If `MARGIN` is missing, the whole spectra matrix is handed to
#' `FUN`, see also the examples.
#' @param FUN function to compute the summary statistics
#' @param ... further arguments passed to `FUN`
#' @param simplify ignored: apply for hyperSpec results are always simplified
#' @param label.wl,label.spc new labels for wavelength and spectral intensity
#' axes
#' @param new.wavelength for `MARGIN = 2`: numeric vector or name of the
#' argument in \dots{} that is to be used (character) as wavelength axis of
#' the resulting object.
#'
#' @return A `hyperSpec` object
#'
#'
#' @seealso [base::apply()], for applying `FUN` to subgroups of
#' the `hyperSpec` object: [hyperSpec::aggregate()].
#'
#'
#' @author C. Beleites
#'
#'
#' @keywords methods iteration
#' @concept manipulation
#' @concept stats
#'
#' @export
#'
#' @examples
#'
#'
#' plot_spc(apply(faux_cell, 2, range))
#'
#' avg_flu <- apply(flu, 1, mean,
#' label.spc = expression(bar(I)),
#' new.wavelength = mean(wl(flu))
#' )
#' avg_flu
#'
#' flu[[, , 405:407]]
#' apply(flu, 1:2, "*", -1)[[, , 405:407]]
#'
#' ## without MARGIN the whole matrix is handed to FUN
#' apply(flu[, , 405:407], , print)[[]]
#'
#' ## whereas MARGIN = 1 : 2 leads to FUN being called
#' ## for each element separately
#' apply(flu[, , 405:407], 1:2, print)[[]]
setMethod("apply", signature = signature(X = "hyperSpec"), .apply)
# Unit tests -----------------------------------------------------------------
hySpc.testthat::test(.apply) <- function() {
context("apply")
test_that("check whether .na.if.different is working correctly", {
flu$equal <- 1
tmp <- apply(flu, 2, mean)$..
expect_equal(
is.na(tmp),
structure(c(TRUE, TRUE, FALSE),
.Dim = c(1L, 3L),
.Dimnames = list(NULL, c("filename", "c", "equal"))
)
)
expect_equal(tmp$equal, 1)
})
test_that("POSIXct", {
flu$ct <- as.POSIXct(Sys.time())
expect_equal(apply(flu, 2, mean)$ct, flu$ct[1])
})
test_that("POSIXlt", {
flu$lt <- as.POSIXlt(Sys.time())
expect_equal(apply(flu, 2, mean)$lt, flu$lt[1])
})
}