Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
keller-mark committed Jun 20, 2024
2 parents fd4f462 + 0825c03 commit 7478064
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 88 deletions.
39 changes: 21 additions & 18 deletions R/array-nested.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,24 +239,27 @@ NestedArray <- R6::R6Class("NestedArray",
message(value)
stop("Got unexpected type for value in NestedArray$set()")
}

# Cannot figure out how to dynamically set values in an array
# of arbitrary dimensions.
# Tried: abind::afill <- but it doesn't seem to work with arbitrary dims or do.call
if(length(selection_list) == 1) {
self$data[selection_list[[1]]] <- value_data
} else if(length(selection_list) == 2) {
self$data[selection_list[[1]], selection_list[[2]]] <- value_data
} else if(length(selection_list) == 3) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]]] <- value_data
} else if(length(selection_list) == 4) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]], selection_list[[4]]] <- value_data
} else if(length(selection_list) == 5) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]], selection_list[[4]], selection_list[[5]]] <- value_data
} else if(length(selection_list) == 6) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]], selection_list[[4]], selection_list[[5]], selection_list[[6]]] <- value_data
} else {
stop("NestedArray$set() can only handle up to 6D arrays at the moment. Please make a feature request if you need to handle more dims.")

# Only set values if the array is not meant to be empty.
if(sum(length(value_data)) > 0 || sum(dim(value_data)) > 0) {
# Cannot figure out how to dynamically set values in an array
# of arbitrary dimensions.
# Tried: abind::afill <- but it doesn't seem to work with arbitrary dims or do.call
if(length(selection_list) == 1) {
self$data[selection_list[[1]]] <- value_data
} else if(length(selection_list) == 2) {
self$data[selection_list[[1]], selection_list[[2]]] <- value_data
} else if(length(selection_list) == 3) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]]] <- value_data
} else if(length(selection_list) == 4) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]], selection_list[[4]]] <- value_data
} else if(length(selection_list) == 5) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]], selection_list[[4]], selection_list[[5]]] <- value_data
} else if(length(selection_list) == 6) {
self$data[selection_list[[1]], selection_list[[2]], selection_list[[3]], selection_list[[4]], selection_list[[5]], selection_list[[6]]] <- value_data
} else {
stop("NestedArray$set() can only handle up to 6D arrays at the moment. Please make a feature request if you need to handle more dims.")
}
}
},
#' @description
Expand Down
2 changes: 1 addition & 1 deletion R/normalize.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ guess_chunks <- function(shape, typesize) {
break # Element size larger than CHUNK_MAX
}

chunks[idx %% ndims] <- ceiling(chunks[idx %% ndims] / 2.0)
chunks[idx %% ndims + 1] <- ceiling(chunks[idx %% ndims + 1] / 2.0)
idx <- idx + 1
}

Expand Down
2 changes: 1 addition & 1 deletion R/slicing.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ slice <- function(start, stop = NA, step = NA, zero_based = FALSE) {
))
}

#' Convenience function for the internal Sliceclass constructor
#' Convenience function for the internal Slice class constructor
#' with zero-based indexing and exclusive stop index.
#' @param start The start index.
#' @param stop The stop index.
Expand Down
66 changes: 33 additions & 33 deletions R/zarr-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -434,41 +434,41 @@ ZarrArray <- R6::R6Class("ZarrArray",

selection_shape <- indexer$shape
selection_shape_vec <- ensure_integer_vec(indexer$shape)

# Check value shape
if (length(selection_shape) == 0) {
# Setting a single value
} else if (is_scalar(value)) {
# Setting a scalar value
} else if("array" %in% class(value)) {
if (!all(ensure_integer_vec(dim(value)) == selection_shape_vec)) {
stop("Shape mismatch in source array and set selection: ${dim(value)} and ${selectionShape}")
}
value <- NestedArray$new(value, shape = selection_shape_vec, dtype=private$dtype, order = private$order)
} else if ("NestedArray" %in% class(value)) {
if (!all(ensure_integer_vec(value$shape) == selection_shape_vec)) {
stop("Shape mismatch in source NestedArray and set selection: ${value.shape} and ${selectionShape}")
}
} else {
# // TODO(zarr.js) support TypedArrays, buffers, etc
stop("Unknown data type for setting :(")
}

if(getOption("pizzarr.parallel_write_enabled")) {
if(!requireNamespace("foreach", quietly=TRUE)) {
stop("Parallel writing requires the 'foreach' package.")
}
# If optional package "foreach" is installed, try to run in parallel.
foreach::foreach(proj=indexer$iter(), .combine = c, .inorder = FALSE, .init = NULL) %dopar% {
chunk_value <- private$get_chunk_value(proj, indexer, value, selection_shape)
private$chunk_setitem(proj$chunk_coords, proj$chunk_sel, chunk_value)
NULL # return null since we are not using the combined result
if(sum(as.numeric(selection_shape)) > 0) {
# Check value shape
if (length(selection_shape) == 0) {
# Setting a single value
} else if (is_scalar(value)) {
# Setting a scalar value
} else if("array" %in% class(value)) {
if (!all(ensure_integer_vec(dim(value)) == selection_shape_vec)) {
stop("Shape mismatch in source array and set selection: ${dim(value)} and ${selectionShape}")
}
value <- NestedArray$new(value, shape = selection_shape_vec, dtype=private$dtype, order = private$order)
} else if ("NestedArray" %in% class(value)) {
if (!all(ensure_integer_vec(value$shape) == selection_shape_vec)) {
stop("Shape mismatch in source NestedArray and set selection: ${value.shape} and ${selectionShape}")
}
} else {
# // TODO(zarr.js) support TypedArrays, buffers, etc
stop("Unknown data type for setting :(")
}
} else {
# Foreach package was not installed, instead use a normal for loop.
for (proj in indexer$iter()) {
chunk_value <- private$get_chunk_value(proj, indexer, value, selection_shape)
private$chunk_setitem(proj$chunk_coords, proj$chunk_sel, chunk_value)

if(getOption("pizzarr.parallel_write_enabled")) {
if(!requireNamespace("foreach", quietly=TRUE)) {
stop("Parallel writing requires the 'foreach' package.")
}
foreach::foreach(proj=indexer$iter(), .combine = c, .inorder = FALSE, .init = NULL) %dopar% {
chunk_value <- private$get_chunk_value(proj, indexer, value, selection_shape)
private$chunk_setitem(proj$chunk_coords, proj$chunk_sel, chunk_value)
NULL # return null since we are not using the combined result
}
} else {
for (proj in indexer$iter()) {
chunk_value <- private$get_chunk_value(proj, indexer, value, selection_shape)
private$chunk_setitem(proj$chunk_coords, proj$chunk_sel, chunk_value)
}
}
}
},
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/helper-vcr.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
library("vcr") # *Required* as vcr is set up on loading
invisible(vcr::vcr_configure(
dir = vcr::vcr_test_path("fixtures"),
log = TRUE
dir = vcr::vcr_test_path("fixtures")
))
vcr::check_cassette_names()
13 changes: 13 additions & 0 deletions tests/testthat/test-chunks.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
library(pizzarr)

test_that("no infinite loop", {
shape <- c(27557749)
dtype <- "<i4"
chunks <- NA

dtype <- normalize_dtype(dtype, object_codec = NA)
shape <- normalize_shape(shape)

dtype_itemsize <- dtype$num_bytes
chunks <- normalize_chunks(chunks, shape, dtype_itemsize)
expect_equal(chunks, c(215295))
})

test_that("can get array that spans multiple chunks", {
a <- array(data=1:100, dim=c(10, 10))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
Expand Down
Loading

0 comments on commit 7478064

Please sign in to comment.