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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
#############################################################################
##
## Copyright 2016 Novartis Institutes for BioMedical Research Inc.
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
#############################################################################
setOldClass("factor_ext")
INT_MIN <- as.integer64(-2^31)
INT_MAX <- as.integer64(2^31 - 1)
##' Create an extended factor
##'
##' An extended version of a regular \code{factor} variable. Instead of the levels having values from
##' 1 to n where n is the number of levels, any integer value can be used for any level (including 64bit integers). If
##' all values are in the range of a regular 32-bit integer, it is coerced to int. Automatic coercion of extended factors
##' to factors in \code{\link{H5ToR_Post}} for enums only works for 32-bit integer base types.
##' In this page this is heavily used, as constants in HDF5 can be arbitrary integer values.
##' @title Create an extended factor
##' @param x The object to convert to an \code{factor_ext}
##' @param values The values used for the levels; This is were \code{factor_ext} is different from a
##' \code{factor}, as values for levels do not have to be consecutive or start at 1.
##' @param levels The levels of the object; character string
##' @param drop Should non-occurring levels be dropped
##' @return An object of S3 class \code{factor_ext}
##' @author Holger Hoefling
##' @importFrom methods setOldClass
##' @export
factor_ext <- function(x, values, levels, drop=FALSE) {
if(inherits(x, "factor_ext") && missing(values)) {
values <- attr(x, "values")
}
if(inherits(x, "factor_ext") && missing(levels)) {
levels <- attr(x, "levels")
}
if(is.numeric(x) && missing(values)) {
values <- sort(unique(x))
}
## treat values first
if(is.integer(values)) {
values <- as.integer(values)
}
else if(is.numeric(values) && !is.integer64(values)) {
if(any(as.numeric(as.integer64(values)) != values)) {
stop("x needs to be integer valued")
}
values <- as.integer64(values)
}
if(is.integer64(values)) {
## check if it can be forced into an integer
if(all(values >= INT_MIN & values <= INT_MAX)) {
values <- as.integer(values)
}
}
if(is.numeric(x)) {
if(missing(levels)) {
stop("For x numeric, levels have to be set")
}
if(missing(values)) {
## already taken care of above; not relevant anymore
}
if(length(values) != length(levels)) {
stop("values and levels have to have the same length")
}
if(drop) {
incl <- values %in% x
values <- unname(values[incl])
levels <- levels[incl]
}
## coerce x to the same type as values
if(is.integer64(values)) {
x <- as.integer64(x)
}
else {
x <- as.integer(x)
}
x[!(x %in% values)] <- NA
}
else if(is.character(x)) {
if(missing(values)) {
stop("For x character, values have to be set")
}
if(missing(levels)) {
levels <- sort(unique(x))
}
if(length(values) != length(levels)) {
stop("values and levels have to have the same length")
}
if(drop) {
incl <- levels %in% x
values <- unname(values[incl])
levels <- levels[incl]
}
x[!(x %in% levels)] <- NA
x <- values[match(x, levels)]
}
else {
stop("x has to be numeric or character")
}
## now sort the levels by increasing values
val_order <- order(values, decreasing=FALSE)
levels <- levels[val_order]
values <- values[val_order]
if(is.integer64(x)) {
class(x) <- c("factor_ext", "integer64")
}
else {
class(x) <- "factor_ext"
}
levels(x) <- levels
attr(x, "values") <- values
return(x)
}
## need to overload the same functions that are also available for factors
## also, is.factor and a conversion function as.factor (which should be really simple)
##' Various functions for \code{factor_ext} objects
##'
##' \describe{
##' \item{values}{Extracts the underlying values of an object (the generic here)}
##' \item{values.factor_ext}{Extracts the underlying values of a \code{factor_ext} object}
##' \item{values.factor}{Extracts the underlying values of a \code{factor}}
##' \item{values.default}{Default of the values function; currently returns an error}
##' \item{as.character}{Coerces \code{factor_ext} to a character-representation using it levels, not values}
##' \item{[[.factor_ext}{Single-item subsetting of a \code{factor_ext} object}
##' \item{[[<-.factor_ext}{Single-item subset assignment to a \code{factor_ext} object}
##' \item{[.factor_ext}{Subsetting of a \code{factor_ext} object}
##' \item{[<-.factor_ext}{Subset assignment to a \code{factor_ext} object}
##' \item{is.factor_ext}{Check if it is a \code{factor_ext} object. Returns a logical}
##' \item{coercible_to_factor}{Checks if a \code{factor_ext} could be coerced to a \code{factor}. Return a logical.}
##' \item{coerce_to_factor}{Coerces to a \code{factor}, otherwise throws an error if not possible.}
##' \item{print.factor_ext}{Prints a \code{factor_ext} object.}
##' \item{==.factor_ext}{Compare two \code{factor_ext} for equality.}
##' \item{!=.factor_ext}{Compare two \code{factor_ext} for inequality.}
##' \item{c.factor_ext}{Concatenate objects of type \code{factor_ext}.}
##' }
##' @title Various functions for \code{factor_ext} objects
##' @param x Object of type \code{factor_ext}
##' @param quote logical, indicating whether or not strings should be printed with surrounding quotes.
##' @param max.levels integer, indicating how many levels should be printed. if '0', no extra "Levels" line will be printed. The
##' default, 'NULL', entails choosing 'max.levels' such that the levels print on one line of width 'width' (same for values).
##' @param width only used when \code{max.levels} is NULL (see above)
##' @param e1,e2 The two objects in the equality or inequality comparison.
##' @param ... Currently ignored
##' @param drop Should dimensions of size 1 be dropped?
##' @param value The object to assign; here has be a level of \code{factor_ext}
##' @return Depending on the function
##' @author Holger Hoefling
##' @name factor_ext_functions
NULL
##' @export
##' @rdname factor_ext_functions
values <- function(x, ...) {
UseMethod("values")
}
##' @export
##' @rdname factor_ext_functions
values.factor_ext <- function(x, ...) {
attr(x, "values")
}
##' @export
##' @rdname factor_ext_functions
values.factor <- function(x, ...) {
as.numeric(x)
}
##' @export
##' @rdname factor_ext_functions
values.default <- function(x, ...) {
stop("Currently not implemented")
}
## internal helper function
position <- function(x) {
.match <- function(x, y) match(unclass(x), unclass(y))
if(is.integer64(values(x)) && is.integer(x)) {
.match(as.integer64(unclass(x)), as.integer64(values(x)))
}
else if(is.integer(values(x)) && is.integer64(x)) {
.match(x, as.integer64(values(x)))
}
else {
.match(x, values(x))
}
}
##' @export
##' @rdname factor_ext_functions
as.character.factor_ext <- function (x, ...) {
levels(x)[position(x)]
}
##' @export
##' @rdname factor_ext_functions
"[[.factor_ext" <- function (x, ...) {
y <- NextMethod("[[")
attr(y, "contrasts") <- attr(x, "contrasts")
attr(y, "levels") <- attr(x, "levels")
attr(y, "values") <- attr(x, "values")
class(y) <- oldClass(x)
y
}
##' @export
##' @rdname factor_ext_functions
"[[<-.factor_ext" <- function(x, ..., value) {
lx <- levels(x)
vx <- values(x)
cx <- oldClass(x)
if (is.factor_ext(value))
value <- levels(value)[position(value)]
m <- vx[match(value, lx)]
if (any(is.na(m) & !is.na(value)))
warning("invalid factor_ext level, NA generated")
class(x) <- NULL
x[[...]] <- m
attr(x, "levels") <- lx
attr(x, "values") <- vx
class(x) <- cx
x
}
##' @export
##' @rdname factor_ext_functions
"[.factor_ext" <- function (x, ..., drop = FALSE)
{
y <- NextMethod("[")
attr(y, "contrasts") <- attr(x, "contrasts")
attr(y, "levels") <- attr(x, "levels")
attr(y, "values") <- attr(x, "values")
class(y) <- oldClass(x)
if (drop)
factor(y, exclude = if (anyNA(levels(x)))
NULL
else NA)
else y
}
##' @export
##' @rdname factor_ext_functions
"[<-.factor_ext" <- function(x, ..., value) {
lx <- levels(x)
vx <- values(x)
cx <- oldClass(x)
if (is.factor_ext(value))
value <- levels(value)[position(value)]
m <- vx[match(value, lx)]
if (any(is.na(m) & !is.na(value)))
warning("invalid factor_ext level, NA generated")
class(x) <- NULL
x[...] <- m
attr(x, "levels") <- lx
attr(x, "values") <- vx
class(x) <- cx
x
}
##' @export
##' @rdname factor_ext_functions
is.factor_ext <- function(x) {
return(inherits(x, "factor_ext"))
}
##' @export
##' @rdname factor_ext_functions
coercible_to_factor <- function(x) {
if(!inherits(x, "factor_ext")) {
stop("Only for objects of factor_ext classes")
}
vx <- values(x)
if(all(vx == seq_along(vx))) {
return(TRUE)
}
else {
return(FALSE)
}
}
##' @export
##' @rdname factor_ext_functions
coerce_to_factor <- function(x) {
if(!inherits(x, "factor_ext")) {
stop("Only for objects of factor_ext classes")
}
if(coercible_to_factor(x)) {
y <- factor(as.character(x), levels=levels(x))
return(y)
}
else {
stop("Cannot coerce to factor; values not a sequence starting at 1")
}
}
##' @export
##' @rdname factor_ext_functions
print.factor_ext <- function (x, quote = FALSE, max.levels = NULL, width = getOption("width"), ...) {
if (length(x) == 0L)
cat("factor_ext", "(0)\n", sep = "")
else {
xx <- x
class(xx) <- NULL
levels(xx) <- NULL
attr(xx, "values") <- NULL
xx[] <- as.character(x)
print(xx, quote = quote, ...)
}
maxl <- if (is.null(max.levels))
TRUE
else max.levels
if (maxl) {
n <- length(lev <- encodeString(levels(x), quote = ifelse(quote,
"\"", "")))
colsep <- " "
T0 <- "Levels: "
if (is.logical(maxl))
maxl <- {
width <- width - (nchar(T0, "w") + 3L + 1L +
3L)
lenl <- cumsum(nchar(lev, "w") + nchar(colsep,
"w"))
if (n <= 1L || lenl[n] <= width)
n
else max(1L, which.max(lenl > width) - 1L)
}
drop <- n > maxl
cat(if (drop)
paste(format(n), ""), T0, paste(if (drop)
c(lev[1L:max(1, maxl - 1)], "...", if (maxl > 1) lev[n])
else lev, collapse = colsep), "\n", sep = "")
}
if (maxl) {
n <- length(val <- encodeString(values(x), quote = ifelse(quote,
"\"", "")))
colsep <- " "
T0 <- "Values: "
if (is.logical(maxl))
maxl <- {
width <- width - (nchar(T0, "w") + 3L + 1L +
3L)
lenl <- cumsum(nchar(val, "w") + nchar(colsep,
"w"))
if (n <= 1L || lenl[n] <= width)
n
else max(1L, which.max(lenl > width) - 1L)
}
drop <- n > maxl
cat(if (drop)
paste(format(n), ""), T0, paste(if (drop)
c(val[1L:max(1, maxl - 1)], "...", if (maxl > 1) val[n])
else val, collapse = colsep), "\n", sep = "")
}
invisible(x)
}
##' @export
##' @rdname factor_ext_functions
"==.factor_ext" <- function(e1, e2){
if(inherits(e2, "factor_ext")) {
e1 <- as.character(e1)
e2 <- as.character(e2)
return(e1==e2)
}
if(is.character(e2)) {
e1 <- as.character(e1)
return(e1 == e2)
}
else {
class(e1) <- NULL
return(e1 == e2)
}
}
##' @export
##' @rdname factor_ext_functions
"!=.factor_ext" <- function(e1, e2){
if(inherits(e2, "factor_ext")) {
e1 <- as.character(e1)
e2 <- as.character(e2)
return(e1!=e2)
}
if(is.character(e2)) {
e1 <- as.character(e1)
return(e1 != e2)
}
else {
class(e1) <- NULL
return(e1 != e2)
}
}
##' @export
##' @rdname factor_ext_functions
c.factor_ext <- function(...) {
l <- list(...)
## check that the values and the levels for all of them the same and the class is all the same
inherits_factor_ext <- unique(unlist(lapply(l, inherits, what="factor_ext")))
if(all(inherits_factor_ext)) {
item1_levels <- levels(l[[1]])
item1_values <- values(l[[1]])
all_levels <- unique(unlist(lapply(l, levels)))
all_values <- unique(unlist(lapply(l, values)))
if(length(item1_levels)==length(all_levels) && length(item1_values)==length(all_values)) {
y <- do.call("c", lapply(l, as.integer))
class(y) <- "factor_ext"
attr(y, "levels") <- item1_levels
attr(y, "values") <- item1_values
return(y)
}
else {
stop("Not all factor_ext the same levels")
}
}
else {
stop("Can only concatenate with factor_ext object if all are of the same class")
}
}
|