1
2
3
4
5
6
7
8
9 """Helper to verify presence of external libraries and modules
10 """
11
12 __docformat__ = 'restructuredtext'
13
14 from mvpa.misc import warning
15
16 if __debug__:
17 from mvpa.misc import debug
18
20 """Check if version of shogun is high enough (or custom known) to
21 be enabled in the testsuite"""
22 import shogun.Classifier as __sc
23 ver = __sc.Version_get_version_revision()
24 if (ver in custom_versions) or (ver >= bottom_version) :
25 return True
26 else:
27 raise ImportError, 'Version %s is smaller than needed %s' % \
28 (ver, bottom_version)
29
30
31
32 _KNOWN = {'libsvm':'import mvpa.clfs.libsvm._svm as __; x=__.convert2SVMNode',
33 'nifti':'from nifti import NiftiImage as __',
34 'shogun':'import shogun as __',
35 'shogun.lightsvm': 'import shogun.Classifier as __; x=__.SVMLight',
36 'shogun.svrlight': 'from shogun.Regression import SVRLight as __',
37 'rpy': "import rpy",
38 'lars': "import rpy; rpy.r.library('lars')",
39 'pylab': "import pylab as __",
40 'sg_fixedcachesize': "__check_shogun(3043)",
41 }
42
43 _VERIFIED = {}
44
45 _caught_exceptions = [ImportError, AttributeError]
46 """Exceptions which are silently caught while running tests for externals"""
47 try:
48 import rpy
49 _caught_exceptions += [rpy.RException]
50 except:
51 pass
52
54 """
55 Test whether a known dependency is installed on the system.
56
57 This method allows us to test for individual dependencies without
58 testing all known dependencies. It also ensures that we only test
59 for a dependency once.
60
61 :Parameters:
62 dep : string
63 The dependency key to test.
64 force : boolean
65 Whether to force the test even if it has already been
66 performed.
67
68 """
69 if _VERIFIED.has_key(dep) and not force:
70
71 return _VERIFIED[dep]
72 elif not _KNOWN.has_key(dep):
73 warning("%s is not a known dependency key." % (dep))
74 return False
75 else:
76
77
78 _VERIFIED[dep] = False
79
80 if __debug__:
81 debug('EXT', "Checking for the presence of %s" % dep)
82
83 try:
84 exec _KNOWN[dep]
85 _VERIFIED[dep] = True
86 except tuple(_caught_exceptions):
87 pass
88
89 if __debug__:
90 debug('EXT', "Presence of %s is%s verified" %
91 (dep, {True:'', False:' NOT'}[_VERIFIED[dep]]))
92
93 return _VERIFIED[dep]
94
95
97 """
98 Test for all known dependencies.
99
100 :Parameters:
101 force : boolean
102 Whether to force the test even if it has already been
103 performed.
104
105 """
106
107 for dep in _KNOWN:
108 if not exists(dep, force):
109 warning("Known dependency %s is not present, thus not available." \
110 % dep)
111
112 if __debug__:
113 debug('EXT', 'The following optional externals are present: %s' \
114 % [ k for k in _VERIFIED.keys() if _VERIFIED[k]])
115