digraph inheritance2c00aae88a { rankdir=LR; ratio=compress; fontsize=14; size="6.0, 8.0"; "AnalyzeHeader" [shape=ellipse,URL="#nibabel.analyze.AnalyzeHeader",fontname=Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans,fontsize=14,color=dodgerblue1,style=filled,height=0.75]; }
Class for basic analyze header
Implements zoom-only setting of affine transform, and no image scaling
Initialize header from binary data block
Parameters : | binaryblock : {None, string} optional
endianness : {None, ‘<’,’>’, other endian code} string, optional
check : bool, optional
|
---|
Examples
>>> hdr1 = AnalyzeHeader() # an empty header
>>> hdr1.endianness == native_code
True
>>> hdr1.get_data_shape()
(0,)
>>> hdr1.set_data_shape((1,2,3)) # now with some content
>>> hdr1.get_data_shape()
(1, 2, 3)
We can set the binary block directly via this initialization. Here we get it from the header we have just made
>>> binblock2 = hdr1.binaryblock
>>> hdr2 = AnalyzeHeader(binblock2)
>>> hdr2.get_data_shape()
(1, 2, 3)
Empty headers are native endian by default
>>> hdr2.endianness == native_code
True
You can pass valid opposite endian headers with the endianness parameter. Even empty headers can have endianness
>>> hdr3 = AnalyzeHeader(endianness=swapped_code)
>>> hdr3.endianness == swapped_code
True
If you do not pass an endianness, and you pass some data, we will try to guess from the passed data.
>>> binblock3 = hdr3.binaryblock
>>> hdr4 = AnalyzeHeader(binblock3)
>>> hdr4.endianness == swapped_code
True
return new byteswapped header object with given endianness
Guaranteed to make a copy even if endianness is the same as the current endianness.
Parameters : | endianness : None or string, optional
|
---|---|
Returns : | hdr : header object
|
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.endianness == native_code
True
>>> bs_hdr = hdr.as_byteswapped()
>>> bs_hdr.endianness == swapped_code
True
>>> bs_hdr = hdr.as_byteswapped(swapped_code)
>>> bs_hdr.endianness == swapped_code
True
>>> bs_hdr is hdr
False
>>> bs_hdr == hdr
True
If you write to the resulting byteswapped data, it does not change the original.
>>> bs_hdr['dim'][1] = 2
>>> bs_hdr == hdr
False
If you swap to the same endianness, it returns a copy
>>> nbs_hdr = hdr.as_byteswapped(native_code)
>>> nbs_hdr.endianness == native_code
True
>>> nbs_hdr is hdr
False
binary block of data as string
Returns : | binaryblock : string
|
---|
Examples
>>> # Make default empty header
>>> hdr = AnalyzeHeader()
>>> len(hdr.binaryblock)
348
Check header data with checks
Return copy of header
>>> hdr = AnalyzeHeader()
>>> hdr['dim'][0]
0
>>> hdr['dim'][0] = 2
>>> hdr2 = hdr.copy()
>>> hdr2 is hdr
False
>>> hdr['dim'][0] = 3
>>> hdr2['dim'][0]
2
Read scaled data array from fileobj
Parameters : | fileobj : file-like
|
---|---|
Returns : | arr : ndarray
|
Write data to fileobj, maybe modifying self
In writing the data, we match the header to the written data, by setting the header scaling factors. Thus we modify self in the process of writing the data.
Parameters : | data : array-like
fileobj : file-like object
|
---|
Examples
>>> from nibabel.analyze import AnalyzeHeader
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_shape((1, 2, 3))
>>> hdr.set_data_dtype(np.float64)
>>> from StringIO import StringIO
>>> str_io = StringIO()
>>> data = np.arange(6).reshape(1,2,3)
>>> hdr.data_to_fileobj(data, str_io)
>>> data.astype(np.float64).tostring('F') == str_io.getvalue()
True
Run checks over header binary data, return string
endian code of binary data
The endianness code gives the current byte order interpretation of the binary data.
Notes
Endianness gives endian interpretation of binary data. It is read only because the only common use case is to set the endianness on initialization, or occasionally byteswapping the data - but this is done via the as_byteswapped method
Examples
>>> hdr = AnalyzeHeader()
>>> code = hdr.endianness
>>> code == native_code
True
Return read header with given or guessed endiancode
Parameters : | fileobj : file-like object
endianness : None or endian code, optional
|
---|---|
Returns : | hdr : AnalyzeHeader object
|
Examples
>>> import StringIO
>>> hdr = AnalyzeHeader()
>>> fileobj = StringIO.StringIO(hdr.binaryblock)
>>> fileobj.seek(0)
>>> hdr2 = AnalyzeHeader.from_fileobj(fileobj)
>>> hdr2.binaryblock == hdr.binaryblock
True
You can write to the resulting object data
>>> hdr2['dim'][1] = 1
Class method to create header from another header
Parameters : | header : Header instance or mapping
check : {True, False}
|
---|---|
Returns : | hdr : header instance
|
Get affine from basic (shared) header fields
Note that we get the translations from the center of the image.
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.set_zooms((3, 2, 1))
>>> hdr.default_x_flip
True
>>> hdr.get_base_affine() # from center of image
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
Get affine from basic (shared) header fields
Note that we get the translations from the center of the image.
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.set_zooms((3, 2, 1))
>>> hdr.default_x_flip
True
>>> hdr.get_base_affine() # from center of image
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
Get numpy dtype for data
For examples see set_data_dtype
Return offset into data file to read data
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.get_data_offset()
0
>>> hdr['vox_offset'] = 12
>>> hdr.get_data_offset()
12
Get shape of data
Examples
>>> hdr = AnalyzeHeader()
>>> 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)
Get scalefactor and intercept
These are not implemented for basic Analyze
Returns label for coded field
A coded field is an int field containing codes that stand for discrete values that also have string labels.
Parameters : | fieldname : str
|
---|---|
Returns : | label : str
|
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.get_value_label('datatype')
'float32'
Get zooms from header
Returns : | z : tuple
|
---|
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.get_zooms()
(1.0,)
>>> hdr.set_data_shape((1,2))
>>> hdr.get_zooms()
(1.0, 1.0)
>>> hdr.set_zooms((3, 4))
>>> hdr.get_zooms()
(3.0, 4.0)
Return items from header data
Return keys from header data
Read unscaled data array from fileobj
Parameters : | fileobj : file-like
|
---|---|
Returns : | arr : ndarray
|
Calculate slope, intercept, min, max from data given header
Check that the data can be sensibly adapted to this header data dtype. If the header type does support useful scaling to allow this, raise a HeaderTypeError.
Parameters : | data : array-like
|
---|---|
Returns : | divslope : None or scalar
intercept : None or scalar
mn : None or scalar
mx : None or scalar
|
Set numpy dtype for data from code or dtype or type
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_dtype(np.uint8)
>>> hdr.get_data_dtype()
dtype('uint8')
>>> hdr.set_data_dtype(np.dtype(np.uint8))
>>> hdr.get_data_dtype()
dtype('uint8')
>>> hdr.set_data_dtype('implausible')
Traceback (most recent call last):
...
HeaderDataError: data dtype "implausible" not recognized
>>> hdr.set_data_dtype('none')
Traceback (most recent call last):
...
HeaderDataError: data dtype "none" known but not supported
>>> hdr.set_data_dtype(np.void)
Traceback (most recent call last):
...
HeaderDataError: data dtype "<type 'numpy.void'>" known but not supported
Set shape of data
If ndims == len(shape) then we set zooms for dimensions higher than ndims to 1.0
Parameters : | shape : sequence
|
---|
Set slope and / or intercept into header
Set slope and intercept for image data, such that, if the image data is arr, then the scaled image data will be (arr * slope) + inter
Note that trying to set not-default values raises error for Analyze header - which cannot contain slope or intercept terms.
Parameters : | slope : None or float
inter : float, optional
|
---|
Set zooms into header fields
See docstring for get_zooms for examples
header data, with data fields
Examples
>>> hdr1 = AnalyzeHeader() # an empty header
>>> sz = hdr1.structarr['sizeof_hdr']
>>> hdr1.structarr = None
Traceback (most recent call last):
...
AttributeError: can't set attribute
Return values from header data
Write header to fileobj
Write starts at fileobj current file position.
Parameters : | fileobj : file-like object
|
---|---|
Returns : | None : |
Examples
>>> hdr = AnalyzeHeader()
>>> import StringIO
>>> str_io = StringIO.StringIO()
>>> hdr.write_to(str_io)
>>> hdr.binaryblock == str_io.getvalue()
True