예제 #1
0
파일: simulator.py 프로젝트: astoeckel/PyNN
def create_cells(cellclass, cellparams=None, n=1, parent=None):
    """
    Create cells in NEST.
    
    `cellclass`  -- a PyNN standard cell or the name of a native NEST cell model.
    `cellparams` -- a dictionary of cell parameters.
    `n`          -- the number of cells to create
    `parent`     -- the parent Population, or None if the cells don't belong to
                    a Population.
    
    This function is used by both `create()` and `Population.__init__()`
    
    Return:
        - a 1D array of all cell IDs
        - a 1D boolean array indicating which IDs are present on the local MPI
          node
        - the ID of the first cell created
        - the ID of the last cell created
    """
    assert n > 0, 'n must be a positive integer'
    if isinstance(cellclass, basestring):  # celltype is not a standard cell
        nest_model = cellclass
        cell_parameters = cellparams or {}
    elif isinstance(cellclass, type) and issubclass(cellclass,
                                                    common.StandardCellType):
        celltype = cellclass(cellparams)
        nest_model = celltype.nest_name
        cell_parameters = celltype.parameters
    else:
        raise Exception("Invalid cell type: %s" % type(cellclass))
    try:
        cell_gids = nest.Create(nest_model, n)
    except nest.NESTError, errmsg:
        raise common.InvalidModelError(errmsg)
예제 #2
0
def create_cells(cellclass, cellparams, n, parent=None):
    """
    Create cells in PCSIM.
    
    `cellclass`  -- a PyNN standard cell or a native PCSIM cell class.
    `cellparams` -- a dictionary of cell parameters.
    `n`          -- the number of cells to create
    `parent`     -- the parent Population, or None if the cells don't belong to
                    a Population.
    
    This function is used by both `create()` and `Population.__init__()`
    
    Return:
        - a 1D array of all cell IDs
        - a 1D boolean array indicating which IDs are present on the local MPI
          node
        - the ID of the first cell created
        - the ID of the last cell created
    """
    global net
    assert n > 0, 'n must be a positive integer'
    if isinstance(cellclass, type):
        if issubclass(cellclass, common.StandardCellType):
            cellfactory = cellclass(cellparams).simObjFactory
        elif issubclass(cellclass, pypcsim.SimObject):
            #cellfactory = apply(cellclass, (), cellparams)
            cellfactory = cellclass(**cellparams)
        else:
            raise common.InvalidModelError(
                "Trying to create non-existent cellclass %s" %
                cellclass.__name__)
    else:
        raise common.InvalidModelError(
            "Trying to create non-existent cellclass %s" % cellclass)

    all_ids = numpy.array([i for i in net.add(cellfactory, n)], ID)
    first_id = all_ids[0]
    last_id = all_ids[-1]
    # mask_local is used to extract those elements from arrays that apply to the cells on the current node
    mask_local = numpy.array([is_local(id) for id in all_ids])
    for i, (id, local) in enumerate(zip(all_ids, mask_local)):
        #if local:
        all_ids[i] = ID(id)
        all_ids[i].parent = parent
        all_ids[i].local = local
    return all_ids, mask_local, first_id, last_id
예제 #3
0
파일: simulator.py 프로젝트: astoeckel/PyNN
def create_cells(cellclass, cellparams, n, parent=None):
    """
    Create cells in NEURON.
    
    `cellclass`  -- a PyNN standard cell or a native NEURON cell class that
                   implements an as-yet-undescribed interface.
    `cellparams` -- a dictionary of cell parameters.
    `n`          -- the number of cells to create
    `parent`     -- the parent Population, or None if the cells don't belong to
                    a Population.
    
    This function is used by both `create()` and `Population.__init__()`
    
    Return:
        - a 1D array of all cell IDs
        - a 1D boolean array indicating which IDs are present on the local MPI
          node
        - the ID of the first cell created
        - the ID of the last cell created
    """
    assert n > 0, 'n must be a positive integer'
    if isinstance(cellclass, basestring):  # cell defined in hoc template
        try:
            cell_model = getattr(h, cellclass)
        except AttributeError:
            raise common.InvalidModelError(
                "There is no hoc template called %s" % cellclass)
        cell_parameters = cellparams or {}
    elif isinstance(cellclass, type) and issubclass(cellclass,
                                                    common.StandardCellType):
        celltype = cellclass(cellparams)
        cell_model = celltype.model
        cell_parameters = celltype.parameters
    else:
        cell_model = cellclass
        cell_parameters = cellparams
    first_id = state.gid_counter
    last_id = state.gid_counter + n - 1
    all_ids = numpy.array([id for id in range(first_id, last_id + 1)], ID)
    # mask_local is used to extract those elements from arrays that apply to the cells on the current node
    mask_local = all_ids % state.num_processes == state.mpi_rank  # round-robin distribution of cells between nodes
    for i, (id, is_local) in enumerate(zip(all_ids, mask_local)):
        all_ids[i] = ID(id)
        all_ids[i].parent = parent
        if is_local:
            all_ids[i].local = True
            all_ids[i]._build_cell(cell_model, cell_parameters)
        else:
            all_ids[i].local = False
    initializer.register(*all_ids[mask_local])
    state.gid_counter += n
    return all_ids, mask_local, first_id, last_id
예제 #4
0
파일: simulator.py 프로젝트: astoeckel/PyNN
def create_cells(cellclass, cellparams=None, n=1, parent=None):
    """
    Create cells in Brian.
    
    `cellclass`  -- a PyNN standard cell or a native Brian cell class.
    `cellparams` -- a dictionary of cell parameters.
    `n`          -- the number of cells to create
    `parent`     -- the parent Population, or None if the cells don't belong to
                    a Population.
    
    This function is used by both `create()` and `Population.__init__()`
    
    Return:
        - a 1D array of all cell IDs
        - a 1D boolean array indicating which IDs are present on the local MPI
          node
        - the ID of the first cell created
        - the ID of the last cell created
    """
    # currently, we create a single NeuronGroup for create(), but
    # arguably we should use n NeuronGroups each containing a single cell
    # either that or use the subgroup() method in connect(), etc
    assert n > 0, 'n must be a positive integer'
    if isinstance(cellclass, basestring):  # celltype is not a standard cell
        try:
            eqs = brian.Equations(cellclass)
        except Exception, errmsg:
            raise common.InvalidModelError(errmsg)
        v_thresh = cellparams['v_thresh']
        v_reset = cellparams['v_reset']
        tau_refrac = cellparams['tau_refrac']
        brian_cells = brian.NeuronGroup(n,
                                        model=eqs,
                                        threshold=v_thresh,
                                        reset=v_reset,
                                        clock=state.simclock,
                                        compile=True,
                                        max_delay=state.max_delay)
        cell_parameters = cellparams or {}
예제 #5
0
    def __init__(self, dims, cellclass, cellparams=None, label=None):
        """
        dims should be a tuple containing the population dimensions, or a single
          integer, for a one-dimensional population.
          e.g., (10,10) will create a two-dimensional population of size 10x10.
        cellclass should either be a standardized cell class (a class inheriting
        from common.StandardCellType) or a string giving the name of the
        simulator-specific model that makes up the population.
        cellparams should be a dict which is passed to the neuron model
          constructor
        label is an optional name for the population.
        """
        ##if isinstance(dims, int): # also allow a single integer, for a 1D population
        ##    #print "Converting integer dims to tuple"
        ##    dims = (dims,)
        ##elif len(dims) > 3:
        ##    raise exceptions.AttributeError('PCSIM does not support populations with more than 3 dimensions')
        ##
        ##self.actual_ndim = len(dims)
        ##while len(dims) < 3:
        ##    dims += (1,)
        ### There is a problem here, since self.dim should hold the nominal dimensions of the
        ### population, while in PCSIM the population is always really 3D, even if some of the
        ### dimensions have size 1. We should add a variable self._dims to hold the PCSIM dimensions,
        ### and make self.dims be the nominal dimensions.
        common.Population.__init__(self, dims, cellclass, cellparams, label)

        ### set the steps list, used by the __getitem__() method.
        ##self.steps = [1]*self.ndim
        ##for i in range(self.ndim-1):
        ##    for j in range(i+1, self.ndim):
        ##        self.steps[i] *= self.dim[j]

        if isinstance(cellclass, str):
            if not cellclass in dir(pypcsim):
                raise common.InvalidModelError(
                    'Trying to create non-existent cellclass ' + cellclass)
            cellclass = getattr(pypcsim, cellclass)
            self.celltype = cellclass
        if issubclass(cellclass, common.StandardCellType):
            self.celltype = cellclass(cellparams)
            self.cellfactory = self.celltype.simObjFactory
        else:
            self.celltype = cellclass
            if issubclass(cellclass, pypcsim.SimObject):
                self.cellfactory = apply(cellclass, (), cellparams)
            else:
                raise exceptions.AttributeError(
                    'Trying to create non-existent cellclass ' +
                    cellclass.__name__)

        # CuboidGridPopulation(SimNetwork &net, GridPoint3D origin, Volume3DSize dims, SimObjectFactory &objFactory)
        ##self.pcsim_population = pypcsim.CuboidGridObjectPopulation(
        ##                            simulator.net,
        ##                            pypcsim.GridPoint3D(0,0,0),
        ##                            pypcsim.Volume3DSize(dims[0], dims[1], dims[2]),
        ##                            self.cellfactory)
        ##self.cell = numpy.array(self.pcsim_population.idVector())
        ##self.first_id = 0
        ##self.cell -= self.cell[0]
        ##self.all_cells = self.cell
        ##self.local_cells = numpy.array(self.pcsim_population.localIndexes())
        ##
        self.all_cells, self._mask_local, self.first_id, self.last_id = simulator.create_cells(
            cellclass, cellparams, self.size, parent=self)
        self.local_cells = self.all_cells[self._mask_local]
        self.all_cells = self.all_cells.reshape(self.dim)
        self._mask_local = self._mask_local.reshape(self.dim)
        self.cell = self.all_cells  # temporary, awaiting harmonisation

        self.recorders = {
            'spikes': Recorder('spikes', population=self),
            'v': Recorder('v', population=self),
            'gsyn': Recorder('gsyn', population=self)
        }