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
24
25
26
27
28
29
30
31
32
33
34
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
46 """Return a command for saving a snapshot named filename."""
47 return "from topo.command.basic import save_snapshot ; save_snapshot('%s')"%filename
48
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
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
109
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
123 examples = find_examples(**kw)
124 if examples:
125 print "Found examples in %s"%examples
126
128 import topo
129
130 if not specified_examples:
131
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
139 os.path.join(topo._package_path,"../examples"),
140
141 os.path.join(topo._package_path,"../../../share/topographica/examples"),
142
143 os.path.join(topo._package_path,"../../../../share/topographica/examples"),
144
145 os.path.join(topo._package_path,"../share/topographica/examples"),
146
147 "/usr/local/share/topographica/examples",
148
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
156
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
173
174 -def _stuff(specified_targets):
175
176
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
183
184
185
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
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):
260