Skip to contents

This function cuts an object into a list of sub-objects based on a vector of cluster indices. The resulting list contains each of the clusters as separate objects.

Usage

split_clusters(x, clusters, ...)

# S4 method for NeuroVol,ClusteredNeuroVol
split_clusters(x, clusters)

# S4 method for NeuroVol,integer
split_clusters(x, clusters)

# S4 method for NeuroVol,numeric
split_clusters(x, clusters)

# S4 method for ClusteredNeuroVol,missing
split_clusters(x, clusters)

# S4 method for NeuroVec,integer
split_clusters(x, clusters, ...)

# S4 method for NeuroVec,numeric
split_clusters(x, clusters, ...)

# S4 method for NeuroVec,ClusteredNeuroVol
split_clusters(x, clusters, ...)

Arguments

x

The object to split. The input object must be a 4D tensor, where the first three dimensions correspond to the spatial dimensions of the data and the fourth dimension corresponds to time.

clusters

A vector of cluster indices to split by.

...

Additional arguments to be passed to methods.

Examples


## split 'NeuroVol' with a 'ClusteredNeuroVol'
vol <- NeuroVol(array(runif(10*10*10),c(10,10,10)), NeuroSpace(c(10,10,10)))
mask <- as.logical(vol > .5)
mask.idx <- which(mask != 0)
grid <- index_to_coord(mask, mask.idx)
vox <- index_to_grid(mask, mask.idx)

library(purrr)
## partition coordinates into 50 clusters using 'kmeans'
kres <- kmeans(grid, centers=50, iter.max=500)
kvol <- ClusteredNeuroVol(mask, kres$cluster)
klis <- split_clusters(mask, kvol)
ret1 <- vol %>% split_clusters(kvol) %>% purrr::map_dbl(~ mean(values(.)))

## split NeuroVol with 'integer' vector of clusters.
indices <- numeric(prod(dim(mask)[1:3]))

## locations with a cluster value of 0 are ignored
indices[mask.idx] <- kres$cluster

ret2 <- vol %>% split_clusters(as.integer(indices)) %>% purrr::map_dbl(~ mean(values(.)))
all(ret1 == ret1)
#> [1] TRUE