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

Source Code for Module mvpa.base.externals

  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  """Helper to verify presence of external libraries and modules 
 10  """ 
 11   
 12  __docformat__ = 'restructuredtext' 
 13  import os 
 14   
 15  from mvpa.base import warning 
 16  from mvpa import cfg 
 17  from mvpa.misc.support import SmartVersion 
 18   
 19  if __debug__: 
 20      from mvpa.base import debug 
 21   
22 -class _VersionsChecker(dict):
23 """Helper class to check the versions of the available externals 24 """
25 - def __getitem__(self, key):
26 if not self.has_key(key): 27 exists(key, force=True, raiseException=True) 28 return super(_VersionsChecker, self).__getitem__(key)
29 30 versions = _VersionsChecker() 31 """Versions of available externals, as tuples 32 """ 33 34
35 -def __check_scipy():
36 """Check if scipy is present an if it is -- store its version 37 """ 38 import warnings 39 exists('numpy', raiseException=True) 40 # To don't allow any crappy warning to sneak in 41 warnings.simplefilter('ignore', DeprecationWarning) 42 try: 43 import scipy as sp 44 except: 45 warnings.simplefilter('default', DeprecationWarning) 46 raise 47 warnings.simplefilter('default', DeprecationWarning) 48 # Infiltrate warnings if necessary 49 numpy_ver = versions['numpy'] 50 scipy_ver = versions['scipy'] = SmartVersion(sp.__version__) 51 # There is way too much deprecation warnings spit out onto the 52 # user. Lets assume that they should be fixed by scipy 0.7.0 time 53 if scipy_ver >= "0.6.0" and scipy_ver < "0.7.0" \ 54 and numpy_ver > "1.1.0": 55 import warnings 56 if not __debug__ or (__debug__ and not 'PY' in debug.active): 57 if __debug__: 58 debug('EXT', "Setting up filters for numpy DeprecationWarnings") 59 filter_lines = [ 60 ('NumpyTest will be removed in the next release.*', 61 DeprecationWarning), 62 ('PyArray_FromDims: use PyArray_SimpleNew.', 63 DeprecationWarning), 64 ('PyArray_FromDimsAndDataAndDescr: use PyArray_NewFromDescr.', 65 DeprecationWarning), 66 # Trick re.match, since in warnings absent re.DOTALL in re.compile 67 ('[\na-z \t0-9]*The original semantics of histogram is scheduled to be.*' 68 '[\na-z \t0-9]*', Warning) ] 69 for f, w in filter_lines: 70 warnings.filterwarnings('ignore', f, w)
71 72
73 -def __check_numpy():
74 """Check if numpy is present (it must be) an if it is -- store its version 75 """ 76 import numpy as N 77 versions['numpy'] = SmartVersion(N.__version__)
78 79
80 -def __check_pywt(features=None):
81 """Check for available functionality within pywt 82 83 :Parameters: 84 features : list of basestring 85 List of known features to check such as 'wp reconstruct', 86 'wp reconstruct fixed' 87 """ 88 import pywt 89 import numpy as N 90 data = N.array([ 0.57316901, 0.65292526, 0.75266733, 0.67020084, 0.46505364, 91 0.76478331, 0.33034164, 0.49165547, 0.32979941, 0.09696717, 92 0.72552711, 0.4138999 , 0.54460628, 0.786351 , 0.50096306, 93 0.72436454, 0.2193098 , -0.0135051 , 0.34283984, 0.65596245, 94 0.49598417, 0.39935064, 0.26370727, 0.05572373, 0.40194438, 95 0.47004551, 0.60327258, 0.25628266, 0.32964893, 0.24009889,]) 96 mode = 'per' 97 wp = pywt.WaveletPacket(data, 'sym2', mode) 98 wp2 = pywt.WaveletPacket(data=None, wavelet='sym2', mode=mode) 99 try: 100 for node in wp.get_level(2): wp2[node.path] = node.data 101 except: 102 raise ImportError, \ 103 "Failed to reconstruct WP by specifying data in the layer" 104 105 if 'wp reconstruct fixed' in features: 106 rec = wp2.reconstruct() 107 if N.linalg.norm(rec[:len(data)] - data) > 1e-3: 108 raise ImportError, \ 109 "Failed to reconstruct WP correctly" 110 return True
111 112
113 -def __check_libsvm_verbosity_control():
114 """Check for available verbose control functionality 115 """ 116 import mvpa.clfs.libsvmc._svmc as _svmc 117 try: 118 _svmc.svm_set_verbosity(0) 119 except: 120 raise ImportError, "Provided version of libsvm has no way to control " \ 121 "its level of verbosity"
122
123 -def __check_shogun(bottom_version, custom_versions=[]):
124 """Check if version of shogun is high enough (or custom known) to 125 be enabled in the testsuite 126 127 :Parameters: 128 bottom_version : int 129 Bottom version which must be satisfied 130 custom_versions : list of int 131 Arbitrary list of versions which could got patched for 132 a specific issue 133 """ 134 import shogun.Classifier as __sc 135 ver = __sc.Version_get_version_revision() 136 if (ver in custom_versions) or (ver >= bottom_version): 137 return True 138 else: 139 raise ImportError, 'Version %s is smaller than needed %s' % \ 140 (ver, bottom_version)
141 142
143 -def __check_weave():
144 """Apparently presence of scipy is not sufficient since some 145 versions experience problems. E.g. in Sep,Oct 2008 lenny's weave 146 failed to work. May be some other converter could work (? See 147 http://lists.debian.org/debian-devel/2008/08/msg00730.html for a 148 similar report. 149 150 Following simple snippet checks compilation of the basic code using 151 weave 152 """ 153 from scipy import weave 154 from scipy.weave import converters, build_tools 155 import numpy as N 156 # to shut weave up 157 import sys 158 # we can't rely on weave at all at the restoring argv. On etch box 159 # restore_sys_argv() is apparently is insufficient 160 oargv = sys.argv[:] 161 ostdout = sys.stdout 162 if not( __debug__ and 'EXT_' in debug.active): 163 from StringIO import StringIO 164 sys.stdout = StringIO() 165 # *nix specific solution to shut weave up. 166 # Some users must complain and someone 167 # needs to fix this to become more generic. 168 cargs = [">/dev/null", "2>&1"] 169 else: 170 cargs = [] 171 fmsg = None 172 try: 173 data = N.array([1,2,3]) 174 counter = weave.inline("data[0]=fabs(-1);", ['data'], 175 type_converters=converters.blitz, 176 verbose=0, 177 extra_compile_args=cargs, 178 compiler = 'gcc') 179 except Exception, e: 180 fmsg = "Failed to build simple weave sample." \ 181 " Exception was %s" % str(e) 182 183 sys.stdout = ostdout 184 # needed to fix sweave which might "forget" to restore sysv 185 # build_tools.restore_sys_argv() 186 sys.argv = oargv 187 if fmsg is not None: 188 raise ImportError, fmsg 189 else: 190 return "Everything is cool"
191 192
193 -def __check_atlas_family(family):
194 # XXX I guess pylint will dislike it a lot 195 from mvpa.atlases.warehouse import KNOWN_ATLAS_FAMILIES 196 names, pathpattern = KNOWN_ATLAS_FAMILIES[family] 197 filename = pathpattern % {'name':names[0]} 198 if not os.path.exists(filename): 199 raise ImportError, "Cannot find file %s for atlas family %s" \ 200 % (filename, family) 201 pass
202 203
204 -def __check_stablerdist():
205 import scipy.stats 206 import numpy as N 207 ## Unfortunately 0.7.0 hasn't fixed the issue so no chance but to do 208 ## a proper numerical test here 209 try: 210 scipy.stats.rdist(1.32, 0, 1).cdf(-1.0 + N.finfo(float).eps) 211 # Actually previous test is insufficient for 0.6, so enabling 212 # elderly test on top 213 # ATM all known implementations which implement custom cdf for 214 # rdist are misbehaving, so there should be no _cdf 215 if '_cdf' in scipy.stats.distributions.rdist_gen.__dict__.keys(): 216 raise ImportError, \ 217 "scipy.stats carries misbehaving rdist distribution" 218 except ZeroDivisionError: 219 raise RuntimeError, "RDist in scipy is still unstable on the boundaries"
220 221
222 -def __check_rv_discrete_ppf():
223 """Unfortunately 0.6.0-12 of scipy pukes on simple ppf 224 """ 225 import scipy.stats 226 try: 227 bdist = scipy.stats.binom(100, 0.5) 228 bdist.ppf(0.9) 229 except TypeError: 230 raise RuntimeError, "pmf is broken in discrete dists of scipy.stats"
231 232
233 -def __check_in_ipython():
234 # figure out if ran within IPython 235 if '__IPYTHON__' in globals()['__builtins__']: 236 return 237 raise RuntimeError, "Not running in IPython session"
238
239 -def __check_openopt():
240 try: 241 import openopt as _ 242 return 243 except ImportError: 244 pass 245 import scikits.openopt as _ 246 return
247
248 -def __check_matplotlib():
249 """Check for presence of matplotlib and set backend if requested.""" 250 import matplotlib 251 backend = cfg.get('matplotlib', 'backend') 252 if backend: 253 matplotlib.use(backend)
254
255 -def __check_pylab():
256 """Check if matplotlib is there and then pylab""" 257 exists('matplotlib', raiseException=True) 258 import pylab as P
259
260 -def __check_pylab_plottable():
261 """Simple check either we can plot anything using pylab. 262 263 Primary use in unittests 264 """ 265 try: 266 exists('pylab', raiseException=True) 267 import pylab as P 268 fig = P.figure() 269 P.plot([1,2], [1,2]) 270 P.close(fig) 271 except: 272 raise RuntimeError, "Cannot plot in pylab" 273 return True
274 275
276 -def __check_griddata():
277 """griddata might be independent module or part of mlab 278 """ 279 280 try: 281 from griddata import griddata as __ 282 return True 283 except ImportError: 284 if __debug__: 285 debug('EXT_', 'No python-griddata available') 286 287 from matplotlib.mlab import griddata as __ 288 return True
289 290
291 -def __check_reportlab():
292 import reportlab as rl 293 versions['reportlab'] = SmartVersion(rl.Version)
294 295
296 -def __check_rpy():
297 """Check either rpy is available and also set it for the sane execution 298 """ 299 #import rpy_options 300 #rpy_options.set_options(VERBOSE=False, SETUP_READ_CONSOLE=False) # SETUP_WRITE_CONSOLE=False) 301 #rpy_options.set_options(VERBOSE=False, SETUP_WRITE_CONSOLE=False) # SETUP_WRITE_CONSOLE=False) 302 # if not cfg.get('rpy', 'read_console', default=False): 303 # print "no read" 304 # rpy_options.set_options(SETUP_READ_CONSOLE=False) 305 # if not cfg.get('rpy', 'write_console', default=False): 306 # print "no write" 307 # rpy_options.set_options(SETUP_WRITE_CONSOLE=False) 308 import rpy 309 if not cfg.getboolean('rpy', 'interactive', default=True) \ 310 and (rpy.get_rpy_input() is rpy.rpy_io.rpy_input): 311 if __debug__: 312 debug('EXT_', "RPy: providing dummy callback for input to return '1'") 313 def input1(*args): return "1" # which is "1: abort (with core dump, if enabled)" 314 rpy.set_rpy_input(input1)
315 316 317 # contains list of available (optional) external classifier extensions 318 _KNOWN = {'libsvm':'import mvpa.clfs.libsvmc._svm as __; x=__.convert2SVMNode', 319 'libsvm verbosity control':'__check_libsvm_verbosity_control();', 320 'nifti':'from nifti import NiftiImage as __', 321 'nifti ge 0.20090205.1': 322 'from nifti.clib import detachDataFromImage as __', 323 'ctypes':'import ctypes as __', 324 'shogun':'import shogun as __', 325 'shogun.krr': 'import shogun.Regression as __; x=__.KRR', 326 'shogun.mpd': 'import shogun.Classifier as __; x=__.MPDSVM', 327 'shogun.lightsvm': 'import shogun.Classifier as __; x=__.SVMLight', 328 'shogun.svrlight': 'from shogun.Regression import SVRLight as __', 329 'numpy': "__check_numpy()", 330 'scipy': "__check_scipy()", 331 'good scipy.stats.rdist': "__check_stablerdist()", 332 'good scipy.stats.rv_discrete.ppf': "__check_rv_discrete_ppf()", 333 'weave': "__check_weave()", 334 'pywt': "import pywt as __", 335 'pywt wp reconstruct': "__check_pywt(['wp reconstruct'])", 336 'pywt wp reconstruct fixed': "__check_pywt(['wp reconstruct fixed'])", 337 'rpy': "__check_rpy()", 338 'lars': "exists('rpy', raiseException=True); import rpy; rpy.r.library('lars')", 339 'elasticnet': "exists('rpy', raiseException=True); import rpy; rpy.r.library('elasticnet')", 340 'glmnet': "exists('rpy', raiseException=True); import rpy; rpy.r.library('glmnet')", 341 'matplotlib': "__check_matplotlib()", 342 'pylab': "__check_pylab()", 343 'pylab plottable': "__check_pylab_plottable()", 344 'openopt': "__check_openopt()", 345 'mdp': "import mdp as __", 346 'mdp ge 2.4': "from mdp.nodes import LLENode as __", 347 'sg_fixedcachesize': "__check_shogun(3043, [2456])", 348 # 3318 corresponds to release 0.6.4 349 'sg ge 0.6.4': "__check_shogun(3318)", 350 'hcluster': "import hcluster as __", 351 'griddata': "__check_griddata()", 352 'cPickle': "import cPickle as __", 353 'gzip': "import gzip as __", 354 'lxml': "from lxml import objectify as __", 355 'atlas_pymvpa': "__check_atlas_family('pymvpa')", 356 'atlas_fsl': "__check_atlas_family('fsl')", 357 'running ipython env': "__check_in_ipython()", 358 'reportlab': "__check_reportlab()", 359 'nose': "import nose as __", 360 } 361 362
363 -def exists(dep, force=False, raiseException=False, issueWarning=None):
364 """ 365 Test whether a known dependency is installed on the system. 366 367 This method allows us to test for individual dependencies without 368 testing all known dependencies. It also ensures that we only test 369 for a dependency once. 370 371 :Parameters: 372 dep : string or list of string 373 The dependency key(s) to test. 374 force : boolean 375 Whether to force the test even if it has already been 376 performed. 377 raiseException : boolean 378 Whether to raise RuntimeError if dependency is missing. 379 issueWarning : string or None or True 380 If string, warning with given message would be thrown. 381 If True, standard message would be used for the warning 382 text. 383 """ 384 # if we are provided with a list of deps - go through all of them 385 if isinstance(dep, list) or isinstance(dep, tuple): 386 results = [ exists(dep_, force, raiseException) for dep_ in dep ] 387 return bool(reduce(lambda x,y: x and y, results, True)) 388 389 # where to look in cfg 390 cfgid = 'have ' + dep 391 392 # prevent unnecessarry testing 393 if cfg.has_option('externals', cfgid) \ 394 and not cfg.getboolean('externals', 'retest', default='no') \ 395 and not force: 396 if __debug__: 397 debug('EXT', "Skip retesting for '%s'." % dep) 398 399 # check whether an exception should be raised, even though the external 400 # was already tested previously 401 if not cfg.getboolean('externals', cfgid) \ 402 and raiseException \ 403 and cfg.getboolean('externals', 'raise exception', True): 404 raise RuntimeError, "Required external '%s' was not found" % dep 405 return cfg.getboolean('externals', cfgid) 406 407 408 # determine availability of external (non-cached) 409 410 # default to 'not found' 411 result = False 412 413 if not _KNOWN.has_key(dep): 414 raise ValueError, "%s is not a known dependency key." % (dep) 415 else: 416 # try and load the specific dependency 417 if __debug__: 418 debug('EXT', "Checking for the presence of %s" % dep) 419 420 # Exceptions which are silently caught while running tests for externals 421 _caught_exceptions = [ImportError, AttributeError, RuntimeError] 422 423 # check whether RPy is involved and catch its excpetions as well. 424 # however, try to determine whether this is really necessary, as 425 # importing RPy also involved starting a full-blown R session, which can 426 # take seconds and therefore is quite nasty... 427 if dep.count('rpy') or _KNOWN[dep].count('rpy'): 428 try: 429 if dep == 'rpy': 430 __check_rpy() # needed to be run to adjust options first 431 else: 432 if exists('rpy'): 433 # otherwise no need to add anything -- test 434 # would fail since rpy isn't available 435 from rpy import RException 436 _caught_exceptions += [RException] 437 except: 438 pass 439 440 441 estr = '' 442 try: 443 exec _KNOWN[dep] 444 result = True 445 except tuple(_caught_exceptions), e: 446 estr = ". Caught exception was: " + str(e) 447 448 if __debug__: 449 debug('EXT', "Presence of %s is%s verified%s" % 450 (dep, {True:'', False:' NOT'}[result], estr)) 451 452 if not result: 453 if raiseException \ 454 and cfg.getboolean('externals', 'raise exception', True): 455 raise RuntimeError, "Required external '%s' was not found" % dep 456 if issueWarning is not None \ 457 and cfg.getboolean('externals', 'issue warning', True): 458 if issueWarning is True: 459 warning("Required external '%s' was not found" % dep) 460 else: 461 warning(issueWarning) 462 463 464 # store result in config manager 465 if not cfg.has_section('externals'): 466 cfg.add_section('externals') 467 if result: 468 cfg.set('externals', 'have ' + dep, 'yes') 469 else: 470 cfg.set('externals', 'have ' + dep, 'no') 471 472 return result
473 474
475 -def testAllDependencies(force=False):
476 """ 477 Test for all known dependencies. 478 479 :Parameters: 480 force : boolean 481 Whether to force the test even if it has already been 482 performed. 483 484 """ 485 # loop over all known dependencies 486 for dep in _KNOWN: 487 if not exists(dep, force): 488 warning("%s is not available." % dep) 489 490 if __debug__: 491 debug('EXT', 'The following optional externals are present: %s' \ 492 % [ k[5:] for k in cfg.options('externals') 493 if k.startswith('have') \ 494 and cfg.getboolean('externals', k) == True ])
495