Changeset 96

Show
Ignore:
Timestamp:
06/07/07 11:35:13 (1 year ago)
Author:
apdavison
Message:

:

  • Improved ID class now working with neuron module
  • Renamed ID.set() method to setParameters(), and added getParameters()

The reason for not having a parameters property, is that setParameters() should allow setting only a sub-set of the cell parameters and so parameters would have behave in some ways like a dict and in other ways not, which could be confusing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/improved-ID/common.py

    r95 r96  
    4444    """ 
    4545     
     46    non_parameter_attributes = ('parent','_cellclass','cellclass','_position','position','hocname') 
     47     
    4648    def __init__(self,n): 
    4749        int.__init__(n) 
    4850        self.parent = None 
    4951        self._cellclass = None 
    50         #self._hocname   = None 
    5152 
    5253    def __getattr__(self,name): 
     
    5556     
    5657    def __setattr__(self,name,value): 
    57         if name in ('parent','_cellclass','cellclass','_position','position')
     58        if name in ID.non_parameter_attributes
    5859            object.__setattr__(self,name,value) 
    5960        else: 
    60             return self.set(**{name:value}) 
     61            return self.setParameters(**{name:value}) 
    6162 
    6263    def _set_cellclass(self, cellclass): 
     
    9697    position = property(_get_position, _set_position) 
    9798 
    98     def set(self,**parameters): 
     99    def setParameters(self,**parameters): 
    99100        """Set cell parameters, given as a sequence of parameter=value arguments.""" 
     101        return _abstractMethod(self) 
     102     
     103    def getParameters(self): 
     104        """Return a dict of all cell parameters.""" 
    100105        return _abstractMethod(self) 
    101106 
  • branches/improved-ID/nest.py

    r95 r96  
    3939        return pynest.getDict([int(self)])[0][translated_name] 
    4040     
    41     def set(self,**parameters): 
     41    def setParameters(self,**parameters): 
    4242        # We perform a call to the low-level function set() of the API. 
    4343        # If the cellclass is not defined in the ID object : 
     
    4848            set(self, self.cellclass, parameters)  
    4949 
     50    def getParameters(self): 
     51        """Note that this currently does not translate units.""" 
     52        pynest_params = pynest.getDict([int(self)])[0] 
     53        params = {} 
     54        for k,v in self.cellclass.translations.items(): 
     55            params[k] = pynest_params[v[0]] 
     56        return params 
     57             
    5058 
    5159# ============================================================================== 
  • branches/improved-ID/neuron.py

    r87 r96  
    4040    """ 
    4141     
     42    def __init__(self,n): 
     43        common.ID.__init__(self,n) 
     44        self.hocname = None 
     45     
    4246    def __getattr__(self,name): 
    4347        """Note that this currently does not translate units.""" 
    44         translated_name = self._cellclass.translations[name][0] 
    45         if self._hocname: 
    46             return HocToPy.get('%s.%s' % (self._hocname, translated_name),'float') 
     48        translated_name = self.cellclass.translations[name][0] 
     49        if self.hocname: 
     50            return HocToPy.get('%s.%s' % (self.hocname, translated_name),'float') 
    4751        else: 
    4852            return HocToPy.get('cell%d.%s' % (int(self), translated_name),'float') 
    4953     
    50     def set(self,param,val=None): 
     54    def setParameters(self,**parameters): 
    5155        # We perform a call to the low-level function set() of the API. 
    52         # If the cellclass is not defined in the ID object, we have an error (?)
    53         if (self._cellclass == None): 
     56        # If the cellclass is not defined in the ID object, we have an error
     57        if (self.cellclass == None): 
    5458            raise Exception("Unknown cellclass") 
    5559        else: 
     
    5862            #several nodes. Then a call like cell[i,j].set() should be performed only on the 
    5963            #node who owns the cell. To do that, if the node doesn't have the cell, a call to set() 
    60             #do nothing... 
    61             ##if self._hocname != None: 
    62             ##    set(self,self._cellclass,param,val, self._hocname) 
    63             set(self,self._cellclass,param,val) 
    64      
    65     def get(self,param): 
    66         return self.__getattr__(param) 
    67      
    68     # Fonctions used only by the neuron version of pyNN, to optimize the 
    69     # creation of networks 
    70     def setHocName(self, name): 
    71         self._hocname = name 
    72  
    73     def getHocName(self): 
    74         return self._hocname 
    75      
     64            #does nothing... 
     65            set(self, self.cellclass, parameters) 
     66 
     67    def getParameters(self): 
     68        params = {} 
     69        for k,v in self.cellclass.translations.items(): 
     70            params[k] = HocToPy.get('%s.%s' % (self.hocname, v[0]),'float') 
     71        return params 
    7672 
    7773# ============================================================================== 
     
    499495    cell_list = [ID(i) for i in range(gid,gid+n)] 
    500496    for id in cell_list: 
    501         id.setCellClass(cellclass) 
     497        id.cellclass = cellclass 
    502498    gid = gid+n 
    503499    if n == 1: 
     
    544540    return range(nc_start,ncid) 
    545541 
    546 def set(cells,cellclass,param,val=None): #,hocname=None): 
     542def set(cells,cellclass,param,val=None): 
    547543    """Set one or more parameters of an individual cell or list of cells. 
    548544    param can be a dict, in which case val should not be supplied, or a string 
     
    560556    for param,val in paramDict.items(): 
    561557        if isinstance(val,str): 
    562             ## If we know the hoc name of the object (set() applied to a population object), we use it 
    563             #if (hocname != None): 
    564             #    fmt = '%s.%s = "%s"' 
    565             #else: 
    566             #    fmt = 'cell%d.%s = "%s"' 
    567558            fmt = 'pc.gid2cell(%d).%s = "%s"' 
    568559        elif isinstance(val,list): 
    569560            cmds,argstr = _hoc_arglist([val]) 
    570561            hoc_commands += cmds 
    571             ## If we know the hoc name of the object (set() applied to a population object), we use it 
    572             #if (hocname != None): 
    573             #    fmt = '%s.%s = %s' 
    574             #else: 
    575             #    fmt = 'cell%d.%s = %s' 
    576562            fmt = 'pc.gid2cell(%d).%s = %s' 
    577563            val = argstr 
    578564        else: 
    579             ## If we know the hoc name of the object (set() applied to a population object), we use it 
    580             #if (hocname != None): 
    581             #    fmt = '%s.%s = %g' 
    582             #else: 
    583             #    fmt = 'cell%d.%s = %g' 
    584565            fmt = 'pc.gid2cell(%d).%s = %g' 
    585566        for cell in cells: 
    586567            if cell in gidlist: 
    587                 ## If we know the hoc name of the object (set() applied to a population object), we use it 
    588                 #if (hocname != None): 
    589                 #    hoc_commands += [fmt % (hocname,param,val), 
    590                 #                     'tmp = %s.param_update()' %hocname] 
    591                 #else: 
    592                 #    hoc_commands += [fmt % (cell,param,val), 
    593                 #                     'tmp = cell%d.param_update()' %cell] 
    594568                hoc_commands += [fmt % (cell,param,val), 
    595569                                 'tmp = pc.gid2cell(%d).param_update()' % cell] 
     
    699673        # each population should store what we call a "fullgidlist" with the ID of all the cells in the populations  
    700674        # (and therefore their positions) 
    701         self.fullgidlist = [ID(i) for i in range(gid, gid+self.size) if i < gid+self.size] 
     675        self.fullgidlist = numpy.array([ID(i) for i in range(gid, gid+self.size) if i < gid+self.size], ID) 
     676        self.cell = self.fullgidlist 
    702677         
    703678        # self.gidlist is now derived from self.fullgidlist since it contains only the cells of the population located on 
     
    728703        # in the population 
    729704        for cell_id in self.fullgidlist: 
    730             cell_id.setCellClass(cellclass) 
    731             cell_id.setPosition(self.locate(cell_id)) 
     705            cell_id.parent = self 
     706            #cell_id.setPosition(self.locate(cell_id)) 
    732707                     
    733708        # On the opposite, each node has to know only the precise hocname of its cells, if we 
    734709        # want to be able to use the low level set() function 
    735710        for cell_id in self.gidlist: 
    736             cell_id.setHocName("%s.o(%d)" %(self.hoc_label, self.gidlist.index(cell_id))) 
     711            cell_id.hocname = "%s.o(%d)" % (self.hoc_label, self.gidlist.index(cell_id)) 
    737712 
    738713    def __getitem__(self,addr): 
     
    11821157        population is scaled to the size of the source population.""" 
    11831158        dist = 0.0 
    1184         src_position = src.getPosition() 
    1185         tgt_position = tgt.getPosition() 
     1159        src_position = src.position 
     1160        tgt_position = tgt.position 
    11861161        if (len(src_position) == len(tgt_position)): 
    11871162            for i in xrange(len(src_position)): 
     
    15811556                tgt = self.connections[i][1] 
    15821557                # calculate the distance between the two cells 
    1583                 idx_src = self.pre.fullgidlist.index(src) 
    1584                 idx_tgt = self.post.fullgidlist.index(tgt) 
     1558                idx_src = numpy.where(self.pre.fullgidlist == src)[0][0] 
     1559                idx_tgt = numpy.where(self.post.fullgidlist == tgt)[0][0] 
    15851560                dist = self._distance(self.pre, self.post, self.pre.fullgidlist[idx_src], self.post.fullgidlist[idx_tgt]) 
    15861561                # then evaluate the delay according to the delay rule 
  • branches/improved-ID/test/neurontests.py

    r86 r96  
    595595        assert (mean_weight_before < mean_weight_after) 
    596596         
    597     def testSetAndGetID(self): 
    598         # Small test to see if the ID class is working 
    599         self.target[0,2].set({'tau_m' : 15.1}) 
    600         assert (self.target[0,2].get('tau_m') == 15.1) 
    601          
    602     def testSetAndGetPositionID(self): 
    603         # Small test to see if the position of the ID class is working 
    604         self.target[0,2].setPosition((0.5,1.5)) 
    605         assert (self.target[0,2].getPosition() == (0.5,1.5)) 
    606          
    607597    def testSetTopographicDelay(self): 
    608598        # We fix arbitrarily the positions of 2 cells in 2 populations and check  
    609599        # the topographical delay between them is linked to the distance 
    610         self.source[0,0].setPosition((0,0)
    611         self.target[2,2].setPosition((0,10)
     600        self.source[0,0].position = (0,0,0
     601        self.target[2,2].position = (0,10,0
    612602        prj1 = neuron.Projection(self.source, self.target, 'allToAll') 
    613603        rule="5.432*d" 
     
    661651    def setUp(self): 
    662652        neuron.Population.nPop = 0 
    663         self.pop = neuron.Population((5,),neuron.IF_curr_alpha,{'tau_m':10.0}) 
    664      
    665     def testIDSet(self): 
    666         self.pop[3].set('tau_m',20.0) 
    667         self.assertEqual(HocToPy.get('%s.object(3).tau_m' % self.pop.label, 'float'), 20.0) 
    668         self.assertEqual(HocToPy.get('%s.object(1).tau_m' % self.pop.label, 'float'), 10.0) 
    669  
     653        self.pop1 = neuron.Population((5,),neuron.IF_curr_alpha,{'tau_m':10.0}) 
     654        self.pop2 = neuron.Population((5,4),neuron.IF_curr_exp,{'v_reset':-60.0}) 
     655     
     656    def testIDSetAndGet(self): 
     657        self.pop1[3].tau_m = 20.0 
     658        self.pop2[3,2].v_reset = -70.0 
     659        self.assertEqual(HocToPy.get('%s.object(3).tau_m' % self.pop1.label, 'float'), 20.0) 
     660        self.assertEqual(20.0, self.pop1[3].tau_m) 
     661        self.assertEqual(10.0, self.pop1[0].tau_m) 
     662        self.assertEqual(-70.0, self.pop2[3,2].v_reset) 
     663        self.assertEqual(-60.0, self.pop2[0,0].v_reset) 
     664 
     665    def testGetCellClass(self): 
     666        self.assertEqual(neuron.IF_curr_alpha, self.pop1[0].cellclass) 
     667        self.assertEqual(neuron.IF_curr_exp, self.pop2[4,3].cellclass) 
     668         
     669    def testSetAndGetPosition(self): 
     670        self.assert_((self.pop2[0,2].position == (0.0,2.0,0.0)).all()) 
     671        new_pos = (0.5,1.5,0.0) 
     672        self.pop2[0,2].position = new_pos 
     673        self.assert_((self.pop2[0,2].position == (0.5,1.5,0.0)).all()) 
     674        new_pos = (-0.6,3.5,-100.0) # check that position is set-by-value from new_pos 
     675        self.assert_((self.pop2[0,2].position == (0.5,1.5,0.0)).all()) 
     676         
    670677if __name__ == "__main__": 
    671678    sys.argv = ['./nrnpython']