data_chunks: Return a set of data chunks

View source: R/all_generic.R

data_chunksR Documentation

Return a set of data chunks

Description

Split a dataset into manageable chunks for processing. This is particularly useful for parallel processing of large fMRI datasets. Chunks can be created either by run (runwise=TRUE) or by dividing the data into a specified number of pieces. Each chunk contains a subset of the data and metadata about its position in the full dataset.

Usage

data_chunks(x, nchunks, ...)

Arguments

x

The dataset to chunk (typically an fmri_dataset or matrix_dataset)

...

Additional arguments passed to methods. Common arguments include:

nchunks

Integer; number of chunks to create

runwise

Logical; if TRUE, create one chunk per run

parallel

Logical; if TRUE, prepare chunks for parallel processing

Value

An iterator object that yields data chunks, where each chunk contains:

data

Matrix of data values for this chunk

chunk_num

Index of this chunk

voxel_ind

Indices of voxels in this chunk

row_ind

Indices of timepoints in this chunk

See Also

matrix_dataset(), fmri_dataset(), foreach::foreach()

Examples

# Create a simple matrix dataset
X <- matrix(rnorm(100 * 1000), 100, 1000)  # 100 timepoints, 1000 voxels
dset <- matrix_dataset(
  X, 
  TR = 2,
  run_length = c(50, 50)  # Two runs of 50 timepoints each
)

# Create chunks by run
run_chunks <- data_chunks(dset, runwise = TRUE)

# Process each run chunk
foreach::foreach(chunk = run_chunks) %do% {
  # chunk$data contains the data for one run
  # chunk$row_ind shows which timepoints are included
  mean_signal <- colMeans(chunk$data)
}

# Create arbitrary number of chunks
vox_chunks <- data_chunks(dset, nchunks = 4)

# Process chunks in parallel
foreach::foreach(chunk = vox_chunks) %dopar% {
  # chunk$data contains subset of voxels
  # chunk$voxel_ind shows which voxels are included
  apply(chunk$data, 2, sd)
}

bbuchsbaum/fmrireg documentation built on March 1, 2025, 11:20 a.m.