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

Source Code for Package mvpa.base

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