library(ieegio)
nifti_file <- "brain.demosubject.nii.gz"
# Use `ieegio_sample_data(nifti_file)`
# to download sample data
if( ieegio_sample_data(nifti_file, test = TRUE) ) {
# ---- NIfTI examples ---------------------------------------------
file <- ieegio_sample_data(nifti_file)
# basic read
vol <- read_volume(file)
# voxel to scanner RAS
vol$transforms$vox2ras
# to freesurfer surface
vol$transforms$vox2ras_tkr
# to FSL
vol$transforms$vox2fsl
plot(vol, position = c(10, 0, 30))
# ---- using other methods --------------------------------------
# default
vol <- read_volume(file, method = "rnifti", format = "nifti")
vol$header
# lazy-load nifti
vol2 <- read_volume(file, method = "oro", format = "nifti")
vol2$header
if (FALSE) {
# requires additional python environment
# Using ANTsPyx
vol3 <- read_volume(file, method = "ants", format = "nifti")
vol3$header
}
# ---- write --------------------------------------------------------
# write as NIfTI
f <- tempfile(fileext = ".nii.gz")
write_volume(vol, f, format = "nifti")
# alternative method
write_volume(vol$header, f, format = "nifti")
# write to mgz/mgh
f2 <- tempfile(fileext = ".mgz")
write_volume(vol, f, format = "mgh")
# clean up
unlink(f)
unlink(f2)
}
Run the code above in your browser using DataLab