| read.fs.volume.analyze | R Documentation |
Read a volume from the two-file image formats ANALYZE 7.5 and NIFTI v1: a 348 byte header
(<base>.hdr) and the voxel data in a separate file (<base>.img). Read
read.analyze.header for the difference between the two variants and for the limitations of the
ANALYZE header.
read.fs.volume.analyze(
filepath,
flatten = FALSE,
with_header = FALSE,
drop_empty_dims = FALSE,
spm = FALSE
)
filepath |
character string, the path to one of the two files of the pair, or to its base name. See
|
flatten |
logical. Whether to flatten the return volume to a 1D vector. Useful if you know that this file contains 1D morphometry data. |
with_header |
logical. Whether to return the header as well. If TRUE, return an instance of class |
drop_empty_dims |
logical, whether to drop empty dimensions of the returned data |
spm |
logical, whether to interpret the ANALYZE header the way SPM, FreeSurfer and the other tools of the
ecosystem do. This concerns the fields that ANALYZE 7.5 leaves undefined but that SPM uses: |
an fs.volume instance, i.e. a named list with the entries data (the voxel data) and header (the
image header), or only the data array if with_header is FALSE. The header is the format specific header (an
ANALYZE 7.5 header as returned by read.analyze.header for ANALYZE files, a NIFTI v1 header as
returned by read.nifti1.header for NIFTI pair files), and not an MGH header. In addition, the
following entries are present:
vox2ras_matrixthe 4x4 voxel-to-RAS transformation matrix, computed from the sform/qform header
fields for NIFTI v1 pair files. For ANALYZE files this entry is only present if spm is TRUE, see the
note below.
vox2ras_sourcecharacter string, a description of where the matrix comes from, one of 'sform',
'qform' (for NIFTI v1 pair files), 'mat sidecar', 'spm origin' or 'analyze convention' (for ANALYZE
files, see the geometry section). Only present if vox2ras_matrix is present.
voldimonly present if flatten is TRUE, exactly as in the other volume readers.
filepath, filepath_header, filepath_imagethe paths of the header and data files.
The ANALYZE 7.5 header stores the voxel sizes (pix_dim) but neither the direction of the voxel axes in world
space nor the position of the image in it. This means that the left/right orientation of an ANALYZE image is
not known from the header, and no implementation of the format can know it: the format is famous for exactly
this problem, and the orient field that was added to fix it is set by some tools and ignored by others.
There are two ways to get a transformation matrix for such a file, and this function supports both:
SPM and FreeSurfer write the transformation matrix into a file named
<base>.mat next to the <base>.img file, as a MATLAB v4 file with a variable named mat or M. This
matrix is real information from the file set, not a convention, so it is used whenever such a file is
present, and the vox2ras_source of the result is 'mat sidecar'. The M variant does not contain the
flip of the first voxel axis that the format's original software applies, so that flip is added (this is
also what the reference implementation of the format, nibabel, does). Note that the matrix in the file maps
the 1-based voxel indices that MATLAB uses, and that the matrix returned here maps the 0-based indices
that R and this package use: the difference is the sum of the rows of the rotation part, i.e. up to several
millimeters, and getting it wrong is exactly the kind of silent error that is hard to notice. FreeSurfer
writes only the M variable, nibabel writes both. MATLAB files of version 5 and newer (the .mat files
that recent MATLAB versions write, which are compressed) cannot be read; such a file is reported and
ignored, the volume data is still returned.
spm = TRUE)If there is no sidecar file, the SPM and FreeSurfer tools derive the
geometry from the fields that ANALYZE leaves unused: the originator field holds the voxel coordinates of
the image origin, the voxel sizes give the axis lengths, and the axes are assumed to point to the left, the
front and the top (diag(-x, y, z), the convention of the format's original software). The same spm = TRUE applies the scale factor that SPM stores in funused1. Both are heuristics that the file does not
state, which is why they are not used by default: a matrix from a convention can be wrong by a mirror
image.
Without a sidecar file and with spm = FALSE (the default), no matrix is reported at all: the voxel sizes and
the orientation code are returned as they are stored, and you can build a matrix from them if the convention of
your choice is known to be the right one for your data.
read.analyze.header, read.analyze.data, read.fs.volume
hdrfile <- system.file("extdata", "analyze", "tiny_u8.hdr",
package = "freesurferformats", mustWork = TRUE
)
vol <- read.fs.volume.analyze(hdrfile, with_header = TRUE)
dim(vol$data)
vol$header$pix_dim
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.