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_iohelpers', 48 'test_report', 49 'test_datasetfx', 50 'test_cmdline', 51 'test_args', 52 'test_eepdataset', 53 'test_meg', 54 # Classifiers (longer tests) 55 'test_kernel', 56 'test_clf', 57 'test_regr', 58 'test_knn', 59 'test_gnb', 60 'test_svm', 61 'test_plr', 62 'test_smlr', 63 # Various algorithms 64 'test_svdmapper', 65 'test_procrust', 66 'test_hyperalignment', 67 'test_samplegroupmapper', 68 'test_transformers', 69 'test_transerror', 70 'test_clfcrossval', 71 'test_searchlight', 72 'test_rfe', 73 'test_ifs', 74 'test_datameasure', 75 'test_perturbsensana', 76 'test_splitsensana', 77 # And the suite (all-in-1) 78 'test_suite', 79 ] 80 81 # provide people with a hint about the warnings that might show up in a 82 # second 83 warning('Testing for availability of external software packages. Test ' 84 'cases depending on missing packages will not be part of the test ' 85 'suite.') 86 87 # So we could see all warnings about missing dependencies 88 warning.maxcount = 1000 89 # fully test of externals 90 externals.testAllDependencies() 91 92 93 __optional_tests = [ ('scipy', 'ridge'), 94 ('scipy', 'stats_sp'), 95 ('scipy', 'datasetfx_sp'), 96 (['lars','scipy'], 'lars'), 97 ('nifti', 'niftidataset'), 98 ('mdp', 'icamapper'), 99 ('scipy', 'zscoremapper'), 100 ('pywt', 'waveletmapper'), 101 (['cPickle', 'gzip'], 'hamster'), 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 exec 'import ' + t 123 124 # instanciate all tests suites and return dict of them (with ID as key) 125 return dict([(t[5:], eval(t + '.suite()')) for t in tests ])
126 127 128
129 -def run(limit=None, verbosity=None):
130 """Runs the full or a subset of the PyMVPA unittest suite. 131 132 :Parameters: 133 limit: None | list 134 If None, the full test suite is run. Alternatively, a list with test IDs 135 can be provides. IDs are the base filenames of the test implementation, 136 e.g. the ID for the suite in 'mvpa/tests/test_niftidataset.py' is 137 'niftidataset'. 138 verbosity: None | int 139 Verbosity of unittests execution. If None, controlled by PyMVPA 140 configuration tests/verbosity. Values higher than 2 enable all Python, 141 NumPy and PyMVPA warnings 142 """ 143 if __debug__: 144 from mvpa.base import debug 145 # Lets add some targets which provide additional testing 146 debug.active += ['CHECK_.*'] 147 148 # collect all tests 149 suites = collectTestSuites() 150 151 if limit is None: 152 # make global test suite (use them all) 153 ts = unittest.TestSuite(suites.values()) 154 else: 155 ts = unittest.TestSuite([suites[s] for s in limit]) 156 157 158 class TextTestRunnerPyMVPA(unittest.TextTestRunner): 159 """Extend TextTestRunner to print out random seed which was 160 used in the case of failure""" 161 def run(self, test): 162 """Run the bloody test and puke the seed value if failed""" 163 result = super(TextTestRunnerPyMVPA, self).run(test) 164 if not result.wasSuccessful(): 165 print "MVPA_SEED=%s" % _random_seed
166 167 if verbosity is None: 168 verbosity = int(cfg.get('tests', 'verbosity', default=1)) 169 170 if verbosity < 3: 171 # no MVPA warnings during whole testsuite (but restore handlers later on) 172 handler_backup = warning.handlers 173 warning.handlers = [] 174 175 # No python warnings (like ctypes version for slmr) 176 import warnings 177 warnings.simplefilter('ignore') 178 179 # No numpy 180 np_errsettings = np.geterr() 181 np.seterr(**dict([(x, 'ignore') for x in np_errsettings])) 182 183 # finally run it 184 TextTestRunnerPyMVPA(verbosity=verbosity).run(ts) 185 186 if verbosity < 3: 187 # restore warning handlers 188 warning.handlers = handler_backup 189 np.seterr(**np_errsettings) 190