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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
IS_INTEL_MAC <- Sys.info()[["sysname"]] == "Darwin" &&
Sys.info()[["machine"]] == "x86_64"
IS_ARM64_MAC <- Sys.info()[["sysname"]] == "Darwin" &&
Sys.info()[["machine"]] == "arm64"
make_3D_logical_array <- function(background=FALSE)
{
a <- array(background, 6:4,
dimnames=list(letters[1:6], NULL, LETTERS[1:4]))
a[c(2:3, 6), 2, 1] <- TRUE
a[ , 3, 1] <- c(TRUE, FALSE, NA, FALSE, FALSE, TRUE)
a[2:3, 4:5, 1] <- NA
a[c(1, 6), 1 , 2] <- FALSE
a[ 2, , 2] <- FALSE
a[ , 3, 2] <- TRUE
a[3:5, 4:5, 2] <- NA
a[c(1, 6), -5, 3] <- NA
a[3:5, 1:3, 3] <- FALSE
a[-3, 5, 3] <- TRUE
a
}
make_3D_integer_array <- function(background=0L)
{
a <- array(background, 6:4,
dimnames=list(letters[1:6], NULL, LETTERS[1:4]))
a[c(2:3, 6), 2, 1] <- 101:103
a[c(1, 6), 1 , 2] <- 201:202
a[c(3:4, 6), c(2, 5), 2] <- a[ , 3, 2] <- a[6, 4, 2] <- 1L
a[4, 5, 2] <- NA
a[1:5, 5, 3] <- -(301:305)
a[6, 5, 4] <- NA
a
}
make_3D_double_array <- function(background=0.0)
{
a <- array(background, c(7, 10, 3),
dimnames=list(NULL, letters[1:10], LETTERS[1:3]))
set.seed(123)
a[5*(1:26)] <- runif(26, min=-5, max=10)
a[2, c(1:4, 7:9), 1] <- c(NA, NaN, Inf, 3e9, 256, -0.999, -1)
a
}
make_3D_complex_array <- function(background=0L)
{
a <- make_3D_integer_array(background)
set.seed(123)
a[6*(1:20)] <- a[6*(1:20)] + runif(20, min=-5, max=10) * 1i
a[2, , 4] <- c(NA, NaN, Inf, 3e9, 256)
a[3, , 4] <- c(NA, NaN, -Inf, -0.999, -1) * 1i
a
}
make_lacunar_leaf <- function(mode, nzoffs)
{
stopifnot(isSingleString(mode))
stopifnot(is.integer(nzoffs))
list(NULL, nzoffs)
}
check_array_like_object <- function(object, expected_class, a0, strict=TRUE)
{
expect_true(class(object) == expected_class)
expect_true(validObject(object))
expect_identical(dim(object), dim(a0))
expect_identical(dimnames(object), dimnames(a0))
expect_identical(type(object), type(a0))
EXPECT_FUN <- if (strict) expect_identical else expect_equal
EXPECT_FUN(as.array(object), a0)
}
check_SVT_SparseArray_object <- function(svt, a0, strict=TRUE)
{
Class0 <- S4Vectors:::capitalize(class(a0)[[1]])
expected_class <- paste0("SVT_Sparse", Class0)
check_array_like_object(svt, expected_class, a0, strict=strict)
if (strict)
expect_identical(svt, as(a0, expected_class))
}
check_NaArray_object <- function(naa, a0, strict=TRUE)
{
Class0 <- S4Vectors:::capitalize(class(a0)[[1]])
expected_class <- paste0("Na", Class0)
check_array_like_object(naa, expected_class, a0, strict=strict)
if (strict)
expect_identical(naa, as(a0, expected_class))
}
### Subsetting a 1D ordinary array always preserves the "dim" and "dimnames"
### attributes i.e. it always returns another 1D array. This is inconsistent
### with the multidimensional case where, for example, linear subsetting (i.e.
### subsetting by a numeric vector or matrix) returns an ordinary vector
### (atomic or list). At the root of the problem is the behavior of
### base::drop() on a 1D array (see drop_even_if_1D() function in S4Arrays,
### in file R/dim-tuning-utils.R). So we "fix" subsetting of a 1D ordinary
### array by passing the result of the subsetting operation thru
### S4Arrays:::drop_even_if_1D().
test_linear_subsetting <- function(a, object, Mindex0)
{
stopifnot(is.array(a))
Lindex0 <- Mindex2Lindex(Mindex0, dim(a))
expected <- S4Arrays:::drop_even_if_1D(a[Mindex0])
expect_identical(object[Lindex0], expected)
expect_identical(object[Mindex0], expected)
expect_identical(object[Lindex0 + 0.99], expected)
expect_identical(object[Mindex0 + 0.99], expected)
revLindex <- rev(Lindex0)
revMindex <- Lindex2Mindex(revLindex, dim(a))
expected <- S4Arrays:::drop_even_if_1D(a[revLindex])
expect_identical(object[revLindex], expected)
expect_identical(object[revMindex], expected)
type(revLindex) <- "double"
type(revMindex) <- "double"
expect_identical(object[revLindex], expected)
expect_identical(object[revMindex], expected)
expect_identical(object[revLindex + 0.99], expected)
expect_identical(object[revMindex + 0.99], expected)
Lindex <- c(NA, Lindex0, NA, NA, Lindex0, NA, rev(Lindex0))
## Lindex2Mindex() and 'object[Mindex]' don't accept NAs at the moment.
#Mindex <- Lindex2Mindex(Lindex, dim(a))
expected <- S4Arrays:::drop_even_if_1D(a[Lindex])
expect_identical(object[Lindex], expected)
#expect_identical(object[Mindex], expected)
Lindex[1L] <- NaN # this coerces 'Lindex' to "numeric"
#Mindex[1L, 1L] <- NaN # this coerces 'Mindex' to "numeric"
expect_identical(object[Lindex], expected)
#expect_identical(object[Mindex], expected)
}
test_summarize_op1 <- function(a, object, op)
{
FUN <- match.fun(op)
expected <- FUN(as.vector(a))
current <- FUN(object)
expect_identical(current, expected)
}
test_summarize_op2 <- function(a, object, op)
{
FUN <- match.fun(op)
if (op %in% c("var", "sd") ||
is.double(a) && op %in% c("sum", "prod", "mean"))
{
EXPECT_FUN <- expect_equal
} else {
EXPECT_FUN <- expect_identical
}
expected <- FUN(as.vector(a))
current <- FUN(object)
if (op == "prod" && is.integer(current))
expected <- as.integer(expected)
EXPECT_FUN(current, expected)
expected <- FUN(as.vector(a), na.rm=TRUE)
current <- FUN(object, na.rm=TRUE)
if (op == "prod" && is.integer(current))
expected <- as.integer(expected)
EXPECT_FUN(current, expected)
}
### - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
### test_3D_colrowMinsMaxs()
###
.fix_simple_colMinsMaxs_result <- function(ans, x, dims)
{
if (type(x) == "integer" && type(ans) == "double")
type(ans) <- "integer"
ans <- S4Arrays:::set_dim(ans, tail(dim(x), n=-dims))
x_dimnames <- dimnames(x)
if (!is.null(x_dimnames))
dimnames(ans) <- tail(x_dimnames, n=-dims)
S4Arrays:::drop_even_if_1D(ans)
}
.fix_simple_rowMinsMaxs_result <- function(ans, x, dims)
{
if (type(x) == "integer" && type(ans) == "double")
type(ans) <- "integer"
ans <- S4Arrays:::set_dim(ans, head(dim(x), n=dims))
x_dimnames <- dimnames(x)
if (!is.null(x_dimnames))
dimnames(ans) <- head(x_dimnames, n=dims)
S4Arrays:::drop_even_if_1D(ans)
}
.simple_3D_colMins <- function(x, na.rm=FALSE, dims=1)
{
stopifnot(length(dim(x)) == 3L, isSingleNumber(dims))
if (dims == 1) {
ans <- apply(x, MARGIN=3, colMins, na.rm=na.rm)
} else if (dims == 2) {
ans <- suppressWarnings(apply(x, MARGIN=3, min, na.rm=na.rm))
} else {
stop("unsupported 'dims'")
}
.fix_simple_colMinsMaxs_result(ans, x, dims)
}
.simple_3D_colMaxs <- function(x, na.rm=FALSE, dims=1)
{
stopifnot(length(dim(x)) == 3L, isSingleNumber(dims))
if (dims == 1) {
ans <- apply(x, MARGIN=3, colMaxs, na.rm=na.rm)
} else if (dims == 2) {
ans <- suppressWarnings(apply(x, MARGIN=3, max, na.rm=na.rm))
} else {
stop("unsupported 'dims'")
}
.fix_simple_colMinsMaxs_result(ans, x, dims)
}
.simple_3D_rowMins <- function(x, na.rm=FALSE, dims=1)
{
stopifnot(length(dim(x)) == 3L, isSingleNumber(dims))
if (dims == 1) {
ans <- suppressWarnings(apply(x, MARGIN=1, min, na.rm=na.rm))
} else if (dims == 2) {
ans <- apply(x, MARGIN=2, rowMins, na.rm=na.rm)
} else {
stop("unsupported 'dims'")
}
.fix_simple_rowMinsMaxs_result(ans, x, dims)
}
.simple_3D_rowMaxs <- function(x, na.rm=FALSE, dims=1)
{
stopifnot(length(dim(x)) == 3L, isSingleNumber(dims))
if (dims == 1) {
ans <- suppressWarnings(apply(x, MARGIN=1, max, na.rm=na.rm))
} else if (dims == 2) {
ans <- apply(x, MARGIN=2, rowMaxs, na.rm=na.rm)
} else {
stop("unsupported 'dims'")
}
.fix_simple_rowMinsMaxs_result(ans, x, dims)
}
### Tests *Mins() and *Maxs() methods on a 3D array-like object.
test_3D_colrowMinsMaxs <- function(object)
{
a <- as.array(object)
## Base R does NOT allow an ordinay array with dimensions of extent
## zero to carry a character(0) in its dimnames, only a NULL. This means
## that, if 'object' is an Array derivative, then 'dimnames(object)'
## and 'dimnames(as.array(object))' are not guaranteed to be identical
## because a character(0) in the former will be replaced with a NULL in
## the latter.
## As a consequence, calling colMins/Maxs() or rowMins/Maxs()
## on 'object' won't necessarily produce the exact same result as
## calling .simple_3D_colMins/Maxs3D() or .simple_3D_rowMins/Maxs3D()
## on 'as.array(object)' when the result is a vector of length 0.
## More precisely, one can be named while the other is not.
## Does not look at the names if 'a1' and 'a2' are vectors of length 0.
expect_almost_identical <- function(a1, a2) {
if (is.vector(a1) && length(a1) == 0L) {
a1 <- unname(a1)
a2 <- unname(a2)
}
expect_identical(a1, a2)
}
## dims == 1 (default)
expected <- .simple_3D_colMins(a)
expect_almost_identical(colMins(object), expected)
expected <- .simple_3D_colMaxs(a)
expect_almost_identical(colMaxs(object), expected)
expected <- .simple_3D_rowMins(a)
expect_almost_identical(rowMins(object), expected)
expected <- .simple_3D_rowMaxs(a)
expect_almost_identical(rowMaxs(object), expected)
expected <- .simple_3D_colMins(a, na.rm=TRUE)
expect_almost_identical(colMins(object, na.rm=TRUE), expected)
expected <- .simple_3D_colMaxs(a, na.rm=TRUE)
expect_almost_identical(colMaxs(object, na.rm=TRUE), expected)
expected <- .simple_3D_rowMins(a, na.rm=TRUE)
expect_almost_identical(rowMins(object, na.rm=TRUE), expected)
expected <- .simple_3D_rowMaxs(a, na.rm=TRUE)
expect_almost_identical(rowMaxs(object, na.rm=TRUE), expected)
## dims == 2
expected <- .simple_3D_colMins(a, dims=2)
expect_almost_identical(colMins(object, dims=2), expected)
expected <- .simple_3D_colMaxs(a, dims=2)
expect_almost_identical(colMaxs(object, dims=2), expected)
expected <- .simple_3D_rowMins(a, dims=2)
expect_almost_identical(rowMins(object, dims=2), expected)
expected <- .simple_3D_rowMaxs(a, dims=2)
expect_almost_identical(rowMaxs(object, dims=2), expected)
expected <- .simple_3D_colMins(a, na.rm=TRUE, dims=2)
expect_almost_identical(colMins(object, na.rm=TRUE, dims=2), expected)
expected <- .simple_3D_colMaxs(a, na.rm=TRUE, dims=2)
expect_almost_identical(colMaxs(object, na.rm=TRUE, dims=2), expected)
expected <- .simple_3D_rowMins(a, na.rm=TRUE, dims=2)
expect_almost_identical(rowMins(object, na.rm=TRUE, dims=2), expected)
expected <- .simple_3D_rowMaxs(a, na.rm=TRUE, dims=2)
expect_almost_identical(rowMaxs(object, na.rm=TRUE, dims=2), expected)
}
|