NIPY logo

Site Navigation

NIPY Community

Table Of Contents

Previous topic

io.imageformats.quaternions

Next topic

io.imageformats.spm2analyze

This Page

io.imageformats.spatialimages

Module: io.imageformats.spatialimages

Inheritance diagram for nipy.io.imageformats.spatialimages:

Very 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 real 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()
  • .get_header()
  • .get_shape()
  • .set_shape(shape)
  • .to_filename(fname) - writes data to filename(s) derived from fname, where the derivation may differ between formats.
  • to_files() - save image to files with which the image is already associated. Or img.to_files(files) saves to the files passed.

classmethods:

  • from_filename(fname) - make instance by loading from filename
  • instance_to_filename(img, fname) - save img instance to filename fname.

There are several ways of writing data.

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 a rather secretive way. In order to just save an image, for which you know there is an associated filename, or other storage, you can do:

img.to_files()

alternatively, you can pass in the needed files yourself, into this method, as an argument.

You can get the data out again with of:

img.get_data(fileobj)

Less commonly, for some image types that support it, you might want to fetch out the unscaled array via the header:

unscaled_data = img.get_unscaled_data()

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.get_header()
hdr.set_data_dtype(data.dtype)
img.to_filename(fname)

SpatialImage

class nipy.io.imageformats.spatialimages.SpatialImage(data, affine, header=None, extra=None)

Bases: object

__init__(data, affine, header=None, extra=None)
static filespec_to_files(filespec)
classmethod from_filename(klass, filename)
from_files(klass, files)
classmethod from_filespec(klass, img, filespec)
classmethod from_image(klass, img)

Create new instance of own class from img

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.

Returns :

cimg : spatialimage instance

Image, of our own class

get_affine()
get_data()
get_data_dtype()
get_header()
get_shape()
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 save(klass, img, filename)
set_data_dtype(dtype)
to_filename(filename)

Write image to files implied by filename string

Returns :None :
to_files(files=None)
to_filespec(filename)