Package mvpa :: Package tests :: Module test_som

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.tests.test_som

     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 SOM mapper"""
    10
    11
    12  import unittest
    13  import numpy as N
    14  from mvpa import cfg
    15  from mvpa.mappers.som import SimpleSOMMapper
    16  from mvpa.datasets import Dataset
    17
    18 -class SOMMapperTests(unittest.TestCase):
    19
    20 -    def testSimpleSOM(self):
    21          colors = [[0., 0., 0.], [0., 0., 1.], [0., 1., 0.],
    22                    [1., 0., 0.], [0., 1., 1.], [1., 0., 1.],
    23                    [1., 1., 0.], [1., 1., 1.]]
    24          ds = Dataset(samples=colors, labels=1)
    25
    26          # only small SOM for speed reasons
    27          som = SimpleSOMMapper((10, 5), 200, learning_rate=0.05)
    28
    29          # no acces when nothing is there
    30          self.failUnlessRaises(RuntimeError, som._accessKohonen)
    31          self.failUnlessRaises(RuntimeError, som.getInSize)
    32          self.failUnlessRaises(RuntimeError, som.getOutSize)
    33
    34          som.train(ds)
    35
    36          self.failUnless(som.getInSize() == 3)
    37          self.failUnless(som.getOutSize() == (10,5))
    38
    39          fmapped = som(colors)
    40          self.failUnless(fmapped.shape == (8, 2))
    41          for fm in fmapped:
    42              self.failUnless(som.isValidOutId(fm))
    43
    44          # reverse mapping
    45          rmapped = som.reverse(fmapped)
    46
    47          if cfg.getboolean('tests', 'labile', default='yes'):
    48              # should approximately restore the input, but could fail
    49              # with bas initialisation
    50              self.failUnless((N.round(rmapped) == ds.samples).all())
    51
    52
    53 -def suite():
    54      return unittest.makeSuite(SOMMapperTests)
    55
    56
    57  if __name__ == '__main__':
    58      import runner
    59