NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.spm2analyze.Spm2AnalyzeImage

Next topic

nibabel.spm2analyze.Spm2AnalyzeImage

Reggie -- the one

nibabel.spm2analyze.Spm2AnalyzeHeader

digraph inheritance5791bfee87 { rankdir=LR; ratio=compress; fontsize=14; size="6.0, 8.0"; "AnalyzeHeader" [shape=ellipse,URL="nibabel.analyze.AnalyzeHeader.html#nibabel.analyze.AnalyzeHeader",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=14,color=dodgerblue1,style=filled,tooltip="Class for basic analyze header",height=0.75]; "WrapStruct" -> "AnalyzeHeader" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Spm2AnalyzeHeader" [shape=ellipse,URL="#nibabel.spm2analyze.Spm2AnalyzeHeader",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=14,color=dodgerblue1,style=filled,tooltip="SPM2 header; adds possibility of reading, but not writing DC",height=0.75]; "Spm99AnalyzeHeader" -> "Spm2AnalyzeHeader" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Spm99AnalyzeHeader" [shape=ellipse,URL="nibabel.spm99analyze.Spm99AnalyzeHeader.html#nibabel.spm99analyze.Spm99AnalyzeHeader",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=14,color=dodgerblue1,style=filled,tooltip="Adds origin functionality to base SPM header",height=0.75]; "SpmAnalyzeHeader" -> "Spm99AnalyzeHeader" [arrowsize=0.5,style="setlinewidth(0.5)"]; "SpmAnalyzeHeader" [shape=ellipse,URL="nibabel.spm99analyze.SpmAnalyzeHeader.html#nibabel.spm99analyze.SpmAnalyzeHeader",fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=14,color=dodgerblue1,style=filled,tooltip="Basic scaling Spm Analyze header",height=0.75]; "AnalyzeHeader" -> "SpmAnalyzeHeader" [arrowsize=0.5,style="setlinewidth(0.5)"]; "WrapStruct" [shape=ellipse,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=14,color=dodgerblue1,style=filled,height=0.75]; }

class nibabel.spm2analyze.Spm2AnalyzeHeader(binaryblock=None, endianness=None, check=True)

SPM2 header; adds possibility of reading, but not writing DC offset for data

Initialize header from binary data block

Parameters:

binaryblock : {None, string} optional

binary block to set into header. By default, None, in which case we insert the default empty header block

endianness : {None, ‘<’,’>’, other endian code} string, optional

endianness of the binaryblock. If None, guess endianness from the data.

check : bool, optional

Whether to check content of header in initialization. Default is True.

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
get_slope_inter()

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

Mapping with fields: * scl_slope - slope * scl_inter - possible intercept (SPM2 use - shared by nifti) * glmax - the (recorded) maximum value in the data (unscaled) * glmin - recorded minimum unscaled value * cal_max - the calibrated (scaled) maximum value in the dataset * cal_min - ditto minimum value

Returns:

scl_slope : None or float

scaling (slope). None if there is no valid scaling from these fields

scl_inter : None or float

offset (intercept). Also None if there is no valid scaling, offset

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
template_dtype = dtype([('sizeof_hdr', '<i4'), ('data_type', 'S10'), ('db_name', 'S18'), ('extents', '<i4'), ('session_error', '<i2'), ('regular', 'S1'), ('hkey_un0', 'S1'), ('dim', '<i2', (8,)), ('vox_units', 'S4'), ('cal_units', 'S8'), ('unused1', '<i2'), ('datatype', '<i2'), ('bitpix', '<i2'), ('dim_un0', '<i2'), ('pixdim', '<f4', (8,)), ('vox_offset', '<f4'), ('scl_slope', '<f4'), ('scl_inter', '<f4'), ('funused3', '<f4'), ('cal_max', '<f4'), ('cal_min', '<f4'), ('compressed', '<i4'), ('verified', '<i4'), ('glmax', '<i4'), ('glmin', '<i4'), ('descrip', 'S80'), ('aux_file', 'S24'), ('orient', 'S1'), ('origin', '<i2', (5,)), ('generated', 'S10'), ('scannum', 'S10'), ('patient_id', 'S10'), ('exp_date', 'S10'), ('exp_time', 'S10'), ('hist_un0', 'S3'), ('views', '<i4'), ('vols_added', '<i4'), ('start_field', '<i4'), ('field_skip', '<i4'), ('omax', '<i4'), ('omin', '<i4'), ('smax', '<i4'), ('smin', '<i4')])