Package mvpa :: Package tests :: Module test_atlases
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_atlases

  1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  # vi: set ft=python 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  """Unit tests for PyMVPA atlases""" 
 10   
 11  import unittest, re 
 12  import numpy as N 
 13   
 14  from mvpa.base import externals, warning 
 15   
 16  if externals.exists('nifti', raiseException=True): 
 17      from mvpa.atlases import * 
 18  else: 
 19      raise RuntimeError, "Don't run me if no nifti is present" 
 20   
 21  import os 
 22  from mvpa import pymvpa_dataroot 
 23   
24 -class AtlasesTests(unittest.TestCase):
25 """Basic tests for support of atlases such as the ones 26 shipped with FSL 27 """
28 - def testTransformations(self):
29 """TODO""" 30 pass
31
32 - def testAtlases(self):
33 """Basic testing of atlases""" 34 35 tested = 0 36 for name in KNOWN_ATLASES.keys(): 37 #filename = KNOWN_ATLASES[name] % {'name': name} 38 try: 39 atlas = Atlas(name=name) 40 tested += 1 41 except IOError: 42 # so we just don't have it 43 continue 44 #print isinstance(atlas.atlas, objectify.ObjectifiedElement) 45 #print atlas.header.images.imagefile.get('offset') 46 #print atlas.labelVoxel( (0, -7, 20) ) 47 #print atlas[ 0, 0, 0 ] 48 coord = (-63, -12, 22) 49 50 # Atlas must have at least 1 level and that one must 51 # have some labels 52 self.failUnless(len(atlas.levels_dict[0].labels) > 0) 53 54 for res in [ atlas[coord], 55 atlas.labelPoint(coord) ]: 56 self.failUnless(res.get('coord_queried', None) == coord, 57 '%s: Comparison failed. Got %s and %s' 58 % (name, res.get('coord_queried', None), coord)) 59 self.failUnless('labels' in res) 60 # all atlases so far are based on voxels 61 self.failUnless('voxel_queried' in res) 62 63 # test explicit level specification via slice, although bogus here 64 # XXX levels in queries should be deprecated -- too much of 65 # performance hit 66 res0 = atlas[coord, range(atlas.Nlevels)] 67 self.failUnless(res0 == res) 68 69 #print atlas[ 0, -7, 20, [1,2,3] ] 70 #print atlas[ (0, -7, 20), 1:2 ] 71 #print atlas[ (0, -7, 20) ] 72 #print atlas[ (0, -7, 20), : ] 73 # print atlas.getLabels(0) 74 if not tested: 75 warning("No atlases were found -- thus no testing was done")
76
77 - def testFind(self):
78 if not externals.exists('atlas_fsl'): return 79 tshape = (182, 218, 182) # target shape of fsl atlas chosen by default 80 atl = Atlas(name='HarvardOxford-Cortical') 81 atl.levels_dict[0].find('Frontal Pole') 82 self.failUnlessEqual( 83 len(atl.find(re.compile('Fusiform'), unique=False)), 84 4) 85 86 m = atl.getMap(1) 87 self.failUnlessEqual(m.shape, tshape) 88 self.failUnless(N.max(m)==100) 89 self.failUnless(N.min(m)==0) 90 91 ms = atl.getMaps('Fusiform') 92 self.failUnlessEqual(len(ms), 4) 93 self.failUnlessEqual(ms[0].shape, tshape) 94 95 ms = atl.getMaps('ZaZaZa') 96 self.failUnless(not len(ms)) 97 98 self.failUnlessRaises(ValueError, atl.getMap, 'Fusiform') 99 self.failUnless(len(atl.find('Fusiform', unique=False)) == 4) 100 self.failUnlessEqual(atl.getMap('Fusiform', strategy='max').shape, 101 tshape) 102 103 # Test loading of custom atlas 104 # for now just on the original file 105 atl2 = Atlas(name='HarvardOxford-Cortical', 106 image_file=atl._image_file) 107 108 # we should get exactly the same maps from both in this dummy case 109 self.failUnless((atl.getMap('Frontal Pole') == 110 atl2.getMap('Frontal Pole')).all()) 111 112 113 # Lets falsify and feed some crammy file as the atlas 114 atl2 = Atlas(name='HarvardOxford-Cortical', 115 image_file=os.path.join(pymvpa_dataroot, 116 'example4d.nii.gz')) 117 118 # we should get not even comparable maps now ;) 119 self.failUnless(atl.getMap('Frontal Pole').shape 120 != atl2.getMap('Frontal Pole').shape)
121 122
123 -def suite():
124 return unittest.makeSuite(AtlasesTests)
125 126 127 if __name__ == '__main__': 128 import runner 129