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 """Set maxcount for the warning""" 132 self.__maxcount = value
133 134 maxcount = property(fget=lambda x:x.__maxcount, fset=_setMaxCount)
135 136 # XXX what is 'bt'? Maybe more verbose name? 137 if cfg.has_option('warnings', 'bt'): 138 warnings_btlevels = cfg.getint('warnings', 'bt') 139 warnings_bt = True 140 else: 141 warnings_btlevels = 10 142 warnings_bt = False 143 144 if cfg.has_option('warnings', 'count'): 145 warnings_maxcount = cfg.getint('warnings', 'count') 146 else: 147 warnings_maxcount = 1 148 149 warning = WarningLog( 150 handlers={ 151 False: cfg.get('warnings', 'output', default='stdout').split(','), 152 True: []}[cfg.getboolean('warnings', 'suppress', default=False)], 153 btlevels=warnings_btlevels, 154 btdefault=warnings_bt, 155 maxcount=warnings_maxcount 156 ) 157 158 159 if __debug__: 160 from mvpa.base.verbosity import DebugLogger 161 # NOTE: all calls to debug must be preconditioned with 162 # if __debug__: 163 164 debug = __Singleton("debug", DebugLogger( 165 handlers=cfg.get('debug', 'output', default='stdout').split(','))) 166 167 # set some debugging matricses to report 168 # debug.registerMetric('vmem') 169 170 # List agreed sets for debug 171 debug.register('DBG', "Debug output itself") 172 debug.register('DOCH', "Doc helpers") 173 debug.register('INIT', "Just sequence of inits") 174 debug.register('RANDOM', "Random number generation") 175 debug.register('EXT', "External dependencies") 176 debug.register('EXT_', "External dependencies (verbose)") 177 debug.register('TEST', "Debug unittests") 178 debug.register('MODULE_IN_REPR', "Include module path in __repr__") 179 debug.register('ID_IN_REPR', "Include id in __repr__") 180 181 debug.register('DG', "Data generators") 182 debug.register('LAZY', "Miscelaneous 'lazy' evaluations") 183 debug.register('LOOP', "Support's loop construct") 184 debug.register('PLR', "PLR call") 185 debug.register('SLC', "Searchlight call") 186 debug.register('SA', "Sensitivity analyzers") 187 debug.register('IRELIEF', "Various I-RELIEFs") 188 debug.register('SA_', "Sensitivity analyzers (verbose)") 189 debug.register('PSA', "Perturbation analyzer call") 190 debug.register('RFEC', "Recursive Feature Elimination call") 191 debug.register('RFEC_', "Recursive Feature Elimination call (verbose)") 192 debug.register('IFSC', "Incremental Feature Search call") 193 debug.register('DS', "*Dataset") 194 debug.register('DS_NIFTI', "NiftiDataset(s)") 195 debug.register('DS_', "*Dataset (verbose)") 196 debug.register('DS_ID', "ID Datasets") 197 debug.register('DS_STATS',"Datasets statistics") 198 debug.register('SPL', "*Splitter") 199 200 debug.register('TRAN', "Transformers") 201 debug.register('TRAN_', "Transformers (verbose)") 202 203 # CHECKs 204 debug.register('CHECK_DS_SELECT', 205 "Check in dataset.select() for sorted and unique indexes") 206 debug.register('CHECK_DS_SORTED', "Check in datasets for sorted") 207 debug.register('CHECK_IDS_SORTED', 208 "Check for ids being sorted in mappers") 209 debug.register('CHECK_TRAINED', 210 "Checking in checking if clf was trained on given dataset") 211 debug.register('CHECK_RETRAIN', "Checking in retraining/retesting") 212 debug.register('CHECK_STABILITY', "Checking for numerical stability") 213 214 debug.register('MAP', "*Mapper") 215 debug.register('MAP_', "*Mapper (verbose)") 216 217 debug.register('COL', "Generic Collectable") 218 debug.register('UATTR', "Attributes with unique") 219 debug.register('ST', "State") 220 debug.register('STV', "State Variable") 221 debug.register('COLR', "Collector for states and classifier parameters") 222 debug.register('ES', "Element selectors") 223 224 debug.register('CLF', "Base Classifiers") 225 debug.register('CLF_', "Base Classifiers (verbose)") 226 #debug.register('CLF_TB', 227 # "Report traceback in train/predict. Helps to resolve WTF calls it") 228 debug.register('CLFBST', "BoostClassifier") 229 #debug.register('CLFBST_TB', "BoostClassifier traceback") 230 debug.register('CLFBIN', "BinaryClassifier") 231 debug.register('CLFMC', "MulticlassClassifier") 232 debug.register('CLFSPL', "SplitClassifier") 233 debug.register('CLFFS', "FeatureSelectionClassifier") 234 debug.register('CLFFS_', "FeatureSelectionClassifier (verbose)") 235 236 debug.register('STAT', "Statistics estimates") 237 debug.register('STAT_', "Statistics estimates (verbose)") 238 debug.register('STAT__', "Statistics estimates (very verbose)") 239 240 debug.register('FS', "FeatureSelections") 241 debug.register('FS_', "FeatureSelections (verbose)") 242 debug.register('FSPL', "FeatureSelectionPipeline") 243 244 debug.register('SVM', "SVM") 245 debug.register('SVM_', "SVM (verbose)") 246 debug.register('LIBSVM', "Internal libsvm output") 247 248 debug.register('SMLR', "SMLR") 249 debug.register('SMLR_', "SMLR verbose") 250 251 debug.register('LARS', "LARS") 252 debug.register('LARS_', "LARS (verbose)") 253 254 debug.register('GPR', "GPR") 255 debug.register('GPR_WEIGHTS', "Track progress of GPRWeights computation") 256 debug.register('KERNEL', "Kernels module") 257 debug.register('MOD_SEL', "Model Selector (also makes openopt's iprint=0)") 258 debug.register('OPENOPT', "OpenOpt toolbox verbose (iprint=1)") 259 260 debug.register('SG', "PyMVPA SG wrapping") 261 debug.register('SG_', "PyMVPA SG wrapping verbose") 262 debug.register('SG__', "PyMVPA SG wrapping debug") 263 debug.register('SG_SVM', "Internal shogun debug output for SVM itself") 264 debug.register('SG_FEATURES', "Internal shogun debug output for features") 265 debug.register('SG_LABELS', "Internal shogun debug output for labels") 266 debug.register('SG_KERNELS', "Internal shogun debug output for kernels") 267 debug.register('SG_PROGRESS', 268 "Internal shogun progress bar during computation") 269 270 debug.register('IOH', "IO Helpers") 271 debug.register('IO_HAM', "Hamster") 272 debug.register('CM', "Confusion matrix computation") 273 debug.register('ROC', "ROC analysis") 274 debug.register('CROSSC',"Cross-validation call") 275 debug.register('CERR', "Various ClassifierErrors") 276 277 debug.register('ATL', "Atlases") 278 debug.register('ATL_', "Atlases (verbose)") 279 debug.register('ATL__', "Atlases (very verbose)") 280 281 # Lets check if environment can tell us smth 282 if cfg.has_option('general', 'debug'): 283 debug.setActiveFromString(cfg.get('general', 'debug')) 284 285 # Lets check if environment can tell us smth 286 if cfg.has_option('debug', 'metrics'): 287 debug.registerMetric(cfg.get('debug', 'metrics').split(",")) 288 289 290 291 if __debug__: 292 debug('INIT', 'mvpa.base end') 293