Package nifti :: Module niftiformat :: Class NiftiFormat
[hide private]
[frames] | no frames]

Class NiftiFormat

source code


NIfTI header representation.

NIfTI header can be created by loading information from an existing NIfTI file or by creating a matching NIfTI header for a ndarray.

In addition, a number of methods to manipulate the header information are provided. However, this class is not able to write a NIfTI header back to disk. Please refer to the NIfTIImage class for this functionality.

Instance Methods [hide private]
 
__init__(self, source, header=None)
This method decides whether to load a nifti image header from file or create one from ndarray data, depending on the datatype of source.
source code
call graph 
 
__del__(self) source code
call graph 
 
__newFromArray(self, data, hdr={})
Create a nifti_image struct from a ndarray.
source code
call graph 
 
__newFromFile(self, filename)
Open a NIfTI file.
source code
call graph 
 
getVoxDims(self)
Returns a 3-tuple a voxel dimensions/size in (x,y,z).
source code
 
setVoxDims(self, value)
Set voxel dimensions/size.
source code
call graph 
 
setPixDims(self, value)
Set the pixel dimensions.
source code
call graph 
 
getPixDims(self)
Returns the pixel dimensions on all 7 dimensions.
source code
call graph 
 
getExtent(self)
Returns the shape of the dataimage.
source code
call graph 
 
getVolumeExtent(self)
Returns the size/shape of the volume(s) in the image as a tuple.
source code
 
getTimepoints(self)
Returns the number of timepoints in the image.
source code
 
getRepetitionTime(self)
Returns the temporal distance between the volumes in a timeseries.
source code
 
setRepetitionTime(self, value)
Set the repetition time of a nifti image (dt).
source code
 
asDict(self)
Returns the header data of the NiftiImage in a dictionary.
source code
call graph 
 
updateFromDict(self, hdrdict)
Update NIfTI header information.
source code
 
setSlope(self, value)
Set the slope attribute in the NIfTI header.
source code
 
setIntercept(self, value)
Set the intercept attribute in the NIfTI header.
source code
 
setDescription(self, value)
Set the description element in the NIfTI header.
source code
call graph 
 
setXFormCode(self, xform, code)
Set the type of space described by the NIfTI transformations.
source code
call graph 
 
getSForm(self)
Returns the sform matrix.
source code
 
setSForm(self, m, code='mni152')
Sets the sform matrix.
source code
 
getInverseSForm(self)
Returns the inverse sform matrix.
source code
 
getQForm(self)
Returns the qform matrix.
source code
 
getInverseQForm(self)
Returns the inverse qform matrix.
source code
 
setQForm(self, m, code='scanner')
Sets the qform matrix.
source code
call graph 
 
updateQFormFromQuaternion(self)
Recalculates the qform matrix (and the inverse) from the quaternion representation.
source code
call graph 
 
setQuaternion(self, value, code='scanner')
Set Quaternion from 3-tuple (qb, qc, qd).
source code
 
getQuaternion(self)
Returns a 3-tuple containing (qb, qc, qd).
source code
 
setQOffset(self, value, code='scanner')
Set QOffset from 3-tuple (qx, qy, qz).
source code
call graph 
 
getQOffset(self)
Returns a 3-tuple containing (qx, qy, qz).
source code
 
setQFac(self, value, code='scanner')
Set qfac (scaling factor of qform matrix).
source code
 
getQOrientation(self, as_string=False)
Returns to orientation of the i, j and k axis as stored in the qform matrix.
source code
 
getSOrientation(self, as_string=False)
Returns to orientation of the i, j and k axis as stored in the sform matrix.
source code
 
getFilename(self)
Returns the filename.
source code
call graph 

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]
  nvox
  max
  min
  sform_inv
Returns the inverse sform matrix.
  qform_inv
Returns the inverse qform matrix.
  extent
Returns the shape of the dataimage.
  volextent
Returns the size/shape of the volume(s) in the image as a tuple.
  timepoints
Returns the number of timepoints in the image.
  raw_nimg
  filename
Returns the filename.
  slope
  intercept
  voxdim
Returns a 3-tuple a voxel dimensions/size in (x,y,z).
  pixdim
Returns the pixel dimensions on all 7 dimensions.
  description
  header
Returns the header data of the NiftiImage in a dictionary.
  sform
Returns the sform matrix.
  qform
Returns the qform matrix.
  quatern
Returns a 3-tuple containing (qb, qc, qd).
  qoffset
Returns a 3-tuple containing (qx, qy, qz).
  qfac
  rtime
Returns the temporal distance between the volumes in a timeseries.

Inherited from object: __class__

Method Details [hide private]

__init__(self, source, header=None)
(Constructor)

source code 
call graph 
This method decides whether to load a nifti image header from file or create one from ndarray data, depending on the datatype of source.
Parameters:
  • source, str, |, ndarray - If source is a string, it is assumed to be a filename and an attempt will be made to open the corresponding NIfTI file. In case of an ndarray the array data will be used for the to be created nifti image and a matching nifti header is generated. If an object of a different type is supplied as 'source' a ValueError exception will be thrown.
  • header, dict - Additional header data might be supplied. However, dimensionality and datatype are determined from the ndarray and not taken from a header dictionary.
Overrides: object.__init__

__newFromArray(self, data, hdr={})

source code 
call graph 
Create a nifti_image struct from a ndarray.
Parameters:
  • data, ndarray - Source ndarray.
  • hdr, dict - Optional dictionary with NIfTI header data.

__newFromFile(self, filename)

source code 
call graph 
Open a NIfTI file.
Parameters:
  • filename, str - Filename of the to be opened image file.

getVoxDims(self)

source code 

Returns a 3-tuple a voxel dimensions/size in (x,y,z).

The voxdim property is an alternative way to access this function.

setVoxDims(self, value)

source code 
call graph 

Set voxel dimensions/size.

The qform matrix and its inverse will be recalculated automatically.

Besides reading it is also possible to set the voxel dimensions by assigning to the voxdim property.

setPixDims(self, value)

source code 
call graph 

Set the pixel dimensions.

Calling setPixDims() with a length-3 sequence equals calling setVoxDims().

getPixDims(self)

source code 
call graph 

Returns the pixel dimensions on all 7 dimensions.

The function is similar to getVoxDims(), but instead of the 3d spatial dimensions of a voxel it returns the dimensions of an image pixel on all 7 dimensions supported by the NIfTI dataformat.

getExtent(self)

source code 
call graph 

Returns the shape of the dataimage.

Please note that the order of dimensions is different from the tuple returned by calling NiftiImage.data.shape!

See also getVolumeExtent() and getTimepoints().

The extent property is an alternative way to access this function.

Returns:

Tuple with the size in voxel/timepoints.

The order of dimensions is (x,y,z,t,u,v,w). If the image has less dimensions than 7 the return tuple will be shortened accordingly.

getVolumeExtent(self)

source code 

Returns the size/shape of the volume(s) in the image as a tuple.

The volextent property is an alternative way to access this function.

Returns:

Either a 3-tuple or 2-tuple or 1-tuple depending on the available dimensions in the image.

The order of dimensions in the tuple is (x [, y [, z ] ] ).

getTimepoints(self)

source code 

Returns the number of timepoints in the image.

In case of a 3d (or less dimension) image this method returns 1.

The timepoints property is an alternative way to access this function.

getRepetitionTime(self)

source code 

Returns the temporal distance between the volumes in a timeseries.

The rtime property is an alternative way to access this function.

asDict(self)

source code 
call graph 

Returns the header data of the NiftiImage in a dictionary.

Note, that modifications done to this dictionary do not cause any modifications in the NIfTI image. Please use the updateFromDict() method to apply changes to the image.

The header property is an alternative way to access this function. But please note that the header property cannot be used like this:

nimg.header['something'] = 'new value'

Instead one has to get the header dictionary, modify and later reassign it:

h = nimg.header
h['something'] = 'new value'
nimg.header = h

updateFromDict(self, hdrdict)

source code 

Update NIfTI header information.

Updated header data is read from the supplied dictionary. One cannot modify dimensionality and datatype of the image data. If such information is present in the header dictionary it is removed before the update. If resizing or datatype casting are required one has to convert the image data into a separate array (NiftiImage.assarray()) and perform resize and data manipulations on this array. When finished, the array can be converted into a nifti file by calling the NiftiImage constructor with the modified array as 'source' and the nifti header of the original NiftiImage object as 'header'.

setSlope(self, value)

source code 

Set the slope attribute in the NIfTI header.

Besides reading it is also possible to set the slope by assigning to the slope property.

setIntercept(self, value)

source code 

Set the intercept attribute in the NIfTI header.

Besides reading it is also possible to set the intercept by assigning to the intercept property.

setDescription(self, value)

source code 
call graph 

Set the description element in the NIfTI header.

Besides reading it is also possible to set the description by assigning to the description property.

setXFormCode(self, xform, code)

source code 
call graph 

Set the type of space described by the NIfTI transformations.

The NIfTI format defines five coordinate system types which are used to describe the target space of a transformation (qform or sform). Please note, that the last four transformation types are only available in the NIfTI format and not when saving into ANALYZE.

'unkown', NIFTI_XFORM_UNKNOWN, 0:
Transformation is arbitrary. This is the ANALYZE compatibility mode. In this case no sform matrix will be written, even when stored in NIfTI and not in ANALYZE format. Additionally, only the pixdim parts of the qform matrix will be saved (upper-left 3x3).
'scanner', NIFTI_XFORM_SCANNER_ANAT, 1:
Scanner-based anatomical coordinates.
'aligned', NIFTI_XFORM_ALIGNED_ANAT, 2:
Coordinates are aligned to another file's coordinate system.
'talairach', NIFTI_XFORM_TALAIRACH, 3:
Coordinate system is shifted to have its origin (0,0,0) at the anterior commissure, as in the Talairach-Tournoux Atlas.
'mni152', NIFTI_XFORM_MNI_152, 4:
Coordinates are in MNI152 space.

Parameters:

xform: str('qform' | 'sform')
Which of the two NIfTI transformations to set.
code: str | NIFTI_XFORM_CODE | int (0..4)
The Transformation code can be specified either by a string, the NIFTI_XFORM_CODE defined in the nifti1.h header file (accessible via the nifticlib module, or the corresponding integer value (see above list for all possibilities).

getSForm(self)

source code 

Returns the sform matrix.

Please note, that the returned SForm matrix is not bound to the NiftiImage object. Therefore it cannot be successfully modified in-place. Modifications to the SForm matrix can only be done by setting a new SForm matrix either by calling setSForm() or by assigning it to the sform attribute.

The sform property is an alternative way to access this function.

setSForm(self, m, code='mni152')

source code 

Sets the sform matrix.

The supplied value has to be a 4x4 matrix. The matrix elements will be converted to floats. By definition the last row of the sform matrix has to be (0,0,0,1). However, different values can be assigned, but will not be stored when the niftifile is saved.

The inverse sform matrix will be automatically recalculated.

Besides reading it is also possible to set the sform matrix by assigning to the sform property.

Parameters:

m: 4x4 ndarray
The sform matrix.
code: str | NIFTI_XFORM_CODE | int (0..4)
The type of the coordinate system the sform matrix is describing. By default this coordinate system is assumed to be the MNI152 space. Please refer to the setXFormCode() method for a full list of possible codes and their meaning.

getInverseSForm(self)

source code 

Returns the inverse sform matrix.

Please note, that the inverse SForm matrix cannot be modified in-place. One needs to set a new SForm matrix instead. The corresponding inverse matrix is then re-calculated automatically.

The sform_inv property is an alternative way to access this function.

getQForm(self)

source code 

Returns the qform matrix.

Please note, that the returned QForm matrix is not bound to the NiftiImage object. Therefore it cannot be successfully modified in-place. Modifications to the QForm matrix can only be done by setting a new QForm matrix either by calling setQForm() or by assigning it to the qform property.

getInverseQForm(self)

source code 

Returns the inverse qform matrix.

The qform_inv property is an alternative way to access this function.

Please note, that the inverse QForm matrix cannot be modified in-place. One needs to set a new QForm matrix instead. The corresponding inverse matrix is then re-calculated automatically.

setQForm(self, m, code='scanner')

source code 
call graph 

Sets the qform matrix.

The supplied value has to be a 4x4 matrix. The matrix will be converted to float.

The inverse qform matrix and the quaternion representation will be automatically recalculated.

Besides reading it is also possible to set the qform matrix by assigning to the qform property.

Parameters:

m: 4x4 ndarray
The qform matrix.
code: str | NIFTI_XFORM_CODE | int (0..4)
The type of the coordinate system the qform matrix is describing. By default this coordinate system is assumed to be the scanner anatomical space. Please refer to the setXFormCode() method for a full list of possible codes and their meaning.

setQuaternion(self, value, code='scanner')

source code 

Set Quaternion from 3-tuple (qb, qc, qd).

The qform matrix and it's inverse are re-computed automatically.

Besides reading it is also possible to set the quaternion by assigning to the quatern property.

Parameters:

value: length-3 sequence
qb, qc and qd quaternions.
code: str | NIFTI_XFORM_CODE | int (0..4)
The type of the coordinate system the corresponding qform matrix is describing. By default this coordinate system is assumed to be the scanner anatomical space. Please refer to the setXFormCode() method for a full list of possible codes and their meaning.

getQuaternion(self)

source code 

Returns a 3-tuple containing (qb, qc, qd).

The quatern property is an alternative way to access this function.

setQOffset(self, value, code='scanner')

source code 
call graph 

Set QOffset from 3-tuple (qx, qy, qz).

The qform matrix and its inverse are re-computed automatically.

Besides reading it is also possible to set the qoffset by assigning to the qoffset property.

Parameters:

value: length-3 sequence
qx, qy and qz offsets.
code: str | NIFTI_XFORM_CODE | int (0..4)
The type of the coordinate system the corresponding qform matrix is describing. By default this coordinate system is assumed to be the scanner anatomical space. Please refer to the setXFormCode() method for a full list of possible codes and their meaning.

getQOffset(self)

source code 

Returns a 3-tuple containing (qx, qy, qz).

The qoffset property is an alternative way to access this function.

setQFac(self, value, code='scanner')

source code 

Set qfac (scaling factor of qform matrix).

The qform matrix and its inverse are re-computed automatically.

Besides reading it is also possible to set the qfac by assigning to the qfac property.

Parameters:

value: float
Scaling factor.
code: str | NIFTI_XFORM_CODE | int (0..4)
The type of the coordinate system the corresponding qform matrix is describing. By default this coordinate system is assumed to be the scanner anatomical space. Please refer to the setXFormCode() method for a full list of possible codes and their meaning.

getQOrientation(self, as_string=False)

source code 

Returns to orientation of the i, j and k axis as stored in the qform matrix.

By default NIfTI orientation codes are returned, but if as_string is set to true a string representation ala 'Left-to-right' is returned instead.

getSOrientation(self, as_string=False)

source code 

Returns to orientation of the i, j and k axis as stored in the sform matrix.

By default NIfTI orientation codes are returned, but if as_string is set to true a string representation ala 'Left-to-right' is returned instead.

getFilename(self)

source code 
call graph 

Returns the filename.

To distinguish ANALYZE from 2-file NIfTI images the image filename is returned for ANALYZE images while the header filename is returned for NIfTI files.

The filename property is an alternative way to access this function.


Property Details [hide private]

nvox

Get Method:
unreachable(self)

max

Get Method:
unreachable(self)

min

Get Method:
unreachable(self)

sform_inv

Returns the inverse sform matrix.

Please note, that the inverse SForm matrix cannot be modified in-place. One needs to set a new SForm matrix instead. The corresponding inverse matrix is then re-calculated automatically.

The sform_inv property is an alternative way to access this function.

Get Method:
getInverseSForm(self) - Returns the inverse sform matrix.

qform_inv

Returns the inverse qform matrix.

The qform_inv property is an alternative way to access this function.

Please note, that the inverse QForm matrix cannot be modified in-place. One needs to set a new QForm matrix instead. The corresponding inverse matrix is then re-calculated automatically.

Get Method:
getInverseQForm(self) - Returns the inverse qform matrix.

extent

Returns the shape of the dataimage.

Please note that the order of dimensions is different from the tuple returned by calling NiftiImage.data.shape!

See also getVolumeExtent() and getTimepoints().

The extent property is an alternative way to access this function.

Get Method:
getExtent(self) - Returns the shape of the dataimage.

volextent

Returns the size/shape of the volume(s) in the image as a tuple.

The volextent property is an alternative way to access this function.

Get Method:
getVolumeExtent(self) - Returns the size/shape of the volume(s) in the image as a tuple.

timepoints

Returns the number of timepoints in the image.

In case of a 3d (or less dimension) image this method returns 1.

The timepoints property is an alternative way to access this function.

Get Method:
getTimepoints(self) - Returns the number of timepoints in the image.

raw_nimg

Get Method:
unreachable(self)

filename

Returns the filename.

To distinguish ANALYZE from 2-file NIfTI images the image filename is returned for ANALYZE images while the header filename is returned for NIfTI files.

The filename property is an alternative way to access this function.

Get Method:
getFilename(self) - Returns the filename.

slope

Get Method:
unreachable(self)
Set Method:
setSlope(self, value) - Set the slope attribute in the NIfTI header.

intercept

Get Method:
unreachable(self)
Set Method:
setIntercept(self, value) - Set the intercept attribute in the NIfTI header.

voxdim

Returns a 3-tuple a voxel dimensions/size in (x,y,z).

The voxdim property is an alternative way to access this function.

Get Method:
getVoxDims(self) - Returns a 3-tuple a voxel dimensions/size in (x,y,z).
Set Method:
setVoxDims(self, value) - Set voxel dimensions/size.

pixdim

Returns the pixel dimensions on all 7 dimensions.

The function is similar to getVoxDims(), but instead of the 3d spatial dimensions of a voxel it returns the dimensions of an image pixel on all 7 dimensions supported by the NIfTI dataformat.

Get Method:
getPixDims(self) - Returns the pixel dimensions on all 7 dimensions.
Set Method:
setPixDims(self, value) - Set the pixel dimensions.

description

Get Method:
unreachable(self)
Set Method:
setDescription(self, value) - Set the description element in the NIfTI header.

header

Returns the header data of the NiftiImage in a dictionary.

Note, that modifications done to this dictionary do not cause any modifications in the NIfTI image. Please use the updateFromDict() method to apply changes to the image.

The header property is an alternative way to access this function. But please note that the header property cannot be used like this:

nimg.header['something'] = 'new value'

Instead one has to get the header dictionary, modify and later reassign it:

h = nimg.header
h['something'] = 'new value'
nimg.header = h
Get Method:
asDict(self) - Returns the header data of the NiftiImage in a dictionary.
Set Method:
updateFromDict(self, hdrdict) - Update NIfTI header information.

sform

Returns the sform matrix.

Please note, that the returned SForm matrix is not bound to the NiftiImage object. Therefore it cannot be successfully modified in-place. Modifications to the SForm matrix can only be done by setting a new SForm matrix either by calling setSForm() or by assigning it to the sform attribute.

The sform property is an alternative way to access this function.

Get Method:
getSForm(self) - Returns the sform matrix.
Set Method:
setSForm(self, m, code='mni152') - Sets the sform matrix.

qform

Returns the qform matrix.

Please note, that the returned QForm matrix is not bound to the NiftiImage object. Therefore it cannot be successfully modified in-place. Modifications to the QForm matrix can only be done by setting a new QForm matrix either by calling setQForm() or by assigning it to the qform property.

Get Method:
getQForm(self) - Returns the qform matrix.
Set Method:
setQForm(self, m, code='scanner') - Sets the qform matrix.

quatern

Returns a 3-tuple containing (qb, qc, qd).

The quatern property is an alternative way to access this function.

Get Method:
getQuaternion(self) - Returns a 3-tuple containing (qb, qc, qd).
Set Method:
setQuaternion(self, value, code='scanner') - Set Quaternion from 3-tuple (qb, qc, qd).

qoffset

Returns a 3-tuple containing (qx, qy, qz).

The qoffset property is an alternative way to access this function.

Get Method:
getQOffset(self) - Returns a 3-tuple containing (qx, qy, qz).
Set Method:
setQOffset(self, value, code='scanner') - Set QOffset from 3-tuple (qx, qy, qz).

qfac

Get Method:
unreachable(self)
Set Method:
setQFac(self, value, code='scanner') - Set qfac (scaling factor of qform matrix).

rtime

Returns the temporal distance between the volumes in a timeseries.

The rtime property is an alternative way to access this function.

Get Method:
getRepetitionTime(self) - Returns the temporal distance between the volumes in a timeseries.
Set Method:
setRepetitionTime(self, value) - Set the repetition time of a nifti image (dt).