Changeset 420
- Timestamp:
- 07/16/08 20:09:31 (1 month ago)
- Files:
-
- branches/harmonize/src/brian/__init__.py (modified) (5 diffs)
- branches/harmonize/src/nest1/__init__.py (modified) (3 diffs)
- branches/harmonize/src/nest2/__init__.py (modified) (1 diff)
- branches/harmonize/src/pcsim/__init__.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/harmonize/src/brian/__init__.py
r414 r420 243 243 self.cell_id = numpy.arange(len(self.cell)) 244 244 self.cell_id = numpy.reshape(self.cell_id, self.dim) 245 self.first_id = 0 245 246 self.spike_recorder = None 246 247 self.vm_recorder = None … … 270 271 def __iter__(self): 271 272 return self.cell.flat 272 273 def __address_gen(self):274 """275 Generator to produce an iterator over all cells on this node,276 returning addresses.277 """278 for i in self.__iter__():279 yield self.locate(i)280 281 def addresses(self):282 return self.__address_gen()283 284 def ids(self):285 return self.__iter__()286 273 287 274 def index(self, n): … … 289 276 return self.cell.item(n) 290 277 291 def locate(self, id):292 """Given an element id in a Population, return the coordinates.293 e.g. for 4 6 , element 2 has coordinates (1,0) and value 7294 7 9295 """296 # The top two lines (commented out) are the original implementation,297 # which does not scale well when the population size gets large.298 # The next lines are the neuron implementation of the same method. This299 # assumes that the id values in self.cell are consecutive. This should300 # always be the case, I think? A unit test is needed to check this.301 302 ###assert isinstance(id, int)303 ###return tuple([a.tolist()[0] for a in numpy.where(self.cell == id)])304 305 if self.ndim == 3:306 rows = self.dim[1]; cols = self.dim[2]307 i = id/(rows*cols); remainder = id%(rows*cols)308 j = remainder/cols; k = remainder%cols309 coords = (i,j,k)310 elif self.ndim == 2:311 cols = self.dim[1]312 i = id/cols; j = id%cols313 coords = (i,j)314 elif self.ndim == 1:315 coords = (id,)316 else:317 raise common.InvalidDimensionsError318 return coords319 320 278 def get(self, parameter_name, as_array=False): 321 279 """ … … 324 282 values = getattr(self.cell, parameter_name) 325 283 if as_array: 326 values = numpy.array(values) 284 values = numpy.array(values).reshape(self.dim) 327 285 return values 328 286 … … 387 345 assert len(rarr) == len(self.cell_id) 388 346 setattr(self.cell, parametername, rarr) 389 390 def _call(self, methodname, arguments):391 """392 Calls the method methodname(arguments) for every cell in the population.393 e.g. p.call("set_background","0.1") if the cell class has a method394 set_background().395 """396 raise Exception("Method not yet implemented")397 398 def _tcall(self, methodname, objarr):399 """400 `Topographic' call. Calls the method methodname() for every cell in the401 population. The argument to the method depends on the coordinates of the402 cell. objarr is an array with the same dimensions as the Population.403 e.g. p.tcall("memb_init", vinitArray) calls404 p.cell[i][j].memb_init(vInitArray[i][j]) for all i,j.405 """406 raise Exception("Method not yet implemented")407 347 408 348 def record(self, record_from=None, rng=None): branches/harmonize/src/nest1/__init__.py
r416 r420 458 458 def __iter__(self): 459 459 return self.cell.flat 460 461 def __address_gen(self):462 """463 Generator to produce an iterator over all cells on this node,464 returning addresses.465 """466 for i in self.__iter__():467 yield self.locate(i)468 469 def addresses(self):470 return self.__address_gen()471 472 def ids(self):473 return self.__iter__()474 460 475 461 def index(self, n): 476 462 """Return the nth cell in the population (Indexing starts at 0).""" 477 463 return self.cell.item(n) 478 479 def locate(self, id):480 """Given an element id in a Population, return the coordinates.481 e.g. for 4 6 , element 2 has coordinates (1,0) and value 7482 7 9483 """484 # The top two lines (commented out) are the original implementation,485 # which does not scale well when the population size gets large.486 # The next lines are the neuron implementation of the same method. This487 # assumes that the id values in self.cell are consecutive. This should488 # always be the case, I think? A unit test is needed to check this.489 490 ###assert isinstance(id, int)491 ###return tuple([a.tolist()[0] for a in numpy.where(self.cell == id)])492 493 id -= self.first_id494 if self.ndim == 3:495 rows = self.dim[1]; cols = self.dim[2]496 i = id/(rows*cols); remainder = id%(rows*cols)497 j = remainder/cols; k = remainder%cols498 coords = (i,j,k)499 elif self.ndim == 2:500 cols = self.dim[1]501 i = id/cols; j = id%cols502 coords = (i,j)503 elif self.ndim == 1:504 coords = (id,)505 else:506 raise common.InvalidDimensionsError507 return coords508 509 def get(self, parameter_name, as_array=False):510 """511 Get the values of a parameter for every cell in the population.512 """513 values = [getattr(cell, parameter_name) for cell in self.cell.flat]514 if as_array:515 values = numpy.array(values)516 return values517 518 def set(self, param, val=None):519 """520 Set one or more parameters for every cell in the population. param521 can be a dict, in which case val should not be supplied, or a string522 giving the parameter name, in which case val is the parameter value.523 val can be a numeric value, or list of such (e.g. for setting spike times).524 e.g. p.set("tau_m",20.0).525 p.set({'tau_m':20,'v_rest':-65})526 """527 if isinstance(param, str):528 if isinstance(val, str) or isinstance(val, float) or isinstance(val, int):529 param_dict = {param:float(val)}530 else:531 raise common.InvalidParameterValueError532 elif isinstance(param, dict):533 param_dict = param534 else:535 raise common.InvalidParameterValueError536 for cell in self.cell.flat:537 cell.set_parameters(**param_dict)538 539 464 540 465 def tset(self, parametername, value_array): … … 578 503 for cell,val in zip(cells, rarr): 579 504 setattr(cell, parametername, val) 580 581 def _call(self, methodname, arguments):582 """583 Calls the method methodname(arguments) for every cell in the population.584 e.g. p.call("set_background","0.1") if the cell class has a method585 set_background().586 """587 raise Exception("Method not yet implemented")588 589 def _tcall(self, methodname, objarr):590 """591 `Topographic' call. Calls the method methodname() for every cell in the592 population. The argument to the method depends on the coordinates of the593 cell. objarr is an array with the same dimensions as the Population.594 e.g. p.tcall("memb_init", vinitArray) calls595 p.cell[i][j].memb_init(vInitArray[i][j]) for all i,j.596 """597 raise Exception("Method not yet implemented")598 505 599 506 def record(self, record_from=None, rng=None): … … 754 661 n_spikes = status["events"] 755 662 return float(n_spikes)/self.n_rec 756 757 def randomInit(self, rand_distr):758 """759 Set initial membrane potentials for all the cells in the population to760 random values.761 """762 self.rset('v_init', rand_distr)763 #cells = numpy.reshape(self.cell, self.cell.size)764 #rvals = rand_distr.next(n=self.cell.size)765 #for node, v_init in zip(cells, rvals):766 # pynest.setDict([node],{'u': v_init})767 663 768 664 def print_v(self, filename, gather=True, compatible_output=True): branches/harmonize/src/nest2/__init__.py
r419 r420 20 20 common.simulator = simulator 21 21 22 tempdirs = []22 tempdirs = [] 23 23 24 24 DEFAULT_BUFFER_SIZE = 10000 branches/harmonize/src/pcsim/__init__.py
r332 r420 512 512 param can be a dict, in which case val should not be supplied, or a string 513 513 giving the parameter name, in which case val is the parameter value. 514 cellclass must be supplied for doing translation of parameter names."""514 """ 515 515 param_dict = checkParams(param, val) 516 516 for cell in cells: … … 651 651 def __iter__(self): 652 652 return self.__gid_gen() 653 654 def __address_gen(self):655 """656 Generator to produce an iterator over all cells on this node,657 returning addresses.658 """659 for i in self.__iter__():660 yield self.locate(i)661 653 662 654 def __gid_gen(self): … … 670 662 id.parent = self 671 663 yield id 672 673 def addresses(self):674 return self.__address_gen()675 676 def ids(self):677 return self.__iter__()678 664 679 665 def locate(self, id): … … 759 745 rarr = rarr.reshape(self.dim[0:self.actual_ndim]) 760 746 self.tset(parametername, rarr) 761 762 def _call(self, methodname, arguments): 763 """ 764 Calls the method methodname(arguments) for every cell in the population. 765 e.g. p.call("set_background","0.1") if the cell class has a method 766 set_background(). 767 """ 768 """ This works nicely for PCSIM for simulator specific cells, 769 because cells (SimObject classes) are directly wrapped in python """ 770 for i in xrange(0, len(self)): 771 obj = pcsim_globals.net.object(self.pcsim_population[i]) 772 if obj: apply( obj, methodname, (), arguments) 773 774 775 776 def _tcall(self, methodname, objarr): 777 """ 778 `Topographic' call. Calls the method methodname() for every cell in the 779 population. The argument to the method depends on the coordinates of the 780 cell. objarr is an array with the same dimensions as the Population. 781 e.g. p.tcall("memb_init", vinitArray) calls 782 p.cell[i][j].memb_init(vInitArray[i][j]) for all i, j. 783 """ 784 """ PCSIM: iteration at the python level and apply""" 785 for i in xrange(0, len(self)): 786 obj = pcsim_globals.net.object(self.pcsim_population[i]) 787 if obj: apply( obj, methodname, (), arguments) 788 789 747 790 748 def record(self, record_from=None, rng=None): 791 749 """ … … 903 861 return 0; 904 862 905 def randomInit(self, rand_distr):906 """907 Set initial membrane potentials for all the cells in the population to908 random values.909 """910 """ PCSIM: can be reduced to rset() where parameterName is Vinit"""911 self.rset("v_init", rand_distr)912 913 863 914 864 class Projection(common.Projection, WDManager): … … 920 870 921 871 nProj = 0 922 923 #class ConnectionDict:924 #925 # def __init__(self, parent):926 # self.parent = parent927 #928 # def __getitem__(self, id):929 # """Returns a connection id.930 # Suppose we have a 2D Population (5x3) projecting to a 3D Population (4x5x7).931 # Total number of possible connections is 5x3x4x5x7 = 2100.932 # Therefore valid calls are:933 # connection[2099] - 2099th possible connection (may not exist)934 # connection[14,139] - connection between 14th pre- and 139th postsynaptic neuron (may not exist)935 # connection[(4,2),(3,4,6)] - connection between presynaptic neuron with address (4,2)936 # and post-synaptic neuron with address (3,4,6) (may not exist).937 # """938 # if isinstance(id, int): # linear mapping939 # preID = id/self.parent.post.size; postID = id%self.parent.post.size940 # return self.__getitem__((preID, postID))941 # elif isinstance(id, tuple): # (pre, post)942 # if len(id) == 2:943 # pre = id[0]944 # post = id[1]945 # if isinstance(pre, int) and isinstance(post, int):946 # pre_coords = self.parent.pre.locate(pre)947 # post_coords = self.parent.post.locate(post)948 # return self.__getitem__((pre_coords, post_coords))949 # elif isinstance(pre, tuple) and isinstance(post, tuple): # should also allow lists950 # if len(pre) == self.parent.pre.ndim and len(post) == self.parent.post.ndim:951 # fmt = "[%d]"*(len(pre)+len(post))952 # address = fmt % (pre+post)953 # else:954 # raise common.InvalidDimensionsError955 # else:956 # raise KeyError957 # else:958 # raise common.InvalidDimensionsError959 # else:960 # raise KeyError #most appropriate?961 #962 # return address963 #964 872 965 873 def __init__(self, presynaptic_population, postsynaptic_population, method='allToAll', method_parameters=None, source=None, target=None, synapse_dynamics=None, label=None, rng=None):
