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

S3 methods for as.array and bracket notation #58

Merged
merged 16 commits into from
Oct 30, 2023
Merged
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Generated by roxygen2: do not edit by hand

S3method("[",ZarrArray)
S3method("[<-",ZarrArray)
S3method(as.array,NestedArray)
S3method(as.array,ZarrArray)
export(Attributes)
export(BloscCodec)
export(Bz2Codec)
Expand Down
17 changes: 16 additions & 1 deletion R/array-nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,22 @@ NestedArray <- R6::R6Class("NestedArray",
)
}
return(buf)

},
#' Convert NestArray object to R array (for S3 method)
#'
#' @return array
as.array = function() {
# Consider using drop() to simplify dimensions of 1
return(self$data)
}
)
)

#' S3 method for as.array
#'
#' @param obj
#' @keywords internal
#' @export
as.array.NestedArray = function(obj) {
obj$as.array()
}
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,4 @@ get_list_product <- function(dim_indexer_iterables) {
partial_results <- get_list_product_aux(dim_indexer_iterables, i, partial_results)
}
return(partial_results)
}
}
94 changes: 94 additions & 0 deletions R/zarr-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,100 @@ ZarrArray <- R6::R6Class("ZarrArray",
},
get_dimension_separator = function() {
return(private$dimension_separator)
},
#' Set values for a selection using bracket notation (for S3 method).
#'
#' @param ... Contains the slicing parameters, one for each dimension.
#' Use empty space to get whole dimension e.g. [1:5,,]
#'
#' @return Sliced Zarr object
#' @keywords internal
`[` = function(...) {
filters <- substitute(...())
if(length(filters) != length(private$shape)) {
stop("This Zarr object has ", length(private$shape), " dimensions, ", length(filters), " were supplied")
}
filters <- lapply(filters, function(x) {
# Proceed based on type of filter
if(typeof(x) == "symbol") {
# When empty dimension, return everything
if(x == "") {
return(NULL)
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}

} else if(typeof(x) == "double") {
# Return single value for dimension
return(slice(x, x))
} else if(typeof(x) == "language") {
x <- as.list(x)
# Return a range (supplied via : or seq())
if(x[[1]] == ":") {
return(slice(x[[2]], x[[3]]))
} else if(x[[1]] == "seq") {
arg_names <- names(x)
from <- ifelse("from" %in% arg_names, x[[which("from" == arg_names)]], x[[2]])
to <- ifelse("to" %in% arg_names, x[[which("to" == arg_names)]], x[[3]])
if(length(x) > 3) {
stop("Slicing with step size is not supported yet")
by <- ifelse("by" %in% arg_names, x[[which("by" == arg_names)]], x[[4]])
} else {
by <- NA
}
return(slice(from, to, by))
} else if(x[[1]] == "c") {
stop("Custom vector slicing is not yet supported")
# return(eval(y))
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
} else {
stop("Unsupported filter '", as.character(x), "' supplied")
}
})
return(self$get_item(filters))
},
#' Assign values for a selection using bracket notation (for S3 method).
#' @keywords internal
`[<-` = function(...) {
stop("Assignment using bracket notation is not yet supported - use set_item() directly")
},
#' Convert Zarr object to R array (for S3 method). Note that this loads all data into memory.
#'
#' @return array
as.array = function() {
return(self$get_item("...")$data)
}
)
)


#' S3 method for custom bracket subsetting
#'
#' @param obj
#' @param ...
#' @keywords internal
#' @export
`[.ZarrArray` <- function(obj, ...) {
obj$`[`(...)
}

#' S3 method for custom bracket assignment
#'
#' @param obj
#' @param ...
#' @keywords internal
#' @export
`[<-.ZarrArray` <- function(obj, ...) {
obj$`[<-`(...)
}

#' S3 method for as.array
#'
#' @param obj
#' @keywords internal
#' @export
as.array.ZarrArray = function(obj) {
obj$as.array()
}
14 changes: 14 additions & 0 deletions man/NestedArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 71 additions & 1 deletion man/ZarrArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/as.array.NestedArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/as.array.ZarrArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/sub-.ZarrArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/subset-.ZarrArray.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading