Changeset 301

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

gave parameter_search_example some love.
- Use NeuroTools pervasively.
- Instead of showing the figure at the end of the calculation, save it to the working directory.
- remove obsolete code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/examples/parameter_search/parameter_search_example.py

    r296 r301  
    66Author: Michael Schmuker 
    77 
    8 See the method "run_it" for how to use the parameter_search module. 
     8This script demonstrates how to use the parameter_search module for parallel  
     9computation together with PyNN for (minimal) network simulation and NeuroTools  
     10for almost everything else. 
     11 
     12The network consists of a SpikeSource driving a neuron with conductance-based  
     13synapses. 
     14 
     15To run this script, you first have to invoke an IPython controller and  
     16computation engines. If IPython is installed correctly and with parallel  
     17computation support, you can just type: 
     18 
     19> ipcluster -n 2 & 
     20 
     21This will start two computation engines and a controller in the background. 
     22 
     23When the controller is up, run this script: 
     24 
     25> python parameter_search_example.py 
     26 
     27Calculation will start, and after a few seconds (depending on your hardware)  
     28it will save a png graphics file that illustrates the firing rate of a neuron  
     29as a function of the input rate and the weight of the synapse to your current  
     30directory. 
    931 
    1032""" 
     
    3557    import NeuroTools.stgen as stgen 
    3658    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}) 
    3962    neuron = sim.Population(1, sim.IF_cond_alpha) 
    4063    sim.Projection(source, neuron,  
     
    5275    # count the number of spikes 
    5376    spikes = neuron.getSpikes() 
    54     import tempfile 
    55     fd, potfile = tempfile.mkstemp(prefix = 'param_search_example_pot') 
    56     potential = neuron.print_v(potfile) 
    57     import pylab, numpy 
    58     try: 
    59         pot = pylab.load(potfile)[:,0] 
    60     except(ValueError): 
    61         pot = numpy.array([]) 
    62     except Exception, e: 
    63         import pdb 
    64         pdb.set_trace() 
    65     import os 
    66     os.remove(potfile) 
    6777    numspikes = len(spikes) 
    6878     
     
    101111                                      if (r['source_rate'] == rates[r_i]) 
    102112                                      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') 
    107122    pylab.colorbar() 
    108     pylab.show() 
    109123    # 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) 
    110129  
    111 def run_it(numengines = 2): 
     130def run_it(): 
    112131    """" 
    113132    Run the parameter search. 
    114     Parameters: 
    115     numengines - the number of ipengines 
    116133    """ 
    117     # start an ipcontroller. ipcontroller must be in your path. 
    118     # There is certainly a cleaner solution when using iypthon1 directly 
    119134    import parameter_search as ps 
    120     contr_dict = ps.IPythonParameterSearcher.make_controller() 
    121135 
    122136    # search the parameter space 
     
    125139        dictlist = param_dict_list, 
    126140        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)  
    134141    srchr.search() 
    135142    outlist = srchr.harvest() 
    136143 
    137     #kill the controller again 
    138     import os, signal 
    139     os.kill(contr_dict['contr_obj'].pid, signal.SIGINT)  
    140      
    141144    #return the results 
    142145    return outlist