Changeset 436 for trunk

Show
Ignore:
Timestamp:
09/25/09 14:02:04 (3 years ago)
Author:
apdavison
Message:

Modified the mapping of cell id to image position for plotting activity maps/movies.

Location:
trunk/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/io.py

    r415 r436  
    147147        if not ('id_list' in params) or (params['id_list'] is None): 
    148148            if ('first_id' in self.metadata) and ('last_id' in self.metadata): 
    149                 logging.debug("id_list is infered from the file header") 
    150149                params['id_list'] = range(int(self.metadata['first_id']), int(self.metadata['last_id'])+1) 
     150                logging.debug("id_list (%d...%d) is infered from the file header" % (int(self.metadata['first_id']), int(self.metadata['last_id'])+1)) 
    151151            else: 
    152152                raise Exception("id_list can not be infered while reading %s" %self.filename) 
  • trunk/src/signals/spikes.py

    r416 r436  
    16631663 
    16641664     
    1665     def id2position(self, id): 
     1665    def id2position(self, id, offset=0): 
    16661666        """ 
    16671667        Return a position (x,y) from an id if the cells are aranged on a 
    1668         grid of size dims, as defined in the dims attribute of the SpikeList object 
     1668        grid of size dims, as defined in the dims attribute of the SpikeList object. 
     1669        This assumes that cells are ordered from left to right, top to bottom, 
     1670        and that dims specifies (height, width), i.e. if dims = (10,12), this is 
     1671        an array with 10 rows and 12 columns, and hence has width 12 units and 
     1672        height 10 units. 
    16691673         
    16701674        Inputs: 
     
    16721676         
    16731677        The 'dimensions' attribute of the SpikeList must be defined 
    1674          
    1675         Examples: 
    1676             >> spklist.id2position(2536) 
    1677                 (25, 35) 
    16781678         
    16791679        See also 
     
    16831683            raise Exception("Dimensions of the population are not defined ! Set spikelist.dimensions") 
    16841684        if len(self.dimensions) == 1: 
    1685             return id 
     1685            return id-offset 
    16861686        if len(self.dimensions) == 2: 
    1687             x = id % self.dimensions[0] 
    1688             y = numpy.floor(id/self.dimensions[0]) 
     1687            x = (id-offset) % self.dimensions[1] 
     1688            y = self.dimensions[0] - 1 - int(numpy.floor((id-offset)/self.dimensions[1])) 
    16891689            return (x,y) 
    16901690 
    16911691 
    1692     def position2id(self, position): 
     1692    def position2id(self, position, offset=0): 
    16931693        """ 
    16941694        Return the id of the cell at position (x,y) if the cells are aranged on a 
     
    17011701        as the position argument 
    17021702         
    1703         Examples: 
    1704             >> spklist.position2id((25,35)) 
    1705                 2536 
    1706          
    17071703        See also 
    17081704            activity_map, activity_movie, id2position 
     
    17101706        if self.dimensions is None: 
    17111707            raise Exception("Dimensions of the population are not defined ! Set spikelist.dimensions") 
    1712         assert array(position).shape == self.dimensions.shape, "position does not have the correct shape !" 
     1708        assert len(position) == len(tuple(self.dimensions)), "position does not have the correct shape !" 
    17131709        if len(self.dimensions) == 1: 
    1714             return position 
     1710            return position+offset 
    17151711        if len(self.dimensions) == 2: 
    1716             return position[0]*self.dimensions[0] + position[1] 
     1712            return (self.dimensions[0] - 1 - position[1])*self.dimensions[1] + position[0] + offset 
    17171713 
    17181714 
     
    17281724            float_positions - None by default, meaning that the dimensions attribute  
    17291725                              of the SpikeList is used to arange the ids on a 2D grid.  
    1730                               Otherwise, if the cells have flotting positions,  
     1726                              Otherwise, if the cells have floating positions,  
    17311727                              float_positions should be an array of size 
    17321728                              (2, nb_cells) with the x (first line) and y (second line)  
     
    17631759            activity_map = numpy.zeros(self.dimensions, float) 
    17641760            rates        = spklist.mean_rates() 
     1761            id_offset = min(self.id_list()) 
    17651762            for count, id in enumerate(spklist.id_list()): 
    1766                 position = spklist.id2position(id) 
    1767                 activity_map[position] = rates[count] 
     1763                x,y = spklist.id2position(id, id_offset) 
     1764                j,i = x, self.dimensions[0] - 1 -y 
     1765                activity_map[i,j] = rates[count] 
    17681766            if not subplot or not HAVE_PYLAB or not HAVE_MATPLOTLIB: 
    17691767                return activity_map 
     
    22292227                while (t_start < t_stop): 
    22302228                    activity_map = numpy.zeros(spk.dimensions) 
    2231                     while ((time[idx] < t_start + time_bin) and (idx < max_idx)): 
    2232                         addr = spk.id2position(pos[idx]) 
    2233                         activity_map[addr] += 1 
     2229                    while ((time[idx] < t_start + time_bin) and (idx < max_idx)):                         
     2230                        x,y = spk.id2position(pos[idx]) 
     2231                        j,i = x, self.dimensions[0] - 1 -y 
     2232                        activity_map[i,j] += 1 
    22342233                        idx += 1 
    22352234                    im.set_array(activity_map) 
  • trunk/src/visualization/__init__.py

    r416 r436  
    103103        return self.t, numpy.sin(self.t + self.phase) 
    104104 
    105 def xy2ij(coordinates): 
     105def xy2ij(coordinates, height): 
    106106    """ 
    107107    Generally, we use (x,y) coordinates, but since arrays use matrix coordinates, 
     
    109109    """ 
    110110    assert len(coordinates) == 2 
    111     return (coordinates[1], coordinates[0]) 
     111    x,y = coordinates 
     112    j = x 
     113    i = height - 1 - y 
     114    return (i,j) 
    112115 
    113116class ActivityMap(object): 
     
    122125            raise Exception("Dimensions of the population are not defined ! Set spikelist.dims") 
    123126         
    124         self.time, self.pos = self.spikelist.convert("times, ids") 
     127        self.time, self.ids = self.spikelist.convert("times, ids") 
    125128        # We sort the spikes to allow faster process later 
    126129        sort_idx = self.time.ravel().argsort(kind="quicksort") 
    127130        self.time = self.time[sort_idx] 
    128         self.pos = self.pos[sort_idx] 
    129         self.idx = 0 
    130         self.max_idx = len(self.time)-1 
     131        self.ids = self.ids[sort_idx] 
     132        self.i = 0 
     133        self.max_i = len(self.time)-1 
    131134        self.t_start = 0 
    132135         
    133136    def next_frame(self): 
    134137        spk = self.spikelist 
    135         activity_map = numpy.zeros(xy2ij(spk.dimensions)) 
    136          
    137         while ((self.idx < self.max_idx) and (self.time[self.idx] < self.t_start + self.frame_duration)): 
    138             addr = spk.id2position(self.pos[self.idx]) 
    139             activity_map[xy2ij(addr)] += 1 
    140             self.idx += 1 
     138        activity_map = numpy.zeros(spk.dimensions) 
     139        h,w = spk.dimensions 
     140        id_offset = min(spk.id_list()) 
     141        while (self.i < self.max_i) and (self.time[self.i] < self.t_start + self.frame_duration): 
     142            xy = spk.id2position(self.ids[self.i] - id_offset)  
     143            activity_map[xy2ij(xy, h)] += 1 
     144            self.i += 1 
    141145        self.t_start += self.frame_duration 
    142         #logging.debug("next_frame: idx=%d, t_start=%g, max_idx=%d, time[idx]=%g" % (self.idx, self.t_start, self.max_idx, self.time[self.idx])) 
     146        #logging.debug("next_frame: i=%d, t_start=%g, max_i=%d, time[i]=%g" % (self.i, self.t_start, self.max_i, self.time[self.i])) 
    143147        activity_map *= 1000.0/self.frame_duration # convert to spikes/second     
    144148        return [activity_map]