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

Source Code for Module mvpa.base.externals

  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  """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   
 18  if __debug__: 
 19      from mvpa.base import debug 
 20   
21 -def __check_shogun(bottom_version, custom_versions=[2456]):
22 """Check if version of shogun is high enough (or custom known) to 23 be enabled in the testsuite""" 24 import shogun.Classifier as __sc 25 ver = __sc.Version_get_version_revision() 26 if (ver in custom_versions) or (ver >= bottom_version) : # custom built 27 return True 28 else: 29 raise ImportError, 'Version %s is smaller than needed %s' % \ 30 (ver, bottom_version)
31 32
33 -def __check_weave():
34 """Apparently presence of scipy is not sufficient since some 35 versions experience problems. E.g. in Sep,Oct 2008 lenny's weave 36 failed to work. May be some other converter could work (? See 37 http://lists.debian.org/debian-devel/2008/08/msg00730.html for a 38 similar report. 39 40 Following simple snippet checks compilation of the basic code using 41 weave 42 """ 43 from scipy import weave 44 from scipy.weave import converters, build_tools 45 import numpy as N 46 # to shut weave up 47 import sys 48 # we can't rely on weave at all at the restoring argv. On etch box 49 # restore_sys_argv() is apparently is insufficient 50 oargv = sys.argv[:] 51 ostdout = sys.stdout 52 if not( __debug__ and 'EXT_' in debug.active): 53 from StringIO import StringIO 54 sys.stdout = StringIO() 55 # *nix specific solution to shut weave up. 56 # Some users must complain and someone 57 # needs to fix this to become more generic. 58 cargs = [">/dev/null", "2>&1"] 59 else: 60 cargs = [] 61 fmsg = None 62 try: 63 data = N.array([1,2,3]) 64 counter = weave.inline("data[0]=fabs(-1);", ['data'], 65 type_converters=converters.blitz, 66 verbose=0, 67 extra_compile_args=cargs, 68 compiler = 'gcc') 69 except Exception, e: 70 fmsg = "Failed to build simple weave sample." \ 71 " Exception was %s" % str(e) 72 73 sys.stdout = ostdout 74 # needed to fix sweave which might "forget" to restore sysv 75 # build_tools.restore_sys_argv() 76 sys.argv = oargv 77 if fmsg is not None: 78 raise ImportError, fmsg 79 else: 80 return "Everything is cool"
81 82
83 -def __check_atlas_family(family):
84 # XXX I guess pylint will dislike it a lot 85 from mvpa.atlases.warehouse import KNOWN_ATLAS_FAMILIES 86 names, pathpattern = KNOWN_ATLAS_FAMILIES[family] 87 filename = pathpattern % {'name':names[0]} 88 if not os.path.exists(filename): 89 raise ImportError, "Cannot find file %s for atlas family %s" \ 90 % (filename, family) 91 pass
92 93 94 # contains list of available (optional) external classifier extensions 95 _KNOWN = {'libsvm':'import mvpa.clfs.libsvmc._svm as __; x=__.convert2SVMNode', 96 'nifti':'from nifti import NiftiImage as __', 97 'nifti >= 0.20081017.1': 98 'from nifti.nifticlib import detachDataFromImage as __', 99 'ctypes':'import ctypes as __', 100 'shogun':'import shogun as __', 101 'shogun.mpd': 'import shogun.Classifier as __; x=__.MPDSVM', 102 'shogun.lightsvm': 'import shogun.Classifier as __; x=__.SVMLight', 103 'shogun.svrlight': 'from shogun.Regression import SVRLight as __', 104 'scipy': "import scipy as __", 105 'weave': "__check_weave()", 106 'pywt': "import pywt as __", 107 'rpy': "import rpy as __", 108 'lars': "import rpy; rpy.r.library('lars')", 109 'pylab': "import pylab as __", 110 'openopt': "import scikits.openopt as __", 111 'mdp': "import mdp as __", 112 'sg_fixedcachesize': "__check_shogun(3043)", 113 # 3318 corresponds to release 0.6.4 114 'sg >= 0.6.4': "__check_shogun(3318)", 115 'hcluster': "import hcluster as __", 116 'griddata': "import griddata as __", 117 'cPickle': "import cPickle as __", 118 'gzip': "import gzip as __", 119 'lxml': "from lxml import objectify as __", 120 'atlas_pymvpa': "__check_atlas_family('pymvpa')", 121 'atlas_fsl': "__check_atlas_family('fsl')", 122 } 123 124 _caught_exceptions = [ImportError, AttributeError, RuntimeError] 125 """Exceptions which are silently caught while running tests for externals""" 126 try: 127 import rpy 128 _caught_exceptions += [rpy.RException] 129 except: 130 pass 131
132 -def exists(dep, force=False, raiseException=False):
133 """ 134 Test whether a known dependency is installed on the system. 135 136 This method allows us to test for individual dependencies without 137 testing all known dependencies. It also ensures that we only test 138 for a dependency once. 139 140 :Parameters: 141 dep : string or list of string 142 The dependency key(s) to test. 143 force : boolean 144 Whether to force the test even if it has already been 145 performed. 146 raiseException : boolean 147 Whether to raise RuntimeError if dependency is missing. 148 149 """ 150 # if we are provided with a list of deps - go through all of them 151 if isinstance(dep, list) or isinstance(dep, tuple): 152 results = [ exists(dep_, force, raiseException) for dep_ in dep ] 153 return bool(reduce(lambda x,y: x and y, results, True)) 154 155 # where to look in cfg 156 cfgid = 'have ' + dep 157 158 # prevent unnecessarry testing 159 if cfg.has_option('externals', cfgid) \ 160 and not cfg.getboolean('externals', 'retest', default='no') \ 161 and not force: 162 if __debug__: 163 debug('EXT', "Skip restesting for '%s'." % dep) 164 return cfg.getboolean('externals', cfgid) 165 166 # default to 'not found' 167 result = False 168 169 if not _KNOWN.has_key(dep): 170 raise ValueError, "%s is not a known dependency key." % (dep) 171 else: 172 # try and load the specific dependency 173 if __debug__: 174 debug('EXT', "Checking for the presence of %s" % dep) 175 176 estr = '' 177 try: 178 exec _KNOWN[dep] 179 result = True 180 except tuple(_caught_exceptions), e: 181 estr = ". Caught exception was: " + str(e) 182 183 if __debug__: 184 debug('EXT', "Presence of %s is%s verified%s" % 185 (dep, {True:'', False:' NOT'}[result], estr)) 186 187 if not result and raiseException \ 188 and cfg.getboolean('externals', 'raise exception', True): 189 raise RuntimeError, "Required external '%s' was not found" % dep 190 191 # store result in config manager 192 if not cfg.has_section('externals'): 193 cfg.add_section('externals') 194 if result: 195 cfg.set('externals', 'have ' + dep, 'yes') 196 else: 197 cfg.set('externals', 'have ' + dep, 'no') 198 199 return result
200 201
202 -def testAllDependencies(force=False):
203 """ 204 Test for all known dependencies. 205 206 :Parameters: 207 force : boolean 208 Whether to force the test even if it has already been 209 performed. 210 211 """ 212 # loop over all known dependencies 213 for dep in _KNOWN: 214 if not exists(dep, force): 215 warning("Known dependency %s is not present or is broken, " \ 216 "thus not available, or only available in an " \ 217 "outdated/insufficient version." % dep) 218 219 if __debug__: 220 debug('EXT', 'The following optional externals are present: %s' \ 221 % [ k[5:] for k in cfg.options('externals') 222 if k.startswith('have')])
223