knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The NiftiArray
package is under development please check back later!
The goal of NiftiArray
is to allow for memory efficient fast random access of NIfTI objects. NiftiArray
is an R package that allows for convenient and memory-efficient containers for on-disk representation of NIfTI objects. We allow for DelayedArray
extensions and support all operations supported by DelayedArray objects. These operations can be either delayed or block-processed.
You can find a package vignette here.
You can install the development version of NiftiArray
from GitHub with:
# install.packages('remotes') remotes::install_github("muschellij2/NiftiArray")
We are working to get a stable version on Neuroconductor.
Here we use the example image from RNifti
. We use the writeNiftiArray
function to create a NiftiArray
object:
library(NiftiArray) nii_fname = system.file("extdata", "example.nii.gz", package = "RNifti") res = writeNiftiArray(nii_fname) class(res) dim(res) res
We can see the file on disk that was written out:
res@seed@filepath
We see that the object is a low-memory DelayedArray
:
object.size(res)
You can also simply use the NiftiArray
function of the NIfTI filename to create the array:
res = NiftiArray(nii_fname)
We see the header information is encoded in the seed
slot of the object, which can be accessed using the nifti_header
function:
nifti_header(res)
mat = as(res, "NiftiMatrix") mat
Now that the image is a matrix, we can bind the columns together,
mat = DelayedArray::acbind(mat, mat, mat, mat) testthat::expect_is(mat, "DelayedMatrix") object.size(mat)
Now that we have the data in a DelayedMatrix
class, we can use the package DelayedMatrixStats
package that calls the matrixStats
package for quick operations:
vec_result = DelayedMatrixStats::rowMedians(mat) head(vec_result)
Turning the output back into a NiftiArray
, we have to pass in the header
argument, passing in the correct header information. We can either create the NiftiArray
output by creating a matrix, then the NiftiMatrix
, then the NiftiArray
.
res_mat = matrix(vec_result, ncol = 1) res_mat = as(res_mat, "NiftiMatrix") hdr = nifti_header(res) res_mat = writeNiftiArray(res_mat, header = hdr) class(res_mat) res_arr = as(res_mat, "NiftiArray")
Or we can create an array and then making the NiftiArray
:
arr = array(vec_result, dim = dim(res) ) hdr = nifti_header(res) res_arr = writeNiftiArray(arr, header = hdr) res_arr nifti_header(res_arr)
We can return a niftiImage
from the NiftiArray
object, as follows:
nii = as(res_arr, "niftiImage") nii
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.