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

Source Code for Module mvpa.tests.test_neighbor

  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 metrics""" 
 10   
 11   
 12  from mvpa.mappers.metric import * 
 13  from mvpa.clfs.distance import * 
 14  import unittest 
 15  import numpy as N 
 16   
17 -class MetricTests(unittest.TestCase):
18
19 - def testDistances(self):
20 a = N.array([3,8]) 21 b = N.array([6,4]) 22 # test distances or yarik recalls unit testing ;) 23 self.failUnless( cartesianDistance(a, b) == 5.0 ) 24 self.failUnless( manhattenDistance(a, b) == 7 ) 25 self.failUnless( absminDistance(a, b) == 4 )
26 27
28 - def testDescreteMetric(self):
29 # who said that we will not use FSL's data 30 # with negative dimensions? :-) 31 elsize = [-2.5, 1.5] 32 distance = 3 33 34 # use default function 35 metric = DescreteMetric(elsize) 36 37 # simple check 38 target = N.array([ [1,2], [2,1], [2,2], [2,3], [3,2] ]) 39 self.failUnless( (metric.getNeighbors([2,2], 2.6) == target).all()) 40 41 # a bit longer one... not sure what for 42 for point in metric.getNeighbor([2,2], distance): 43 self.failUnless( cartesianDistance(point, [2,2]) <= distance) 44 45 # use manhattenDistance function 46 metric = DescreteMetric(elsize, manhattenDistance) 47 for point in metric.getNeighbor([2,2], distance): 48 self.failUnless( manhattenDistance(point, [2,2]) <= distance) 49 50 metric.elementsize = [10, 1.5] 51 """We can reassign element size as a whole""" 52 self.failUnless((metric.elementsize == [10, 1.5]).all()) 53 54 try: 55 metric.elementsize[1] = 1 56 self.fail(msg="We should not be able to reassign parts of elementsize") 57 except RuntimeError: 58 pass 59 60 self.failUnless((metric.getNeighbors([2,2], 2.6) == 61 [t for t in target if t[0]==2]).all()) 62 """Check if new elementsize is in effect for getNeighbors"""
63 64
66 # let's play fMRI: 3x3x3.3 mm and 2s TR, but in NIfTI we have it 67 # reversed 68 esize = [2, 3.3, 3, 3] 69 # only the last three axis are spatial ones and compatible in terms 70 # of a meaningful distance among them. 71 metric = DescreteMetric(esize, compatmask=[0,1,1,1]) 72 73 # neighbors in compat space will be simply propagated along remaining 74 # dimensions (somewhat like a backprojection from the subspace into the 75 # original space 76 self.failUnless((metric.getNeighbors([0,0,0,0], 1) == 77 [[-1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0]]).all()) 78 # test different radius in compat and in remaining space 79 # in this case usual spatial neighborhood, but no temporal 80 self.failUnless((metric.getNeighbors([0,0,0,0], [0,4,4,4]) == 81 [[0,-1,0,0],[0,0,-1,0],[0,0,0,-1],[0,0,0,0], 82 [0,0,0,1],[0,0,1,0],[0,1,0,0]]).all()) 83 84 # no spatial but temporal neighborhood 85 self.failUnless((metric.getNeighbors([0,0,0,0], [2,0,0,0]) == 86 [[-1,0,0,0],[0,0,0,0],[1,0,0,0]]).all()) 87 88 # check axis scaling in non-compat space 89 self.failUnless(len(metric.getNeighbors([0,0,0,0], [7.9,0,0,0])) == 9)
90 91
92 - def testGetNeighbors(self):
93 """Test if generator getNeighbor and method getNeighbors 94 return the right thing""" 95 96 class B(Metric): 97 """ Class which overrides only getNeighbor 98 """ 99 def getNeighbor(self): 100 for n in [4,5,6]: yield n
101 102 class C(Metric): 103 """ Class which overrides only getNeighbor 104 """ 105 def getNeighbors(self): 106 return [1,2,3] 107 108 b = B() 109 self.failUnless(b.getNeighbors() == [4,5,6]) 110 c = C() 111 self.failUnless([ x for x in c.getNeighbor()] == [1,2,3]) 112 113
114 -def suite():
115 return unittest.makeSuite(MetricTests)
116 117 118 if __name__ == '__main__': 119 import runner 120