Changeset 435

Show
Ignore:
Timestamp:
08/07/08 19:19:34 (2 weeks ago)
Author:
pierre
Message:

Continue the work on the brian API. All the standard cells models are now well working, so does the SpikeSourceArray? and the SpikeSourcePoisson?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/brian/__init__.py

    r432 r435  
    3939    def __getattr__(self, name): 
    4040        try: 
    41             val = float(self.parent.brian_cells[int(self)].__getattr__(name)) 
     41            if isinstance(self.parent.brian_cells,brian.MultipleSpikeGeneratorGroup): 
     42                if name == "spike_times": 
     43                    return 1000*numpy.array(self.parent.brian_cells._threshold.spiketimes[int(self)]) 
     44            else: 
     45                val = float(self.parent.brian_cells[int(self)].__getattr__(name)) 
     46                return val 
    4247        except KeyError: 
    4348            raise NonExistentParameterError(name, self.cellclass) 
    44         return val 
    4549     
    4650    def set_native_parameters(self, parameters): 
    4751        for key, value in parameters.items(): 
    48             self.parent.brian_cells[int(self)].__setattr__(key,value) 
    49          
    50  
     52            if isinstance(self.parent.brian_cells,brian.MultipleSpikeGeneratorGroup): 
     53                if key == "spike_times": 
     54                    spike_times = 0.001*numpy.array(value) 
     55                    self.parent.brian_cells._threshold.spiketimes[int(self)] = spike_times 
     56            else: 
     57                self.parent.brian_cells[int(self)].__setattr__(key,value) 
    5158         
    5259def list_standard_models(): 
     
    7178    simulator but not by others. 
    7279    """ 
     80    global net, simclock, _max_delay, _min_delay 
    7381    common.setup(timestep, min_delay, max_delay, debug, **extra_params) 
    74     global tempdir, net, simclock 
    75      
     82 
    7683    # Initialisation of the log module. To write in the logfile, simply enter 
    7784    # logging.critical(), logging.debug(), logging.info(), logging.warning()  
     
    8895 
    8996    logging.info("Initialization of Brian") 
    90     timestep  = 0.001*timestep 
    91     min_delay = 0.001*min_delay 
    92     max_delay = 0.001*max_delay 
     97    _min_delay = 0.001*min_delay 
     98    _max_delay = 0.001*max_delay 
    9399    net       = brian.Network() 
    94     simclock  = brian.Clock(dt=timestep) 
     100    simclock  = brian.Clock(dt=0.001*timestep) 
    95101    return 0 
    96102 
     
    105111 
    106112def get_min_delay(): 
    107     return 0.1*brian.ms 
     113    global _min_delay 
     114    return _min_delay 
    108115common.get_min_delay = get_min_delay 
    109116 
     117def get_max_delay(): 
     118    global _max_delay 
     119    return _max_delay 
     120common.get_max_delay = get_max_delay 
     121 
    110122def get_time_step(): 
    111     return 0.1*brian.ms 
     123    global simclock 
     124    return simclock.dt 
    112125common.get_time_step = get_time_step 
    113126 
    114127def get_current_time(): 
    115     pass 
     128    global simclock 
     129    return simclock.t 
    116130 
    117131def num_processes(): 
     
    231245        common.Population.__init__(self, dims, cellclass, cellparams, label)  # move this to common.Population.__init__() 
    232246         
    233         # Should perhaps use "LayoutNetwork"? 
    234          
    235247        if isinstance(cellclass, type): 
    236248            self.celltype = cellclass(cellparams) 
    237249            self.cellparams = self.celltype.parameters 
    238             if isinstance(self.celltype,SpikeSourcePoisson): 
    239                 rate       = self.cellparams['rate'] 
    240                 fct        = self.celltype.fct 
     250            if isinstance(self.celltype, SpikeSourcePoisson): 
     251                rate              = self.cellparams['rate'] 
     252                fct               = self.celltype.fct 
    241253                self.brian_cells  = brian.PoissonGroup(self.size, rates = fct, clock=simclock) 
     254                #self.brian_cells._max_delay = get_max_delay()/get_time_step() 
     255            elif isinstance(self.celltype, SpikeSourceArray): 
     256                spike_times = 0.001*numpy.array(self.cellparams['spike_times']) 
     257                self.brian_cells  = brian.MultipleSpikeGeneratorGroup([spike_times for i in xrange(self.size)]) 
     258                #self.brian_cells._max_delay = get_max_delay()/get_time_step() 
    242259            else: 
    243260                v_thresh   = self.cellparams['v_thresh'] 
    244261                v_reset    = self.cellparams['v_reset'] 
    245262                tau_refrac = self.cellparams['tau_refrac'] 
    246                 self.brian_cells = brian.NeuronGroup(self.size,model=cellclass.eqs,threshold=v_thresh,reset=v_reset, refractory=tau_refrac, clock=simclock, compile=True
     263                self.brian_cells = brian.NeuronGroup(self.size,model=cellclass.eqs,threshold=v_thresh,reset=v_reset, refractory=tau_refrac, clock=simclock, compile=True, max_delay=get_max_delay()
    247264 
    248265        elif isinstance(cellclass, str): 
     
    250267            v_reset    = self.cellparams['v_reset'] 
    251268            tau_refrac = self.cellparams['tau_refrac'] 
    252             self.brian_cells = brian.NeuronGroup(self.size,model=cellclass,threshold=v_thresh,reset=v_reset, clock=simclock
     269            self.brian_cells = brian.NeuronGroup(self.size,model=cellclass,threshold=v_thresh,reset=v_reset, clock=simclock, compile=True, max_delay=get_max_delay()
    253270            self.cellparams = self.celltype.parameters 
    254271 
    255         useless_params=['v_thresh','v_reset','tau_refrac','cm'] 
     272        useless_params=['v_thresh', 'v_reset', 'tau_refrac', 'spike_times'] 
    256273        if self.cellparams: 
    257274            for key, value in self.cellparams.items(): 
     
    273290        net.add(self.brian_cells) 
    274291     
     292 
    275293    def __getitem__(self, addr): 
    276294        """Return a representation of the cell with coordinates given by addr, 
     
    470488                N = record_from 
    471489                record_from = numpy.random.permutation(self.cell.flatten()[0:N]) 
    472             self.vm_recorder = brian.StateMonitor(self.brian_cells,'v',record=record_from) 
    473         else: 
    474             self.vm_recorder = brian.StateMonitor(self.brian_cells,'v',record=True
     490        else: 
     491            record_from = True 
     492        self.vm_recorder = brian.StateMonitor(self.brian_cells,'v',record=record_from
    475493        net.add(self.vm_recorder) 
    476494     
     
    488506                N = record_from 
    489507                record_from = numpy.random.permutation(self.cell.flatten()[0:N]) 
    490             self.ce_recorder = brian.StateMonitor(self.brian_cells,'ge',record=record_from) 
    491             self.ci_recorder = brian.StateMonitor(self.brian_cells,'gi',record=record_from) 
    492         else: 
    493             self.ce_recorder = brian.StateMonitor(self.brian_cells,'ge',record=True) 
    494             self.ci_recorder = brian.StateMonitor(self.brian_cells,'gi',record=True) 
     508        else: 
     509            record_from = True 
     510        self.ce_recorder = brian.StateMonitor(self.brian_cells,'ge',record=record_from) 
     511        self.ci_recorder = brian.StateMonitor(self.brian_cells,'gi',record=record_from) 
    495512        net.add(self.ce_recorder) 
    496513        net.add(self.ci_recorder) 
     
    525542            f.write("# dt = %g\n" % dt) 
    526543            spikes = numpy.array(self.spike_recorder.spikes) 
    527             spikes[:,1]=1000*spikes[:,1] 
     544            try: 
     545                spikes[:,1]=1000*spikes[:,1] 
     546            except Exception: 
     547                pass 
    528548            for item in spikes: 
    529549                f.write("%g\t%d\n" %(item[1], item[0])) 
     
    545565        # gather is not relevant, but is needed for API consistency 
    546566         
    547         # TO DO : add a recordede list otherwise wrong 
     567        # TO DO : add a recordeded list otherwise wrong 
    548568         
    549569        return float(self.spike_recorder.nspikes)/len(self) 
  • trunk/src/brian/cells.py

    r432 r435  
    2626    ) 
    2727    eqs= brian.Equations(''' 
    28         dv/dt  = (ge + gi + (v_rest-v))/tau_m : volt 
    29         dge/dt = (ye-ge)/tau_syn_E              : 1  
    30         dye/dt = -ye/tau_syn_E                  : 1 
    31         dgi/dt = (yi-gi)/tau_syn_I              : 1  
    32         dyi/dt = -yi/tau_syn_I                  : 1 
    33         tau_syn_E : second 
    34         tau_syn_I : second 
    35         tau_m     : second 
    36         v_rest    : volt 
    37         ''' 
    38         ) 
    39  
     28        dv/dt  = (ge + gi)/cm + (v_rest-v)/tau_m   : volt 
     29        dge/dt = (2.7182818284590451*ye-ge)/tau_syn_E : 1  
     30        dye/dt = -ye/tau_syn_E                        : 1 
     31        dgi/dt = (2.7182818284590451*yi-gi)/tau_syn_I : 1  
     32        dyi/dt = -yi/tau_syn_I                        : 1 
     33        cm                                    : farad 
     34        tau_syn_E                             : second 
     35        tau_syn_I                             : second 
     36        tau_m                                 : second 
     37        v_rest                                : volt 
     38        ''' 
     39        ) 
     40 
     41    synapses = {'exc' : 'ye', 'inh' : 'yi'} 
    4042 
    4143class IF_curr_exp(common.IF_curr_exp): 
     
    5759    ) 
    5860    eqs= brian.Equations(''' 
    59         dv/dt  = (ge + gi + (v_rest-v))/tau_m : volt 
    60         dge/dt = -ge/tau_syn_E              : 1 
    61         dgi/dt = -gi/tau_syn_I              : 1 
    62         tau_syn_E                           : second 
    63         tau_syn_I                           : second 
    64         tau_m                               : second 
    65         v_rest                              : volt 
    66         ''' 
    67         ) 
    68      
    69  
    70 class IF_cond_alpha(common.ModelNotAvailable): 
     61        dv/dt  = (ge + gi)/cm + (v_rest-v)/tau_m : volt 
     62        dge/dt = -ge/tau_syn_E                : 1 
     63        dgi/dt = -gi/tau_syn_I                : 1 
     64        tau_syn_E                             : second 
     65        tau_syn_I                             : second 
     66        tau_m                                 : second 
     67        cm                                    : farad 
     68        v_rest                                : volt 
     69        ''' 
     70        ) 
     71     
     72    synapses = {'exc' : 'ge', 'inh' : 'gi'} 
     73     
     74 
     75class IF_cond_alpha(common.IF_cond_alpha): 
    7176    translations = common.build_translations( 
    7277        ('v_rest',     'v_rest',0.001)    , 
     
    8489    ) 
    8590    eqs= brian.Equations(''' 
    86         dv/dt  = ((v_rest-v) + ge*(e_rev_E-v) + gi*(e_rev_I-v))/tau_m : volt 
    87         dge/dt = ye-ge/tau_syn_E              : 1  
    88         dye/dt = -ye/tau_syn_E                : 1 
    89         dgi/dt = yi-gi/tau_syn_I              : 1  
    90         dyi/dt = -yi/tau_syn_I                : 1 
    91         tau_syn_E              : second 
    92         tau_syn_I              : second 
    93         tau_m                  : second 
    94         v_rest                 : volt 
    95         e_rev_E                : volt 
    96         e_rev_I                : volt 
    97         ''' 
    98         ) 
    99  
     91        dv/dt  = (v_rest-v)/tau_m + (ge*(e_rev_E-v) + gi*(e_rev_I-v))/(0.001*cm) : volt 
     92        dge/dt = (2.7182818284590451*ye-ge)/tau_syn_E  : 1  
     93        dye/dt = -ye/tau_syn_E                         : 1 
     94        dgi/dt = (2.7182818284590451*yi-gi)/tau_syn_I  : 1  
     95        dyi/dt = -yi/tau_syn_I                         : 1 
     96        tau_syn_E                             : second 
     97        tau_syn_I                             : second 
     98        tau_m                                 : second 
     99        v_rest                                : volt 
     100        e_rev_E                               : volt 
     101        e_rev_I                               : volt 
     102        cm                                    : farad 
     103        ''' 
     104        ) 
     105 
     106    synapses = {'exc' : 'ye', 'inh' : 'yi'} 
    100107 
    101108class IF_cond_exp(common.IF_cond_exp): 
     
    117124    ) 
    118125    eqs= brian.Equations(''' 
    119         dv/dt  = ((v_rest-v) + ge*(e_rev_E-v) + gi*(e_rev_I-v))/tau_m : volt 
     126        dv/dt  = (v_rest-v)/tau_m + (ge*(e_rev_E-v) + gi*(e_rev_I-v))/(0.001*cm) : volt 
    120127        dge/dt = -ge/tau_syn_E : 1 
    121128        dgi/dt = -gi/tau_syn_I : 1 
     
    123130        tau_syn_I              : second 
    124131        tau_m                  : second 
     132        cm                     : farad 
    125133        v_rest                 : volt 
    126134        e_rev_E                : volt 
     
    128136        ''' 
    129137        ) 
     138     
     139    synapses = {'exc' : 'ge', 'inh' : 'gi'} 
    130140 
    131141class IF_facets_hardware1(common.IF_facets_hardware1): 
     
    155165class SpikeSourceArray(common.SpikeSourceArray): 
    156166    """Spike source generating spikes at the times given in the spike_times array.""" 
     167    translations = common.build_translations( 
     168        ('spike_times', 'spike_times'), 
     169    ) 
     170     
     171    def __init__(self, parameters): 
     172        common.SpikeSourceArray.__init__(self, parameters) 
    157173 
    158174 
  • trunk/src/brian/connectors.py

    r427 r435  
    77from pyNN.brian.__init__ import numpy 
    88import brian_no_units_no_warnings 
    9 import brian 
     9import brian, types 
    1010from pyNN.random import RandomDistribution, NativeRNG 
    1111from math import * 
     
    1414                  sin, sinh, sqrt, tan, tanh 
    1515 
     16def is_number(n): 
     17    return type(n) == types.FloatType or type(n) == types.IntType or type(n) == numpy.float64 
     18 
    1619def _targetConnection(Connector, projection): 
    17     if projection.synapse_type == "excitatory"
    18         target="ge" 
     20    if projection.synapse_type == "excitatory" or projection.synapse_type is None
     21        target=projection.post.celltype.synapses['exc'] 
    1922    else: 
    20         target="gi" 
     23        target=projection.post.celltype.synapses['inh'] 
    2124    return brian.Connection(projection.pre.brian_cells,projection.post.brian_cells,target, delay=Connector.delays*0.001) 
     25 
     26def _convertWeight(weight, projection): 
     27    #if isinstance(projection.pre.brian_cells, brian.PoissonGroup): 
     28        #weight *= projection.pre.brian_cells.rate[0]*projection.pre.brian_cells.clock.dt 
     29    if isinstance(weight, numpy.ndarray): 
     30        all_negative = (weight<=0).all() 
     31        all_positive = (weight>=0).all() 
     32        assert all_negative or all_positive, "Weights must be either all positive or all negative" 
     33        if not projection.post.celltype.default_parameters.has_key('e_rev_E'): 
     34            if projection.synapse_type == 'inhibitory' and all_positive: 
     35                weight *= -1 
     36        else: 
     37            if projection.synapse_type == "inhibitory" and all_negative: 
     38                weight *= 1 
     39    elif is_number(weight): 
     40        if not projection.post.celltype.default_parameters.has_key('e_rev_E'): 
     41            if projection.synapse_type == 'inhibitory' and weight > 0: 
     42                weight *= -1 
     43        else: 
     44            if projection.synapse_type == 'inhibitory' and weight < 0: 
     45                weight *= -1 
     46    return weight 
    2247 
    2348class AllToAllConnector(common.AllToAllConnector):     
     
    2550    def connect(self, projection): 
    2651        projection._connections = _targetConnection(self, projection) 
    27         projection._connections.connect_full(projection.pre.brian_cells,projection.post.brian_cells, weight=self.weights) 
     52        weight = _convertWeight(self.weights, projection) 
     53        projection._connections.connect_full(projection.pre.brian_cells,projection.post.brian_cells, weight=weight) 
    2854        return projection._connections.W.getnnz() 
    2955 
     
    3258    def connect(self, projection): 
    3359        projection._connections = _targetConnection(self, projection) 
    34         projection._connections.connect_one_to_one(projection.pre.brian_cells,projection.post.brian_cells, weight=self.weights) 
     60        weight = _convertWeight(self.weights, projection) 
     61        projection._connections.connect_one_to_one(projection.pre.brian_cells,projection.post.brian_cells, weight=weight) 
    3562        return projection._connections.W.getnnz() 
    3663     
     
    3966    def connect(self, projection): 
    4067        projection._connections = _targetConnection(self, projection) 
    41         projection._connections.connect_random(projection.pre.brian_cells,projection.post.brian_cells, self.p_connect, weight=self.weights) 
     68        weight = _convertWeight(self.weights, projection) 
     69        projection._connections.connect_random(projection.pre.brian_cells,projection.post.brian_cells, self.p_connect, weight=weight) 
    4270        return projection._connections.W.getnnz() 
    4371     
     
    87115            else: 
    88116                weights = self.weights*numpy.ones(N) 
     117            weights = _convertWeight(weights, projection) 
    89118            if isinstance(self.delays,str): 
    90119                raise Exception('''The string definition for delays in Brian is not  
  • trunk/test/VAbenchmarks.py

    r433 r435  
    9292    w_exc = 1e-3*Gexc*(Erev_exc - v_mean) # (nA) weight of excitatory synapses 
    9393    w_inh = 1e-3*Ginh*(Erev_inh - v_mean) # (nA) 
    94     if simulator == "brian": 
    95         w_exc = w_exc*0.1 
    96         w_inh = w_inh*0.1 
    9794    assert w_exc > 0; assert w_inh < 0 
    9895