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

Source Code for Module mvpa.tests.test_neighbor

 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  """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 - def testGetNeighbors(self):
51 """Test if generator getNeighbor and method getNeighbors 52 return the right thing""" 53 54 class B(Metric): 55 """ Class which overrides only getNeighbor 56 """ 57 def getNeighbor(self): 58 for n in [4,5,6]: yield n
59 60 class C(Metric): 61 """ Class which overrides only getNeighbor 62 """ 63 def getNeighbors(self): 64 return [1,2,3] 65 66 b = B() 67 self.failUnless(b.getNeighbors() == [4,5,6]) 68 c = C() 69 self.failUnless([ x for x in c.getNeighbor()] == [1,2,3]) 70 71
72 -def suite():
73 return unittest.makeSuite(MetricTests)
74 75 76 if __name__ == '__main__': 77 import runner 78