| | 152 | |
| | 153 | class 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 | |