timeserieswindow2matrix.Rd
Extract a matrix from a time-series image after applying a mask where each row is a space-time vector
timeserieswindow2matrix(
timeseriesmatrix,
mask,
eventlist,
timewindow,
zeropadvalue = 0,
spacing = NA
)
Input timeseriesmatrix from timeseries2matrix
Input mask of type 'antsImage' ... the mask will be replicated into a 4D image of length timewindow + zeropadvalue.
time indices for the events to extract
n-timepoints around each event, forward in time.
pads the mask by this amount fwd and bwd in time.
optional 4d spacing vector that will impact smoothing parameters
Success – an R matrix of dimensions
ntimewindow*sizeofnonzerovaluesinmask*nevents
img <- makeImage(c(10, 10, 10, 50), 0)
mask <- as.antsImage(array(1, dim(img)[1:3]))
mat <- timeseries2matrix(img, mask)
mat <- timeserieswindow2matrix(mat, mask, c(1, 4, 5), 2, 0)
print(dim(mat$eventmatrix))
#> [1] 3 2000
print(dim(mat$mask4d))
#> [1] 10 10 10 2
##### another approach
dim4 <- c(20, 30, 10, 100)
i1 <- 10:15
i2 <- 11:18
i3 <- 4:8
i4 <- 10:90
arr3d <- array(data = 0, dim = dim4[1:3])
arr3d[i1, i2, i3] <- 1
arr <- array(data = 0, dim = dim4)
for (t in i4) arr[, , , t] <- t
nois <- which(arr > 0)
noisv <- rnorm(length(nois))
arr[nois] <- arr[nois] + noisv * 0.0
msk <- as.antsImage(arr3d)
img <- as.antsImage(arr)
mat <- timeseries2matrix(img, msk)
eanat <- sparseDecom(mat, msk, sparseness = 0.1, z = 0.5, nvecs = 2,
its = 5, cthresh = 0, mycoption = 1)
eanat2 <- sparseDecom(mat, sparseness = 0.1, z = 0.5, nvecs = 2,
its = 5, cthresh = 0, mycoption = 1)
enomask <- eanat2$eigenanatomyimages[1, ]
emask <- eanat$eigenanatomyimages[1, ]
print(enomask[31:40])
#> [1] 0.03987620 0.03343536 0.07381637 -0.07219767 0.04771875 0.14776873
#> [7] -0.14532009 -0.02779675 0.02243894 0.02661600
print(emask[31:40])
#> [1] 0.03987620 0.03343536 0.07381637 -0.07219767 0.04771875 0.14776873
#> [7] -0.14532009 -0.02779675 0.02243894 0.02661600
# same thing with event matrices ....
ttt <- timeserieswindow2matrix(mat, msk, c(20, 40, 60, 70), 6, 0)
tte <- ttt$eventmatrix
eanat <- sparseDecom(tte, ttt$mask4d, sparseness = -0.9, z = 0.5,
nvecs = 2, its = 5, cthresh = 0, mycoption = 1)
eanat2 <- sparseDecom(tte, sparseness = -0.9, z = 0.5, nvecs = 2,
its = 5, cthresh = 0, mycoption = 1)
enomask <- eanat2$eigenanatomyimages[, 1]
# back to timematrix
tmat <- matrix(enomask, nrow = 6)
# back to image
eimg <- antsImageClone(msk)
eimg[msk == 1] <- tmat[1, ]
# convert image space to evec space
emat <- eanat2$eigenanatomyimages
# convert emat to events FIXME this does not currently work
# eavent<-timeserieswindow2matrix( data.matrix(emat) , msk, 1 , 6, 0 )
# emask<-eavent$eventmatrix[1,]
#############################