Changeset 383

Show
Ignore:
Timestamp:
06/21/08 08:39:07 (5 months ago)
Author:
pierre
Message:

Implement a rough getSubPopulation() function in the common.py file and for nest2 only. By rough I mean that it is a fast fix to help people here to be convinced to use pyNN, but things could be better implemented. Especially concerning the shared parameters that may caused problems

Files:

Legend:

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

    r375 r383  
    700700    """ 
    701701     
    702     def __init__(self, dims, cellclass, cellparams=None, label=None): 
     702    def __init__(self, dims, cellclass, cellparams=None, label=None, create_cells=True): 
    703703        """ 
    704704        dims should be a tuple containing the population dimensions, or a single 
     
    930930        """ 
    931931        return _abstract_method(self) 
    932  
     932     
     933    def getSubPopulation(self, cells): 
     934        """ 
     935        Returns a sub population from a population object. The shape of cells will 
     936        determine the dimensions of the sub population. cells should contains cells 
     937        member of the parent population. 
     938        Ex z = pop.getSubPopulation([pop[1],pop[3],pop[5]]) 
     939        """ 
     940        return _abstract_method(self) 
     941     
    933942# ============================================================================== 
    934943 
  • trunk/src/nest2/__init__.py

    r382 r383  
    530530    nPop = 0 
    531531 
    532     def __init__(self, dims, cellclass, cellparams=None, label=None): 
     532    def __init__(self, dims, cellclass, cellparams=None, label=None, create_cells=True): 
    533533        """ 
    534534        dims should be a tuple containing the population dimensions, or a single 
     
    543543        """ 
    544544 
    545         common.Population.__init__(self, dims, cellclass, cellparams, label
     545        common.Population.__init__(self, dims, cellclass, cellparams, label, create_cells
    546546 
    547547        # Should perhaps use "LayoutNetwork"? 
     
    549549        if isinstance(cellclass, type): 
    550550            self.celltype = cellclass(cellparams) 
    551             self.cell = nest.Create(self.celltype.nest_name, self.size) 
     551            if create_cells: 
     552                self.cell = nest.Create(self.celltype.nest_name, self.size) 
     553            else: 
     554                #A SubPopulation has been created, those fields will be filled later 
     555                self.cell = [] 
    552556            self.cellparams = self.celltype.parameters 
    553557        elif isinstance(cellclass, str): 
    554             self.cell = nest.Create(cellclass, self.size) 
    555  
    556         self.cell = numpy.array([ ID(GID) for GID in self.cell ], ID) 
    557         self.cell_local = self.cell[numpy.array(nest.GetStatus(self.cell.tolist(),'local'))] 
    558         self.first_id = self.cell.reshape(self.size,)[0] 
    559  
    560         for id in self.cell: 
    561             id.parent = self 
     558            if create_cells: 
     559                self.cell = nest.Create(cellclass, self.size) 
     560            else: 
     561                #A SubPopulation has been created, those fields will be filled later 
     562                self.cell = [] 
     563        ### Warning : not tested yet, quickly implemented in Okinawa. To allow the 
     564        ### construction of subpopulation without creating the cells 
     565        elif isinstance(cellclass,common.StandardCellType): 
     566            self.cell = [] 
     567 
     568        if create_cells: 
     569            self.cell = numpy.array([ ID(GID) for GID in self.cell ], ID) 
     570            self.cell_local = self.cell[numpy.array(nest.GetStatus(self.cell.tolist(),'local'))] 
     571            self.first_id = self.cell.reshape(self.size,)[0] 
     572 
     573            for id in self.cell: 
     574                id.parent = self 
    562575            #id.setCellClass(cellclass) 
    563576            #id.setPosition(self.locate(id)) 
    564  
    565         if self.cellparams: 
    566             nest.SetStatus(self.cell_local, [self.cellparams]) 
    567  
    568         self.cell = numpy.reshape(self.cell, self.dim) 
     577            self.cell = numpy.reshape(self.cell, self.dim) 
     578            if self.cellparams: 
     579                nest.SetStatus(self.cell_local, [self.cellparams]) 
    569580 
    570581        if not self.label: 
     
    899910        self.recorders['conductance'].write(filename, gather, compatible_output) 
    900911 
     912    def getSubPopulation(self, cell_list, label=None): 
     913         
     914        # We get the dimensions of the new population 
     915        dims = numpy.array(cell_list).shape 
     916        # We create an empty population 
     917        pop = Population(dims, cellclass=self.celltype, label=label, create_cells=False) 
     918        # And then copy parameters from its parent 
     919        pop.cellparams  = self.cellparams 
     920        pop.cell        = cell_list 
     921        pop.first_id    = self.first_id 
     922        idx             = pop.cell.flatten() -pop.first_id 
     923        pop.cell_local  = self.cell_local[idx] 
     924        pop.position    = self.positions[:,idx] 
     925        return pop 
     926 
     927 
    901928    def describe(self): 
    902929        """ 
     
    904931        """ 
    905932        print "\n------- Population description -------" 
    906         print "Population called %s is made of %d cells [%d being local]" %(self.label, len(self.cell), len(self.cell_local)) 
     933        print "Population called %s is made of %d cells [%d being local]" %(self.label, len(self.cell.flatten()), len(self.cell_local)) 
    907934        print "-> Cells are aranged on a %dD grid of size %s" %(len(self.dim), self.dim) 
    908935        print "-> Celltype is %s" %self.celltype