Package nifti :: Module utils
[hide private]
[frames] | no frames]

Module utils

source code

Utility functions for PyNifti
Functions [hide private]
 
time2vol(t, tr, lag=0.0, decimals=0)
Translates a time 't' into a volume number.
source code
 
applyFxToVolumes(ts, vols, fx, **kwargs)
Apply a function on selected volumes of a timeseries.
source code
 
getPeristimulusTimeseries(ts, onsetvols, nvols=10, fx=<function mean at 0x847d80c>)
Returns 4d array with peristimulus timeseries.
source code
 
Ndtype2niftidtype(array)
Return the NIfTI datatype id for a corresponding NumPy datatype.
source code
call graph 
 
nhdr2dict(nhdr)
Convert a NIfTI header struct into a python dictionary.
source code
call graph 
 
updateNiftiHeaderFromDict(nhdr, hdrdict)
Update a NIfTI header struct with data from a dictionary.
source code
call graph 
 
splitFilename(filename)
Split a NIfTI filename into basename and extension.
source code
call graph 
Variables [hide private]
  filetypes = ['ANALYZE', 'NIFTI', 'NIFTI_PAIR', 'ANALYZE_GZ', '...
Typecodes of all supported NIfTI image formats.
  N2nifti_dtype_map = {<type 'numpy.int8'>: 256, <type 'numpy.in...
Mapping of NumPy datatypes to NIfTI datatypes.
  nifti2numpy_dtype_map = {2: 'u1', 4: 'i2', 8: 'i4', 16: 'f4', ...
Mapping of NIfTI to NumPy datatypes (necessary for handling memory mapped array with proper byte-order handling.
  nifti_xform_map = {'aligned': 2, 'mni152': 4, 'scanner': 1, 't...

Imports: N, nifticlib


Function Details [hide private]

time2vol(t, tr, lag=0.0, decimals=0)

source code 

Translates a time 't' into a volume number. By default function returns the volume number that is closest in time. Volumes are assumed to be recorded exactly (and completely) after tr/2, e.g. if 'tr' is 2 secs the first volume is recorded at exactly one second.

't' might be a single value, a sequence or an array.

The repetition 'tr' might be specified directly, but can also be a NiftiImage object. In the latter case the value of 'tr' is determined from the 'rtime' property of the NiftiImage object.

't' and 'tr' can be given in an arbitrary unit (but both have to be in the same unit).

The 'lag' argument can be used to shift the times by constant offset.

Please note that numpy.round() is used to round to interger value (rounds to even numbers). The 'decimals' argument will be passed to numpy.round().

applyFxToVolumes(ts, vols, fx, **kwargs)

source code 

Apply a function on selected volumes of a timeseries.

'ts' is a 4d timeseries. It can be a NiftiImage or a ndarray. In case of a ndarray one has to make sure that the time is on the first axis. 'ts' can actually be of any dimensionality, but datasets aka volumes are assumed to be along the first axis.

'vols' is either a sequence of sequences or a 2d array indicating which volumes fx should be applied to. Each row defines a set of volumes.

'fx' is a callable function to get an array of the selected volumes as argument. Additonal arguments may be specified as keyword arguments and are passed to 'fx'.

The output will be a 4d array with one computed volume per row in the 'vols' array.

getPeristimulusTimeseries(ts, onsetvols, nvols=10, fx=<function mean at 0x847d80c>)

source code 
Returns 4d array with peristimulus timeseries.

Parameters:
    ts        - source 4d timeseries
    onsetvols - sequence of onsetvolumes to be averaged over
    nvols     - length of the peristimulus timeseries in volumes
                (starting from onsetvol)
    fx        - function to be applied to the list of corresponding
                volumes. Typically this will be mean(), so it is default,
                but it could also be var() or something different. The
                supplied function is to be able to handle an 'axis=0'
                argument similiar to NumPy's mean(), var(), ...

nhdr2dict(nhdr)

source code 
call graph 

Convert a NIfTI header struct into a python dictionary.

While most elements of the header struct will be translated 1:1 some (e.g. sform matrix) are converted into more convenient datatypes (i.e. 4x4 matrix instead of 16 separate values).

Returns:
dict

Parameters: nhdr: nifti_1_header

updateNiftiHeaderFromDict(nhdr, hdrdict)

source code 
call graph 

Update a NIfTI header struct with data from a dictionary.

The supplied dictionary might contain additonal data elements that do not match any nifti header element. These are silently ignored.

Several checks are performed to ensure validity of the resulting nifti header struct. If any check fails a ValueError exception will be thrown. However, some tests are still missing.

Parameters:
  • nhdr, nifti_1_header - To be updated NIfTI header struct (in-place update).
  • hdrdict, dict - Dictionary containing information intented to be merged into the NIfTI header struct.

splitFilename(filename)

source code 
call graph 
Split a NIfTI filename into basename and extension.
Parameters:
  • filename, str - Filename to be split.
Returns:
The function returns a tuple of basename and extension. If no valid NIfTI filename extension is found, the whole string is returned as basename and the extension string will be empty.

Variables Details [hide private]

filetypes

Typecodes of all supported NIfTI image formats.
Value:
['ANALYZE',
 'NIFTI',
 'NIFTI_PAIR',
 'ANALYZE_GZ',
 'NIFTI_GZ',
 'NIFTI_PAIR_GZ']

N2nifti_dtype_map

Mapping of NumPy datatypes to NIfTI datatypes.
Value:
{<type 'numpy.int8'>: 256,
 <type 'numpy.int16'>: 4,
 <type 'numpy.int32'>: 8,
 <type 'numpy.int64'>: 1024,
 <type 'numpy.uint8'>: 2,
 <type 'numpy.uint16'>: 512,
 <type 'numpy.uint32'>: 768,
 <type 'numpy.uint64'>: 1280,
...

nifti2numpy_dtype_map

Mapping of NIfTI to NumPy datatypes (necessary for handling memory mapped array with proper byte-order handling.
Value:
{2: 'u1',
 4: 'i2',
 8: 'i4',
 16: 'f4',
 64: 'f8',
 256: 'i1',
 512: 'u2',
 768: 'u4',
...

nifti_xform_map

Value:
{'aligned': 2,
 'mni152': 4,
 'scanner': 1,
 'talairach': 3,
 'unknown': 0}