Changeset 444

Show
Ignore:
Timestamp:
08/20/08 11:42:21 (3 months ago)
Author:
apdavison
Message:

gather=True is working again for neuron.

When writing compatible output, if there are no spikes then an empty file (with just the header) is written. Before, no file was written.

Files:

Legend:

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

    r431 r444  
    2323import logging 
    2424import platform 
     25import tempfile 
    2526Set = set 
    2627 
     
    330331     
    331332    def write(self, file=None, gather=False, compatible_output=True): 
    332         hoc_execute(['objref gathered_vec_list', 
    333                      'gathered_vec_list =  new List()']) 
     333        hoc_execute(['objref gathered_vec_list, gathered_id_vec', 
     334                     'gathered_vec_list =  new List()', 
     335                     'gathered_id_vec = new Vector(0)']) 
    334336        vector_operation = '' 
    335337        if self.variable == 'spikes': 
     
    338340        if self.population is None: 
    339341            cell_template = "cell%d" 
    340             post_label = "node%d: post cellX.%s" % (myid, self.variable) 
     342            post_fmt = "node%%d: post cellX.%s" % self.variable 
    341343            id_list = gidlist 
    342344            padding = 0 
    343345        else: 
    344346            cell_template = "%s.object(%s)" % (self.population.hoc_label, "%d") 
    345             post_label = 'node%d: post_%s.%s' % (myid, self.population.hoc_label, self.variable) 
     347            post_fmt = 'node%%d: post_%s.%s' % (self.population.hoc_label, self.variable) 
    346348            id_list = self.population.gidlist 
    347349            padding = self.population.first_id 
     
    349351        def post_data(): 
    350352            pack_template = 'tmp = pc.pack(%s.%s%s)' % (cell_template, 
    351                                                         RECORDING_VECTOR_NAME[self.variable], 
     353                                                        RECORDING_VECTOR_NAMES[self.variable], 
    352354                                                        vector_operation) 
     355            hoc_commands = ['tmp = pc.pack(%d)' % len(self.recorded)] 
    353356            for cell in self.recorded: 
    354                 hoc_commands += ['tmp = pc.pack(%d)' % id_list.index(cell), 
     357                hoc_commands += ['tmp = pc.pack(%d)' % cell, #id_list.index(cell), 
    355358                                 pack_template % id_list.index(cell)] 
    356             hoc_commands += ['tmp = pc.post("%s")' % post_label
    357             hoc_execute(hoc_commands,"--- Population[%s].__print()__ --- [Post objects to master]" %self.label) 
     359            hoc_commands += ['tmp = pc.post("%s")' % (post_fmt % myid,)
     360            hoc_execute(hoc_commands,"--- Population[%s].__print()__ --- [Post objects to master]" % self.population.label) 
    358361        def take_data(): 
    359             hoc_commands = ['tmp = pc.take(post_label)'] 
     362            #hoc_commands = ['tmp = pc.take(post_label)'] 
    360363            for node in range(1, num_processes()): 
    361                 hoc_commands += ['gathered_vec_list.append(pc.upkscalar())', 
    362                                  'gathered_vec_list.append(pc.upkvec())'] 
     364                take_label = post_fmt % node 
     365                hoc_commands = ['tmp = pc.take("%s")' % take_label, 
     366                                'n = pc.upkscalar()', 
     367                                'for i = 1,n { gathered_id_vec.append(pc.upkscalar()) gathered_vec_list.append(pc.upkvec()) }'] 
     368                hoc_execute(hoc_commands,"--- Population[%s].__print()__ --- [Take objects from node %d]" % (self.population.label, node)) 
    363369        def write_data(): 
    364370            if self.population is None: 
     
    388394                hoc_commands += ['fmt = "%s\\t%d\\n"' % (num_format, cell-padding), 
    389395                                 write_template % id_list.index(cell)] 
    390             # writing gathered data is currently broken 
    391             #hoc_commands += ['while i < gathered_vec_list.count()-2 { gathered_vec_list.o(i+1).printf(fileobj, "%s broken") ' % num_format] 
    392             hoc_commands += ['tmp = fileobj.close()'] 
    393396            hoc_execute(hoc_commands, "Recorder.write()") 
     397            # write gathered data 
     398            for id, vec in zip(h.gathered_id_vec, h.gathered_vec_list): 
     399                fmt = "%s\t%d\n" % (num_format, int(id)-padding) 
     400                vec.printf(h.fileobj, fmt) 
     401            h.fileobj.close() 
     402             
    394403             
    395404        if gather: 
     
    401410        else: 
    402411            if num_processes() > 1: 
    403                 self.filename += ".%d" % myid     
     412                if file: 
     413                    file += ".%d" % myid 
     414                else: 
     415                    self.filename += ".%d" % myid 
    404416            write_data() 
    405417                 
     
    585597        if tgt > gid or tgt < 0 or not isinstance(tgt, int): 
    586598            raise common.ConnectionError, "Postsynaptic cell id %s does not exist." % str(tgt) 
    587         if "cond" in tgt.cellclass.__name__: 
     599        if hasattr(tgt.cellclass, '__name__') and "cond" in tgt.cellclass.__name__: 
    588600            weight = abs(weight) # weights must be positive for conductance-based synapses 
    589601        if tgt in gidlist: # only create connections to cells that exist on this machine 
     
    598610                else: 
    599611                    if p >= 1.0 or rarr[j] < p: # might be more efficient to vectorise the latter comparison 
    600                         hoc_commands += [#'nc = pc.gid_connect(%d, pc.gid2cell(%d).%s)' % (src, tgt, syn_objref), 
    601                                          'nc = pc.gid_connect(%d, cell%d.%s)' % (src, tgt, syn_objref), 
     612                        hoc_commands += ['nc = pc.gid_connect(%d, pc.gid2cell(%d).%s)' % (src, tgt, syn_objref), 
     613                                         #'nc = pc.gid_connect(%d, cell%d.%s)' % (src, tgt, syn_objref), 
    602614                                         'nc.delay = %g' % delay, 
    603615                                         'nc.weight = %g' % weight, 
     
    608620                if src > gid or src < 0 or not isinstance(src, int): 
    609621                    raise common.ConnectionError, "Presynaptic cell id %s does not exist." % str(src) 
    610     print hoc_commands 
    611622    hoc_execute(hoc_commands, "--- connect(%s,%s) ---" % (str(source), str(target))) 
    612623    return range(nc_start, ncid) 
     
    10411052        """ 
    10421053        # This is a bit of a hack implemetation 
    1043         tmpfile = "neuron_tmpfile" # should really use tempfile module 
     1054        tmpfile = os.path.join(tempfile.mkdtemp(prefix="pyNN_neuron"), "getSpikes_tmpfile") # should really use tempfile module 
    10441055        self.recorders['spikes'].write(tmpfile, gather, compatible_output=False) 
    1045         if not gather
    1046             tmpfile += '%d' % myid 
     1056        if not gather and num_processes()>1
     1057            tmpfile += '.%d' % myid 
    10471058        if myid==0 or not gather: 
    10481059            f = open(tmpfile, 'r') 
     
    10511062            spikes = numpy.array([line2spike(line.split()) for line in lines]) 
    10521063            f.close() 
    1053             #os.remove(tmpfile) 
     1064            #os.remove(tmpfile) # should also remove tmp parent directory 
    10541065            return spikes 
    10551066        else: 
  • trunk/src/recording.py

    r430 r444  
    7979        N = 0 
    8080     
     81    result = open(user_filename,'w',DEFAULT_BUFFER_SIZE)     
     82    # Write header info (e.g., dimensions of the population) 
     83    if population is not None: 
     84        result.write("# dimensions =" + "\t".join([str(d) for d in population.dim]) + "\n") 
     85        result.write("# first_id = %d\n" % population.first_id) 
     86        result.write("# last_id = %d\n" % (population.first_id+len(population)-1,)) 
     87        padding = population.first_id 
     88    else: 
     89        padding = 0 
     90    result.write("# dt = %g\n" % dt) 
     91         
    8192    if N > 0: 
    8293        data = readArray(sim_filename, sepchar=None) 
    83          
    84         result = open(user_filename,'w',DEFAULT_BUFFER_SIZE)     
    85         # Write header info (e.g., dimensions of the population) 
    86         if population is not None: 
    87             result.write("# dimensions =" + "\t".join([str(d) for d in population.dim]) + "\n") 
    88             result.write("# first_id = %d\n" % population.first_id) 
    89             result.write("# last_id = %d\n" % (population.first_id+len(population)-1,)) 
    90             padding = population.first_id 
    91         else: 
    92             padding = 0 
    93         result.write("# dt = %g\n" % dt) 
    94          
    9594        data[:,0] = data[:,0] - padding 
    96          
    9795        # sort 
    9896        #indx = data.argsort(axis=0, kind='mergesort')[:,0] # will quicksort (not stable) work? 
     
    119117        else: 
    120118            raise Exception("Data file should have 2,3 or 4 columns, actually has %d" % data.shape[1]) 
    121         result.close() 
    122119    else: 
    123120        logging.info("%s is empty" % sim_filename) 
     121        result.write("# n = 0\n") 
     122    result.close() 
    124123 
    125124def write_compatible_output1(data_source, user_filename, variable, input_format, population, dt):