NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.spatialimages.Header

Next topic

nibabel.volumeutils

Reggie -- the one

nibabel.spatialimages.SpatialImage

digraph inheritancee6a1f66a83 { rankdir=LR; ratio=compress; fontsize=14; size="6.0, 8.0"; "SpatialImage" [shape=ellipse,URL="#nibabel.spatialimages.SpatialImage",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,fontsize=14,color=dodgerblue1,style=filled,height=0.75]; }

class nibabel.spatialimages.SpatialImage(data, affine, header=None, extra=None, file_map=None)

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 :

data : array-like

image data. It should be some object that retuns an array from np.asanyarray

affine : None or (4,4) array-like

homogenous affine giving relationship between voxel coordinates and world coordinates. Affine can also be None. In this case, obj.get_affine() also returns None, and the affine as written to disk will depend on the file format.

header : None or mapping or header instance, optional

metadata for this image format

extra : None or mapping, optional

metadata to associate with image that cannot be stored in the metadata of this image type

file_map : mapping, optional

mapping giving file information for this image format

classmethod filespec_to_file_map(klass, filespec)
classmethod filespec_to_files(klass, filespec)
classmethod from_file_map(klass, file_map)
classmethod from_filename(klass, filename)
classmethod from_files(klass, file_map)
classmethod from_filespec(klass, filespec)
classmethod from_image(klass, img)

Class method to create new instance of own class from img

Parameters :

img : spatialimage instance

In fact, an object with the API of spatialimage - specifically get_data, get_affine, get_header and extra.

Returns :

cimg : spatialimage instance

Image, of our own class

get_affine()
get_data()
get_data_dtype()
get_filename()

Fetch the image filename

Parameters :

None :

Returns :

fname : None or str

Returns None if there is no filename, or a filename string. If an image may have several filenames assoctiated with it (e.g Analyze .img, .hdr pair) then we return the more characteristic filename (the .img filename in the case of Analyze’)

get_header()
header_class

alias of Header

classmethod instance_to_filename(klass, img, filename)

Save img in our own format, to name implied by filename

This is a class method

Parameters :

img : spatialimage instance

In fact, an object with the API of spatialimage - specifically get_data, get_affine, get_header and extra.

filename : str

Filename, implying name to which to save image.

classmethod load(klass, filename)
classmethod make_file_map(klass, mapping=None)

Class method to make files holder for this image type

Parameters :

mapping : None or mapping, optional

mapping with keys corresponding to image file types (such as ‘image’, ‘header’ etc, depending on image class) and values that are filenames or file-like. Default is None

Returns :

file_map : dict

dict with string keys given by first entry in tuples in sequence klass.files_types, and values of type FileHolder, where FileHolder objects have default values, other than those given by mapping

set_data_dtype(dtype)
set_filename(filename)

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

If the image format only has one file associated with it, this will be the only filename set into the image .file_map attribute. Otherwise, the image instance will try and guess the other filenames from this given filename.

shape
to_file_map(file_map=None)
to_filename(filename)

Write image to files implied by filename string

Parameters :

filename : str

filename to which to save image. We will parse filename with filespec_to_file_map to work out names for image, header etc.

Returns :

None :

to_files(file_map=None)
to_filespec(filename)
update_header()

Update header from information in image