Package mvpa :: Package datasets :: Module niftidataset
[hide private]
[frames] | no frames]

Source Code for Module mvpa.datasets.niftidataset

  1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  #ex: set sts=4 ts=4 sw=4 et: 
  3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
  4  # 
  5  #   See COPYING file distributed along with the PyMVPA package for the 
  6  #   copyright and license terms. 
  7  # 
  8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
  9  """Dataset that gets its samples from a NIfTI file""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  from nifti import NiftiImage 
 14   
 15  from mvpa.datasets.maskeddataset import MaskedDataset 
 16  from mvpa.datasets.metric import DescreteMetric, cartesianDistance 
 17  from mvpa.misc import warning 
 18   
19 -class NiftiDataset(MaskedDataset):
20 """Dataset based on NiftiImage provided by pynifti. 21 22 See http://niftilib.sourceforge.net/pynifti/ for more information 23 about pynifti. 24 """ 25 # XXX: Every dataset should really have an example of howto instantiate 26 # it (necessary parameters).
27 - def __init__(self, samples=None, mask=None, dsattr=None, **kwargs):
28 """Initialize NiftiDataset. 29 30 :Parameters: 31 - `samples`: Filename (string) of a NIfTI image or a `NiftiImage` 32 object 33 - `mask`: Filename (string) of a NIfTI image or a `NiftiImage` 34 object 35 36 """ 37 # if dsattr is none, set it to an empty dict 38 if dsattr is None: 39 dsattr = {} 40 41 # we have to handle the nifti elementsize at the end if 42 # mask is not already a MaskMapper 43 set_elementsize = False 44 if not dsattr.has_key('mapper'): 45 set_elementsize = True 46 47 # default way to use the constructor: with NIfTI image filename 48 if not samples is None: 49 if isinstance(samples, str): 50 # open the nifti file 51 try: 52 nifti = NiftiImage(samples) 53 except RuntimeError, e: 54 warning("ERROR: NiftiDatasets: Cannot open samples file %s" \ 55 % samples) # should we make also error? 56 raise e 57 elif isinstance(samples, NiftiImage): 58 # nothing special 59 nifti = samples 60 else: 61 raise ValueError, \ 62 "NiftiDataset constructor takes the filename of a " \ 63 "NIfTI image or a NiftiImage object as 'samples' " \ 64 "argument." 65 samples = nifti.data 66 # do not put the whole NiftiImage in the dict as this will most 67 # likely be deepcopy'ed at some point and ensuring data integrity 68 # of the complex Python-C-Swig hybrid might be a tricky task. 69 # Only storing the header dict should achieve the same and is more 70 # memory efficient and even simpler 71 dsattr['niftihdr'] = nifti.header 72 if isinstance(mask, str): 73 # if mask is also a nifti file open, it and take the image array 74 # use a copy of the mask data as otherwise segfault will 75 # embarass you, once the 'mask' NiftiImage get deleted 76 try: 77 mask = NiftiImage(mask).asarray() 78 except RuntimeError, e: 79 warning("ERROR: NiftiDatasets: Cannot open mask file %s" \ 80 % mask) 81 raise e 82 83 elif isinstance(mask, NiftiImage): 84 # just use data array as masl 85 mask = mask.asarray() 86 87 # by default init the dataset now 88 # if mask is a MaskMapper already, this is a cheap init. This is 89 # important as this is the default mode for the copy constructor 90 # and might be called really often!! 91 MaskedDataset.__init__(self, 92 samples=samples, 93 mask=mask, 94 dsattr=dsattr, 95 **(kwargs)) 96 97 98 if set_elementsize: 99 # in case the MaskMapper wasn't already passed to the constructor 100 # overwrite the default metric of it here to take the NIfTI element 101 # properties into account 102 103 # NiftiDataset uses a MaskMapper with DescreteMetric with cartesian 104 # distance and element size from the NIfTI header 105 106 # 'voxdim' is (x,y,z) while 'samples' are (t,z,y,x) 107 elementsize = [i for i in reversed(nifti.voxdim)] 108 self.mapper.setMetric( 109 DescreteMetric(elementsize=elementsize, 110 distance_function=cartesianDistance))
111 112
113 - def map2Nifti(self, data=None):
114 """Maps a data vector into the dataspace and wraps it with a 115 NiftiImage. The header data of this object is used to initialize 116 the new NiftiImage. 117 118 :Parameters: 119 data : ndarray 120 The data to be wrapped into NiftiImage. If None (default), it 121 would wrap samples of the current dataset 122 """ 123 if data is None: 124 data = self.samples 125 dsarray = self.mapper.reverse(data) 126 return NiftiImage(dsarray, self.niftihdr)
127 128 129 niftihdr = property(fget=lambda self: self._dsattr['niftihdr'], 130 doc='Access to the NIfTI header dictionary.')
131