Package mvpa :: Package atlases :: Module fsl
[hide private]
[frames] | no frames]

Source Code for Module mvpa.atlases.fsl

  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  """FSL atlases interfaces 
 10   
 11  """ 
 12   
 13  import os, re 
 14  import numpy as N 
 15   
 16  from mvpa.base import warning, externals 
 17  from mvpa.misc.support import reuseAbsolutePath 
 18   
 19  externals.exists('nifti', raiseException=True) 
 20  from nifti import NiftiImage 
 21   
 22  from mvpa.atlases.base import XMLBasedAtlas, LabelsLevel 
 23   
 24  if __debug__: 
 25          from mvpa.base import debug 
26 27 # 28 # Atlases from FSL 29 # 30 31 -class FSLAtlas(XMLBasedAtlas):
32 """Base class for FSL atlases 33 34 """ 35 source = 'FSL' 36
37 - def __init__(self, *args, **kwargs):
38 """ 39 40 :Parameters: 41 filename : string 42 Filename for the xml definition of the atlas 43 resolution : None or float 44 Some atlases link to multiple images at different 45 resolutions. if None -- best resolution is selected 46 using 0th dimension resolution 47 """ 48 XMLBasedAtlas.__init__(self, *args, **kwargs) 49 self.space = 'MNI'
50 51
52 - def _loadImages(self):
53 resolution = self._resolution 54 header = self.header 55 images = header.images 56 # Load present images 57 # XXX might be refactored to avoid duplication of 58 # effort with PyMVPAAtlas 59 ni_image = None 60 resolutions = [] 61 for image in images: 62 imagefile = image.imagefile 63 imagefilename = reuseAbsolutePath( 64 self._filename, imagefile.text, force=True) 65 66 try: 67 ni_image_ = NiftiImage(imagefilename, load=False) 68 except RuntimeError, e: 69 raise RuntimeError, " Cannot open file " + imagefilename 70 71 resolution_ = ni_image_.pixdim[0] 72 if resolution is None: 73 # select this one if the best 74 if ni_image is None or \ 75 resolution_ < ni_image.pixdim[0]: 76 ni_image = ni_image_ 77 self._imagefile = imagefilename 78 else: 79 if resolution_ == resolution: 80 ni_image = ni_image_ 81 self._imagefile = imagefilename 82 break 83 else: 84 resolutions += [resolution_] 85 # TODO: also make use of summaryimagefile may be? 86 87 if ni_image is None: 88 msg = "Could not find an appropriate atlas among %d atlases." 89 if resolution is not None: 90 msg += " Atlases had resolutions %s" % \ 91 (resolutions,) 92 raise RuntimeError, msg 93 if __debug__: 94 debug('ATL__', "Loading atlas data from %s" % self._imagefile) 95 self._image = ni_image 96 self._resolution = ni_image.pixdim[0] 97 self._origin = N.abs(ni_image.header['qoffset']) * 1.0 # XXX 98 self._data = self._image.data
99 100
101 - def _loadData(self):
102 """ """ 103 # Load levels 104 self._levels_dict = {} 105 # preprocess labels for different levels 106 self.Nlevels = 1 107 #level = Level.generateFromXML(self.data, levelType='label') 108 level = LabelsLevel.generateFromXML(self.data)#, levelType='label') 109 level.description = self.header.name.text 110 self._levels_dict = {0: level}
111 #for index, child in enumerate(self.data.getchildren()): 112 # if child.tag == 'level': 113 # level = Level.generateFromXML(child) 114 # self._levels_dict[level.description] = level 115 # try: 116 # self._levels_dict[level.index] = level 117 # except: 118 # pass 119 # else: 120 # raise XMLAtlasException("Unknown child '%s' within data" % child.tag) 121 # self.Nlevels += 1 122 #pass 123 124 125 @staticmethod
126 - def _checkVersion(version):
127 return re.search('^[0-9]+\.[0-9]', version) is not None
128
129 130 -class FSLProbabilisticAtlas(FSLAtlas):
131
132 - def __init__(self, thr=0.0, strategy='all', sort=True, *args, **kwargs):
133 """ 134 135 :Parameters: 136 thr : float 137 Value to threshold at 138 strategy : basestring 139 Possible values 140 all - all entries above thr 141 max - entry with maximal value 142 sort : bool 143 Either to sort entries for 'all' strategy according to 144 probability 145 """ 146 147 FSLAtlas.__init__(self, *args, **kwargs) 148 self.thr = thr 149 self.strategy = strategy 150 self.sort = sort
151
152 - def labelVoxel(self, c, levels=None):
153 if levels is not None and not (levels in [0, [0], (0,)]): 154 raise ValueError, \ 155 "I guess we don't support levels other than 0 in FSL atlas" 156 157 # check range 158 c = self._checkRange(c) 159 160 # XXX think -- may be we should better assign each map to a 161 # different level 162 level = 0 163 resultLabels = [] 164 for index, area in enumerate(self._levels_dict[level]): 165 prob = int(self._data[index, c[2], c[1], c[0]]) 166 if prob > self.thr: 167 resultLabels += [dict(index=index, 168 #id= 169 label=area.text, 170 prob=prob)] 171 172 if self.sort or self.strategy == 'max': 173 resultLabels.sort(cmp=lambda x,y: cmp(x['prob'], y['prob']), 174 reverse=True) 175 176 if self.strategy == 'max': 177 resultLabels = resultLabels[:1] 178 elif self.strategy == 'all': 179 pass 180 else: 181 raise ValueError, 'Unknown strategy %s' % self.strategy 182 183 result = {'voxel_queried' : c, 184 # in the list since we have only single level but 185 # with multiple entries 186 'labels': [resultLabels]} 187 188 return result
189
190 191 -class FSLLabelsAtlas(XMLBasedAtlas):
192 - def __init__(self, *args, **kwargs):
193 FSLAtlas.__init__(self, *args, **kwargs) 194 raise NotImplementedError
195