# NiBabel

### Access a cacophony of neuro-imaging file formats

# `nifti2`

Read / write access to NIfTI2 image format

Format described here:

> https://www.nitrc.org/forum/message.php?msg_id=3738

`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: `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(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)

  classmethod may_contain_header(binaryblock)
  pair_magic = b'ni2'
  pair_vox_offset = 0
  quaternion_threshold: floating = -6.661338147750939e-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 = b'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, dtype=None)

  Bases: `Nifti1Image`

  Class for single file NIfTI2 format image

  Initialize image

  The image is a combination of (array-like, affine matrix, header), with optional metadata in extra, and filename / file-like objects contained in the file_map mapping.

  Parameters:
    dataobj object

      Object containing image data. It should be some object that returns an array from `np.asanyarray`. It should have a `shape` attribute or property

    affine None or (4,4) array-like

      homogeneous affine giving relationship between voxel coordinates and world coordinates. Affine can also be None. In this case, `obj.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

  Notes

  If both a header and an affine are specified, and the affine does not match the affine that is in the header, the affine will be used, but the `sform_code` and `qform_code` fields in the header will be re-initialised to their default values. This is performed on the basis that, if you are changing the affine, you are likely to be changing the space to which the affine is pointing. The `set_sform()` and `set_qform()` methods can be used to update the codes after an image has been created - see those methods, and the manual for more details.

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

    Initialize image

    The image is a combination of (array-like, affine matrix, header), with optional metadata in extra, and filename / file-like objects contained in the file_map mapping.

    Parameters:
      dataobj object

        Object containing image data. It should be some object that returns an array from `np.asanyarray`. It should have a `shape` attribute or property

      affine None or (4,4) array-like

        homogeneous affine giving relationship between voxel coordinates and world coordinates. Affine can also be None. In this case, `obj.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

    Notes

    If both a header and an affine are specified, and the affine does not match the affine that is in the header, the affine will be used, but the `sform_code` and `qform_code` fields in the header will be re-initialised to their default values. This is performed on the basis that, if you are changing the affine, you are likely to be changing the space to which the affine is pointing. The `set_sform()` and `set_qform()` methods can be used to update the codes after an image has been created - see those methods, and the manual for more details.

  header_class

    alias of `Nifti2Header`

## `Nifti2Pair`

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

  Bases: `Nifti1Pair`

  Class for NIfTI2 format image, header pair

  Initialize image

  The image is a combination of (array-like, affine matrix, header), with optional metadata in extra, and filename / file-like objects contained in the file_map mapping.

  Parameters:
    dataobj object

      Object containing image data. It should be some object that returns an array from `np.asanyarray`. It should have a `shape` attribute or property

    affine None or (4,4) array-like

      homogeneous affine giving relationship between voxel coordinates and world coordinates. Affine can also be None. In this case, `obj.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

  Notes

  If both a header and an affine are specified, and the affine does not match the affine that is in the header, the affine will be used, but the `sform_code` and `qform_code` fields in the header will be re-initialised to their default values. This is performed on the basis that, if you are changing the affine, you are likely to be changing the space to which the affine is pointing. The `set_sform()` and `set_qform()` methods can be used to update the codes after an image has been created - see those methods, and the manual for more details.

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

    Initialize image

    The image is a combination of (array-like, affine matrix, header), with optional metadata in extra, and filename / file-like objects contained in the file_map mapping.

    Parameters:
      dataobj object

        Object containing image data. It should be some object that returns an array from `np.asanyarray`. It should have a `shape` attribute or property

      affine None or (4,4) array-like

        homogeneous affine giving relationship between voxel coordinates and world coordinates. Affine can also be None. In this case, `obj.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

    Notes

    If both a header and an affine are specified, and the affine does not match the affine that is in the header, the affine will be used, but the `sform_code` and `qform_code` fields in the header will be re-initialised to their default values. This is performed on the basis that, if you are changing the affine, you are likely to be changing the space to which the affine is pointing. The `set_sform()` and `set_qform()` methods can be used to update the codes after an image has been created - see those methods, and the manual for more details.

  header_class

    alias of `Nifti2PairHeader`

## `Nifti2PairHeader`

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

  Bases: `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;

    OSError

      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