oro.nifti (version 0.11.0)

writeNIfTI-methods: writeNIfTI

Description

This function saves a NIfTI-class object to a single binary file in NIfTI format.

Usage

# S4 method for nifti
writeNIfTI(
  nim,
  filename,
  onefile = TRUE,
  gzipped = TRUE,
  verbose = FALSE,
  warn = -1,
  compression = 9
)

# S4 method for niftiExtension writeNIfTI( nim, filename, onefile = TRUE, gzipped = TRUE, verbose = FALSE, warn = -1, compression = 9 )

# S4 method for anlz writeNIfTI( nim, filename, onefile = TRUE, gzipped = TRUE, verbose = FALSE, warn = -1, compression = 9 )

# S4 method for array writeNIfTI( nim, filename, onefile = TRUE, gzipped = TRUE, verbose = FALSE, warn = -1, compression = 9 )

Arguments

nim

is an object of class nifti or anlz.

filename

is the path and file name to save the NIfTI file (.nii) without the suffix.

onefile

is a logical value that allows the scanning of single-file (.nii) or dual-file format (.hdr and .img) NIfTI files (default = TRUE).

gzipped

is a character string that enables exportation of compressed (.gz) files (default = TRUE).

verbose

is a logical variable (default = FALSE) that allows text-based feedback during execution of the function.

warn

is a number to regulate the display of warnings (default = -1). See options for more details.

compression

The amount of compression to be applied when writing a file when gzipped = TRUE

Value

Nothing.

Methods

object = "anlz"

Convert ANALYZE object to class nifti and write the NIfTI volume to disk.

object = "array"

Convert array to class nifti and write the NIfTI volume to disk.

object = "nifti"

Write NIfTI volume to disk.

Details

The writeNIfTI function utilizes the internal writeBin and writeChar command to write information to a binary file.

Current acceptable data types include

list("UINT8")

DT BINARY (1 bit per voxel)

list("INT16")

DT SIGNED SHORT (16 bits per voxel)

list("INT32")

DT SINGED INT (32 bits per voxel)

list("FLOAT32")

DT FLOAT (32 bits per voxel)

list("DOUBLE64")

DT DOUBLE (64 bits per voxel)

list("UINT16")

DT UNSIGNED SHORT (16 bits per voxel)

References

NIfTI-1 http://nifti.nimh.nih.gov/

See Also

writeAFNI, writeANALYZE

Examples

Run this code
# NOT RUN {
norm <- dnorm(seq(-5, 5, length=32), sd=2)
norm <- (norm-min(norm)) / max(norm-min(norm))
img <- outer(outer(norm, norm), norm)
img <- round(255 * img)
img[17:32,,] <- 255 - img[17:32,,]
img.nifti <- nifti(img) # create NIfTI object

fname = file.path(tempdir(), "test-nifti-image-uint8")

writeNIfTI(img.nifti, fname, verbose=TRUE)
## These files should be viewable in, for example, FSLview
## Make sure you adjust the min/max values for proper visualization
data <- readNIfTI(fname, verbose=TRUE)
image(img.nifti, oma=rep(2,4), bg="white")
image(data, oma=rep(2,4), bg="white")
abs.err <- abs(data - img.nifti)
image(as(abs.err, "nifti"), zlim=range(img.nifti), oma=rep(2,4),
      bg="white")

# }
# NOT RUN {
## Loop through all possible data types
datatypes <- list(code=c(2, 4, 8, 16, 64),
                  name=c("uint8", "int16", "int32", "float", "double"))
equal <- vector("list")
for (i in 1:length(datatypes$code)) {
  fname <- paste("test-nifti-image-", datatypes$name[i], sep="")
  fname = file.path(tempdir(), fname)
  rm(img.nifti)
  img.nifti <- nifti(img, datatype=datatypes$code[i])
  writeNIfTI(img.nifti, fname, verbose=TRUE)
  equal[[i]] <- all(readNIfTI(fname) == img)
}
names(equal) <- datatypes$name
unlist(equal)
# }

Run the code above in your browser using DataLab