set.seed(1)
x1 <- filearray_create(tempfile(), dimension = c(100,20,3))
x1[] <- rnorm(6000)
x2 <- filearray_create(tempfile(), dimension = c(100,20,3))
x2[] <- rnorm(6000)
# Add two arrays
output <- filearray_create(tempfile(), dimension = c(100,20,3))
fmap(list(x1, x2), function(input){
input[[1]] + input[[2]]
}, output)
# check
range(output[] - (x1[] + x2[]))
output$delete()
# Calculate the maximum of x1/x2 for every 100 elements
# total 60 batches/loops (`.buffer_count`)
output <- filearray_create(tempfile(), dimension = c(20,3))
fmap(list(x1, x2), function(input){
max(input[[1]] / input[[2]])
}, .y = output, .buffer_count = 60)
# check
range(output[] - apply(x1[] / x2[], c(2,3), max))
output$delete()
# A large array example
if(interactive()){
x <- filearray_create(tempfile(), dimension = c(287, 100, 301, 4))
dimnames(x) <- list(
Trial = 1:287,
Marker = 1:100,
Time = 1:301,
Location = 1:4
)
for(i in 1:4){
x[,,,i] <- runif(8638700)
}
# Step 1:
# for each location, trial, and marker, calibrate (baseline)
# according to first 50 time-points
output <- filearray_create(tempfile(), dimension = dim(x))
# baseline-percentage change
fmap(
list(x),
function(input){
# get locational data
location_data <- input[[1]]
dim(location_data) <- c(287, 100, 301)
# collapse over first 50 time points for
# each trial, and marker
baseline <- apply(location_data[,,1:50], c(1,2), mean)
# calibrate
calibrated <- sweep(location_data, c(1,2), baseline,
FUN = function(data, bl){
(data / bl - 1) * 100
})
return(calibrated)
},
.y = output,
# input dimension is 287 x 100 x 301 for each location
# hence 4 loops in total
.buffer_count = 4
)
# cleanup
x$delete()
}
# cleanup
x1$delete()
x2$delete()
output$delete()
Run the code above in your browser using DataLab