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

Source Code for Package mvpa.base

  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  """Base functionality of PyMVPA 
 10   
 11  Module Organization 
 12  =================== 
 13   
 14  mvpa.base module contains various modules which are used through out 
 15  PyMVPA code, and are generic building blocks 
 16   
 17  .. packagetree:: 
 18     :style: UML 
 19   
 20  :group Basic: externals, config, verbosity, dochelpers 
 21  """ 
 22   
 23  __docformat__ = 'restructuredtext' 
 24   
 25   
 26  from sys import stdout, stderr 
 27   
 28  from mvpa.base.config import ConfigManager 
 29  from mvpa.base.verbosity import LevelLogger, OnceLogger, Logger 
 30   
 31  # 
 32  # Setup verbose and debug outputs 
 33  # 
34 -class _SingletonType(type):
35 """Simple singleton implementation adjusted from 36 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/412551 37 """
38 - def __init__(mcs, *args):
39 type.__init__(mcs, *args) 40 mcs._instances = {}
41
42 - def __call__(mcs, sid, instance, *args):
43 if not sid in mcs._instances: 44 mcs._instances[sid] = instance 45 return mcs._instances[sid]
46
47 -class __Singleton:
48 """To ensure single instance of a class instantiation (object) 49 50 """ 51 52 __metaclass__ = _SingletonType
53 - def __init__(self, *args):
54 pass
55 # Provided __call__ just to make silly pylint happy
56 - def __call__(self):
57 raise NotImplementedError
58 59 # 60 # As the very first step: Setup configuration registry instance and 61 # read all configuration settings from files and env variables 62 # 63 cfg = __Singleton('cfg', ConfigManager()) 64 65 verbose = __Singleton("verbose", LevelLogger( 66 handlers = cfg.get('verbose', 'output', default='stdout').split(','))) 67 68 # Not supported/explained/used by now since verbose(0, is to print errors 69 #error = __Singleton("error", LevelLogger( 70 # handlers=environ.get('MVPA_ERROR_OUTPUT', 'stderr').split(','))) 71 72 # Levels for verbose 73 # 0 -- nothing besides errors 74 # 1 -- high level stuff -- top level operation or file operations 75 # 2 -- cmdline handling 76 # 3 -- 77 # 4 -- computation/algorithm relevant thingies 78 79 # Lets check if environment can tell us smth 80 if cfg.has_option('general', 'verbose'): 81 verbose.level = cfg.getint('general', 'verbose') 82 83
84 -class WarningLog(OnceLogger):
85 """Logging class of messsages to be printed just once per each message 86 87 """ 88
89 - def __init__(self, btlevels=10, btdefault=False, 90 maxcount=1, *args, **kwargs):
91 """Define Warning logger. 92 93 It is defined by 94 btlevels : int 95 how many levels of backtrack to print to give a hint on WTF 96 btdefault : bool 97 if to print backtrace for all warnings at all 98 maxcount : int 99 how many times to print each warning 100 """ 101 OnceLogger.__init__(self, *args, **kwargs) 102 self.__btlevels = btlevels 103 self.__btdefault = btdefault 104 self.__maxcount = maxcount 105 self.__explanation_seen = False
106 107
108 - def __call__(self, msg, bt=None):
109 import traceback 110 if bt is None: 111 bt = self.__btdefault 112 tb = traceback.extract_stack(limit=2) 113 msgid = repr(tb[-2]) # take parent as the source of ID 114 fullmsg = "WARNING: %s" % msg 115 if not self.__explanation_seen: 116 self.__explanation_seen = True 117 fullmsg += "\n * Please note: warnings are " + \ 118 "printed only once, but underlying problem might " + \ 119 "occur many times *" 120 if bt and self.__btlevels > 0: 121 fullmsg += "Top-most backtrace:\n" 122 fullmsg += reduce(lambda x, y: x + "\t%s:%d in %s where '%s'\n" % \ 123 y, 124 traceback.extract_stack(limit=self.__btlevels), 125 "") 126 127 OnceLogger.__call__(self, msgid, fullmsg, self.__maxcount)
128 129
130 - def _setMaxCount(self, value):
131 self.__maxcount = value
132 133 maxcount = property(fget=lambda x:x.__maxcount, fset=_setMaxCount)
134 135 # XXX what is 'bt'? Maybe more verbose name? 136 if cfg.has_option('warnings', 'bt'): 137 warnings_btlevels = cfg.getint('warnings', 'bt') 138 warnings_bt = True 139 else: 140 warnings_btlevels = 10 141 warnings_bt = False 142 143 if cfg.has_option('warnings', 'count'): 144 warnings_maxcount = cfg.getint('warnings', 'count') 145 else: 146 warnings_maxcount = 1 147 148 warning = WarningLog( 149 handlers={ 150 False: cfg.get('warnings', 'output', default='stdout').split(','), 151 True: []}[cfg.getboolean('warnings', 'suppress', default=False)], 152 btlevels=warnings_btlevels, 153 btdefault=warnings_bt, 154 maxcount=warnings_maxcount 155 ) 156 157 158 if __debug__: 159 from mvpa.base.verbosity import DebugLogger 160 # NOTE: all calls to debug must be preconditioned with 161 # if __debug__: 162 163 debug = __Singleton("debug", DebugLogger( 164 handlers=cfg.get('debug', 'output', default='stdout').split(','))) 165 166 # set some debugging matricses to report 167 # debug.registerMetric('vmem') 168 169 # List agreed sets for debug 170 debug.register('DBG', "Debug output itself") 171 debug.register('INIT', "Just sequence of inits") 172 debug.register('RANDOM', "Random number generation") 173 debug.register('EXT', "External dependencies") 174 debug.register('EXT_', "External dependencies (verbose)") 175 debug.register('TEST', "Debug unittests") 176 debug.register('MODULE_IN_REPR', "Include module path in __repr__") 177 debug.register('ID_IN_REPR', "Include id in __repr__") 178 179 debug.register('DG', "Data generators") 180 debug.register('LAZY', "Miscelaneous 'lazy' evaluations") 181 debug.register('LOOP', "Support's loop construct") 182 debug.register('PLR', "PLR call") 183 debug.register('SLC', "Searchlight call") 184 debug.register('SA', "Sensitivity analyzers") 185 debug.register('IRELIEF', "Various I-RELIEFs") 186 debug.register('SA_', "Sensitivity analyzers (verbose)") 187 debug.register('PSA', "Perturbation analyzer call") 188 debug.register('RFEC', "Recursive Feature Elimination call") 189 debug.register('RFEC_', "Recursive Feature Elimination call (verbose)") 190 debug.register('IFSC', "Incremental Feature Search call") 191 debug.register('DS', "*Dataset") 192 debug.register('DS_', "*Dataset (verbose)") 193 debug.register('DS_ID', "ID Datasets") 194 debug.register('DS_STATS',"Datasets statistics") 195 debug.register('SPL', "*Splitter") 196 197 # CHECKs 198 debug.register('CHECK_DS_SELECT', 199 "Check in dataset.select() for sorted and unique indexes") 200 debug.register('CHECK_DS_SORTED', "Check in datasets for sorted") 201 debug.register('CHECK_IDS_SORTED', 202 "Check for ids being sorted in mappers") 203 debug.register('CHECK_TRAINED', 204 "Checking in checking if clf was trained on given dataset") 205 debug.register('CHECK_RETRAIN', "Checking in retraining/retesting") 206 debug.register('CHECK_STABILITY', "Checking for numerical stability") 207 208 debug.register('MAP', "*Mapper") 209 debug.register('MAP_', "*Mapper (verbose)") 210 211 debug.register('COL', "Generic Collectable") 212 debug.register('UATTR', "Attributes with unique") 213 debug.register('ST', "State") 214 debug.register('STV', "State Variable") 215 debug.register('COLR', "Collector for states and classifier parameters") 216 217 debug.register('CLF', "Base Classifiers") 218 debug.register('CLF_', "Base Classifiers (verbose)") 219 #debug.register('CLF_TB', 220 # "Report traceback in train/predict. Helps to resolve WTF calls it") 221 debug.register('CLFBST', "BoostClassifier") 222 #debug.register('CLFBST_TB', "BoostClassifier traceback") 223 debug.register('CLFBIN', "BinaryClassifier") 224 debug.register('CLFMC', "MulticlassClassifier") 225 debug.register('CLFSPL', "SplitClassifier") 226 debug.register('CLFFS', "FeatureSelectionClassifier") 227 debug.register('CLFFS_', "FeatureSelectionClassifier (verbose)") 228 229 debug.register('STAT', "Statistics estimates") 230 debug.register('STAT_', "Statistics estimates (verbose)") 231 debug.register('STAT__', "Statistics estimates (very verbose)") 232 233 debug.register('FS', "FeatureSelections") 234 debug.register('FS_', "FeatureSelections (verbose)") 235 debug.register('FSPL', "FeatureSelectionPipeline") 236 237 debug.register('SVM', "SVM") 238 debug.register('SVM_', "SVM (verbose)") 239 debug.register('LIBSVM', "Internal libsvm output") 240 241 debug.register('SMLR', "SMLR") 242 debug.register('SMLR_', "SMLR verbose") 243 244 debug.register('LARS', "LARS") 245 debug.register('LARS_', "LARS (verbose)") 246 247 debug.register('GPR', "GPR") 248 debug.register('GPR_WEIGHTS', "Track progress of GPRWeights computation") 249 debug.register('KERNEL', "Kernels module") 250 debug.register('MOD_SEL', "Model Selector (also makes openopt's iprint=0)") 251 debug.register('OPENOPT', "OpenOpt toolbox verbose (iprint=1)") 252 253 debug.register('SG', "PyMVPA SG wrapping") 254 debug.register('SG_', "PyMVPA SG wrapping verbose") 255 debug.register('SG__', "PyMVPA SG wrapping debug") 256 debug.register('SG_SVM', "Internal shogun debug output for SVM itself") 257 debug.register('SG_FEATURES', "Internal shogun debug output for features") 258 debug.register('SG_LABELS', "Internal shogun debug output for labels") 259 debug.register('SG_KERNELS', "Internal shogun debug output for kernels") 260 debug.register('SG_PROGRESS', 261 "Internal shogun progress bar during computation") 262 263 debug.register('IOH', "IO Helpers") 264 debug.register('CM', "Confusion matrix computation") 265 debug.register('ROC', "ROC analysis") 266 debug.register('CROSSC',"Cross-validation call") 267 debug.register('CERR', "Various ClassifierErrors") 268 269 debug.register('ATL', "Atlases") 270 debug.register('ATL_', "Atlases (verbose)") 271 debug.register('ATL__', "Atlases (very verbose)") 272 273 # Lets check if environment can tell us smth 274 if cfg.has_option('general', 'debug'): 275 debug.setActiveFromString(cfg.get('general', 'debug')) 276 277 # Lets check if environment can tell us smth 278 if cfg.has_option('debug', 'metrics'): 279 debug.registerMetric(cfg.get('debug', 'metrics').split(",")) 280 281 282 283 if __debug__: 284 debug('INIT', 'mvpa.base end') 285