Changeset 398 for trunk

Show
Ignore:
Timestamp:
06/15/09 20:56:42 (3 years ago)
Author:
pierre
Message:

Add a DistanceDependentPairs? selector, to allow precise selection of cells when SpikeList are arranged on a grid

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/signals/pairs.py

    r389 r398  
    11import numpy 
     2 
     3 
     4# Function to calculate the euclidian distance between two positions 
     5# For the moment, we suppose the cells to be located in the same grid 
     6# of size NxN. Should then include a scaling parameter to allow 
     7# distances between distincts populations ? 
     8def distance(pos_1, pos_2, N): 
     9    # Since we deal with a toroidal space, we have to take the min distance 
     10    # on the torus. 
     11    dx = numpy.minimum(abs(pos_1[0]-pos_2[0]), N-(abs(pos_1[0]-pos_2[0]))) 
     12    dy = numpy.minimum(abs(pos_1[1]-pos_2[1]), N-(abs(pos_1[1]-pos_2[1]))) 
     13    return numpy.sqrt(dx*dx + dy*dy) 
     14 
     15 
     16 
    217 
    318class PairsGenerator(object): 
     
    1833        >> p.get_pairs(100) 
    1934     
    20     See also AutoPairs, RandomPairs, CustomPairs 
     35    See also AutoPairs, RandomPairs, CustomPairs, DistantDependentPairs 
    2136    """ 
    2237    def __init__(self, spk1, spk2, no_silent=False): 
     
    6681            [[1,1],[2,2],[4,4],[5,5]] 
    6782     
    68     See also RandomPairs, CustomPairs 
     83    See also RandomPairs, CustomPairs, DistantDependentPairs 
    6984    """ 
    7085     
     
    109124         
    110125         
    111     See also RandomPairs, CustomPairs 
     126    See also RandomPairs, CustomPairs, DistantDependentPairs 
    112127    """ 
    113128    def __init__(self, spk1, spk2, no_silent=False, no_auto=True): 
     
    116131     
    117132    def get_pairs(self, nb_pairs): 
    118         cells1 = numpy.array(list(self.ids_1)) 
    119         cells2 = numpy.array(list(self.ids_2)) 
     133        cells1 = numpy.array(list(self.ids_1), int) 
     134        cells2 = numpy.array(list(self.ids_2), int) 
    120135        pairs  = numpy.zeros((0,2), int) 
    121136        N1     = len(cells1) 
     
    135150 
    136151 
     152 
     153class DistantDependentPairs(PairsGenerator): 
     154    """ 
     155    DistantDependentPairs(SpikeList, SpikeList, no_silent, no_auto). Inherits from PairsGenerator. 
     156    Generator that will return pairs of elements according to the distances between the cells. The 
     157    dimensions attribute of the SpikeList should be not empty. 
     158         
     159    Inputs: 
     160        spk1      - First SpikeList object to take cells from 
     161        spk2      - Second SpikeList object to take cells from 
     162        no_silent - Boolean to say if only non silent cells should 
     163                    be considered. False by default  
     164        no_auto   - Boolean to say if pairs with the same element (id,id) should 
     165                    be remove. True by default, i.e those pairs are discarded 
     166        length    - the lenght (in mm) covered by the extend of spk1 and spk2. Currently, spk1 
     167                    and spk2 should cover the same surface. Default is spk1.length 
     168         
     169    Examples: 
     170        >> p = DistantDependentPairs(spk1, spk1, True, False) 
     171        >> p.get_pairs(4, d_min=0, d_max = 50) 
     172            [[1,3],[2,5],[1,4],[5,5]] 
     173        >> p = DistantDependentPairs(spk1, spk1, True, True, lenght=1) 
     174        >> p.get_pairs(3, d_min=0.25, d_max=0.35) 
     175            [[1,3],[2,5],[1,4]] 
     176         
     177         
     178    See also RandomPairs, CustomPairs, AutoPairs 
     179    """ 
     180    def __init__(self, spk1, spk2, no_silent=False, no_auto=True, lenght=1.): 
     181        PairsGenerator.__init__(self, spk1, spk2, no_silent) 
     182        self.lenght  = lenght 
     183        self.no_auto = no_auto 
     184     
     185    def get_pairs(self, nb_pairs, d_min, d_max): 
     186        cells1 = numpy.array(list(self.ids_1), int) 
     187        cells2 = numpy.array(list(self.ids_2), int) 
     188        pairs  = numpy.zeros((0,2), int) 
     189        N1     = len(cells1) 
     190        N2     = len(cells2) 
     191        T      = min(N1,N2) 
     192        while len(pairs) < nb_pairs: 
     193            N         = min(nb_pairs-len(pairs), T) 
     194            cell1     = cells1[numpy.floor(numpy.random.uniform(0, N1, N)).astype(int)] 
     195            cell2     = cells2[numpy.floor(numpy.random.uniform(0, N1, N)).astype(int)] 
     196            pos_cell1 = numpy.array(self.spk1.id2position(cell1))*self.lenght/self.spk1.dimensions[0] 
     197            pos_cell2 = numpy.array(self.spk2.id2position(cell2))*self.lenght/self.spk2.dimensions[0] 
     198            dist      = distance(pos_cell1, pos_cell2, self.lenght) 
     199            idx       = numpy.where((dist >= d_min) & (dist < d_max))[0] 
     200            N         = len(idx) 
     201            if N > 0: 
     202                tmp_pairs = numpy.zeros((N, 2),int) 
     203                tmp_pairs[:,0] = cell1[idx] 
     204                tmp_pairs[:,1] = cell2[idx] 
     205                if self.no_auto: 
     206                    idx = numpy.where(tmp_pairs[:,0] == tmp_pairs[:,1])[0] 
     207                    pairs = numpy.concatenate((pairs, numpy.delete(tmp_pairs, idx, axis=0))) 
     208                else: 
     209                    pairs = numpy.concatenate((pairs, tmp_pairs)) 
     210        return pairs 
     211 
     212 
    137213class CustomPairs(PairsGenerator): 
    138214    """ 
     
    151227            [[1,1],[2,2],[3,3],[4,4]] 
    152228         
    153     See also RandomPairs, CustomPairs 
     229    See also RandomPairs, CustomPairs, DistantDependentPairs, AutoPairs 
    154230    """ 
    155231    def __init__(self, spk1, spk2, pairs=[[],[]]):