Changeset 96
- Timestamp:
- 06/07/07 11:35:13 (1 year ago)
- Files:
-
- branches/improved-ID/common.py (modified) (3 diffs)
- branches/improved-ID/nest.py (modified) (2 diffs)
- branches/improved-ID/neuron.py (modified) (9 diffs)
- branches/improved-ID/test/neurontests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/improved-ID/common.py
r95 r96 44 44 """ 45 45 46 non_parameter_attributes = ('parent','_cellclass','cellclass','_position','position','hocname') 47 46 48 def __init__(self,n): 47 49 int.__init__(n) 48 50 self.parent = None 49 51 self._cellclass = None 50 #self._hocname = None51 52 52 53 def __getattr__(self,name): … … 55 56 56 57 def __setattr__(self,name,value): 57 if name in ('parent','_cellclass','cellclass','_position','position'):58 if name in ID.non_parameter_attributes: 58 59 object.__setattr__(self,name,value) 59 60 else: 60 return self.set (**{name:value})61 return self.setParameters(**{name:value}) 61 62 62 63 def _set_cellclass(self, cellclass): … … 96 97 position = property(_get_position, _set_position) 97 98 98 def set (self,**parameters):99 def setParameters(self,**parameters): 99 100 """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.""" 100 105 return _abstractMethod(self) 101 106 branches/improved-ID/nest.py
r95 r96 39 39 return pynest.getDict([int(self)])[0][translated_name] 40 40 41 def set (self,**parameters):41 def setParameters(self,**parameters): 42 42 # We perform a call to the low-level function set() of the API. 43 43 # If the cellclass is not defined in the ID object : … … 48 48 set(self, self.cellclass, parameters) 49 49 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 50 58 51 59 # ============================================================================== branches/improved-ID/neuron.py
r87 r96 40 40 """ 41 41 42 def __init__(self,n): 43 common.ID.__init__(self,n) 44 self.hocname = None 45 42 46 def __getattr__(self,name): 43 47 """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') 47 51 else: 48 52 return HocToPy.get('cell%d.%s' % (int(self), translated_name),'float') 49 53 50 def set (self,param,val=None):54 def setParameters(self,**parameters): 51 55 # 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): 54 58 raise Exception("Unknown cellclass") 55 59 else: … … 58 62 #several nodes. Then a call like cell[i,j].set() should be performed only on the 59 63 #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 76 72 77 73 # ============================================================================== … … 499 495 cell_list = [ID(i) for i in range(gid,gid+n)] 500 496 for id in cell_list: 501 id. setCellClass(cellclass)497 id.cellclass = cellclass 502 498 gid = gid+n 503 499 if n == 1: … … 544 540 return range(nc_start,ncid) 545 541 546 def set(cells,cellclass,param,val=None): #,hocname=None):542 def set(cells,cellclass,param,val=None): 547 543 """Set one or more parameters of an individual cell or list of cells. 548 544 param can be a dict, in which case val should not be supplied, or a string … … 560 556 for param,val in paramDict.items(): 561 557 if isinstance(val,str): 562 ## If we know the hoc name of the object (set() applied to a population object), we use it563 #if (hocname != None):564 # fmt = '%s.%s = "%s"'565 #else:566 # fmt = 'cell%d.%s = "%s"'567 558 fmt = 'pc.gid2cell(%d).%s = "%s"' 568 559 elif isinstance(val,list): 569 560 cmds,argstr = _hoc_arglist([val]) 570 561 hoc_commands += cmds 571 ## If we know the hoc name of the object (set() applied to a population object), we use it572 #if (hocname != None):573 # fmt = '%s.%s = %s'574 #else:575 # fmt = 'cell%d.%s = %s'576 562 fmt = 'pc.gid2cell(%d).%s = %s' 577 563 val = argstr 578 564 else: 579 ## If we know the hoc name of the object (set() applied to a population object), we use it580 #if (hocname != None):581 # fmt = '%s.%s = %g'582 #else:583 # fmt = 'cell%d.%s = %g'584 565 fmt = 'pc.gid2cell(%d).%s = %g' 585 566 for cell in cells: 586 567 if cell in gidlist: 587 ## If we know the hoc name of the object (set() applied to a population object), we use it588 #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]594 568 hoc_commands += [fmt % (cell,param,val), 595 569 'tmp = pc.gid2cell(%d).param_update()' % cell] … … 699 673 # each population should store what we call a "fullgidlist" with the ID of all the cells in the populations 700 674 # (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 702 677 703 678 # self.gidlist is now derived from self.fullgidlist since it contains only the cells of the population located on … … 728 703 # in the population 729 704 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)) 732 707 733 708 # On the opposite, each node has to know only the precise hocname of its cells, if we 734 709 # want to be able to use the low level set() function 735 710 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)) 737 712 738 713 def __getitem__(self,addr): … … 1182 1157 population is scaled to the size of the source population.""" 1183 1158 dist = 0.0 1184 src_position = src. getPosition()1185 tgt_position = tgt. getPosition()1159 src_position = src.position 1160 tgt_position = tgt.position 1186 1161 if (len(src_position) == len(tgt_position)): 1187 1162 for i in xrange(len(src_position)): … … 1581 1556 tgt = self.connections[i][1] 1582 1557 # 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] 1585 1560 dist = self._distance(self.pre, self.post, self.pre.fullgidlist[idx_src], self.post.fullgidlist[idx_tgt]) 1586 1561 # then evaluate the delay according to the delay rule branches/improved-ID/test/neurontests.py
r86 r96 595 595 assert (mean_weight_before < mean_weight_after) 596 596 597 def testSetAndGetID(self):598 # Small test to see if the ID class is working599 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 working604 self.target[0,2].setPosition((0.5,1.5))605 assert (self.target[0,2].getPosition() == (0.5,1.5))606 607 597 def testSetTopographicDelay(self): 608 598 # We fix arbitrarily the positions of 2 cells in 2 populations and check 609 599 # 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) 612 602 prj1 = neuron.Projection(self.source, self.target, 'allToAll') 613 603 rule="5.432*d" … … 661 651 def setUp(self): 662 652 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 670 677 if __name__ == "__main__": 671 678 sys.argv = ['./nrnpython']

