Package nifti :: Module niftiimage :: Class NiftiImage
[hide private]
[frames] | no frames]

Class NiftiImage

source code


Wrapper class for convenient access to NIfTI data.

The class can either load an image from file or convert a NumPy ndarray into a NIfTI file structure. Either way is automatically determined by the type of the 'source' argument. If source is a string, it is assumed to be a filename an ndarray is treated as such.

One can optionally specify whether the image data should be loaded into memory when opening NIfTI data from files (load). When converting a NumPy array one can optionally specify a dictionary with NIfTI header data as available via the header attribute.

Instance Methods [hide private]
 
__init__(self, source, header={}, load=False)
This method decides whether to load a nifti image from file or create one from ndarray data, depending on the datatype of source.
source code
call graph 
 
__del__(self) source code
call graph 
 
save(self, filename=None, filetype='NIFTI')
Save the image.
source code
call graph 
 
__haveImageData(self)
Returns if the image data is accessible -- either loaded into memory or memory mapped.
source code
call graph 
 
load(self)
Load the image data into memory, if it is not already accessible.
source code
call graph 
 
unload(self)
Unload image data and free allocated memory.
source code
call graph 
 
getDataArray(self)
Return the NIfTI image data wrapped into a NumPy array.
source code
call graph 
 
asarray(self, copy=True)
Convert the image data into a ndarray.
source code
call graph 
 
getScaledData(self)
Returns a scaled copy of the data array.
source code
 
updateCalMinMax(self)
Update the image data maximum and minimum value in the nifti header.
source code
call graph 
 
getBoundingBox(self)
Get the bounding box of the image.
source code
 
setFilename(self, filename, filetype='NIFTI')
Set the filename for the NIfTI image.
source code
call graph 
 
updateHeader(self, hdrdict)
Deprecated method only here for backward compatibility.
source code

Inherited from niftiformat.NiftiFormat: asDict, getExtent, getFilename, getInverseQForm, getInverseSForm, getPixDims, getQForm, getQOffset, getQOrientation, getQuaternion, getRepetitionTime, getSForm, getSOrientation, getTimepoints, getVolumeExtent, getVoxDims, setDescription, setIntercept, setPixDims, setQFac, setQForm, setQOffset, setQuaternion, setRepetitionTime, setSForm, setSlope, setVoxDims, setXFormCode, updateFromDict, updateQFormFromQuaternion

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

Properties [hide private]
  data
Return the NIfTI image data wrapped into a NumPy array.
  bbox
Get the bounding box of the image.

Inherited from niftiformat.NiftiFormat: description, extent, filename, header, intercept, max, min, nvox, pixdim, qfac, qform, qform_inv, qoffset, quatern, raw_nimg, rtime, sform, sform_inv, slope, timepoints, volextent, voxdim

Inherited from object: __class__

Method Details [hide private]

__init__(self, source, header={}, load=False)
(Constructor)

source code 
call graph 
This method decides whether to load a nifti image 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.
  • load, Boolean - If set to True the image data will be loaded into memory. This is only useful if loading a NIfTI image from file.
Overrides: object.__init__

__del__(self)
(Destructor)

source code 
call graph 
Overrides: niftiformat.NiftiFormat.__del__

save(self, filename=None, filetype='NIFTI')

source code 
call graph 

Save the image.

If the image was created using array data (not loaded from a file) one has to specify a filename.

Warning: There will be no exception if writing fails for any reason, as the underlying function nifti_write_hdr_img() from libniftiio does not provide any feedback. Suggestions for improvements are appreciated.

If not yet done already, the image data will be loaded into memory before saving the file.

Parameters:
  • filename, str, |, None - Calling save() with filename equal None on a NiftiImage loaded from a file, it will overwrite the original file.

    Usually setting the filename also determines the filetype (NIfTI/ANALYZE). Please see the documentation of the setFilename() method for some more details.

  • filetype, str - Override filetype. Please see the documentation of the setFilename() method for some more details.

__haveImageData(self)

source code 
call graph 

Returns if the image data is accessible -- either loaded into memory or memory mapped.

See: load(), unload()

load(self)

source code 
call graph 

Load the image data into memory, if it is not already accessible.

It is save to call this method several times successively.

unload(self)

source code 
call graph 

Unload image data and free allocated memory.

This methods does nothing in case of memory mapped files.

getDataArray(self)

source code 
call graph 

Return the NIfTI image data wrapped into a NumPy array.

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

asarray(self, copy=True)

source code 
call graph 
Convert the image data into a ndarray.
Parameters:
  • copy, Boolean - If set to False the array only wraps the image data, while True will return a copy of the data array.

getScaledData(self)

source code 

Returns a scaled copy of the data array.

Scaling is done by multiplying with the slope and adding the intercept that is stored in the NIfTI header.

Returns:
ndarray

getBoundingBox(self)

source code 

Get the bounding box of the image.

This functions returns a tuple of (min, max) tuples. It contains as many tuples as image dimensions. The order of dimensions is identical to that in the data array.

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

setFilename(self, filename, filetype='NIFTI')

source code 
call graph 
Set the filename for the NIfTI image.

Setting the filename also determines the filetype. If the filename
ends with '.nii' the type will be set to NIfTI single file. A '.hdr'
extension can be used for NIfTI file pairs. If the desired filetype
is ANALYZE the extension should be '.img'. However, one can use the
'.hdr' extension and force the filetype to ANALYZE by setting the
filetype argument to ANALYZE. Setting filetype if the filename
extension is '.nii' has no effect, the file will always be in NIFTI
format.

If the filename carries an additional '.gz' the resulting file(s) will
be compressed.

Uncompressed NIfTI single files are the default filetype that will be
used if the filename has no valid extension. The '.nii' extension is
appended automatically. The 'filetype' argument can be used to force a
certain filetype when no extension can be used to determine it. 
'filetype' can be one of the nifticlibs filtetypes or any of 'NIFTI',
'NIFTI_GZ', 'NIFTI_PAIR', 'NIFTI_PAIR_GZ', 'ANALYZE', 'ANALYZE_GZ'.

Setting the filename will cause the image data to be loaded into memory
if not yet done already. This has to be done, because without the
filename of the original image file there would be no access to the
image data anymore. As a side-effect a simple operation like setting a
filename may take a significant amount of time (e.g. for a large 4d
dataset).

By passing an empty string or none as filename one can reset the
filename and detach the NiftiImage object from any file on disk.

Examples:

  Filename          Output of save()
  ----------------------------------
  exmpl.nii         exmpl.nii (NIfTI)
  exmpl.hdr         exmpl.hdr, exmpl.img (NIfTI)
  exmpl.img         exmpl.hdr, exmpl.img (ANALYZE)
  exmpl             exmpl.nii (NIfTI)
  exmpl.hdr.gz      exmpl.hdr.gz, exmpl.img.gz (NIfTI)

! exmpl.gz          exmpl.gz.nii (uncompressed NIfTI)

Setting the filename is also possible by assigning to the 'filename'
property.

updateHeader(self, hdrdict)

source code 

Deprecated method only here for backward compatibility.

Please refer to NiftiFormat.updateFromDict()


Property Details [hide private]

data

Return the NIfTI image data wrapped into a NumPy array.

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

Get Method:
getDataArray(self) - Return the NIfTI image data wrapped into a NumPy array.

bbox

Get the bounding box of the image.

This functions returns a tuple of (min, max) tuples. It contains as many tuples as image dimensions. The order of dimensions is identical to that in the data array.

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

Get Method:
getBoundingBox(self) - Get the bounding box of the image.