Debian Codegraph @

pyspike/cython/cython_distances.pyx

raw
1 #cython: language_level=3
2 #cython: boundscheck=False
3 #cython: wraparound=False
4 #cython: cdivision=True
5
6 """
7 cython_distances.pyx
8
9 cython implementation of the isi-, spike- and spike-sync distances
10
11 Note: using cython memoryviews (e.g. double[:]) instead of ndarray objects
12 improves the performance of spike_distance by a factor of 10!
13
14 Copyright 2015, Mario Mulansky <[email protected]>
15
16 Distributed under the BSD License
17
18 """
19
20 """
21 To test whether things can be optimized: remove all yellow stuff
22 in the html output::
23
24 cython -a cython_distances.pyx
25
26 which gives::
27
28 cython_distances.html
29
30 """
31
32 from pyspike.cython.cython_get_tau cimport get_tau
33
34 import numpy as np
35 cimport numpy as np
36
37 from libc.math cimport fabs
38 from libc.math cimport fmax
39 from libc.math cimport fmin
40
41 #DTYPE = float
42 #ctypedef np.float_t DTYPE_t
43
44 ############################################################
45 # isi_distance_cython
46 ############################################################
47 def isi_distance_cython(double[:] s1, double[:] s2,
48 double t_start, double t_end,
49 double MRTS=0.):
50
51 cdef double isi_value
52 cdef int index1, index2, index
53 cdef int N1, N2
54 cdef double nu1, nu2
55 cdef double last_t, curr_t, curr_isi
56 isi_value = 0.0
57 N1 = len(s1)
58 N2 = len(s2)
59
60 # first interspike interval - check if a spike exists at the start time
61 # and also account for spike trains with single spikes
62 if s1[0] > t_start:
63 # edge correction for the first interspike interval:
64 # take the maximum of the distance from the beginning to the first
65 # spike and the interval between the first two spikes.
66 # if there is only one spike, take the its distance to the beginning
67 nu1 = fmax(s1[0]-t_start, s1[1]-s1[0]) if N1 > 1 else s1[0]-t_start
68 index1 = -1
69 else:
70 # if the first spike is exactly at the start, take the distance
71 # to the next spike. If this is the only spike, take the distance to
72 # the end.
73 nu1 = s1[1]-s1[0] if N1 > 1 else t_end-s1[0]
74 index1 = 0
75
76 if s2[0] > t_start:
77 # edge correction as above
78 nu2 = fmax(s2[0]-t_start, s2[1]-s2[0]) if N2 > 1 else s2[0]-t_start
79 index2 = -1
80 else:
81 nu2 = s2[1]-s2[0] if N2 > 1 else t_end-s2[0]
82 index2 = 0
83
84 last_t = t_start
85 curr_isi = fabs(nu1-nu2)/fmax(MRTS, fmax(nu1, nu2))
86 index = 1
87
88 with nogil: # release the interpreter to allow multithreading
89 while index1+index2 < N1+N2-2:
90 # check which spike is next, only if there are spikes left in 1
91 # next spike in 1 is earlier, or there are no spikes left in 2
92 if (index1 < N1-1) and ((index2 == N2-1) or
93 (s1[index1+1] < s2[index2+1])):
94 index1 += 1
95 curr_t = s1[index1]
96 if index1 < N1-1:
97 nu1 = s1[index1+1]-s1[index1]
98 else:
99 # edge correction for the last ISI:
100 # take the max of the distance of the last
101 # spike to the end and the previous ISI. If there was only
102 # one spike, always take the distance to the end.
103 nu1 = fmax(t_end-s1[index1], nu1) if N1 > 1 \
104 else t_end-s1[index1]
105 elif (index2 < N2-1) and ((index1 == N1-1) or
106 (s1[index1+1] > s2[index2+1])):
107 index2 += 1
108 curr_t = s2[index2]
109 if index2 < N2-1:
110 nu2 = s2[index2+1]-s2[index2]
111 else:
112 # edge correction for the end as above
113 nu2 = fmax(t_end-s2[index2], nu2) if N2 > 1 \
114 else t_end-s2[index2]
115 else: # s1[index1+1] == s2[index2+1]
116 index1 += 1
117 index2 += 1
118 curr_t = s1[index1]
119 if index1 < N1-1:
120 nu1 = s1[index1+1]-s1[index1]
121 else:
122 # edge correction for the end as above
123 nu1 = fmax(t_end-s1[index1], nu1) if N1 > 1 \
124 else t_end-s1[index1]
125 if index2 < N2-1:
126 nu2 = s2[index2+1]-s2[index2]
127 else:
128 # edge correction for the end as above
129 nu2 = fmax(t_end-s2[index2], nu2) if N2 > 1 \
130 else t_end-s2[index2]
131 # compute the corresponding isi-distance
132 isi_value += curr_isi * (curr_t - last_t)
133 curr_isi = fabs(nu1 - nu2) / fmax(MRTS, fmax(nu1, nu2))
134 last_t = curr_t
135 index += 1
136
137 isi_value += curr_isi * (t_end - last_t)
138 # end nogil
139
140 return isi_value / (t_end-t_start)
141
142
143 ############################################################
144 # get_min_dist_cython
145 ############################################################
146 cdef inline double get_min_dist_cython(double spike_time,
147 double[:] spike_train,
148 # use memory view to ensure inlining
149 # np.ndarray[DTYPE_t,ndim=1] spike_train,
150 int N,
151 int start_index,
152 double t_start, double t_end) nogil:
153 """ Returns the minimal distance |spike_time - spike_train[i]|
154 with i>=start_index.
155 """
156 cdef double d, d_temp
157 # start with the distance to the start time
158 d = fabs(spike_time - t_start)
159 if start_index < 0:
160 start_index = 0
161 while start_index < N:
162 d_temp = fabs(spike_time - spike_train[start_index])
163 if d_temp > d:
164 return d
165 else:
166 d = d_temp
167 start_index += 1
168
169 # finally, check the distance to end time
170 d_temp = fabs(t_end - spike_time)
171 if d_temp > d:
172 return d
173 else:
174 return d_temp
175
176
177 ############################################################
178 # dist_at_t
179 ############################################################
180 cdef inline double dist_at_t(double isi1, double isi2,
181 double s1, double s2,
182 double MRTS, int RI) nogil:
183 """ Compute instantaneous Spike Distance
184 In: isi1, isi2 - spike time differences around current times in each trains
185 s1, s2 - weighted spike time differences between trains
186 MRTS -minimum relevant time scal (0 for legacy logic)
187 RI - Rate Independent Adaptive spike distance
188 (False for legacy SPIKE distance)
189 Out: Spike Distance at current time
190 """
191 cdef double meanISI = .5*(isi1+isi2)
192 cdef double limitedISI = fmax(MRTS, meanISI)
193
194 if RI:
195 return .5*(s1+s2)/limitedISI
196 else:
197 return .5*(s1*isi2 + s2*isi1)/(meanISI*limitedISI)
198 #denominator = fmax(.5*(isi1+isi2), MRTS)
199 #if RI == 0:
200 # denominator *= (isi1+isi2)
201 #return denominator
202
203 ############################################################
204 # spike_distance_cython
205 ############################################################
206 def spike_distance_cython(double[:] t1, double[:] t2,
207 double t_start, double t_end,
208 double MRTS=0., int RI = 0):
209
210 cdef int N1, N2, index1, index2, index
211 cdef double t_p1, t_f1, t_p2, t_f2, dt_p1, dt_p2, dt_f1, dt_f2
212 cdef double isi1, isi2, s1, s2
213 cdef double y_start, y_end, t_last, t_current, spike_value
214 cdef double[:] t_aux1 = np.empty(2)
215 cdef double[:] t_aux2 = np.empty(2)
216
217 spike_value = 0.0
218
219 N1 = len(t1)
220 N2 = len(t2)
221
222 # we can assume at least one spikes per spike train
223 assert N1 > 0
224 assert N2 > 0
225
226
227 with nogil: # release the interpreter to allow multithreading
228 t_last = t_start
229 # auxiliary spikes for edge correction - consistent with first/last ISI
230 t_aux1[0] = fmin(t_start, 2*t1[0]-t1[1]) if N1 > 1 else t_start
231 t_aux1[1] = fmax(t_end, 2*t1[N1-1]-t1[N1-2]) if N1 > 1 else t_end
232 t_aux2[0] = fmin(t_start, 2*t2[0]-t2[1]) if N2 > 1 else t_start
233 t_aux2[1] = fmax(t_end, 2*t2[N2-1]+-t2[N2-2]) if N2 > 1 else t_end
234 # print "aux spikes %.15f, %.15f ; %.15f, %.15f" % (t_aux1[0], t_aux1[1], t_aux2[0], t_aux2[1])
235 t_p1 = t_start if (t1[0] == t_start) else t_aux1[0]
236 t_p2 = t_start if (t2[0] == t_start) else t_aux2[0]
237 if t1[0] > t_start:
238 # dt_p1 = t2[0]-t_start
239 t_f1 = t1[0]
240 dt_f1 = get_min_dist_cython(t_f1, t2, N2, 0, t_aux2[0], t_aux2[1])
241 isi1 = fmax(t_f1-t_start, t1[1]-t1[0]) if N1 > 1 else t_f1-t_start
242 dt_p1 = dt_f1
243 # s1 = dt_p1*(t_f1-t_start)/isi1
244 s1 = dt_p1
245 index1 = -1
246 else: # t1[0] == t_start
247 t_f1 = t1[1] if N1 > 1 else t_end
248 dt_f1 = get_min_dist_cython(t_f1, t2, N2, 0, t_aux2[0], t_aux2[1])
249 dt_p1 = get_min_dist_cython(t_p1, t2, N2, 0, t_aux2[0], t_aux2[1])
250 isi1 = t_f1-t1[0]
251 s1 = dt_p1
252 index1 = 0
253 if t2[0] > t_start:
254 # dt_p1 = t2[0]-t_start
255 t_f2 = t2[0]
256 dt_f2 = get_min_dist_cython(t_f2, t1, N1, 0, t_aux1[0], t_aux1[1])
257 dt_p2 = dt_f2
258 isi2 = fmax(t_f2-t_start, t2[1]-t2[0]) if N2 > 1 else t_f2-t_start
259 # s2 = dt_p2*(t_f2-t_start)/isi2
260 s2 = dt_p2
261 index2 = -1
262 else: # t2[0] == t_start
263 t_f2 = t2[1] if N2 > 1 else t_end
264 dt_f2 = get_min_dist_cython(t_f2, t1, N1, 0, t_aux1[0], t_aux1[1])
265 # dt_p2 = t_start-t_p1 # 0.0
266 dt_p2 = get_min_dist_cython(t_p2, t1, N1, 0, t_aux1[0], t_aux1[1])
267 isi2 = t_f2-t2[0]
268 s2 = dt_p2
269 index2 = 0
270
271 y_start = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
272 index = 1
273
274 while index1+index2 < N1+N2-2:
275 # print(index, index1, index2)
276 if (index1 < N1-1) and (t_f1 < t_f2 or index2 == N2-1):
277 index1 += 1
278 # first calculate the previous interval end value
279 s1 = dt_f1*(t_f1-t_p1) / isi1
280 # the previous time now was the following time before:
281 dt_p1 = dt_f1
282 t_p1 = t_f1 # t_p1 contains the current time point
283 # get the next time
284 if index1 < N1-1:
285 t_f1 = t1[index1+1]
286 else:
287 t_f1 = t_aux1[1]
288 t_curr = t_p1
289 s2 = (dt_p2*(t_f2-t_p1) + dt_f2*(t_p1-t_p2)) / isi2
290 y_end = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
291
292 spike_value += 0.5*(y_start + y_end) * (t_curr - t_last)
293
294 # now the next interval start value
295 if index1 < N1-1:
296 dt_f1 = get_min_dist_cython(t_f1, t2, N2, index2,
297 t_aux2[0], t_aux2[1])
298 isi1 = t_f1-t_p1
299 s1 = dt_p1
300 else:
301 dt_f1 = dt_p1
302 isi1 = fmax(t_end-t1[N1-1], t1[N1-1]-t1[N1-2]) if N1 > 1 \
303 else t_end-t1[N1-1]
304 # s1 needs adjustment due to change of isi1
305 # s1 = dt_p1*(t_end-t1[N1-1])/isi1
306 # Eero's correction: no adjustment
307 s1 = dt_p1
308 # s2 is the same as above, thus we can compute y2 immediately
309 y_start = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
310 elif (index2 < N2-1) and (t_f1 > t_f2 or index1 == N1-1):
311 index2 += 1
312 # first calculate the previous interval end value
313 s2 = dt_f2*(t_f2-t_p2) / isi2
314 # the previous time now was the following time before:
315 dt_p2 = dt_f2
316 t_p2 = t_f2 # t_p2 contains the current time point
317 # get the next time
318 if index2 < N2-1:
319 t_f2 = t2[index2+1]
320 else:
321 t_f2 = t_aux2[1]
322 t_curr = t_p2
323 s1 = (dt_p1*(t_f1-t_p2) + dt_f1*(t_p2-t_p1)) / isi1
324 y_end = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
325
326 spike_value += 0.5*(y_start + y_end) * (t_curr - t_last)
327
328 # now the next interval start value
329 if index2 < N2-1:
330 dt_f2 = get_min_dist_cython(t_f2, t1, N1, index1,
331 t_aux1[0], t_aux1[1])
332 isi2 = t_f2-t_p2
333 s2 = dt_p2
334 else:
335 dt_f2 = dt_p2
336 isi2 = fmax(t_end-t2[N2-1], t2[N2-1]-t2[N2-2]) if N2 > 1 \
337 else t_end-t2[N2-1]
338 # s2 needs adjustment due to change of isi2
339 # s2 = dt_p2*(t_end-t2[N2-1])/isi2
340 # Eero's correction: no adjustment
341 s2 = dt_p2
342 # s1 is the same as above, thus we can compute y2 immediately
343 y_start = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
344
345 else: # t_f1 == t_f2 - generate only one event
346 index1 += 1
347 index2 += 1
348 t_p1 = t_f1
349 t_p2 = t_f2
350 dt_p1 = 0.0
351 dt_p2 = 0.0
352 t_curr = t_f1
353 y_end = 0.0
354 spike_value += 0.5*(y_start + y_end) * (t_curr - t_last)
355 y_start = 0.0
356 if index1 < N1-1:
357 t_f1 = t1[index1+1]
358 dt_f1 = get_min_dist_cython(t_f1, t2, N2, index2,
359 t_aux2[0], t_aux2[1])
360 isi1 = t_f1 - t_p1
361 else:
362 t_f1 = t_aux1[1]
363 dt_f1 = dt_p1
364 isi1 = fmax(t_end-t1[N1-1], t1[N1-1]-t1[N1-2]) if N1 > 1 \
365 else t_end-t1[N1-1]
366 if index2 < N2-1:
367 t_f2 = t2[index2+1]
368 dt_f2 = get_min_dist_cython(t_f2, t1, N1, index1,
369 t_aux1[0], t_aux1[1])
370 isi2 = t_f2 - t_p2
371 else:
372 t_f2 = t_aux2[1]
373 dt_f2 = dt_p2
374 isi2 = fmax(t_end-t2[N2-1], t2[N2-1]-t2[N2-2]) if N2 > 1 \
375 else t_end-t2[N2-1]
376 index += 1
377 t_last = t_curr
378 # isi1 = max(t_end-t1[N1-1], t1[N1-1]-t1[N1-2])
379 # isi2 = max(t_end-t2[N2-1], t2[N2-1]-t2[N2-2])
380 s1 = dt_f1 # *(t_end-t1[N1-1])/isi1
381 s2 = dt_f2 # *(t_end-t2[N2-1])/isi2
382 y_end = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
383
384 spike_value += 0.5*(y_start + y_end) * (t_end - t_last)
385 # end nogil
386
387 # use only the data added above
388 # could be less than original length due to equal spike times
389 return spike_value / (t_end-t_start)
390
391
392 ############################################################
393 # isi_avrg_rf_cython
394 ############################################################
395 cdef inline double isi_avrg_rf_cython(double isi1, double isi2) nogil:
396 # rate free version
397 return (isi1+isi2)
398
399
400 ############################################################
401 # spike_distance_rf_cython
402 ############################################################
403 def spike_distance_rf_cython(double[:] t1, double[:] t2,
404 double t_start, double t_end):
405
406 cdef int N1, N2, index1, index2, index
407 cdef double t_p1, t_f1, t_p2, t_f2, dt_p1, dt_p2, dt_f1, dt_f2
408 cdef double isi1, isi2, s1, s2
409 cdef double y_start, y_end, t_last, t_current, spike_value
410
411 spike_value = 0.0
412
413 N1 = len(t1)
414 N2 = len(t2)
415
416 with nogil: # release the interpreter to allow multithreading
417 t_last = t_start
418 t_p1 = t_start
419 t_p2 = t_start
420 if t1[0] > t_start:
421 # dt_p1 = t2[0]-t_start
422 t_f1 = t1[0]
423 dt_f1 = get_min_dist_cython(t_f1, t2, N2, 0, t_start, t_end)
424 isi1 = fmax(t_f1-t_start, t1[1]-t1[0])
425 dt_p1 = dt_f1
426 s1 = dt_p1*(t_f1-t_start)/isi1
427 index1 = -1
428 else:
429 t_f1 = t1[1]
430 dt_f1 = get_min_dist_cython(t_f1, t2, N2, 0, t_start, t_end)
431 dt_p1 = 0.0
432 isi1 = t1[1]-t1[0]
433 s1 = dt_p1
434 index1 = 0
435 if t2[0] > t_start:
436 # dt_p1 = t2[0]-t_start
437 t_f2 = t2[0]
438 dt_f2 = get_min_dist_cython(t_f2, t1, N1, 0, t_start, t_end)
439 dt_p2 = dt_f2
440 isi2 = fmax(t_f2-t_start, t2[1]-t2[0])
441 s2 = dt_p2*(t_f2-t_start)/isi2
442 index2 = -1
443 else:
444 t_f2 = t2[1]
445 dt_f2 = get_min_dist_cython(t_f2, t1, N1, 0, t_start, t_end)
446 dt_p2 = 0.0
447 isi2 = t2[1]-t2[0]
448 s2 = dt_p2
449 index2 = 0
450
451 y_start = (s1 + s2) / isi_avrg_rf_cython(isi1, isi2)
452 index = 1
453
454 while index1+index2 < N1+N2-2:
455 # print(index, index1, index2)
456 if (index1 < N1-1) and (t_f1 < t_f2 or index2 == N2-1):
457 index1 += 1
458 # first calculate the previous interval end value
459 s1 = dt_f1*(t_f1-t_p1) / isi1
460 # the previous time now was the following time before:
461 dt_p1 = dt_f1
462 t_p1 = t_f1 # t_p1 contains the current time point
463 # get the next time
464 if index1 < N1-1:
465 t_f1 = t1[index1+1]
466 else:
467 t_f1 = t_end
468 t_curr = t_p1
469 s2 = (dt_p2*(t_f2-t_p1) + dt_f2*(t_p1-t_p2)) / isi2
470 y_end = (s1 + s2) / isi_avrg_rf_cython(isi1, isi2)
471
472 spike_value += 0.5*(y_start + y_end) * (t_curr - t_last)
473
474 # now the next interval start value
475 if index1 < N1-1:
476 dt_f1 = get_min_dist_cython(t_f1, t2, N2, index2,
477 t_start, t_end)
478 isi1 = t_f1-t_p1
479 s1 = dt_p1
480 else:
481 dt_f1 = dt_p1
482 isi1 = fmax(t_end-t1[N1-1], t1[N1-1]-t1[N1-2])
483 # s1 needs adjustment due to change of isi1
484 s1 = dt_p1*(t_end-t1[N1-1])/isi1
485 # s2 is the same as above, thus we can compute y2 immediately
486 y_start = (s1 + s2) / isi_avrg_rf_cython(isi1, isi2)
487 elif (index2 < N2-1) and (t_f1 > t_f2 or index1 == N1-1):
488 index2 += 1
489 # first calculate the previous interval end value
490 s2 = dt_f2*(t_f2-t_p2) / isi2
491 # the previous time now was the following time before:
492 dt_p2 = dt_f2
493 t_p2 = t_f2 # t_p2 contains the current time point
494 # get the next time
495 if index2 < N2-1:
496 t_f2 = t2[index2+1]
497 else:
498 t_f2 = t_end
499 t_curr = t_p2
500 s1 = (dt_p1*(t_f1-t_p2) + dt_f1*(t_p2-t_p1)) / isi1
501 y_end = (s1 + s2) / isi_avrg_rf_cython(isi1, isi2)
502
503 spike_value += 0.5*(y_start + y_end) * (t_curr - t_last)
504
505 # now the next interval start value
506 if index2 < N2-1:
507 dt_f2 = get_min_dist_cython(t_f2, t1, N1, index1,
508 t_start, t_end)
509 isi2 = t_f2-t_p2
510 s2 = dt_p2
511 else:
512 dt_f2 = dt_p2
513 isi2 = fmax(t_end-t2[N2-1], t2[N2-1]-t2[N2-2])
514 # s2 needs adjustment due to change of isi2
515 s2 = dt_p2*(t_end-t2[N2-1])/isi2
516 # s1 is the same as above, thus we can compute y2 immediately
517 y_start = (s1 + s2) / isi_avrg_rf_cython(isi1, isi2)
518
519 else: # t_f1 == t_f2 - generate only one event
520 index1 += 1
521 index2 += 1
522 t_p1 = t_f1
523 t_p2 = t_f2
524 dt_p1 = 0.0
525 dt_p2 = 0.0
526 t_curr = t_f1
527 y_end = 0.0
528 spike_value += 0.5*(y_start + y_end) * (t_curr - t_last)
529 y_start = 0.0
530 if index1 < N1-1:
531 t_f1 = t1[index1+1]
532 dt_f1 = get_min_dist_cython(t_f1, t2, N2, index2,
533 t_start, t_end)
534 isi1 = t_f1 - t_p1
535 else:
536 t_f1 = t_end
537 dt_f1 = dt_p1
538 isi1 = fmax(t_end-t1[N1-1], t1[N1-1]-t1[N1-2])
539 if index2 < N2-1:
540 t_f2 = t2[index2+1]
541 dt_f2 = get_min_dist_cython(t_f2, t1, N1, index1,
542 t_start, t_end)
543 isi2 = t_f2 - t_p2
544 else:
545 t_f2 = t_end
546 dt_f2 = dt_p2
547 isi2 = fmax(t_end-t2[N2-1], t2[N2-1]-t2[N2-2])
548 index += 1
549 t_last = t_curr
550 # isi1 = max(t_end-t1[N1-1], t1[N1-1]-t1[N1-2])
551 # isi2 = max(t_end-t2[N2-1], t2[N2-1]-t2[N2-2])
552 s1 = dt_f1*(t_end-t1[N1-1])/isi1
553 s2 = dt_f2*(t_end-t2[N2-1])/isi2
554 y_end = (s1 + s2) / isi_avrg_rf_cython(isi1, isi2)
555
556 spike_value += 0.5*(y_start + y_end) * (t_end - t_last)
557 # end nogil
558
559 # use only the data added above
560 # could be less than original length due to equal spike times
561 return spike_value / (t_end-t_start)
562
563
564
565
566 ############################################################
567 # coincidence_value_cython
568 ############################################################
569 def coincidence_value_cython(double[:] spikes1, double[:] spikes2,
570 double t_start, double t_end, double max_tau,
571 double MRTS = 0.):
572
573 cdef int N1 = len(spikes1)
574 cdef int N2 = len(spikes2)
575 cdef int i = -1
576 cdef int j = -1
577 cdef double coinc = 0.0
578 cdef double mp = 0.0
579 cdef double interval = t_end - t_start
580 cdef double tau
581
582 cdef double true_max = t_end - t_start
583 if max_tau > 0:
584 true_max = fmin(true_max, 2*max_tau)
585
586 while i + j < N1 + N2 - 2:
587 if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]):
588 i += 1
589 mp += 1
590 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
591 if j > -1 and spikes1[i]-spikes2[j] < tau:
592 # coincidence between the current spike and the previous spike
593 # both get marked with 1
594 coinc += 2
595 elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]):
596 j += 1
597 mp += 1
598 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
599 if i > -1 and spikes2[j]-spikes1[i] < tau:
600 # coincidence between the current spike and the previous spike
601 # both get marked with 1
602 coinc += 2
603 else: # spikes1[i+1] = spikes2[j+1]
604 # advance in both spike trains
605 j += 1
606 i += 1
607 # add only one event, but with coincidence 2 and multiplicity 2
608 mp += 2
609 coinc += 2
610
611 return coinc, mp