Changeset 365

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

Began updating the neuron module to use the new features of NEURON (HocObject, etc)

Files:

Legend:

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

    r364 r365  
    4848    hit is it to replace integers with ID objects? 
    4949    """ 
    50      
    51     #def __init__(self, n): 
    52     #    common.ID.__init__(self, n) 
    53     #    self.hocname = None 
     50 
    5451    def __init__(self, n): 
    5552        int.__init__(n) 
     
    6663     
    6764    def _hoc_cell(self): 
     65        """Returns the hoc object corresponding to the cell with this id.""" 
    6866        assert self in gidlist, "Cell %d does not exist on this node" % self 
    6967        if self.parent: 
    7068            hoc_cell_list = getattr(h, self.parent.label) 
    7169            try: 
    72                 #cell = hoc_cell_list.object(self - self.parent.gid_start) 
    7370                list_index = self.parent.gidlist.index(self) 
    7471                cell = hoc_cell_list.object(list_index) 
     
    313310        new_ids = list( ids.difference(self.recorded) ) 
    314311        self.recorded = self.recorded.union(ids) 
    315         if self.population is None: 
    316             cell_template = "cell%d" 
    317             id_list = new_ids 
    318         else: 
    319             cell_template = "%s.object(%s)" % (self.population.hoc_label, "%d") 
    320             id_list = self.population.gidlist 
     312         
    321313        if self.variable == 'spikes': 
    322             template = 'tmp = %s.record(1)' % cell_template 
    323         elif self.variable == 'v':  
    324             template = 'tmp = %s.record_v(1,%g)' % (cell_template, get_time_step()) 
    325         hoc_commands = [] 
    326         for src in new_ids: 
    327             hoc_commands += [template % id_list.index(src)] 
    328         hoc_execute(hoc_commands, "---Recorder.record() ---") 
     314            for cell in new_ids: 
     315                cell._hoc_cell().record(1) 
     316        elif self.variable == 'v': 
     317            dt = get_time_step() 
     318            for cell in new_ids: 
     319                cell._hoc_cell().record_v(1, dt)     
    329320         
    330321    def get(self, gather=False): 
    331322        """Returns the recorded data.""" 
     323        # not implemented yet 
    332324        data = recording.readArray(filename, sepchar=None) 
    333325        data = recording.convert_compatible_output(data, self.population, variable) 
     
    440432     
    441433    if initialised: 
    442         hoc_commands = ['dt = %f' % timestep, 
    443                         'min_delay = %g' % min_delay] 
     434        h.dt = timestep 
     435        h.min_delay = min_delay 
    444436        running = False 
    445437    else: 
    446         hoc_commands = [ 
    447             'tmp = xopen("%s")' % os.path.join(pyNN_path[0],'hoc','standardCells.hoc'), 
    448             'tmp = xopen("%s")' % os.path.join(pyNN_path[0],'hoc','odict.hoc'), 
    449             'objref pc', 
    450             'pc = new ParallelContext()', 
    451             'dt = %f' % timestep, 
    452             'tstop = 0', 
    453             'min_delay = %g' % min_delay, 
    454             'create dummy_section', 
    455             'access dummy_section', 
    456             'objref netconlist, nil', 
    457             'netconlist = new List()',  
    458             'strdef cmd', 
    459             'strdef fmt',  
    460             'objref nc',  
    461             'objref rng', 
    462             'objref cell']     
     438        tmp = h.xopen(os.path.join(pyNN_path[0],'hoc','standardCells.hoc')) 
     439        tmp = h.xopen(os.path.join(pyNN_path[0],'hoc','odict.hoc')) 
     440        h('objref pc') 
     441        h('pc = new ParallelContext()') 
     442        h.dt = timestep 
     443        h('tstop = 0') 
     444        h('min_delay = %f' % min_delay) 
     445        #'create dummy_section', 
     446        #    'access dummy_section', 
     447        h('objref netconlist, nil') 
     448        h('netconlist = new List()')  
     449        h('strdef cmd') 
     450        h('strdef fmt')  
     451        h('objref nc')  
     452        h('objref rng') 
     453        h('objref cell')     
    463454        #---Experimental--- Optimize the simulation time ? / Reduce inter-processors exchanges ? 
    464         hoc_commands += [ 
    465             'tmp   = pc.spike_compress(1,0)'] 
     455        tmp = h.pc.spike_compress(1,0) 
    466456        if extra_params.has_key('use_cvode') and extra_params['use_cvode'] == True: 
    467             hoc_commands += [ 
    468                 'objref cvode', 
    469                 'cvode = new CVode()', 
    470                 'cvode.active(1)']    
    471          
    472     hoc_execute(hoc_commands,"--- setup() ---") 
    473      
    474     #nhost = HocToPy.get('pc.nhost()','int') 
     457            h('objref cvode') 
     458            h('cvode = new CVode()') 
     459            h.cvode.active(1) 
     460         
    475461    nhost = int(h.pc.nhost()) 
    476462    if nhost < 2: 
    477463        nhost = 1; myid = 0 
    478464    else: 
    479         #myid = HocToPy.get('pc.id()','int') 
    480465        myid = int(h.pc.id()) 
    481     #print "\nHost #%d of %d" % (myid+1, nhost) 
    482     vfilelist = {} 
    483     spikefilelist = {} 
    484466     
    485467    initialised = True 
     
    492474    for recorder in recorder_list: 
    493475        recorder.write(gather=False, compatible_output=compatible_output) 
    494     hoc_commands = [] 
    495     hoc_commands += ['tmp = pc.runworker()', 
    496                      'tmp = pc.done()'] 
    497     hoc_execute(hoc_commands,"--- end() ---") 
     476    h.pc.runworker() 
     477    h.pc.done() 
    498478    if quit_on_end: 
    499         hoc.execute('tmp = quit()') # sometimes needed, sometimes not wanted. Maybe a 'quit_on_end' kwarg for setup? 
     479        h.quit() 
    500480        logging.info("Finishing up with NEURON.") 
    501481        sys.exit(0) 
     
    504484    """Run the simulation for simtime ms.""" 
    505485    global running 
    506     hoc_commands = [] 
     486 
    507487    if not running: 
    508488        running = True 
    509         hoc_commands += ['local_minimum_delay = pc.set_maxstep(10)', 
    510                          'tmp = finitialize()', 
    511                          'tstop = 0'] 
    512         hoc_execute(hoc_commands,"--- run() ---") 
     489        h('local_minimum_delay = pc.set_maxstep(10)') 
     490        h.finitialize() 
     491        h.tstop = 0 
    513492        logging.debug("local_minimum_delay on host #%d = %g" % (myid, h.local_minimum_delay)) 
    514493        if nhost > 1: 
    515494            assert h.local_minimum_delay >= get_min_delay(),\ 
    516495                   "There are connections with delays (%g) shorter than the minimum delay (%g)" % (h.local_minimum_delay, get_min_delay()) 
    517     hoc_commands += ['tstop += %f' % simtime, 
    518                      'tmp = pc.psolve(tstop)'] 
    519     hoc_execute(hoc_commands,"--- run() ---") 
     496    h.tstop = simtime 
     497    h.pc.psolve(h.tstop) 
    520498    return get_current_time() 
    521499 
     
    572550                         #'nc = new NetCon(cell%d.source, nil)' % cell_id, 
    573551                         'tmp = pc.cell(%d, nc)' % cell_id] 
     552        #h('tmp = pc.set_gid2node(%d,%d)' % (cell_id, myid)) 
     553        #h('objref cell%d' % cell_id) 
     554        #h('cell%d = new %s(%s)' % (cell_id, hoc_name, argstr)) 
     555        #h('tmp = cell%d.connect2target(nil, nc)' % cell_id) 
     556        #h('tmp = pc.cell(%d, nc)' % cell_id) 
    574557    hoc_execute(hoc_commands, "--- create() ---") 
    575558 
     
    971954            # Each node will record N/nhost cells... 
    972955            nrec = int(record_from/nhost) 
    973             if rng: 
    974                 record_from = rng.permutation(self.gidlist) 
    975             else: 
    976                 record_from = numpy.random.permutation(self.gidlist) 
    977             # Taken as random in self.gidlist 
    978             record_from = record_from[0:nrec] 
    979             record_from = numpy.array(record_from) # is this line necessary? 
     956            if not rng: 
     957                rng = numpy.random 
     958            record_from = rng.permutation(self.gidlist)[0:nrec] # need ID objects, permutation returns integers 
     959            # need ID objects, permutation returns integers 
     960            record_from = [self.gidlist.index(i) for i in record_from] 
    980961        else: 
    981962            raise Exception("record_from must be either a list of cells or the number of cells to record from")