Changeset 427

Show
Ignore:
Timestamp:
07/27/08 18:22:35 (3 weeks ago)
Author:
pierre
Message:

Some work performed on the brian implementation of the pyNN API, after having met Dan at the CNS Workshop. Still experimental, VA benchmark is working but not Brunel one

Files:

Legend:

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

    r414 r427  
    3636        int.__init__(n) 
    3737        common.IDMixin.__init__(self) 
     38     
     39    def __getattr__(self, name): 
     40        try: 
     41            val = float(self.parent.brian_cells[int(self)].__getattr__(name)) 
     42        except KeyError: 
     43            raise NonExistentParameterError(name, self.cellclass) 
     44        return val 
     45     
     46    def set_native_parameters(self, parameters): 
     47        for key, value in parameters.items(): 
     48            self.parent.brian_cells[int(self)].__setattr__(key,value) 
     49         
     50 
    3851         
    3952def list_standard_models(): 
     
    7891    min_delay = 0.001*min_delay 
    7992    max_delay = 0.001*max_delay 
    80     net      = brian.Network() 
    81     simclock = brian.Clock(dt=timestep) 
     93    net      = brian.Network() 
     94    simclock = brian.Clock(dt=timestep) 
    8295    return 0 
    8396 
     
    225238            if isinstance(self.celltype,SpikeSourcePoisson): 
    226239                rate       = self.cellparams['rate'] 
    227                 self.cell  = brian.PoissonGroup(self.size, rates = rate, clock=simclock) 
     240                fct        = self.celltype.fct 
     241                self.brian_cells  = brian.PoissonGroup(self.size, rates = fct, clock=simclock) 
    228242            else: 
    229243                v_thresh   = self.cellparams['v_thresh'] 
    230244                v_reset    = self.cellparams['v_reset'] 
    231245                tau_refrac = self.cellparams['tau_refrac'] 
    232                 self.cell = brian.NeuronGroup(self.size,model=cellclass.eqs,threshold=v_thresh,reset=v_reset, refractory=tau_refrac, clock=simclock
     246                self.brian_cells = brian.NeuronGroup(self.size,model=cellclass.eqs,threshold=v_thresh,reset=v_reset, refractory=tau_refrac, clock=simclock, compile=True
    233247 
    234248        elif isinstance(cellclass, str): 
    235             self.cell = brian.NeuronGroup(self.size,model=cellclass.eqs,threshold=v_thresh,reset=v_reset, clock=simclock) 
     249            v_thresh   = self.cellparams['v_thresh'] 
     250            v_reset    = self.cellparams['v_reset'] 
     251            tau_refrac = self.cellparams['tau_refrac'] 
     252            self.brian_cells = brian.NeuronGroup(self.size,model=cellclass,threshold=v_thresh,reset=v_reset, clock=simclock) 
    236253            self.cellparams = self.celltype.parameters 
    237254 
    238         unchangeable_params=['v_thresh','v_reset','tau_refrac'] 
     255        useless_params=['v_thresh','v_reset','tau_refrac','cm'] 
    239256        if self.cellparams: 
    240257            for key, value in self.cellparams.items(): 
    241258                if not key in unchangeable_params: 
    242                     setattr(self.cell,key,value) 
    243         self.cell_id = numpy.arange(len(self.cell)) 
    244         self.cell_id = numpy.reshape(self.cell_id, self.dim) 
     259                    setattr(self.brian_cells,key,value) 
     260        self.cell = numpy.array([ID(cell) for cell in xrange(len(self.brian_cells))],ID) 
     261        for id in self.cell: 
     262            id.parent = self 
     263        self.cell = numpy.reshape(self.cell, self.dim) 
    245264        self.spike_recorder = None 
    246265        self.vm_recorder    = None 
     266        self.ce_recorder    = None 
     267        self.ci_recorder    = None 
     268        self.first_id       = 0 
    247269         
    248270        if not self.label: 
    249271            self.label = 'population%d' % Population.nPop 
    250272        Population.nPop += 1 
    251         net.add(self.cell
     273        net.add(self.brian_cells
    252274     
    253275    def __getitem__(self, addr): 
     
    261283            addr = (addr,) 
    262284        if len(addr) == self.ndim: 
    263             id = self.cell_id[addr] 
     285            id = self.cell[addr] 
    264286        else: 
    265287            raise common.InvalidDimensionsError, "Population has %d dimensions. Address was %s" % (self.ndim, str(addr)) 
    266288        if addr != self.locate(id): 
    267289            raise IndexError, 'Invalid cell address %s' % str(addr) 
    268         return self.cell[id] 
     290        return id 
    269291     
    270292    def __iter__(self): 
     
    322344        Get the values of a parameter for every cell in the population. 
    323345        """ 
    324         values = getattr(self.cell, parameter_name) 
     346        values = getattr(self.brian_cells, parameter_name) 
    325347        if as_array: 
    326348            values = numpy.array(values) 
     
    346368            raise common.InvalidParameterValueError 
    347369        for key, value in para_dict.items(): 
    348             setattr(self.cell, key, value) 
     370            setattr(self.brian_cells, key, value) 
    349371         
    350372 
     
    385407        else: 
    386408            rarr = rand_distr.next(n=self.size) 
    387             assert len(rarr) == len(self.cell_id
    388             setattr(self.cell, parametername, rarr) 
     409            assert len(rarr) == len(self.brian_cells
     410            setattr(self.brian_cells, parametername, rarr) 
    389411         
    390412    def _call(self, methodname, arguments): 
     
    399421        """ 
    400422        `Topographic' call. Calls the method methodname() for every cell in the  
    401         population. The argument to the method depends on the coordinates of the 
     423        population. The argument     to the method depends on the coordinates of the 
    402424        cell. objarr is an array with the same dimensions as the Population. 
    403425        e.g. p.tcall("memb_init", vinitArray) calls 
     
    421443                N = record_from 
    422444            print "Warning: Brian can record only the %d first cells of the population" %N 
    423             self.spike_recorder = brian.SpikeMonitor(self.cell[0:N],True) 
    424         else: 
    425             self.spike_recorder = brian.SpikeMonitor(self.cell,True) 
     445            self.spike_recorder = brian.SpikeMonitor(self.brian_cells[0:N],True) 
     446        else: 
     447            self.spike_recorder = brian.SpikeMonitor(self.brian_cells,True) 
    426448        net.add(self.spike_recorder) 
    427449 
     
    436458        global net 
    437459        if record_from: 
    438             if isinstance(record_from,list): 
    439                 N = len(record_from) 
    440460            if isinstance(record_from,int): 
    441461                N = record_from 
    442             print "Warning: Brian can record only the %d first cells of the population" %N 
    443             self.vm_recorder = brian.StateMonitor(self.cell[0:N],'v',record=True
    444         else: 
    445             self.vm_recorder = brian.StateMonitor(self.cell,'v',record=True) 
     462                record_from = numpy.random.permutation(self.cell.flatten()[0:N]) 
     463            self.vm_recorder = brian.StateMonitor(self.brian_cells,'v',record=record_from
     464        else: 
     465            self.vm_recorder = brian.StateMonitor(self.brian_cells,'v',record=True) 
    446466        net.add(self.vm_recorder) 
     467     
     468    def record_c(self, record_from=None, rng=None, to_file=True): 
     469        """ 
     470        If record_from is not given, record the conductances/currents for all cells in 
     471        the Population. 
     472        record_from can be an integer - the number of cells to record from, chosen 
     473        at random (in this case a random number generator can also be supplied) 
     474        - or a list containing the ids of the cells to record. 
     475        """ 
     476        global net 
     477        if record_from: 
     478            if isinstance(record_from,int): 
     479                N = record_from 
     480                record_from = numpy.random.permutation(self.cell.flatten()[0:N]) 
     481            self.ce_recorder = brian.StateMonitor(self.brian_cells,'ge',record=record_from) 
     482            self.ci_recorder = brian.StateMonitor(self.brian_cells,'gi',record=record_from) 
     483        else: 
     484            self.ce_recorder = brian.StateMonitor(self.brian_cells,'ge',record=True) 
     485            self.ci_recorder = brian.StateMonitor(self.brian_cells,'gi',record=True) 
     486        net.add(self.ce_recorder) 
     487        net.add(self.ci_recorder) 
    447488     
    448489    def printSpikes(self, filename, gather=True, compatible_output=True): 
     
    471512            f = open(filename,"w", DEFAULT_BUFFER_SIZE) 
    472513            f.write("# dimensions =" + "\t".join([str(d) for d in self.dim]) + "\n") 
     514            f.write("# first_id = %d\n" % self.first_id) 
     515            f.write("# last_id = %d\n" % (self.first_id+len(self)-1,)) 
    473516            f.write("# dt = %g\n" % dt) 
    474             for item in self.spike_recorder.spikes: 
    475                 f.write("%g\t%d\n" %(1000*item[1], item[0])) 
     517            spikes = numpy.array(self.spike_recorder.spikes) 
     518            spikes[:,1]=1000*spikes[:,1] 
     519            for item in spikes: 
     520                f.write("%g\t%d\n" %(item[1], item[0])) 
    476521            f.close() 
    477522     
     
    490535        """ 
    491536        # gather is not relevant, but is needed for API consistency 
    492         return float(self.spike_recorder.nspikes)/len(self.cell) 
     537         
     538        # TO DO : add a recordede list otherwise wrong 
     539         
     540        return float(self.spike_recorder.nspikes)/len(self) 
    493541 
    494542    def randomInit(self, rand_distr): 
     
    497545        random values. 
    498546        """ 
    499         self.rset('v_init', rand_distr) 
     547        self.rset('v', rand_distr) 
    500548 
    501549 
     
    524572            N = len(self.vm_recorder[0]) 
    525573            f.write("# dimensions =" + "\t".join([str(d) for d in self.dim]) + "\n") 
     574            f.write("# first_id = %d\n" % self.first_id) 
     575            f.write("# last_id = %d\n" % (self.first_id+len(self)-1,)) 
    526576            f.write("# dt = %g\n" % dt) 
    527577            f.write("# n = %d\n" % N) 
    528578            cells = self.vm_recorder.get_record_indices() 
    529579            for cell in cells: 
     580                vm = 1000*self.vm_recorder[cell] 
    530581                for idx in xrange(N): 
    531                     f.write("%g\t%g\n" %(self.vm_recorder[cell][idx]*1000.,cell)) 
     582                    f.write("%g\t%g\n" %(vm[idx],cell)) 
    532583            f.close() 
    533         pass 
    534584 
    535585     
     
    546596         
    547597        source - string specifying which attribute of the presynaptic cell 
    548                  signals action potentials 
     598                 signals action potentialss 
    549599                  
    550600        target - string specifying which synapse on the postsynaptic cell to 
  • trunk/src/brian/cells.py

    r317 r427  
    1414    shaped post-synaptic current.""" 
    1515    translations = common.build_translations( 
     16        ('v_rest',     'v_rest', mV), 
     17        ('v_reset',    'v_reset', mV), 
     18        ('cm',         'cm'), # C is in pF, cm in nF 
     19        ('tau_m',      'tau_m', ms), 
     20        ('tau_refrac', 'tau_refrac',"max(get_time_step(), tau_refrac*0.001)", "tau_refrac"), 
     21        ('tau_syn_E',  'tau_syn_E', ms), 
     22        ('tau_syn_I',  'tau_syn_I', ms), 
     23        ('v_thresh',   'v_thresh', mV), 
     24        ('i_offset',   'i_offset', pA), # I0 is in pA, i_offset in nA 
     25        ('v_init',     'v', mV), 
     26    ) 
     27    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 
     40 
     41class IF_curr_exp(common.IF_curr_exp): 
     42    """Leaky integrate and fire model with fixed threshold and 
     43    decaying-exponential post-synaptic current. (Separate synaptic currents for 
     44    excitatory and inhibitory synapses.""" 
     45     
     46    translations = common.build_translations( 
    1647        ('v_rest',     'v_rest', 0.001), 
    1748        ('v_reset',    'v_reset', 0.001), 
    1849        ('cm',         'cm'), # C is in pF, cm in nF 
    1950        ('tau_m',      'tau_m', 0.001), 
    20         ('tau_refrac', 'tau_refrac', "max(get_time_step(), tau_refrac)", "tau_refrac"), 
     51        ('tau_refrac', 'tau_refrac',"max(get_time_step(), tau_refrac*0.001)", "tau_refrac"), 
    2152        ('tau_syn_E',  'tau_syn_E', 0.001), 
    2253        ('tau_syn_I',  'tau_syn_I', 0.001), 
     
    2657    ) 
    2758    eqs= brian.Equations(''' 
    28         dv/dt  = (ge + gi-(v-v_rest))/tau_m : volt 
    29         dge/dt = (y-ge)/tau_syn_E           : volt  
    30         dy/dt = -y/tau_syn_E                : volt 
    31         dgi/dt = (y-gi)/tau_syn_I           : volt  
    32         dy/dt = -y/tau_syn_I                : volt 
    33         tau_syn_E : second 
    34         tau_syn_I : second 
    35         tau_m     : second 
    36         v_rest    : volt 
    37         ''' 
    38         ) 
    39  
    40  
    41 class IF_curr_exp(common.IF_curr_exp): 
    42     """Leaky integrate and fire model with fixed threshold and 
    43     decaying-exponential post-synaptic current. (Separate synaptic currents for 
    44     excitatory and inhibitory synapses.""" 
    45      
    46     translations = common.build_translations( 
    47         ('v_rest',     'v_rest', 0.001), 
    48         ('v_reset',    'v_reset', 0.001), 
    49         ('cm',         'cm'), # C is in pF, cm in nF 
    50         ('tau_m',      'tau_m', 0.001), 
    51         ('tau_refrac', 'tau_refrac',  "max(get_time_step(), tau_refrac)*0.001", "tau_refrac"), 
    52         ('tau_syn_E',  'tau_syn_E', 0.001), 
    53         ('tau_syn_I',  'tau_syn_I', 0.001), 
    54         ('v_thresh',   'v_thresh', 0.001), 
    55         ('i_offset',   'i_offset'), # I0 is in pA, i_offset in nA 
    56         ('v_init',     'v', 0.001), 
    57     ) 
    58     eqs= brian.Equations(''' 
    59         dv/dt  = (ge + gi-(v-v_rest))/tau_m : volt 
    60         dge/dt = -ge/tau_syn_E              : volt 
    61         dgi/dt = -gi/tau_syn_I              : volt 
    62         tau_syn_E : second 
    63         tau_syn_I : second 
    64         tau_m     : second 
    65         v_rest    : volt 
    66         ''' 
    67         ) 
    68          
     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        ) 
    6968     
    7069 
    7170class IF_cond_alpha(common.ModelNotAvailable): 
    72     pass 
     71    translations = common.build_translations( 
     72        ('v_rest',     'v_rest',0.001)    , 
     73        ('v_reset',    'v_reset',0.001), 
     74        ('cm',         'cm'), # C_m is in pF, cm in nF 
     75        ('tau_m',      'tau_m',0.001), 
     76        ('tau_refrac', 'tau_refrac',"max(get_time_step(), tau_refrac*0.001)", "tau_refrac"), 
     77        ('tau_syn_E',  'tau_syn_E',0.001), 
     78        ('tau_syn_I',  'tau_syn_I',0.001), 
     79        ('v_thresh',   'v_thresh',0.001), 
     80        ('i_offset',   'i_offset',0.001), # I_e is in pA, i_offset in nA 
     81        ('e_rev_E',    'e_rev_E',0.001), 
     82        ('e_rev_I',    'e_rev_I',0.001), 
     83        ('v_init',     'v',0.001), 
     84    ) 
     85    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        ) 
    7399 
    74100 
     
    76102    """Leaky integrate and fire model with fixed threshold and  
    77103    exponentially-decaying post-synaptic conductance.""" 
    78     pass 
    79      
     104    translations = common.build_translations( 
     105        ('v_rest',     'v_rest',0.001)    , 
     106        ('v_reset',    'v_reset',0.001), 
     107        ('cm',         'cm'), # C_m is in pF, cm in nF 
     108        ('tau_m',      'tau_m',0.001), 
     109        ('tau_refrac', 'tau_refrac',"max(get_time_step(), tau_refrac*0.001)", "tau_refrac"), 
     110        ('tau_syn_E',  'tau_syn_E',0.001), 
     111        ('tau_syn_I',  'tau_syn_I',0.001), 
     112        ('v_thresh',   'v_thresh',0.001), 
     113        ('i_offset',   'i_offset',0.001), # I_e is in pA, i_offset in nA 
     114        ('e_rev_E',    'e_rev_E',0.001), 
     115        ('e_rev_I',    'e_rev_I',0.001), 
     116        ('v_init',     'v',0.001), 
     117    ) 
     118    eqs= brian.Equations(''' 
     119        dv/dt  = ((v_rest-v) + ge*(e_rev_E-v) + gi*(e_rev_I-v))/tau_m : volt 
     120        dge/dt = -ge/tau_syn_E : 1 
     121        dgi/dt = -gi/tau_syn_I : 1 
     122        tau_syn_E              : second 
     123        tau_syn_I              : second 
     124        tau_m                  : second 
     125        v_rest                 : volt 
     126        e_rev_E                : volt 
     127        e_rev_I                : volt 
     128        ''' 
     129        ) 
    80130 
    81131class IF_facets_hardware1(common.IF_facets_hardware1): 
     
    92142    translations = common.build_translations( 
    93143        ('rate',     'rate'), 
    94         ('start',    'start'), 
    95         ('duration', 'duration'), 
    96     ) 
     144        ('start',    'start', 0.001), 
     145        ('duration', 'duration', 0.001), 
     146    ) 
     147     
     148    def __init__(self, parameters): 
     149        common.SpikeSourcePoisson.__init__(self, parameters) 
     150        start    = self.parameters['start'] 
     151        duration = self.parameters['duration'] 
     152        rate     = self.parameters['rate'] 
     153        self.fct = lambda t: (start <= t <= start + duration and rate) 
    97154     
    98155class SpikeSourceArray(common.SpikeSourceArray): 
    99156    """Spike source generating spikes at the times given in the spike_times array.""" 
    100     pass 
    101  
    102      
     157 
     158 
    103159class EIF_cond_alpha_isfa_ista(common.ModelNotAvailable): 
    104160    pass 
    105161 
    106 class HH_cond_exp(common.ModelNotAvailable): 
    107     pass 
     162class HH_cond_exp(common.HH_cond_exp): 
     163     
     164    translations = common.build_translations( 
     165        ('gbar_Na',    'gbar_Na'),    
     166        ('gbar_K',     'gbar_K'),     
     167        ('g_leak',     'g_leak'),     
     168        ('cm',         'cm'),   
     169        ('v_offset',   'v_offset'), 
     170        ('e_rev_Na',   'e_rev_Na'), 
     171        ('e_rev_K',    'e_rev_K'),  
     172        ('e_rev_leak', 'e_rev_leak'), 
     173        ('e_rev_E',    'e_rev_E'), 
     174        ('e_rev_I',    'e_rev_I'), 
     175        ('tau_syn_E',  'tau_syn_E'), 
     176        ('tau_syn_I',  'tau_syn_I'), 
     177        ('i_offset',   'i_offset'), 
     178        ('v_init',     'v'), 
     179    ) 
     180     
     181    eqs= brian.Equations(''' 
     182        dv/dt = (g_leak*(e_rev_leak-v)+ge*(e_rev_E-v)+gi*(e_rev_I-v)-gbar_Na*(m*m*m)*h*(v-e_rev_Na)-gbar_K*(n*n*n*n)*(v-e_rev_K))/cm : volt 
     183        dm/dt = alpham*(1-m)-betam*m : 1 
     184        dn/dt = alphan*(1-n)-betan*n : 1 
     185        dh/dt = alphah*(1-h)-betah*h : 1 
     186        dge/dt = -ge/tau_syn_E : siemens 
     187        dgi/dt = -gi/tau_syn_I : siemens 
     188        alpham = 0.32*(mV**-1)*(13*mV-v+VT)/(exp((13*mV-v+VT)/(4*mV))-1.)/ms : Hz 
     189        betam = 0.28*(mV**-1)*(v-VT-40*mV)/(exp((v-VT-40*mV)/(5*mV))-1)/ms : Hz 
     190        alphah = 0.128*exp((17*mV-v+VT)/(18*mV))/ms : Hz 
     191        betah = 4./(1+exp((40*mV-v+VT)/(5*mV)))/ms : Hz 
     192        alphan = 0.032*(mV**-1)*(15*mV-v+VT)/(exp((15*mV-v+VT)/(5*mV))-1.)/ms : Hz 
     193        betan = .5*exp((10*mV-v+VT)/(40*mV))/ms : Hz 
     194        tau_syn_E              : second 
     195        tau_syn_I              : second 
     196        e_rev_E                : volt 
     197        e_rev_I                : volt 
     198        e_rev_Na               : volt 
     199        e_rev_K                : volt 
     200        e_rev_leak             : volt 
     201        gbar_Na                : nS 
     202        gbar_K                 : nS 
     203        gbar_leak              : nS 
     204        v_offset               : volt 
     205        cm                     : nF 
     206    ''') 
    108207 
    109208class SpikeSourceInhGamma(common.ModelNotAvailable): 
  • trunk/src/brian/connectors.py

    r317 r427  
    1010from pyNN.random import RandomDistribution, NativeRNG 
    1111from math import * 
     12from numpy import arccos, arcsin, arctan, arctan2, ceil, cos, cosh, e, exp, \ 
     13                  fabs, floor, fmod, hypot, ldexp, log, log10, modf, pi, power, \ 
     14                  sin, sinh, sqrt, tan, tanh 
     15 
     16def _targetConnection(Connector, projection): 
     17    if projection.synapse_type == "excitatory": 
     18        target="ge" 
     19    else: 
     20        target="gi" 
     21    return brian.Connection(projection.pre.brian_cells,projection.post.brian_cells,target, delay=Connector.delays*0.001) 
    1222 
    1323class AllToAllConnector(common.AllToAllConnector):     
    1424     
    1525    def connect(self, projection): 
    16         if projection.synapse_type == "excitatory": 
    17             projection._connections = brian.Connection(projection.pre.cell, projection.post.cell,'ge', delay=self.delays*0.001) 
    18         else: 
    19             projection._connections = brian.Connection(projection.pre.cell, projection.post.cell,'gi', delay=self.delays*0.001) 
    20         projection._connections.connect_full(projection.pre.cell,projection.post.cell, weight=self.weights) 
     26        projection._connections = _targetConnection(self, projection) 
     27        projection._connections.connect_full(projection.pre.brian_cells,projection.post.brian_cells, weight=self.weights) 
    2128        return projection._connections.W.getnnz() 
    2229 
     
    2431     
    2532    def connect(self, projection): 
    26         raise Exception("Not implemented yet !") 
     33        projection._connections = _targetConnection(self, projection) 
     34        projection._connections.connect_one_to_one(projection.pre.brian_cells,projection.post.brian_cells, weight=self.weights) 
     35        return projection._connections.W.getnnz() 
    2736     
    2837class FixedProbabilityConnector(common.FixedProbabilityConnector): 
    2938     
    3039    def connect(self, projection): 
    31         if projection.synapse_type == "excitatory": 
    32             projection._connections = brian.Connection(projection.pre.cell,projection.post.cell,'ge', delay=self.delays*0.001) 
    33         else: 
    34             projection._connections = brian.Connection(projection.pre.cell,projection.post.cell,'gi', delay=self.delays*0.001) 
    35         projection._connections.connect_random(projection.pre.cell,projection.post.cell, self.p_connect, weight=self.weights) 
     40        projection._connections = _targetConnection(self, projection) 
     41        projection._connections.connect_random(projection.pre.brian_cells,projection.post.brian_cells, self.p_connect, weight=self.weights) 
    3642        return projection._connections.W.getnnz() 
    3743     
     
    3945     
    4046    def connect(self, projection): 
    41         raise Exception("Not implemented yet !") 
     47         
     48        periodic_boundaries = self.periodic_boundaries 
     49        if periodic_boundaries is True: 
     50            dimensions = projection.post.dim 
     51            periodic_boundaries = tuple(numpy.concatenate((dimensions, numpy.zeros(3-len(dimensions))))) 
     52        if periodic_boundaries: 
     53            print "Periodic boundaries activated and set to size ", periodic_boundaries 
     54        if projection.rng: 
     55            if isinstance(projection.rng, NativeRNG): 
     56                print "Warning: use of NativeRNG not implemented. Using NumpyRNG" 
     57                rng = numpy.random 
     58            else: 
     59                rng = projection.rng 
     60        else: 
     61            rng = numpy.random 
     62        global nb_conn, post, N 
     63        # This is just a temporary counter implemented to be sure the 
     64        # connections are build as wanted. Because now, for the moment, 
     65        # the connect_full method of Brian established all the connections,  
     66        # and not only those with non zero elements.  
     67        # The -2 comes from the fact that brian will make 2 tests calls before 
     68        # using the function. 
     69        nb_conn = -2 
     70        post    = projection.post 
     71        N       = len(post) 
     72         
     73        def topoconnect(i,j): 
     74            global nb_conn, post, N 
     75            pre  = projection.pre.cell.flatten()[i] 
     76            distances = common.distances(pre, post, self.mask, 
     77                                         self.scale_factor, self.offset, 
     78                                         periodic_boundaries) 
     79            func = eval("lambda d: %s" %self.d_expression) 
     80            p = func(distances[0]) 
     81            # We get the list of cells that will established a connection 
     82            rarr  = rng.uniform(0, 1, N) 
     83            conn  = ((p >= 1) | ((0 < p) & (p < 1) & (rarr <= p))) 
     84            if isinstance(self.weights,str): 
     85                func = eval("lambda d: %s" %self.weights) 
     86                weights = func(distances[0]) 
     87            else: 
     88                weights = self.weights*numpy.ones(N) 
     89            if isinstance(self.delays,str): 
     90                raise Exception('''The string definition for delays in Brian is not  
     91                                implemented since Brian does not support yet  
     92                                heterogeneous delays''') 
     93            if not self.allow_self_connections and conn[i]: 
     94                conn[i] = False 
     95            #non_conn = numpy.where(conn == False)[0] 
     96            #nb_conn += N - len(non_conn) 
     97            weights[numpy.equal(conn, False)] = 0. 
     98            return weights 
     99        projection._connections = _targetConnection(self, projection) 
     100        projection._connections.connect_full(projection.pre.brian_cells,projection.post.brian_cells,weight=topoconnect) 
     101        return projection._connections.W.getnnz() 
    42102     
    43103 
  • trunk/src/brian/synapses.py

    r314 r427  
    88 
    99class SynapseDynamics(common.SynapseDynamics): 
    10     def __init__(self, *args, **kwargs): 
    11         raise NotImplementedError("Dynamic synapses are not available for this simulator."
     10    def __init__(self, fast=None, slow=None): 
     11        common.SynapseDynamics.__init__(self, fast, slow
    1212 
    1313class STDPMechanism(common.STDPMechanism): 
    14     def __init__(self, *args, **kwargs): 
    15         raise NotImplementedError("STDP is not available for this simulator.") 
     14    def __init__(self, timing_dependence=None, weight_dependence=None, 
     15                 voltage_dependence=None, dendritic_delay_fraction=1.0): 
     16        assert dendritic_delay_fraction == 1, """Brian does not currently support axonal delays: 
     17                                                 for the purpose of STDP calculations all delays 
     18                                                 are assumed to be dendritic.""" 
     19        common.STDPMechanism.__init__(self, timing_dependence, weight_dependence, 
     20                                      voltage_dependence, dendritic_delay_fraction) 
    1621 
    1722class TsodkysMarkramMechanism(common.ModelNotAvailable): 
    18     pass 
     23     
     24    def __init__(self, U=0.5, tau_rec=100.0, tau_facil=0.0, u0=0.0, x0=1.0, y0=0.0): 
     25        common.TsodyksMarkramMechanism.__init__(self, U, tau_rec, tau_facil, u0, x0, y0) 
     26        self.parameters = self.translate(parameters) 
     27        self.eqs = ''' 
     28              dR/dt=(1-R)/%g : 1 
     29              tau_rec        : ms 
     30              ''' %tau_rec 
     31 
     32    def reset(population,spikes, v_reset): 
     33        population.R_[spikes]-=U_SE*population.R_[spikes] 
     34        population.v_[spikes]= v_reset 
    1935 
    2036class AdditiveWeightDependence(common.ModelNotAvailable):