Changeset 446

Show
Ignore:
Timestamp:
04/12/10 15:37:12 (22 months ago)
Author:
apdavison
Message:

SpikeTrain.time_slice() now (again) accepts a sequence of t_start values and a sequence of t_stop values, if you wish to extract multiple slices.

Location:
trunk/src
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/__init__.py

    r442 r446  
    119119    import ll.ansistyle 
    120120    def colour(col, text): 
    121         return unicode(ll.ansistyle.Text(col, unicode(text))) 
     121        try: 
     122            return unicode(ll.ansistyle.Text(col, unicode(text))) 
     123        except UnicodeDecodeError, e: 
     124            raise UnicodeDecodeError("%s. text was %s" % (e, text)) 
    122125except ImportError: 
    123126    def colour(col, text): 
  • trunk/src/facets/fkbtools.py

    r298 r446  
    1919if check_dependency('srblib'): 
    2020    import srblib 
     21else: 
     22    import urllib as srblib 
    2123 
    2224import os, sys, zipfile, tempfile, shutil, subprocess, logging, time, numpy 
  • trunk/src/io.py

    r445 r446  
    393393 
    394394 
     395class PyNNNumpyBinaryFile(FileHandler): 
     396     
     397    def __init__(self, filename): 
     398        FileHandler.__init__(self, filename) 
     399        self.fileobj = open(self.filename, 'r') 
     400         
     401    def read_spikes(self, params): 
     402        from NeuroTools.signals import spikes 
     403        contents = numpy.load(self.fileobj) 
     404        spike_data = contents['data'][:, (1,0)] 
     405        self.metadata = M = {} 
     406        for k,v in contents['metadata']: 
     407            M[k] = eval(v) 
     408        id_list = range(M['first_id'], M['last_id']) 
     409        t_stop = params['t_stop'] 
     410        # really need to check the agreement between file metadata and 
     411        # params for all metadata items 
     412        return spikes.SpikeList(spike_data, id_list, t_start=0.0, t_stop=t_stop, 
     413                                dims=M['dimensions']) 
     414         
     415    #def read_analogs(self, type, params): 
     416    #    contents = numpy.load(self.fileobj) 
     417    #    values, ids = contents['data'].T # need to check the shape first 
     418     
     419 
    395420 
    396421class DataHandler(object): 
  • trunk/src/random.py

    r353 r446  
    2020 
    2121have_scipy = check_dependency('scipy') 
    22 if have_scipy: 
    23     import scipy.stats 
    2422 
    2523     
     
    9593        self.dist_name = 'GammaDist' 
    9694 
    97     if have_scipy:     
     95    if have_scipy: 
     96        import scipy.stats 
    9897        def next(self,n=1): 
    9998            return scipy.stats.gamma.rvs(self.params['a'],size=n)*self.params['b'] 
  • trunk/src/signals/spikes.py

    r443 r446  
    186186     
    187187     
    188     def merge(self, spiketrain): 
     188    def merge(self, spiketrain, relative=False): 
    189189        """ 
    190190        Add the spike times from a spiketrain to the current SpikeTrain 
     
    192192        Inputs: 
    193193            spiketrain - The SpikeTrain that should be added 
     194            relative - if True, relative_times() is called on both spiketrains before merging 
    194195         
    195196        Examples: 
     
    204205                500 
    205206        """ 
     207        if relative: 
     208            self.relative_times() 
     209            spiketrain.relative_times() 
    206210        self.spike_times = numpy.insert(self.spike_times, self.spike_times.searchsorted(spiketrain.spike_times), \ 
    207211                                        spiketrain.spike_times) 
     
    464468    def time_slice(self, t_start, t_stop): 
    465469        """  
    466         Return a new SpikeTrain obtained by slicing between t_start and t_stop. The new  
    467         t_start and t_stop values of the returned SpikeTrain are the one given as arguments 
     470        Return a new SpikeTrain obtained by slicing between t_start and t_stop, 
     471        where t_start and t_stop may either be single values or sequences of 
     472        start and stop times. 
    468473         
    469474        Inputs: 
     
    477482            >> spk.t_stop 
    478483                100 
    479         """ 
    480         spikes = numpy.extract((self.spike_times >= t_start) & (self.spike_times <= t_stop), self.spike_times) 
     484            >>> spk = spktrain.time_slice([20,70], [40,90]) 
     485            >>> spk.t_start 
     486                20 
     487            >>> spk.t_stop 
     488                90 
     489            >>> len(spk.time_slice(41, 69)) 
     490                0 
     491        """ 
     492        if hasattr(t_start, '__len__'): 
     493            if len(t_start) != len(t_stop): 
     494                raise ValueError("t_start has %d values and t_stop %d. They must be of the same length." % (len(t_start), len(t_stop))) 
     495            mask = False 
     496            for t0,t1 in zip(t_start, t_stop): 
     497                mask = mask | ((self.spike_times >= t0) & (self.spike_times <= t1)) 
     498            t_start = t_start[0] 
     499            t_stop = t_stop[-1] 
     500        else: 
     501            mask = (self.spike_times >= t_start) & (self.spike_times <= t_stop) 
     502        spikes = numpy.extract(mask, self.spike_times) 
    481503        return SpikeTrain(spikes, t_start, t_stop) 
    482504 
     
    536558         
    537559        Note that the SpikeTrain object itself is modified, t_start  
    538         is substracted to spike_times, t_start and t_stop 
     560        is subtracted from spike_times, t_start and t_stop 
    539561        """ 
    540562        if self.t_start != 0: 
  • trunk/src/stgen.py

    r438 r446  
    243243    def inh_poisson_generator(self, rate, t, t_stop, array=False): 
    244244        """ 
    245         Returns a SpikeList whose spikes are a realization of an inhomogeneous  
     245        Returns a SpikeTrain whose spikes are a realization of an inhomogeneous  
    246246        poisson process (dynamic rate). The implementation uses the thinning  
    247247        method, as presented in the references. 
  • trunk/src/visualization/__init__.py

    r442 r446  
    143143        h,w = spk.dimensions 
    144144        id_offset = min(spk.id_list()) 
     145        xarr,yarr = spk.id2position(self.spikelist.id_list() - id_offset) 
    145146        while (self.i < self.max_i) and (self.time[self.i] < self.t_start + self.frame_duration): 
    146             xy = spk.id2position(self.ids[self.i] - id_offset)  
    147             activity_map[xy2ij(xy, h)] += 1 
     147            id = self.ids[self.i] - id_offset 
     148            x = xarr[id] 
     149            y = yarr[id] 
     150            #xy = spk.id2position(self.ids[self.i] - id_offset) 
     151            #assert xy == (x,y), "%s != %s" % (xy, str((x,y))) 
     152            activity_map[xy2ij((x,y), h)] += 1 
    148153            self.i += 1 
    149154        self.t_start += self.frame_duration