Changeset 375

Show
Ignore:
Timestamp:
06/18/08 17:30:31 (5 months ago)
Author:
apdavison
Message:

Native cells defined as Python classes now work with the neuron2 module.

Started converting neuron unit tests to work with neuron2

Files:

Legend:

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

    r369 r375  
    2222    """ 
    2323     
    24     def __init__(self, parameter_name, standard_model): 
     24    def __init__(self, parameter_name, model): 
    2525        self.parameter_name = parameter_name 
    26         self.model_name = standard_model.__name__ 
    27         self.valid_parameter_names = standard_model.default_parameters.keys() 
    28         self.valid_parameter_names.sort() 
     26        self.model_name = model.__name__ 
     27        if issubclass(model, StandardModelType): 
     28            self.valid_parameter_names = model.default_parameters.keys() 
     29            self.valid_parameter_names.sort() 
     30        else: 
     31            self.valid_parameter_names = ['unknown'] 
    2932 
    3033    def __str__(self): 
     
    722725        for i in range(1, self.ndim): 
    723726            self.size *= self.dim[i] 
    724         self.cell = None # to be defined by child, simulator-specific classes 
     727        ##self.cell = None # to be defined by child, simulator-specific classes 
    725728     
    726729    def __getitem__(self, addr): 
  • trunk/src/neuron2/__init__.py

    r371 r375  
    116116        common.IDMixin.__init__(self) 
    117117     
    118     def _build_cell(self, celltype, parent=None): 
     118    def _build_cell(self, cell_model, cell_parameters, parent=None): 
    119119        gid = int(self) 
    120         self._cell = celltype.model(**celltype.parameters)  # create the cell object 
    121         parallel_context.set_gid2node(gid, rank())                        # assign the gid to this node 
     120        self._cell = cell_model(**cell_parameters)          # create the cell object 
     121        parallel_context.set_gid2node(gid, rank())          # assign the gid to this node 
    122122        nc = neuron.NetCon(self._cell.source, None)         # } associate the cell spike source 
    123         parallel_context.cell(gid, nc.hoc_obj)                            # } with the gid (using a temporary NetCon) 
     123        parallel_context.cell(gid, nc.hoc_obj)              # } with the gid (using a temporary NetCon) 
    124124        self.parent = parent 
    125125     
     
    139139# ============================================================================== 
    140140 
    141 def _create(celltype, n, parent=None): 
     141def _create(cellclass, param_dict, n, parent=None): 
    142142    """ 
    143143    Function used by both `create()` and `Population.__init__()` 
     
    145145    global gid_counter 
    146146    assert n > 0, 'n must be a positive integer' 
     147    if isinstance(cellclass, basestring): # cell defined in hoc template 
     148        try: 
     149            cell_model = getattr(h, cellclass) 
     150        except AttributeError: 
     151            raise common.InvalidModelError("There is no hoc template called %s" % cellclass) 
     152        cell_parameters = param_dict or {} 
     153    elif issubclass(cellclass, common.StandardCellType): 
     154        celltype = cellclass(param_dict) 
     155        cell_model = celltype.model 
     156        cell_parameters = celltype.parameters 
     157    else: 
     158        cell_model = cellclass 
     159        cell_parameters = param_dict 
    147160    first_id = gid_counter 
    148161    last_id = gid_counter + n 
     
    153166        if is_local: 
    154167            all_ids[i] = ID(id) 
    155             all_ids[i]._build_cell(celltype, parent=parent) 
     168            all_ids[i]._build_cell(cell_model, cell_parameters, parent=parent) 
    156169    gid_counter += n 
    157170    return all_ids, mask_local, first_id, last_id 
     
    163176    If n==1, return just the single id. 
    164177    """ 
    165     all_ids, mask_local, first_id, last_id = _create(cellclass(param_dict), n) 
     178    all_ids, mask_local, first_id, last_id = _create(cellclass, param_dict, n) 
    166179    for id in all_ids[mask_local]: 
    167180        id.cellclass = cellclass 
    168181    initializer.register(*all_ids[mask_local]) 
     182    all_ids = all_ids.tolist() # not sure this is desirable, but it is consistent with the other modules 
    169183    if len(all_ids) == 1: 
    170184        all_ids = all_ids[0] 
     
    176190    Used by `connect()` and the `Connector` classes. 
    177191    """ 
     192    if not isinstance(source, int) or source > gid_counter or source < 0: 
     193        raise common.ConnectionError("Invalid source ID: %s" % source) 
     194    if not isinstance(target, ID): 
     195        raise common.ConnectionError("Invalid target ID: %s" % target) 
    178196    if synapse_type is None: 
    179197        synapse_type = weight>=0 and 'excitatory' or 'inhibitory' 
     
    215233        for src in sources: 
    216234            nc = _single_connect(src, tgt, weight, delay, synapse_type) 
    217             connection_list.append((src, tgt, nc)
     235            connection_list.append(nc
    218236    return connection_list 
    219237 
     
    287305                          'v': Recorder('v', population=self)} 
    288306        self.label = self.label or 'population%d' % Population.nPop 
    289         self.celltype = cellclass(cellparams) 
     307        if isinstance(cellclass, type) and issubclass(cellclass, common.StandardCellType): 
     308            self.celltype = cellclass(cellparams) 
     309        else: 
     310            self.celltype = cellclass 
    290311         
    291312        # Build the arrays of cell ids 
     
    293314        # All are stored in a single numpy array for easy lookup by address 
    294315        # The local cells are also stored in a list, for easy iteration 
    295         self._all_ids, self._mask_local, self.first_id, self.last_id = _create(self.celltype, self.size, parent=self) 
     316        self._all_ids, self._mask_local, self.first_id, self.last_id = _create(cellclass, cellparams, self.size, parent=self) 
    296317        self._local_ids = self._all_ids[self._mask_local] 
    297318        self._all_ids = self._all_ids.reshape(self.dim) 
  • trunk/src/neuron2/cells.py

    r371 r375  
    1414ResetRefrac = neuron.new_point_process('ResetRefrac') 
    1515VecStim = neuron.new_hoc_class('VecStim') 
     16NetStim = neuron.new_hoc_class('VecStim') 
    1617 
    1718def _new_property(obj_hierarchy, attr_name): 
     
    4546                 v_thresh=-55, t_refrac=2, i_offset=0, v_reset=None, 
    4647                 v_init=None, tau_e=5, tau_i=5, e_e=0, e_i=-70): 
     48         
     49        if v_reset is None: 
     50            v_reset = v_rest 
     51        if v_init is None: 
     52            v_init = v_rest 
    4753 
    4854        # initialise Section object with 'pas' mechanism 
     
    5460         
    5561        # insert synapses 
    56         assert syn_type in ('current', 'conductance'), "syn_type must be either 'current' or 'conductance'" 
     62        assert syn_type in ('current', 'conductance'), "syn_type must be either 'current' or 'conductance'. Actual value is %s" % syn_type 
    5763        assert syn_shape in ('alpha', 'exp'), "syn_type must be either 'alpha' or 'exp'" 
    5864        synapse_model = StandardIF.synapse_models[syn_type][syn_shape] 
     
    7783        for name in self.parameter_names: 
    7884            setattr(self, name, locals()[name]) 
    79         if self.v_reset is None: 
    80             self.v_reset = self.v_rest 
    81         if self.v_init is None: 
    82             self.v_init = self.v_rest 
     85         
    8386        self.spiketimes = neuron.Vector(0) 
    8487 
     
    126129class SpikeSource(object): 
    127130     
    128     def __init__(self, source_type, spiketimes=[]): 
     131    def __init__(self, source_type, start=0, interval=1e12, number=0, spiketimes=[]): 
    129132        self.source = source_type() 
    130133        if spiketimes: 
     
    133136            self.do_not_record = True 
    134137        else: 
     138            for name in 'start', 'interval', 'number': 
     139                setattr(self.source, name, locals()[name]) 
    135140            self.spiketimes = neuron.Vector() 
    136141            self.do_not_record = False 
     
    286291    def __init__(self, parameters): 
    287292        common.SpikeSourcePoisson.__init__(self, parameters) 
    288         self.parameters['source_type'] = 'NetStim'     
    289         self.parameters['noise'] = 1 
    290          
     293        self.parameters['source_type'] = NetStim            
    291294 
    292295class SpikeSourceArray(SpikeSource, common.SpikeSourceArray):