Package topo :: Package misc :: Module genexamples
[hide private]
[frames] | no frames]

Source Code for Module topo.misc.genexamples

  1  """ 
  2  Commands for running the examples files in various ways. 
  3   
  4  Like a Makefile: contains a list of targets (and groups of targets) 
  5  that specify various commands to run. 
  6   
  7  E.g. 
  8   
  9    topographica -c 'from topo.misc.genexamples import generate; generate(targets=["all_quick","saved_examples"])'  
 10   
 11  Runs the 'all_quick' target if called without any arguments:  
 12   
 13    topographica -c 'from topo.misc.genexamples import generate; generate()' 
 14   
 15  To add new single targets, add to the targets dictionary; 
 16  for groups of targets, add to the group_targets dictionary. 
 17   
 18  $Id$ 
 19  """ 
 20   
 21  __version__='$Revision$' 
 22   
 23  # JABALERT: Should be cut down and simplified; most of what it does is 
 24  # for historical rather than functional reasons.  E.g. the quick 
 25  # options should be in the tests, rather than here, and then the other 
 26  # options need not specify how long they should be run; instead that 
 27  # should be set by a parameter in the .ty file and then respected by 
 28  # this file.  The .ty file could also specify a set of default 
 29  # analysis functions to run, e.g. selecting from some options to be 
 30  # made available in topo.command.basic, and if so this file need not 
 31  # have any handling for specific scripts.  Meanwhile, at least it works. 
 32   
 33  # Note: has none of the Makefile's dependency processing, so just does 
 34  # what you tell it (i.e. over-writes existing files). 
 35   
 36  import sys 
 37  import os.path 
 38   
 39  from os import system 
 40   
 41  import param 
 42  from param import ParamOverrides 
 43   
 44  ### Convenience functions 
45 -def snapshot(filename):
46 """Return a command for saving a snapshot named filename.""" 47 return "from topo.command.basic import save_snapshot ; save_snapshot('%s')"%filename
48
49 -def or_analysis():
50 """Return a command for orientation analysis.""" 51 return """ 52 from topo.command.analysis import measure_or_pref; \ 53 from topo.command.pylabplot import measure_position_pref,measure_cog,measure_or_tuning_fullfield; \ 54 measure_or_pref(); \ 55 #measure_position_pref(); \ 56 measure_cog(); \ 57 #measure_or_tuning_fullfield() 58 """
59
60 -def retinotopy_analysis():
61 """Return a command for retinotopy analysis.""" 62 return "from topo.command.pylabplot import measure_position_pref,measure_cog ;\ 63 measure_position_pref(); \ 64 measure_cog()"
65 ### 66 67 68
69 -def run(examples,script_name,density=None,commands=["topo.sim.run(1)"]):
70 """ 71 Return a complete command for running the given topographica 72 example script (i.e. a script in the examples/ directory) at the 73 given density, along with any additional commands. 74 """ 75 76 if density: 77 density_cmd = ' -c "default_density='+`density`+'" ' 78 else: 79 density_cmd = " " 80 81 cmds = "" 82 for c in commands: 83 cmds+=' -c "'+c+'"' 84 85 topographica = sys.argv[0] 86 script = os.path.join(examples,script_name) 87 88 return topographica+density_cmd+script+' '+cmds
89 90 91 92 scripts = { 93 'hierarchical':'hierarchical.ty', 94 'lissom_or' :'lissom_or.ty', 95 'lissom_oo_or':'lissom_oo_or.ty', 96 'som_retinotopy':'som_retinotopy.ty', 97 'trickysyntax':'hierarchical.ty', 98 'obermayer_pnas90':'obermayer_pnas90.ty', 99 'lissom_fsa':'lissom_fsa.ty', 100 'gcal':'gcal.ty', 101 'lissom_oo_or_10000.typ':'lissom_oo_or.ty', 102 'lissom_fsa_10000.typ':'obermayer_pnas90.ty', 103 'obermayer_pnas90_40000.typ':'obermayer_pnas90.ty', 104 'som_retinotopy_40000.typ':'som_retinotopy.ty', 105 'gcal_10000.typ':'gcal.ty'} 106 107
108 -def copy_examples():
109 # topographica -c "from topo.misc.genexamples import copy_examples; copy_examples()" 110 examples = find_examples() 111 locn = os.path.join(param.normalize_path.prefix,"examples") 112 if os.path.exists(locn): 113 print "%s already exists; delete or rename it if you want to re-copy the examples."%locn 114 return 115 else: 116 print "Creating %s"%locn 117 import shutil 118 print "Copying %s to %s"%(examples,locn) 119 shutil.copytree(examples,locn)
120 121 126
127 -def find_examples(specified_examples=None,dirs=None):
128 import topo 129 130 if not specified_examples: 131 # CEBALERT: hack! 132 specified_examples = ["hierarchical","lissom_oo_or","som_retinotopy"] 133 134 135 if not dirs: 136 candidate_example_dirs = [ 137 os.path.join(os.path.expanduser("~"),'topographica/examples'), 138 # version-controlled topographica dir 139 os.path.join(topo._package_path,"../examples"), 140 # debian package 141 os.path.join(topo._package_path,"../../../share/topographica/examples"), 142 # setup.py package 143 os.path.join(topo._package_path,"../../../../share/topographica/examples"), 144 # egg 145 os.path.join(topo._package_path,"../share/topographica/examples"), 146 # expected bdist_mpkg location... 147 "/usr/local/share/topographica/examples", 148 # ...but actually this; not sure why 149 "/usr/local/share/share/topographica/examples"] 150 else: 151 candidate_example_dirs = dirs 152 153 ced = [os.path.normpath(d) for d in candidate_example_dirs] 154 candidate_example_dirs = ced 155 # CEBALERT: horrible way to find directory that contains all the 156 # examples specified. 157 examples = None 158 for d in candidate_example_dirs: 159 if not examples: 160 for cmd in specified_examples: 161 if os.path.isfile(os.path.join(d,scripts[cmd])): 162 examples = d 163 else: 164 examples = False 165 166 if examples is False: 167 break 168 169 return examples
170 171 172 # CEBALERT: should be rewritten! 173
174 -def _stuff(specified_targets):
175 176 # shortcuts for executing multiple targets 177 group_targets = dict( all_quick=["hierarchical","som_retinotopy","lissom_oo_or"], 178 all_long=["lissom_oo_or_10000.typ","som_retinotopy_40000.typ", 179 "lissom_or_10000.typ","lissom_fsa_10000.typ"], 180 saved_examples=["lissom_oo_or_10000.typ"]) 181 182 ### Create the list of commands to execute either by getting the 183 ### command labels from a target, or by inserting the command label 184 # CB: I don't know any string methods; I'm sure this can 185 # be simplified! 186 command_labels=[] 187 188 189 for a in specified_targets: 190 if a in group_targets: 191 command_labels+=group_targets[a] 192 else: 193 command_labels.append(a) 194 195 examples = find_examples(specified_examples=command_labels) 196 197 if not examples: 198 raise IOError("Could not find examples.") 199 else: 200 print "Found examples in %s"%examples 201 202 # CB: so much repeated typing... 203 204 available_targets = { 205 "hierarchical": run(examples,scripts["hierarchical"],density=4), 206 "lissom_or": run(examples,scripts["lissom_or"],density=4), 207 "lissom_oo_or": run(examples,scripts["lissom_oo_or"],density=4), 208 "som_retinotopy": run(examples,scripts["som_retinotopy"],density=4), 209 210 "trickysyntax":run(examples,scripts["hierarchical"],commands=["topo.sim.run(1)", 211 "print 'printing a string'"]), 212 213 "lissom_oo_or_10000.typ":run(examples,scripts["lissom_oo_or"], 214 commands=["topo.sim.run(10000)", 215 or_analysis(), 216 snapshot("lissom_oo_or_10000.typ")]), 217 218 219 "lissom_or_10000.typ":run(examples,scripts["lissom_or"], 220 commands=["topo.sim.run(10000)", 221 or_analysis(), 222 snapshot("lissom_or_10000.typ")]), 223 224 "lissom_fsa_10000.typ":run(examples,scripts["lissom_fsa"], 225 commands=["topo.sim.run(10000)", 226 snapshot("lissom_fsa_10000.typ")]), 227 228 "obermayer_pnas90_40000.typ":run(examples,scripts["obermayer_pnas90"], 229 commands=["topo.sim.run(40000)", 230 or_analysis(), 231 snapshot("obermayer_pnas90_30000.typ")]), 232 233 "som_retinotopy_40000.typ":run(examples,scripts["som_retinotopy"], 234 commands=["topo.sim.run(40000)", 235 retinotopy_analysis(), 236 snapshot("som_retinotopy_40000.typ")]), 237 238 "gcal_10000.typ":run(examples,scripts["gcal"], 239 commands=["topo.sim.run(10000)", 240 or_analysis(), 241 snapshot("gcal_10000.typ")]) 242 243 } 244 245 return command_labels,available_targets
246 247
248 -class generate(param.ParameterizedFunction):
249 250 targets = param.List(default=['all_quick']) 251
252 - def __call__(self,**params):
253 p = ParamOverrides(self,params) 254 255 command_labels,available_targets = _stuff(p.targets) 256 for cmd in command_labels: 257 c = available_targets[cmd] 258 print c 259 system(c)
260