NiBabel

Access a cacophony of neuro-imaging file formats

Table Of Contents

Previous topic

nifti1

Next topic

ecat

Reggie -- the one

nifti2

Read / write access to NIfTI2 image format

Format described here:

Stuff about the CIFTI file format here:

Nifti2Header([binaryblock, endianness, ...]) Class for NIfTI2 header
Nifti2Image(dataobj, affine[, header, ...]) Class for single file NIfTI2 format image
Nifti2Pair(dataobj, affine[, header, extra, ...]) Class for NIfTI2 format image, header pair
Nifti2PairHeader([binaryblock, endianness, ...]) Class for NIfTI2 pair header
load(filename) Load NIfTI2 single or pair image from filename
save(img, filename) Save NIfTI2 single or pair to filename

Nifti2Header

class nibabel.nifti2.Nifti2Header(binaryblock=None, endianness=None, check=True, extensions=())

Bases: nibabel.nifti1.Nifti1Header

Class for NIfTI2 header

NIfTI2 is a slightly simplified variant of NIfTI1 which replaces 32-bit floats with 64-bit floats, and increases some integer widths to 32 or 64 bits.

Initialize header from binary data block and extensions

__init__(binaryblock=None, endianness=None, check=True, extensions=())

Initialize header from binary data block and extensions

classmethod default_structarr(klass, endianness=None)

Create empty header binary block with given endianness

get_data_shape()

Get shape of data

Notes

Does not use Nifti1 freesurfer hack for large vectors described in Nifti1Header.set_data_shape()

Examples

>>> hdr = Nifti2Header()
>>> hdr.get_data_shape()
(0,)
>>> hdr.set_data_shape((1,2,3))
>>> hdr.get_data_shape()
(1, 2, 3)

Expanding number of dimensions gets default zooms

>>> hdr.get_zooms()
(1.0, 1.0, 1.0)
pair_magic = 'ni2'
pair_vox_offset = 0
quaternion_threshold = -6.6613381477509392e-16
set_data_shape(shape)

Set shape of data

If ndims == len(shape) then we set zooms for dimensions higher than ndims to 1.0

Parameters:

shape : sequence

sequence of integers specifying data array shape

Notes

Does not apply nifti1 Freesurfer hack for long vectors (see Nifti1Header.set_data_shape())

single_magic = 'n+2'
single_vox_offset = 544
sizeof_hdr = 540
template_dtype = dtype([('sizeof_hdr', '<i4'), ('magic', 'S4'), ('eol_check', 'i1', (4,)), ('datatype', '<i2'), ('bitpix', '<i2'), ('dim', '<i8', (8,)), ('intent_p1', '<f8'), ('intent_p2', '<f8'), ('intent_p3', '<f8'), ('pixdim', '<f8', (8,)), ('vox_offset', '<i8'), ('scl_slope', '<f8'), ('scl_inter', '<f8'), ('cal_max', '<f8'), ('cal_min', '<f8'), ('slice_duration', '<f8'), ('toffset', '<f8'), ('slice_start', '<i8'), ('slice_end', '<i8'), ('descrip', 'S80'), ('aux_file', 'S24'), ('qform_code', '<i4'), ('sform_code', '<i4'), ('quatern_b', '<f8'), ('quatern_c', '<f8'), ('quatern_d', '<f8'), ('qoffset_x', '<f8'), ('qoffset_y', '<f8'), ('qoffset_z', '<f8'), ('srow_x', '<f8', (4,)), ('srow_y', '<f8', (4,)), ('srow_z', '<f8', (4,)), ('slice_code', '<i4'), ('xyzt_units', '<i4'), ('intent_code', '<i4'), ('intent_name', 'S16'), ('dim_info', 'u1'), ('unused_str', 'S15')])

Nifti2Image

class nibabel.nifti2.Nifti2Image(dataobj, affine, header=None, extra=None, file_map=None)

Bases: nibabel.nifti1.Nifti1Image

Class for single file NIfTI2 format image

__init__(dataobj, affine, header=None, extra=None, file_map=None)
header_class

alias of Nifti2Header

Nifti2Pair

class nibabel.nifti2.Nifti2Pair(dataobj, affine, header=None, extra=None, file_map=None)

Bases: nibabel.nifti1.Nifti1Pair

Class for NIfTI2 format image, header pair

__init__(dataobj, affine, header=None, extra=None, file_map=None)
header_class

alias of Nifti2PairHeader

Nifti2PairHeader

class nibabel.nifti2.Nifti2PairHeader(binaryblock=None, endianness=None, check=True, extensions=())

Bases: nibabel.nifti2.Nifti2Header

Class for NIfTI2 pair header

Initialize header from binary data block and extensions

__init__(binaryblock=None, endianness=None, check=True, extensions=())

Initialize header from binary data block and extensions

is_single = False

load

nibabel.nifti2.load(filename)

Load NIfTI2 single or pair image from filename

Parameters:

filename : str

filename of image to be loaded

Returns:

img : Nifti2Image or Nifti2Pair

nifti2 single or pair image instance

Raises:

ImageFileError :

if filename doesn’t look like nifti2;

IOError :

if filename does not exist.

save

nibabel.nifti2.save(img, filename)

Save NIfTI2 single or pair to filename

Parameters:

filename : str

filename to which to save image