Package topo
[hide private]
[frames] | no frames]

Source Code for Package topo

  1  """ 
  2  Topographica cortical map simulator package. 
  3   
  4  Topographica is designed as a collection of packages from which 
  5  elements can be selected to model specific systems.  For more 
  6  information, see the individual subpackages:: 
  7   
  8    base           - Core Topographica functions and classes 
  9    plotting       - Visualization functions and classes 
 10    analysis       - Analysis functions and classes (besides plotting) 
 11    tkgui          - Tk-based graphical user interface (GUI) 
 12    command        - High-level user commands 
 13    misc           - Various useful independent modules 
 14   
 15  The Topographica primitives library consists of a family of classes 
 16  that can be used with the above functions and classes:: 
 17   
 18    sheet          - Sheet classes: 2D arrays of processing units 
 19    projection     - Projection classes: connections between Sheets 
 20    pattern        - PatternGenerator classes: 2D input or weight patterns  
 21    ep             - EventProcessor classes: other simulation objects 
 22    transferfn     - Transfer functions, for e.g. normalization or squashing 
 23    responsefn     - Calculate the response of a Projection 
 24    learningfn     - Adjust weights for a Projection 
 25    coordmapper    - CoordinateMapperFn classes: map coords between Sheets 
 26   
 27  Each of the library directories can be extended with new classes of 
 28  the appropriate type, just by adding a new .py file to that directory. 
 29  E.g. new PatternGenerator classes can be added to pattern/, and will 
 30  then show up in the GUI menus as potential input patterns. 
 31   
 32  $Id: __init__.py 11201 2010-07-14 13:07:21Z ceball $ 
 33  """ 
 34  __version__ = "$Revision: 11201 $" 
 35   
 36  # The tests and the GUI are omitted from this list, and have to be 
 37  # imported explicitly if desired. 
 38  __all__ = ['analysis', 
 39             'base', 
 40             'command', 
 41             'coordmapper', 
 42             'ep', 
 43             'learningfn', 
 44             'misc', 
 45             'numbergen', 
 46             'transferfn', 
 47             'pattern', 
 48             'plotting', 
 49             'projection', 
 50             'responsefn', 
 51             'sheet'] 
 52   
 53  # get set by the topographica script 
 54  release = '' 
 55  version = '' 
 56   
 57   
 58  import param  
 59  import os 
 60   
 61   
 62  # Default location in which to create files 
 63  _default_output_path = os.path.join(os.path.expanduser("~"),'topographica') 
 64  if not os.path.exists(_default_output_path): 
 65      print "Creating %s"%_default_output_path 
 66      os.mkdir(_default_output_path) 
 67   
 68  # Location of topo/ package. This kind of thing won't work with py2exe 
 69  # etc. Need to see if we can get rid of it. 
 70  _package_path = os.path.split(__file__)[0] 
 71   
 72  param.normalize_path.prefix = _default_output_path 
 73  param.resolve_path.search_paths+=([_default_output_path] + [_package_path]) 
 74   
 75   
 76   
 77  # CEBALERT: (about PIL) 
 78  # PIL (i.e. the Imaging package) can be installed so that it's 
 79  # e.g. "from PIL import Image" or just "import Image". 
 80  try: 
 81      import Image 
 82  except ImportError: 
 83      from PIL import Image, ImageOps, ImageDraw, ImageFont 
 84      import sys 
 85      sys.modules['Image']=Image 
 86      sys.modules['ImageOps']=ImageOps 
 87      sys.modules['ImageDraw']=ImageDraw 
 88      sys.modules['ImageFont']=ImageFont 
 89   
 90  # ImageTk is completely optional 
 91  try: 
 92      import ImageTk 
 93  except ImportError: 
 94      try: 
 95          from PIL import ImageTk 
 96          import sys 
 97          sys.modules['ImageTk']=ImageTk 
 98      except ImportError: 
 99          pass 
100   
101   
102  # CEBALERT: can we move these pickle support functions elsewhere? 
103  # In fact, can we just gather all the non-legacy pickle garbage into one 
104  # place? I'd even like to get stuff out of classes, but I guess that 
105  # wouldn't always be desirable. 
106  # (What prompted this note is that, apart from the clutter the pickle 
107  # methods add to classes, I cannot remember all the ways one can 
108  # support pickling; the answer to any new pickle problem is often 
109  # spread throughout all the pickle methods I've ever added...) 
110   
111   
112  # (note that these _pickle_support functions also work for deep copying) 
113 -def _numpy_ufunc_pickle_support():
114 """ 115 Allow instances of numpy.ufunc to pickle. 116 """ 117 # Remove this when numpy.ufuncs themselves support pickling. 118 # Code from Robert Kern; see: 119 #http://news.gmane.org/find-root.php?group=gmane.comp.python.numeric.general&article=13400 120 from numpy import ufunc 121 import copy_reg 122 123 def ufunc_pickler(ufunc): 124 """Return the ufunc's name""" 125 return ufunc.__name__
126 127 copy_reg.pickle(ufunc,ufunc_pickler) 128 129 _numpy_ufunc_pickle_support() 130 131 132 # CBENHANCEMENT: should investigate Python 2.6's fraction module (at 133 # least to replace the FixedPoint fallback). 134 # http://docs.python.org/whatsnew/2.6.html 135
136 -def _mpq_pickle_support():
137 """Allow instances of gmpy.mpq to pickle.""" 138 from gmpy import mpq 139 mpq_type = type(mpq(1,10)) # CEBALERT: any idea how to get this properly? 140 import copy_reg 141 copy_reg.pickle(mpq_type,lambda q: (mpq,(q.digits(),)))
142 143 144
145 -def _instance_method_pickle_support():
146 """Allow instance methods to pickle.""" 147 # CB: well, it seems to work - maybe there are cases where this 148 # wouldn't work? 149 # Alternative technique (totally different approach), but would 150 # only work with pickle (not cPickle): 151 # http://code.activestate.com/recipes/572213/ 152 def _pickle_instance_method(mthd): 153 mthd_name = mthd.im_func.__name__ 154 obj = mthd.im_self 155 return getattr, (obj,mthd_name)
156 157 import copy_reg, types 158 copy_reg.pickle(types.MethodType, _pickle_instance_method) 159 160 _instance_method_pickle_support() 161 162 163 # Set the default value of Simulation.time_type to gmpy.mpq. If gmpy 164 # is unavailable, use the slower fixedpoint.FixedPoint. Also, in that 165 # case, provide a fake gmpy.mpq (to allow e.g. pickled test data to be 166 # loaded). If neither gmpy nor fixedpoint is available, the default 167 # will be float. Python 2.7 has suitable fraction.Fraction() 168 time_type = None 169 try: 170 import gmpy 171 time_type = gmpy.mpq 172 time_type_args = () 173 _mpq_pickle_support() 174 except ImportError: 175 import topo.misc.fixedpoint as fixedpoint 176 param.Parameterized().warning('gmpy.mpq not available; using slower fixedpoint.FixedPoint for simulation time.') 177 time_type = fixedpoint.FixedPoint 178 time_type_args = (4,) # gives precision=4 179 180 from topo.misc.util import gmpyImporter 181 import sys 182 sys.meta_path.append(gmpyImporter()) 183 184 from topo.base.simulation import Simulation 185 186 if time_type is not None: 187 Simulation.time_type = time_type 188 Simulation.time_type_args = time_type_args 189 190 sim = Simulation() 191 192 193 194 195
196 -def about(display=True):
197 """Print release and licensing information.""" 198 199 ABOUT_TEXT = """ 200 Pre-release version %s (%s) of Topographica; an updated 201 version may be available from topographica.org. 202 203 This program is free, open-source software available under the BSD 204 license (http://www.opensource.org/licenses/bsd-license.php). 205 """%(release,version) 206 if display: 207 print ABOUT_TEXT 208 else: 209 return ABOUT_TEXT
210 211 212 # Set most floating-point errors to be fatal for safety; see 213 # topo/misc/patternfn.py for examples of how to disable 214 # the exceptions when doing so is safe. Underflow is always 215 # considered safe; e.g. input patterns can be very small 216 # at large distances, and when they are scaled by small 217 # weights underflows are common and not a problem. 218 from numpy import seterr 219 old_seterr_settings=seterr(all="raise",under="ignore") 220