A simple spatial image class
The image class maintains the association between a 3D (or greater) array, and an affine transform that maps voxel coordinates to some world space. It also has a header - some standard set of meta-data that is specific to the image format, and extra - a dictionary container for any other metadata.
It has attributes:
- extra
methods:
- .get_data()
- .get_affine() (deprecated, use affine property instead)
- .get_header() (deprecated, use header property instead)
- .to_filename(fname) - writes data to filename(s) derived from fname, where the derivation may differ between formats.
- to_file_map() - save image to files with which the image is already associated.
- .get_shape() (deprecated)
properties:
- shape
- affine
- header
- dataobj
classmethods:
- from_filename(fname) - make instance by loading from filename
- from_file_map(fmap) - make instance from file map
- instance_to_filename(img, fname) - save img instance to filename fname.
You cannot slice an image, and trying to slice an image generates an informative TypeError.
There is the usual way, which is the default:
img.to_filename(fname)
and that is, to take the data encapsulated by the image and cast it to the datatype the header expects, setting any available header scaling into the header to help the data match.
You can load the data into an image from file with:
img.from_filename(fname)
The image stores its associated files in its file_map attribute. In order to just save an image, for which you know there is an associated filename, or other storage, you can do:
img.to_file_map()
You can get the data out again with:
img.get_data()
Less commonly, for some image types that support it, you might want to fetch out the unscaled array via the object containing the data:
unscaled_data = img.dataoobj.get_unscaled()
Analyze-type images (including nifti) support this, but others may not (MINC, for example).
Sometimes you might to avoid any loss of precision by making the data type the same as the input:
hdr = img.header
hdr.set_data_dtype(data.dtype)
img.to_filename(fname)
The image has an attribute file_map. This is a mapping, that has keys corresponding to the file types that an image needs for storage. For example, the Analyze data format needs an image and a header file type for storage:
>>> import nibabel as nib
>>> data = np.arange(24, dtype='f4').reshape((2,3,4))
>>> img = nib.AnalyzeImage(data, np.eye(4))
>>> sorted(img.file_map)
['header', 'image']
The values of file_map are not in fact files but objects with attributes filename, fileobj and pos.
The reason for this interface, is that the contents of files has to contain enough information so that an existing image instance can save itself back to the files pointed to in file_map. When a file holder holds active file-like objects, then these may be affected by the initial file read; in this case, the contains file-like objects need to carry the position at which a write (with to_files) should place the data. The file_map contents should therefore be such, that this will work:
>>> # write an image to files
>>> from io import BytesIO
>>> file_map = nib.AnalyzeImage.make_file_map()
>>> file_map['image'].fileobj = BytesIO()
>>> file_map['header'].fileobj = BytesIO()
>>> img = nib.AnalyzeImage(data, np.eye(4))
>>> img.file_map = file_map
>>> img.to_file_map()
>>> # read it back again from the written files
>>> img2 = nib.AnalyzeImage.from_file_map(file_map)
>>> np.all(img2.get_data() == data)
True
>>> # write, read it again
>>> img2.to_file_map()
>>> img3 = nib.AnalyzeImage.from_file_map(file_map)
>>> np.all(img3.get_data() == data)
True
Header([data_dtype, shape, zooms]) | Template class to implement header protocol |
HeaderDataError | Class to indicate error in getting or setting header data |
HeaderTypeError | Class to indicate error in parameters into header functions |
ImageDataError | |
ImageFileError | |
SpatialImage(dataobj, affine[, header, ...]) | Initialize image |
supported_np_types(obj) | Numpy data types that instance obj supports |
Bases: object
Template class to implement header protocol
Copy object to independent representation
The copy should not be affected by any changes to the original object.
Read binary image data from fileobj
Write array data data as binary to fileobj
Parameters: | data : array-like
fileobj : file-like object
rescale : {True, False}, optional
|
---|
Bases: object
Initialize image
The image is a combination of (array, affine matrix, header), with optional metadata in extra, and filename / file-like objects contained in the file_map mapping.
Parameters: | dataobj : object
affine : None or (4,4) array-like
header : None or mapping or header instance, optional
extra : None or mapping, optional
file_map : mapping, optional
|
---|
Initialize image
The image is a combination of (array, affine matrix, header), with optional metadata in extra, and filename / file-like objects contained in the file_map mapping.
Parameters: | dataobj : object
affine : None or (4,4) array-like
header : None or mapping or header instance, optional
extra : None or mapping, optional
file_map : mapping, optional
|
---|
Make file_map for this class from filename filespec
Class method
Parameters: | filespec : str
|
---|---|
Returns: | file_map : dict
|
Raises: | ImageFileError :
|
Class method to create new instance of own class from img
Parameters: | img : spatialimage instance
|
---|---|
Returns: | cimg : spatialimage instance
|
Get affine from image
Please use the affine property instead of get_affine; we will deprecate this method in future versions of nibabel.
Return image data from image with any necessary scalng applied
The image dataobj property can be an array proxy or an array. An array proxy is an object that knows how to load the image data from disk. An image with an array proxy dataobj is a proxy image; an image with an array in dataobj is an array image.
The default behavior for get_data() on a proxy image is to read the data from the proxy, and store in an internal cache. Future calls to get_data will return the cached array. This is the behavior selected with caching == “fill”`.
Once the data has been cached and returned from an array proxy, if you modify the returned array, you will also modify the cached array (because they are the same array). Regardless of the caching flag, this is always true of an array image.
Parameters: | caching : {‘fill’, ‘unchanged’}, optional
|
---|---|
Returns: | data : array
|
See also
Notes
All images have a property dataobj that represents the image array data. Images that have been loaded from files usually do not load the array data from file immediately, in order to reduce image load time and memory use. For these images, dataobj is an array proxy; an object that knows how to load the image array data from file.
By default (caching == “fill”), when you call get_data on a proxy image, we load the array data from disk, store (cache) an internal reference to this array data, and return the array. The next time you call get_data, you will get the cached reference to the array, so we don’t have to load the array data from disk again.
Array images have a dataobj property that already refers to an array in memory, so there is no benefit to caching, and the caching keywords have no effect.
For proxy images, you may not want to fill the cache after reading the data from disk because the cache will hold onto the array memory until the image object is deleted, or you use the image uncache method. If you don’t want to fill the cache, then always use get_data(caching='unchanged'); in this case get_data will not fill the cache (store the reference to the array) if the cache is empty (no reference to the array). If the cache is full, “unchanged” leaves the cache full and returns the cached array reference.
The cache can effect the behavior of the image, because if the cache is full, or you have an array image, then modifying the returned array will modify the result of future calls to get_data(). For example you might do this:
>>> import os
>>> import nibabel as nib
>>> from nibabel.testing import data_path
>>> img_fname = os.path.join(data_path, 'example4d.nii.gz')
>>> img = nib.load(img_fname) # This is a proxy image
>>> nib.is_proxy(img.dataobj)
True
The array is not yet cached by a call to “get_data”, so: >>> img.in_memory False
After we call get_data using the default caching=’fill’, the cache contains a reference to the returned array ``data`:
>>> data = img.get_data()
>>> img.in_memory
True
We modify an element in the returned data array:
>>> data[0, 0, 0, 0]
0
>>> data[0, 0, 0, 0] = 99
>>> data[0, 0, 0, 0]
99
The next time we call ‘get_data’, the method returns the cached reference to the (modified) array:
>>> data_again = img.get_data()
>>> data_again is data
True
>>> data_again[0, 0, 0, 0]
99
If you had initially used caching == ‘unchanged’ then the returned data array would have been loaded from file, but not cached, and:
>>> img = nib.load(img_fname) # a proxy image again
>>> data = img.get_data(caching='unchanged')
>>> img.in_memory
False
>>> data[0, 0, 0] = 99
>>> data_again = img.get_data(caching='unchanged')
>>> data_again is data
False
>>> data_again[0, 0, 0, 0]
0
Fetch the image filename
Parameters: | None : |
---|---|
Returns: | fname : None or str
|
Get header from image
Please use the header property instead of get_header; we will deprecate this method in future versions of nibabel.
Return shape for image
This function deprecated; please use the shape property instead
True when array data is in memory
Save img in our own format, to name implied by filename
This is a class method
Parameters: | img : spatialimage instance
filename : str
|
---|
Class method to make files holder for this image type
Parameters: | mapping : None or mapping, optional
|
---|---|
Returns: | file_map : dict
|
Sets the files in the object from a given filename
The different image formats may check whether the filename has an extension characteristic of the format, and raise an error if not.
Parameters: | filename : str
|
---|
Write image to files implied by filename string
Parameters: | filename : str
|
---|---|
Returns: | None : |
Delete any cached read of data from proxied data
Remember there are two types of images:
If you call img.get_data() on a proxy image, the result of reading from the proxy gets cached inside the image object, and this cache is what gets returned from the next call to img.get_data(). If you modify the returned data, as in:
data = img.get_data()
data[:] = 42
then the next call to img.get_data() returns the modified array, whether the image is an array image or a proxy image:
assert np.all(img.get_data() == 42)
When you uncache an array image, this has no effect on the return of img.get_data(), but when you uncache a proxy image, the result of img.get_data() returns to its original value.
Harmonize header with image data and affine
>>> data = np.zeros((2,3,4))
>>> affine = np.diag([1.0,2.0,3.0,1.0])
>>> img = SpatialImage(data, affine)
>>> img.shape == (2, 3, 4)
True
>>> img.update_header()
>>> img.header.get_data_shape() == (2, 3, 4)
True
>>> img.header.get_zooms()
(1.0, 2.0, 3.0)
Numpy data types that instance obj supports
Parameters: | obj : object
|
---|---|
Returns: | np_types : set
|