Inheritance diagram for nipy.io.imageformats.spm2analyze:
Header reading functions for SPM2 version of analyze format
Bases: nipy.io.imageformats.spm99analyze.Spm99AnalyzeHeader
SPM2 header; adds possibility of reading, but not writing DC offset for data
Methods
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
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
Adapt header to separate or same image and header file
This is a rare and exotic case for Analyze files, common for Nifti1. For Analyze, we only need to check that, if the file is single, then the data offset is large enough to leave room for the header.
Parameters : | is_pair : bool, optional
|
---|---|
Returns : | hdr : header
|
Examples
The header starts off as being for two files
>>> hdr = AnalyzeHeader()
>>> hdr.get_data_offset()
0
This is the same as the default behavior for this method
>>> pair_hdr = hdr.for_file_pair()
>>> pair_hdr.get_data_offset()
0
But we can switch it to be for one
>>> unpair_hdr = hdr.for_file_pair(False)
>>> unpair_hdr.get_data_offset()
352
The original header is not affected (a copy is returned)
>>> hdr.get_data_offset()
0
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
Initialize header from mapping
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.]])
>>> hdr.set_data_shape((3, 5))
>>> hdr.get_base_affine()
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -0.],
[ 0., 0., 0., 1.]])
>>> hdr.set_data_shape((3, 5, 7))
>>> 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 header, using SPM origin field if sensible
The default translations are got from the origin field, if set, or from the center of the image otherwise.
Examples
>>> hdr = Spm99AnalyzeHeader()
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.set_zooms((3, 2, 1))
>>> hdr.default_x_flip
True
>>> hdr.get_origin_affine() # from center of image
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
>>> hdr['origin'][:3] = [3,4,5]
>>> hdr.get_origin_affine() # using origin
array([[-3., 0., 0., 6.],
[ 0., 2., 0., -6.],
[ 0., 0., 1., -4.],
[ 0., 0., 0., 1.]])
>>> hdr['origin'] = 0 # unset origin
>>> hdr.set_data_shape((3, 5))
>>> hdr.get_origin_affine()
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -0.],
[ 0., 0., 0., 1.]])
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.get_origin_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)
Return representation of datatype code
This method returns the datatype code, or a string label for the code. Usually you are more interested in the data dtype. To do that more useful thing, use get_data_dtype
Parameters : | code_repr : string
|
---|---|
Returns : | datatype_code : string or integer
|
Examples
>>> hdr = AnalyzeHeader()
>>> hdr['datatype'] = 4 # int16
>>> hdr.get_datatype()
'int16'
Get affine from header, using SPM origin field if sensible
The default translations are got from the origin field, if set, or from the center of the image otherwise.
Examples
>>> hdr = Spm99AnalyzeHeader()
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.set_zooms((3, 2, 1))
>>> hdr.default_x_flip
True
>>> hdr.get_origin_affine() # from center of image
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
>>> hdr['origin'][:3] = [3,4,5]
>>> hdr.get_origin_affine() # using origin
array([[-3., 0., 0., 6.],
[ 0., 2., 0., -6.],
[ 0., 0., 1., -4.],
[ 0., 0., 0., 1.]])
>>> hdr['origin'] = 0 # unset origin
>>> hdr.set_data_shape((3, 5))
>>> hdr.get_origin_affine()
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -0.],
[ 0., 0., 0., 1.]])
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.get_origin_affine() # from center of image
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
Get data scaling (slope) and offset (intercept) from header data
Uses the algorithm from SPM2 spm_vol_ana.m by John Ashburner
Parameters : | self : header
|
---|---|
Returns : | scl_slope : None or float
scl_inter : None or float
|
Examples
>>> fields = {'scl_slope':1,'scl_inter':0,'glmax':0,'glmin':0,'cal_max':0, 'cal_min':0}
>>> hdr = Spm2AnalyzeHeader()
>>> for key, value in fields.items():
... hdr[key] = value
>>> hdr.get_slope_inter()
(1.0, 0.0)
>>> hdr['scl_inter'] = 0.5
>>> hdr.get_slope_inter()
(1.0, 0.5)
>>> hdr['scl_inter'] = np.nan
>>> hdr.get_slope_inter()
(1.0, 0.0)
If ‘scl_slope’ is 0, nan or inf, cannot use ‘scl_slope’. Without valid information in the gl / cal fields, we cannot get scaling, and return None
>>> hdr['scl_slope'] = 0
>>> hdr.get_slope_inter()
(None, None)
>>> hdr['scl_slope'] = np.nan
>>> hdr.get_slope_inter()
(None, None)
Valid information in the gl AND cal fields are needed
>>> hdr['cal_max'] = 0.8
>>> hdr['cal_min'] = 0.2
>>> hdr.get_slope_inter()
(None, None)
>>> hdr['glmax'] = 110
>>> hdr['glmin'] = 10
>>> np.allclose(hdr.get_slope_inter(), [0.6/100, 0.2-0.6/100*10])
True
Get zooms from header
Returns : | z : tuple
|
---|
Examples
>>> hdr = AnalyzeHeader()
>>> hdr.get_zooms()
()
>>> 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
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
Set SPM origin to header from affine matrix.
The origin field was read but not written by SPM99 and 2. It was used for storing a central voxel coordinate, that could be used in aligning the image to some standard position - a proxy for a full translation vector that was usually stored in a separate matlab .mat file.
Nifti uses the space occupied by the SPM origin field for important other information (the transform codes), so writing the origin will make the header a confusing Nifti file. If you work with both Analyze and Nifti, you should probably avoid doing this.
Parameters : | affine : array-like, shape (4,4)
|
---|---|
Returns : | None : |
Examples
>>> hdr = Spm99AnalyzeHeader()
>>> hdr.set_data_shape((3, 5, 7))
>>> hdr.set_zooms((3,2,1))
>>> hdr.get_origin_affine()
array([[-3., 0., 0., 3.],
[ 0., 2., 0., -4.],
[ 0., 0., 1., -3.],
[ 0., 0., 0., 1.]])
>>> affine = np.diag([3,2,1,1])
>>> affine[:3,3] = [-6, -6, -4]
>>> hdr.set_origin_from_affine(affine)
>>> np.all(hdr['origin'][:3] == [3,4,5])
True
>>> hdr.get_origin_affine()
array([[-3., 0., 0., 6.],
[ 0., 2., 0., -6.],
[ 0., 0., 1., -4.],
[ 0., 0., 0., 1.]])
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
Bases: nipy.io.imageformats.spm99analyze.Spm99AnalyzeImage
Create new instance of own class from img
This is a class method
Parameters : | img : spatialimage instance
|
---|---|
Returns : | cimg : spatialimage instance
|
Lazy load of data
Return header
Update header to match data, affine etc in object
Return image data without image scaling applied
Summary: please use the get_data method instead of this method unless you are sure what you are doing, and that you will only be using image formats for which this method exists and returns sensible results.
Use this method with care; the modified Analyze-type formats such as SPM formats, and nifti1, specify that the image data array, as they are expecting to return it, is given by the raw data on disk, multiplied by a scalefactor and maybe with the addition of a constant. This method returns the data on the disk, without these format-specific scalings applied. Please use this method only if you absolutely need the unscaled data, and the magnitude of the data, as given by the scalefactor, is not relevant to your application. The Analyze-type formats have a single scalefactor +/- offset per image on disk. If you do not care about the absolute values, and will be removing the mean from the data, then the unscaled values will have preserved intensity ratios compared to the mean-centered scaled data. However, this is not necessarily true of other formats with more complicated scaling - such as MINC.
Note that - unlike the scaled get_data method, we do not cache the array, to minimize the memory taken by the object.
Save img in our own format, to name implied by filename
This is a class method
Parameters : | img : spatialimage instance
filename : str
|
---|
Write image to files implied by filename string
Returns : | None : |
---|