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