Package mvpa :: Package misc
[hide private]
[frames] | no frames]

Source Code for Package mvpa.misc

  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  """Import helper for PyMVPA misc modules""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  from sys import stdout, stderr 
 14   
 15  from os import environ 
 16   
 17  from mvpa.misc.verbosity import LevelLogger, OnceLogger, Logger 
 18   
 19  # 
 20  # Setup verbose and debug outputs 
 21  # 
22 -class _SingletonType(type):
23 """Simple singleton implementation adjusted from 24 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/412551 25 """
26 - def __init__(self, *args):
27 type.__init__(self, *args) 28 self._instances = {}
29
30 - def __call__(self, sid, instance, *args):
31 if not sid in self._instances: 32 self._instances[sid] = instance 33 return self._instances[sid]
34
35 -class __Singleton:
36 __metaclass__ = _SingletonType
37 - def __init__(self, *args):
38 pass
39 # Provided __call__ just to make silly pylint happy
40 - def __call__(self):
41 raise NotImplementedError
42 43 verbose = __Singleton("verbose", LevelLogger( 44 handlers=environ.get('MVPA_VERBOSE_OUTPUT', 'stdout').split(','))) 45 46 # Not supported/explained/used by now since verbose(0, is to print errors 47 #error = __Singleton("error", LevelLogger( 48 # handlers=environ.get('MVPA_ERROR_OUTPUT', 'stderr').split(','))) 49 50 # Levels for verbose 51 # 0 -- nothing besides errors 52 # 1 -- high level stuff -- top level operation or file operations 53 # 2 -- cmdline handling 54 # 3 -- 55 # 4 -- computation/algorithm relevant thingies 56 57 # Lets check if environment can tell us smth 58 if environ.has_key('MVPA_VERBOSE'): 59 verbose.level = int(environ['MVPA_VERBOSE']) 60 61 62 # Define Warning class so it is printed just once per each message
63 -class WarningLog(OnceLogger):
64
65 - def __init__(self, btlevels=10, btdefault=False, 66 maxcount=1, *args, **kwargs):
67 """Define Warning logger. 68 69 It is defined by 70 btlevels : int 71 how many levels of backtrack to print to give a hint on WTF 72 btdefault : bool 73 if to print backtrace for all warnings at all 74 maxcount : int 75 how many times to print each warning 76 """ 77 OnceLogger.__init__(self, *args, **kwargs) 78 self.__btlevels = btlevels 79 self.__btdefault = btdefault 80 self.__maxcount = maxcount
81 82
83 - def __call__(self, msg, bt=None):
84 import traceback 85 if bt is None: 86 bt = self.__btdefault 87 tb = traceback.extract_stack(limit=2) 88 msgid = `tb[-2]` # take parent as the source of ID 89 fullmsg = "WARNING: %s.\n\t(Please note: this warning is " % msg + \ 90 "printed only once, but underlying problem might " + \ 91 "occur many times.\n" 92 if bt and self.__btlevels > 0: 93 fullmsg += "Top-most backtrace:\n" 94 fullmsg += reduce(lambda x, y: x + "\t%s:%d in %s where '%s'\n" % \ 95 y, 96 traceback.extract_stack(limit=self.__btlevels), 97 "") 98 99 OnceLogger.__call__(self, msgid, fullmsg, self.__maxcount)
100 101 102 if environ.has_key('MVPA_WARNINGS_BT'): 103 warnings_btlevels = int(environ['MVPA_WARNINGS_BT']) 104 warnings_bt = True 105 else: 106 warnings_btlevels = 10 107 warnings_bt = False 108 109 if environ.has_key('MVPA_WARNINGS_COUNT'): 110 warnings_maxcount = int(environ['MVPA_WARNINGS_COUNT']) 111 else: 112 warnings_maxcount = 1 113 114 warning = WarningLog(handlers={False: environ.get('MVPA_WARNING_OUTPUT', 'stdout').split(','), 115 True: []}[environ.has_key('MVPA_NO_WARNINGS')], 116 btlevels=warnings_btlevels, 117 btdefault=warnings_bt, 118 maxcount=warnings_maxcount 119 ) 120 121 122 if __debug__: 123 from mvpa.misc.verbosity import DebugLogger 124 # NOTE: all calls to debug must be preconditioned with 125 # if __debug__: 126 127 debug = __Singleton("debug", DebugLogger( 128 handlers=environ.get('MVPA_DEBUG_OUTPUT', 'stdout').split(','))) 129 130 # set some debugging matricses to report 131 # debug.registerMetric('vmem') 132 133 # List agreed sets for debug 134 debug.register('DBG', "Debug output itself") 135 debug.register('INIT', "Just sequence of inits") 136 debug.register('RANDOM', "Random number generation") 137 debug.register('EXT', "External dependencies") 138 debug.register('TEST', "Debug unittests") 139 debug.register('_QUICKTEST_', "Quick unittests") # TODO: handle levels of unittests properly 140 debug.register('DG', "Data generators") 141 debug.register('LAZY', "Miscelaneous 'lazy' evaluations") 142 debug.register('LOOP', "Support's loop construct") 143 debug.register('PLR', "PLR call") 144 debug.register('SLC', "Searchlight call") 145 debug.register('SA', "Sensitivity analyzers call") 146 debug.register('PSA', "Perturbation analyzer call") 147 debug.register('RFEC', "Recursive Feature Elimination call") 148 debug.register('RFEC_', "Recursive Feature Elimination call (verbose)") 149 debug.register('IFSC', "Incremental Feature Search call") 150 debug.register('DS', "*Dataset") 151 debug.register('DS_', "*Dataset (verbose)") 152 debug.register('DS_ID', "ID Datasets") 153 debug.register('DS_STATS',"Datasets statistics") 154 debug.register('RETRAIN', "Doing additional checking in retraining/retesting") 155 156 debug.register('COL', "Generic Collectable debugging") 157 158 debug.register('MAP', "*Mapper") 159 debug.register('MAP_', "*Mapper (verbose)") 160 161 debug.register('ST', "State") 162 debug.register('STV', "State Variable") 163 debug.register('COLR', "Collector for states and classifier parameters") 164 165 debug.register('CLF', "Base Classifiers") 166 debug.register('CLF_', "Base Classifiers (verbose)") 167 debug.register('CLF_TB', 168 "Report traceback in train/predict. Helps to resolve WTF calls it") 169 debug.register('CLFBST', "BoostClassifier") 170 debug.register('CLFBIN', "BinaryClassifier") 171 debug.register('CLFMC', "MulticlassClassifier") 172 debug.register('CLFSPL', "SplitClassifier") 173 debug.register('CLFFS', "FeatureSelectionClassifier") 174 debug.register('CLFFS_', "FeatureSelectionClassifier (verbose)") 175 176 debug.register('FS', "FeatureSelections") 177 debug.register('FS_', "FeatureSelections (verbose)") 178 debug.register('FSPL', "FeatureSelectionPipeline") 179 180 debug.register('SVM', "SVM") 181 debug.register('LIBSVM', "Internal libsvm output") 182 183 debug.register('SMLR', "SMLR") 184 debug.register('SMLR_', "SMLR verbose") 185 186 debug.register('GPR', "GPR") 187 debug.register('KERNEL', "Kernels module") 188 189 debug.register('SG', "PyMVPA SG wrapping") 190 debug.register('SG_', "PyMVPA SG wrapping verbose") 191 debug.register('SG__', "PyMVPA SG wrapping debug") 192 debug.register('SG_SVM', "Internal shogun debug output for SVM itself") 193 debug.register('SG_FEATURES', "Internal shogun debug output for features") 194 debug.register('SG_LABELS', "Internal shogun debug output for labels") 195 debug.register('SG_KERNELS', "Internal shogun debug output for kernels") 196 debug.register('SG_PROGRESS', "Internal shogun progress bar during computation") 197 198 debug.register('IOH', "IO Helpers") 199 debug.register('CM', "Confusion matrix computation") 200 debug.register('CROSSC',"Cross-validation call") 201 debug.register('CERR', "Various ClassifierErrors") 202 203 # Lets check if environment can tell us smth 204 if environ.has_key('MVPA_DEBUG'): 205 debug.setActiveFromString(environ['MVPA_DEBUG']) 206 207 # Lets check if environment can tell us smth 208 if environ.has_key('MVPA_DEBUG_METRICS'): 209 debug.registerMetric(environ['MVPA_DEBUG_METRICS'].split(",")) 210 211 if __debug__: 212 debug('INIT', 'mvpa.misc end') 213