Changeset 420

Show
Ignore:
Timestamp:
07/16/08 20:09:31 (1 month ago)
Author:
apdavison
Message:

More duplication reduction

Files:

Legend:

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

    r414 r420  
    243243        self.cell_id = numpy.arange(len(self.cell)) 
    244244        self.cell_id = numpy.reshape(self.cell_id, self.dim) 
     245        self.first_id = 0 
    245246        self.spike_recorder = None 
    246247        self.vm_recorder    = None 
     
    270271    def __iter__(self): 
    271272        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__() 
    286273     
    287274    def index(self, n): 
     
    289276        return self.cell.item(n) 
    290277     
    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 7 
    294                          7 9 
    295         """ 
    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. This 
    299         # assumes that the id values in self.cell are consecutive. This should 
    300         # 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%cols 
    309             coords = (i,j,k) 
    310         elif self.ndim == 2: 
    311             cols = self.dim[1] 
    312             i = id/cols; j = id%cols 
    313             coords = (i,j) 
    314         elif self.ndim == 1: 
    315             coords = (id,) 
    316         else: 
    317             raise common.InvalidDimensionsError 
    318         return coords 
    319      
    320278    def get(self, parameter_name, as_array=False): 
    321279        """ 
     
    324282        values = getattr(self.cell, parameter_name) 
    325283        if as_array: 
    326             values = numpy.array(values) 
     284            values = numpy.array(values).reshape(self.dim) 
    327285        return values 
    328286     
     
    387345            assert len(rarr) == len(self.cell_id) 
    388346            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 method 
    394         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 the  
    401         population. The argument to the method depends on the coordinates of the 
    402         cell. objarr is an array with the same dimensions as the Population. 
    403         e.g. p.tcall("memb_init", vinitArray) calls 
    404         p.cell[i][j].memb_init(vInitArray[i][j]) for all i,j. 
    405         """ 
    406         raise Exception("Method not yet implemented") 
    407347 
    408348    def record(self, record_from=None, rng=None): 
  • branches/harmonize/src/nest1/__init__.py

    r416 r420  
    458458    def __iter__(self): 
    459459        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__() 
    474460     
    475461    def index(self, n): 
    476462        """Return the nth cell in the population (Indexing starts at 0).""" 
    477463        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 7 
    482                          7 9 
    483         """ 
    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. This 
    487         # assumes that the id values in self.cell are consecutive. This should 
    488         # 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_id 
    494         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%cols 
    498             coords = (i,j,k) 
    499         elif self.ndim == 2: 
    500             cols = self.dim[1] 
    501             i = id/cols; j = id%cols 
    502             coords = (i,j) 
    503         elif self.ndim == 1: 
    504             coords = (id,) 
    505         else: 
    506             raise common.InvalidDimensionsError 
    507         return coords 
    508      
    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 values 
    517      
    518     def set(self, param, val=None): 
    519         """ 
    520         Set one or more parameters for every cell in the population. param 
    521         can be a dict, in which case val should not be supplied, or a string 
    522         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.InvalidParameterValueError 
    532         elif isinstance(param, dict): 
    533             param_dict = param 
    534         else: 
    535             raise common.InvalidParameterValueError 
    536         for cell in self.cell.flat: 
    537             cell.set_parameters(**param_dict) 
    538          
    539464 
    540465    def tset(self, parametername, value_array): 
     
    578503            for cell,val in zip(cells, rarr): 
    579504                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 method 
    585         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 the  
    592         population. The argument to the method depends on the coordinates of the 
    593         cell. objarr is an array with the same dimensions as the Population. 
    594         e.g. p.tcall("memb_init", vinitArray) calls 
    595         p.cell[i][j].memb_init(vInitArray[i][j]) for all i,j. 
    596         """ 
    597         raise Exception("Method not yet implemented") 
    598505 
    599506    def record(self, record_from=None, rng=None): 
     
    754661        n_spikes = status["events"] 
    755662        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 to 
    760         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}) 
    767663     
    768664    def print_v(self, filename, gather=True, compatible_output=True): 
  • branches/harmonize/src/nest2/__init__.py

    r419 r420  
    2020common.simulator = simulator 
    2121 
    22 tempdirs       = [] 
     22tempdirs = [] 
    2323 
    2424DEFAULT_BUFFER_SIZE = 10000 
  • branches/harmonize/src/pcsim/__init__.py

    r332 r420  
    512512    param can be a dict, in which case val should not be supplied, or a string 
    513513    giving the parameter name, in which case val is the parameter value. 
    514     cellclass must be supplied for doing translation of parameter names."""    
     514    """    
    515515    param_dict = checkParams(param, val) 
    516516    for cell in cells: 
     
    651651    def __iter__(self): 
    652652        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) 
    661653     
    662654    def __gid_gen(self): 
     
    670662            id.parent = self 
    671663            yield id 
    672              
    673     def addresses(self): 
    674         return self.__address_gen() 
    675      
    676     def ids(self): 
    677         return self.__iter__() 
    678664         
    679665    def locate(self, id): 
     
    759745        rarr = rarr.reshape(self.dim[0:self.actual_ndim])          
    760746        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         
    790748    def record(self, record_from=None, rng=None): 
    791749        """ 
     
    903861        return 0; 
    904862 
    905     def randomInit(self, rand_distr): 
    906         """ 
    907         Set initial membrane potentials for all the cells in the population to 
    908         random values. 
    909         """ 
    910         """ PCSIM: can be reduced to rset() where parameterName is Vinit""" 
    911         self.rset("v_init", rand_distr) 
    912  
    913863 
    914864class Projection(common.Projection, WDManager): 
     
    920870     
    921871    nProj = 0 
    922      
    923     #class ConnectionDict: 
    924     #         
    925     #    def __init__(self, parent): 
    926     #        self.parent = parent 
    927     # 
    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 mapping 
    939     #            preID = id/self.parent.post.size; postID = id%self.parent.post.size 
    940     #            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 lists 
    950     #                    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.InvalidDimensionsError 
    955     #                else: 
    956     #                    raise KeyError 
    957     #            else: 
    958     #                raise common.InvalidDimensionsError 
    959     #        else: 
    960     #            raise KeyError #most appropriate? 
    961     #         
    962     #        return address 
    963     # 
    964872     
    965873    def __init__(self, presynaptic_population, postsynaptic_population, method='allToAll', method_parameters=None, source=None, target=None, synapse_dynamics=None, label=None, rng=None):