Changeset 301
- Timestamp:
- 11/06/08 23:06:32 (2 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/examples/parameter_search/parameter_search_example.py
r296 r301 6 6 Author: Michael Schmuker 7 7 8 See the method "run_it" for how to use the parameter_search module. 8 This script demonstrates how to use the parameter_search module for parallel 9 computation together with PyNN for (minimal) network simulation and NeuroTools 10 for almost everything else. 11 12 The network consists of a SpikeSource driving a neuron with conductance-based 13 synapses. 14 15 To run this script, you first have to invoke an IPython controller and 16 computation engines. If IPython is installed correctly and with parallel 17 computation support, you can just type: 18 19 > ipcluster -n 2 & 20 21 This will start two computation engines and a controller in the background. 22 23 When the controller is up, run this script: 24 25 > python parameter_search_example.py 26 27 Calculation will start, and after a few seconds (depending on your hardware) 28 it will save a png graphics file that illustrates the firing rate of a neuron 29 as a function of the input rate and the weight of the synapse to your current 30 directory. 9 31 10 32 """ … … 35 57 import NeuroTools.stgen as stgen 36 58 stgen = stgen.StGen() 37 spiketrain = stgen.poisson_generator(param_dict['rate']/1000., tsim = 1000.) 38 source = sim.Population(1, sim.SpikeSourceArray, {'spike_times':spiketrain}) 59 spiketrain = stgen.poisson_generator(param_dict['rate'], t_stop = 1000.) 60 source = sim.Population(1, sim.SpikeSourceArray, 61 {'spike_times':spiketrain.spike_times}) 39 62 neuron = sim.Population(1, sim.IF_cond_alpha) 40 63 sim.Projection(source, neuron, … … 52 75 # count the number of spikes 53 76 spikes = neuron.getSpikes() 54 import tempfile55 fd, potfile = tempfile.mkstemp(prefix = 'param_search_example_pot')56 potential = neuron.print_v(potfile)57 import pylab, numpy58 try:59 pot = pylab.load(potfile)[:,0]60 except(ValueError):61 pot = numpy.array([])62 except Exception, e:63 import pdb64 pdb.set_trace()65 import os66 os.remove(potfile)67 77 numspikes = len(spikes) 68 78 … … 101 111 if (r['source_rate'] == rates[r_i]) 102 112 and (r['weight'] == weights[w_i])][0] 103 import pylab 104 pylab.imshow(neuron_rates, interpolation = 'nearest', origin = 'lower') 105 pylab.xlabel('rate') 106 pylab.ylabel('weight') 113 import NeuroTools.plotting as plotting 114 pylab = plotting.get_display(True) 115 pylab.rcParams.update(plotting.pylab_params()) 116 subplot = pylab.imshow(neuron_rates, 117 interpolation = 'nearest', 118 origin = 'lower') 119 plotting.set_labels(subplot.get_axes(), 120 xlabel = 'rate', 121 ylabel = 'weight') 107 122 pylab.colorbar() 108 pylab.show()109 123 # could add fancy xticks and yticks here 124 import tempfile, os 125 (fd, figfilename) = tempfile.mkstemp(prefix = 'parameter_search_result', 126 suffix = '.png', 127 dir = os.getcwd()) 128 pylab.gcf().savefig(figfilename) 110 129 111 def run_it( numengines = 2):130 def run_it(): 112 131 """" 113 132 Run the parameter search. 114 Parameters:115 numengines - the number of ipengines116 133 """ 117 # start an ipcontroller. ipcontroller must be in your path.118 # There is certainly a cleaner solution when using iypthon1 directly119 134 import parameter_search as ps 120 contr_dict = ps.IPythonParameterSearcher.make_controller()121 135 122 136 # search the parameter space … … 125 139 dictlist = param_dict_list, 126 140 func = model_network) 127 # srchr = ps.RestartingIPythonParameterSearcher(128 # dictlist = param_dict_list,129 # func = model_network,130 # task_furl = contr_dict['task_furl'],131 # multiengine_furl = contr_dict['multiengine_furl'],132 # engine_furl = contr_dict['engine_furl'],133 # numengines = numengines)134 141 srchr.search() 135 142 outlist = srchr.harvest() 136 143 137 #kill the controller again138 import os, signal139 os.kill(contr_dict['contr_obj'].pid, signal.SIGINT)140 141 144 #return the results 142 145 return outlist

