Package mvpa :: Package tests

[hide private]
[frames] | no frames]

# Source Code for Package mvpa.tests

      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 test interface for PyMVPA"""
     10
     11  import unittest
     12  import numpy as np
     13  from mvpa import _random_seed, cfg
     14  from mvpa.base import externals, warning
     15
     16
     17 -def collectTestSuites():
     18      """Runs over all tests it knows and composes a dictionary with test suite
     19      instances as values and IDs as keys. IDs are the filenames of the unittest
     20      without '.py' extension and 'test_' prefix.
     21
     22      During collection this function will run a full and verbose test for all
     23      known externals.
     24      """
     25      # list all test modules (without .py extension)
     26      tests = [
     27          # Basic data structures/manipulators
     28          'test_externals',
     29          'test_base',
     30          'test_dochelpers',
     31          'test_dataset',
     32          'test_arraymapper',
     33          'test_boxcarmapper',
     34          'test_som',
     35          'test_neighbor',
     36          'test_maskeddataset',
     37          'test_metadataset',
     38          'test_splitter',
     39          'test_state',
     40          'test_params',
     41          'test_eepdataset',
     42          # Misc supporting utilities
     43          'test_config',
     44          'test_stats',
     45          'test_support',
     46          'test_verbosity',
     47          'test_report',
     48          'test_datasetfx',
     49          'test_cmdline',
     50          'test_args',
     51          'test_eepdataset',
     52          'test_meg',
     53          # Classifiers (longer tests)
     54          'test_kernel',
     55          'test_clf',
     56          'test_regr',
     57          'test_knn',
     58          'test_gnb',
     59          'test_svm',
     60          'test_plr',
     61          'test_smlr',
     62          # Various algorithms
     63          'test_svdmapper',
     64          'test_procrust',
     65          'test_hyperalignment',
     66          'test_samplegroupmapper',
     67          'test_transformers',
     68          'test_transerror',
     69          'test_clfcrossval',
     70          'test_searchlight',
     71          'test_rfe',
     72          'test_ifs',
     73          'test_datameasure',
     74          'test_perturbsensana',
     75          'test_splitsensana',
     76          # And the suite (all-in-1)
     77          'test_suite',
     78          ]
     79
     80      # provide people with a hint about the warnings that might show up in a
     81      # second
     82      warning('Testing for availability of external software packages. Test '
     83              'cases depending on missing packages will not be part of the test '
     84              'suite.')
     85
     86      # So we could see all warnings about missing dependencies
     87      warning.maxcount = 1000
     88      # fully test of externals
     89      externals.testAllDependencies()
     90
     91
     92      __optional_tests = [ ('scipy', 'ridge'),
     93                           ('scipy', 'stats_sp'),
     94                           ('scipy', 'datasetfx_sp'),
     95                           (['lars','scipy'], 'lars'),
     96                           ('nifti', 'niftidataset'),
     97                           ('mdp', 'icamapper'),
     98                           ('scipy', 'zscoremapper'),
     99                           ('pywt', 'waveletmapper'),
    100                           (['cPickle', 'gzip'], 'hamster'),
    101                           ('nose', 'iohelpers'),
    102      #                     ('mdp', 'pcamapper'),
    103                         ]
    104
    105      if not cfg.getboolean('tests', 'lowmem', default='no'):
    106          __optional_tests += [(['nifti', 'lxml'], 'atlases')]
    107
    108
    109      # and now for the optional tests
    110      optional_tests = []
    111
    112      for external, testname in __optional_tests:
    113          if externals.exists(external):
    114              optional_tests.append('test_%s' % testname)
    115
    116
    117      # finally merge all of them
    118      tests += optional_tests
    119
    120      # import all test modules
    121      for t in tests:
    122
    123          # TODO: exclude tests which fail to import: e.g. on Windows
    124          # could get WindowsError due to missing msvcr90.dll
    125
    126          exec 'import ' + t
    127
    128      # instanciate all tests suites and return dict of them (with ID as key)
    129      return dict([(t[5:], eval(t + '.suite()')) for t in tests ])
    130
    131
    132
    133 -def run(limit=None, verbosity=None):
    134      """Runs the full or a subset of the PyMVPA unittest suite.
    135
    136      :Parameters:
    137        limit: None | list
    138          If None, the full test suite is run. Alternatively, a list with test IDs
    139          can be provides. IDs are the base filenames of the test implementation,
    140          e.g. the ID for the suite in 'mvpa/tests/test_niftidataset.py' is
    141          'niftidataset'.
    142        verbosity: None | int
    143          Verbosity of unittests execution. If None, controlled by PyMVPA
    144          configuration tests/verbosity.  Values higher than 2 enable all Python,
    145          NumPy and PyMVPA warnings
    146      """
    147      if __debug__:
    148          from mvpa.base import debug
    149          # Lets add some targets which provide additional testing
    150          debug.active += ['CHECK_.*']
    151
    152      # collect all tests
    153      suites = collectTestSuites()
    154
    155      if limit is None:
    156          # make global test suite (use them all)
    157          ts = unittest.TestSuite(suites.values())
    158      else:
    159          ts = unittest.TestSuite([suites[s] for s in limit])
    160
    161
    162      class TextTestRunnerPyMVPA(unittest.TextTestRunner):
    163          """Extend TextTestRunner to print out random seed which was
    164          used in the case of failure"""
    165          def run(self, test):
    166              """Run the bloody test and puke the seed value if failed"""
    167              result = super(TextTestRunnerPyMVPA, self).run(test)
    168              if not result.wasSuccessful():
    169                  print "MVPA_SEED=%s" % _random_seed
    170
    171      if verbosity is None:
    172          verbosity = int(cfg.get('tests', 'verbosity', default=1))
    173
    174      if verbosity < 3:
    175          # no MVPA warnings during whole testsuite (but restore handlers later on)
    176          handler_backup = warning.handlers
    177          warning.handlers = []
    178
    179          # No python warnings (like ctypes version for slmr)
    180          import warnings
    181          warnings.simplefilter('ignore')
    182
    183          # No numpy
    184          np_errsettings = np.geterr()
    185          np.seterr(**dict([(x, 'ignore') for x in np_errsettings]))
    186
    187      # finally run it
    188      TextTestRunnerPyMVPA(verbosity=verbosity).run(ts)
    189
    190      if verbosity < 3:
    191          # restore warning handlers
    192          warning.handlers = handler_backup
    193          np.seterr(**np_errsettings)
    194