Changeset 295

Show
Ignore:
Timestamp:
11/06/08 11:32:13 (2 months ago)
Author:
pierre
Message:

Attempt to set up a common framework for the dependencies. Since NeuroTools will be a lot of heterogeneous subparts, a lots of import will be done. I think, as proposed by Eilif, that we should try to gather as much as possible of those import and check in the init file. Then warning messages will be automatically displayed according to what you're loading. So the idea is that you add non-specific import in the init dependencies dictionnary. Then, in your code, you just have to import the function check_dependency() from NeuroTools.init. By doing check_dependency(.. your package name ...), you'll know if this is present or not, and if not a message is sent to the user automatically. Hope all the packages are working

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/__init__.py

    r247 r295  
    11__all__ = ['analysis', 'parameters', 'plotting', 'sandbox', 'signals', 'stgen', 'utilities', 'io'] 
    22 
    3 """from tables import __version__ 
     3######################################################### 
     4## ALL DEPENDENCIES SHOULD BE GATHERED HERE FOR CLARITY 
     5######################################################### 
     6 
     7# The nice thing would be to gathered every non standard 
     8# dependency here, in order to centralizz the warning 
     9# messages and the check 
     10dependencies = {'numpy' : {'website' : 'http://numpy.scipy.org/' , 'is_present' : False, 'check':False}, 
     11                'pylab' : {'website' : 'http://matplotlib.sourceforge.net/', 'is_present' : False, 'check':False}, 
     12                'tables': {'website' : 'http://www.pytables.org/moin' , 'is_present' : False, 'check':False}, 
     13                'psyco' : {'website' : 'http://psyco.sourceforge.net/', 'is_present' : False, 'check':False}, 
     14                'pygsl' : {'website' : 'http://pygsl.sourceforge.net/', 'is_present' : False, 'check':False}, 
     15                'PIL'   : {'website' : 'http://www.pythonware.com/products/pil/', 'is_present':False, 'check':False}, 
     16                'scipy' : {'website' : 'http://numpy.scipy.org/' , 'is_present' : False, 'check':False}, 
     17                ## Add here your extensions ### 
     18               } 
     19 
     20 
     21 
     22 
     23 
     24 
     25######################################################### 
     26## Function to display error messages on the dependencies 
     27######################################################### 
     28 
     29def get_warning(name): 
     30    return ''' ----------------- Dependency Warning --------------------- 
     31** %s ** package is not installed.  
     32To have functions using %s please install the package. 
     33website : %s 
     34''' %(name, name, dependencies[name]['website']) 
     35 
     36def check_numpy_version(): 
     37    import numpy 
     38    numpy_version = numpy.__version__.split(".")[0:2] 
     39    numpy_version = float(".".join(numpy_version)) 
     40    if numpy_version >= 1.2: 
     41        return True 
     42    else: 
     43        return False 
    444 
    545def check_pytables_version(): 
    646   #v = [int(s) for s in __version__.split('.')] 
    7    if __version__<= 2: #1.4: #v[0] < 1 or (v[0] == 1 and v[1] < 4): 
     47   if tables.__version__<= 2: #1.4: #v[0] < 1 or (v[0] == 1 and v[1] < 4): 
    848       raise Exception('PyTables version must be >= 1.4, installed version is %s' % __version__) 
    9 check_pytables_version() 
    10 """ 
     49 
     50def check_dependency(name): 
     51    if dependencies[name]['check']: 
     52        return dependencies[name]['is_present'] 
     53    else: 
     54        try: 
     55            exec("import %s" %name) 
     56            dependencies[name]['is_present'] = True 
     57        except ImportError: 
     58            print get_warning(name) 
     59        dependencies[name]['check'] = True 
     60        return dependencies[name]['is_present'] 
     61 
     62 
     63#for name in dependencies: 
     64    #if not dependencies[name]['check']: 
     65        #is_present = check_dependency(name) 
     66        #dependencies[name]['check'] = True 
     67    #if is_present: 
     68        #if name == 'numpy': 
     69            #newnum = check_numpy_version() 
     70        #if name == 'pytables': 
     71 
     72 
     73 
     74 
     75 
     76 
     77 
     78 
     79 
     80 
     81 
    1182 
    1283# Setup fancy logging 
  • trunk/src/analysis.py

    r215 r295  
    77""" 
    88 
    9 import os, numpy 
     9import os 
     10from NeuroTools.__init__ import check_dependency 
     11 
     12if check_dependency('numpy'): 
     13    import numpy 
     14 
     15 
    1016 
    1117def ccf(x, y, axis=None): 
  • trunk/src/io.py

    r293 r295  
    1 import numpy, os, logging, cPickle 
     1from NeuroTools.__init__ import check_dependency 
     2 
     3import os, logging, cPickle 
    24DEFAULT_BUFFER_SIZE = 10000 
    35 
    4 try: 
    5     import pytable 
    6     ENABLE_HDF5 = True 
    7 except ImportError: 
    8     ENABLE_HDF5 = False 
     6if check_dependency('numpy'): 
     7    import numpy 
     8 
    99 
    1010 
  • trunk/src/parameters.py

    r280 r295  
    77""" 
    88 
    9 import urllib # to be replaced with srblib 
     9import urllib, copy, warnings # to be replaced with srblib 
    1010from urlparse import urlparse 
    11 import copy 
    12 import warnings 
    13 import numpy 
    14 import numpy.random 
     11from NeuroTools.__init__ import check_dependency 
    1512from NeuroTools.random import ParameterDist, GammaDist, UniformDist, NormalDist 
     13 
     14if check_dependency('numpy'): 
     15    import numpy 
     16    import numpy.random 
     17 
    1618 
    1719def isiterable(x): 
  • trunk/src/plotting.py

    r291 r295  
    77 
    88 
    9 import numpy, sys 
    10 numpy_version = numpy.__version__.split(".")[0:2] 
    11 numpy_version = float(".".join(numpy_version)) 
     9import sys 
     10from NeuroTools.__init__ import check_dependency 
    1211 
    1312 
    1413# Check availability of pylab (essential!) 
    15 try
     14if check_dependency('pylab')
    1615    import pylab 
    1716    from matplotlib.figure import Figure 
    1817    from matplotlib.lines import Line2D 
    1918    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas 
    20 except ImportError: 
    21     MATPLOTLIB_ERROR = \ 
    22 """ 
    23 ERROR: Matplolib not detected! The module NeuroTools.plotting depends on this package. 
    24 Please install the Matplotlib package --> http://matplotlib.sourceforge.net/ 
    25 """ 
    26     raise Exception(MATPLOTLIB_ERROR) 
     19 
     20 
     21if check_dependency('numpy'): 
     22    import numpy 
    2723 
    2824# Check availability of PIL 
    29 try : 
     25PILIMAGEUSE = check_dependency('PIL') 
     26if PILIMAGEUSE: 
    3027    import PIL.Image as Image 
    31     PILIMAGEUSE = True 
    32 except ImportError: 
    33     PILIMAGE_WARNING = \ 
    34 """ 
    35 WARNING: Python Imaging Library PIL not detected! Functions that make use of PIL will not be supported! 
    36 """ 
    37     PILIMAGEUSE = False 
    38     print PILIMAGE_WARNING 
    3928 
    4029 
  • trunk/src/random.py

    r280 r295  
    11# Classes for specifiying a parameter by a statistical distribution 
    22 
    3 import numpy 
    4 import numpy.random 
     3from NeuroTools.__init__ import check_dependency 
    54 
    6 try: 
     5if check_dependency('numpy'): 
     6    import numpy 
     7    import numpy.random 
     8 
     9have_scipy = check_dependency('scipy') 
     10if have_scipy: 
    711    import scipy.stats 
    8     have_scipy = True 
    9 except ImportError: 
    10     warnings.warn('scipy not found. Some distributions will be unable to be realized.', 
    11                   ImportWarning) 
    12     have_scipy = False 
     12 
    1313     
    1414class ParameterDist(object): 
  • trunk/src/signals.py

    r293 r295  
    55""" 
    66 
    7 import numpy, os, logging, re 
     7import os, re 
     8from NeuroTools.__init__ import check_dependency, check_numpy_version 
    89from NeuroTools import analysis 
    910from NeuroTools.io import * 
    1011from NeuroTools.plotting import get_display, set_axis_limits, set_labels, SimpleMultiplot 
    1112 
    12 numpy_version = numpy.__version__.split(".")[0:2] 
    13 numpy_version = float(".".join(numpy_version)) 
    14  
    15 MATPLOTLIB_ERROR = \ 
    16 " ----------------- MATPLOTLIB Warning : ---------------------\n \ 
    17 Matplolib NOT detected so plots will be disabled.\n \ 
    18 To turn them on, please install the Matplotlib package\n \ 
    19 --> http://matplotlib.sourceforge.net/" 
    20  
    21 PSYCO_ERROR = \ 
    22 "----------------- PSYCO Warning : ---------------------\n \ 
    23 Psyco NOT detected. This is an optionnal package that \n \ 
    24 could slightly speed up some NeuroTools functions.\n \ 
    25 --> http://psyco.sourceforge.net/" 
    26  
    27  
    28 NUMPY_WARNING = \ 
    29 " ------------------- NUMPY Warning : -------------------------\n \ 
    30 You should think about updating your numpy version to\n \ 
    31 Numpy >= 1.2\n \ 
    32 --> http://numpy.scipy.org/\n" 
    33  
    34 if numpy_version >= 1.2: 
    35     newnum = True 
    36 else: 
    37     newnum = False 
    38     print NUMPY_WARNING 
    39  
    40 try : 
    41     import pylab 
    42     ENABLE_PLOTS     = True 
    43 except ImportError: 
    44     ENABLE_PLOTS     = False 
    45     print MATPLOTLIB_ERROR 
    46  
    47 try :  
     13if check_dependency('psyco'): 
    4814    import psyco 
    4915    psyco.full() 
    50 except ImportError: 
    51     print PSYCO_ERROR 
    52  
    53  
    54  
    55  
    56  
     16 
     17if check_dependency('numpy'): 
     18    import numpy 
     19    newnum = check_numpy_version() 
     20 
     21ENABLE_PLOTS = check_dependency('pylab') 
     22if ENABLE_PLOTS: 
     23    import pylab 
     24else: 
     25    MATPLOTLIB_ERROR = "No pylab pacakge have been detected" 
    5726 
    5827class SpikeTrain(object): 
  • trunk/src/stgen.py

    r192 r295  
    44# TODO make it generate spiketrains? 
    55 
    6 try: 
     6from NeuroTools.__init__ import check_dependency 
     7 
     8have_gsl = check_dependency('pygsl') 
     9if have_gsl: 
    710    import pygsl 
    8     have_gsl = True 
    9 except ImportError: 
    10     print "Warning: pygsl not available" 
    11     have_gsl = False 
    12 from numpy import array, log 
    13 import numpy 
    14  
     11 
     12if check_dependency('numpy'): 
     13    from numpy import array, log 
     14    import numpy 
    1515 
    1616 
  • trunk/test/test_signals.py

    r287 r295  
    55from NeuroTools import signals, io 
    66import numpy, unittest, os 
    7 from NeuroTools.signals import numpy_version 
    8  
    9 if numpy_version >= 1.2: 
    10     newnum = True 
    11 else: 
    12     newnum = False 
     7from NeuroTools.__init__ import check_numpy_version 
     8newnum = check_numpy_version() 
    139 
    1410try :