Debian Codegraph @

pyspike/cython/cython_profiles.pyx

raw
1 #cython: language_level=3
2 #cython: boundscheck=False
3 #cython: wraparound=False
4 #cython: cdivision=True
5
6 """
7 cython_profiles.pyx
8
9 cython implementation of the isi-, spike- and spike-sync profiles
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 2014-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_profiles.pyx
25
26 which gives::
27
28 cython_profiles.html
29
30 """
31
32 import numpy as np
33 cimport numpy as np
34
35 from libc.math cimport fabs
36 from libc.math cimport fmax
37 from libc.math cimport fmin
38
39 from pyspike.cython.cython_get_tau cimport get_tau
40
41 #DTYPE = float
42 #ctypedef np.float_t DTYPE_t
43
44 ############################################################
45 # isi_profile_cython
46 ############################################################
47 def isi_profile_cython(double[:] s1, double[:] s2,
48 double t_start, double t_end,
49 double MRTS=0.):
50
51 cdef double[:] spike_events
52 cdef double[:] isi_values
53 cdef int index1, index2, index
54 cdef int N1, N2
55 cdef double nu1, nu2
56 N1 = len(s1)
57 N2 = len(s2)
58
59 spike_events = np.empty(N1+N2+2)
60 # the values have one entry less as they are defined at the intervals
61 isi_values = np.empty(N1+N2+1)
62
63 # first x-value of the profile
64 spike_events[0] = t_start
65
66 # first interspike interval - check if a spike exists at the start time
67 if s1[0] > t_start:
68 # edge correction
69 nu1 = fmax(s1[0]-t_start, s1[1]-s1[0]) if N1 > 1 else s1[0]-t_start
70 index1 = -1
71 else:
72 nu1 = s1[1]-s1[0] if N1 > 1 else t_end-s1[0]
73 index1 = 0
74
75 if s2[0] > t_start:
76 # edge correction
77 nu2 = fmax(s2[0]-t_start, s2[1]-s2[0]) if N2 > 1 else s2[0]-t_start
78 index2 = -1
79 else:
80 nu2 = s2[1]-s2[0] if N2 > 1 else t_end-s2[0]
81 index2 = 0
82
83 isi_values[0] = fabs(nu1-nu2)/fmax(MRTS, fmax(nu1, nu2))
84 index = 1
85
86 with nogil: # release the interpreter to allow multithreading
87 while index1+index2 < N1+N2-2:
88 # check which spike is next, only if there are spikes left in 1
89 # next spike in 1 is earlier, or there are no spikes left in 2
90 if (index1 < N1-1) and ((index2 == N2-1) or
91 (s1[index1+1] < s2[index2+1])):
92 index1 += 1
93 spike_events[index] = s1[index1]
94 if index1 < N1-1:
95 nu1 = s1[index1+1]-s1[index1]
96 else:
97 # edge correction
98 nu1 = fmax(t_end-s1[index1], nu1) if N1 > 1 \
99 else t_end-s1[index1]
100 elif (index2 < N2-1) and ((index1 == N1-1) or
101 (s1[index1+1] > s2[index2+1])):
102 index2 += 1
103 spike_events[index] = s2[index2]
104 if index2 < N2-1:
105 nu2 = s2[index2+1]-s2[index2]
106 else:
107 # edge correction
108 nu2 = fmax(t_end-s2[index2], nu2) if N2 > 1 \
109 else t_end-s2[index2]
110 else: # s1[index1+1] == s2[index2+1]
111 index1 += 1
112 index2 += 1
113 spike_events[index] = s1[index1]
114 if index1 < N1-1:
115 nu1 = s1[index1+1]-s1[index1]
116 else:
117 # edge correction
118 nu1 = fmax(t_end-s1[index1], nu1) if N1 > 1 \
119 else t_end-s1[index1]
120 if index2 < N2-1:
121 nu2 = s2[index2+1]-s2[index2]
122 else:
123 # edge correction
124 nu2 = fmax(t_end-s2[index2], nu2) if N2 > 1 \
125 else t_end-s2[index2]
126 # compute the corresponding isi-distance
127 isi_values[index] = fabs(nu1 - nu2) / fmax(MRTS, fmax(nu1, nu2))
128 index += 1
129 # the last event is the interval end
130 if spike_events[index-1] == t_end:
131 index -= 1
132 else:
133 spike_events[index] = t_end
134 # end nogil
135
136 return spike_events[:index+1], isi_values[:index]
137
138
139 ############################################################
140 # get_min_dist_cython
141 ############################################################
142 cdef inline double get_min_dist_cython(double spike_time,
143 double[:] spike_train,
144 # use memory view to ensure inlining
145 # np.ndarray[DTYPE_t,ndim=1] spike_train,
146 int N,
147 int start_index,
148 double t_start, double t_end) nogil:
149 """ Returns the minimal distance |spike_time - spike_train[i]|
150 with i>=start_index.
151 """
152 cdef double d, d_temp
153 # start with the distance to the start time
154 d = fabs(spike_time - t_start)
155 if start_index < 0:
156 start_index = 0
157 while start_index < N:
158 d_temp = fabs(spike_time - spike_train[start_index])
159 if d_temp > d:
160 return d
161 else:
162 d = d_temp
163 start_index += 1
164
165 # finally, check the distance to end time
166 d_temp = fabs(t_end - spike_time)
167 if d_temp > d:
168 return d
169 else:
170 return d_temp
171
172
173 ############################################################
174 # dist_at_t
175 ############################################################
176 cdef inline double dist_at_t(double isi1, double isi2,
177 double s1, double s2,
178 double MRTS, int RI) nogil:
179 """ Compute instantaneous Spike Distance
180 In: isi1, isi2 - spike time differences around current times in each trains
181 s1, s2 - weighted spike time differences between trains
182 MRTS -minimum relevant time scal (0 for legacy logic)
183 RI - Rate Independent Adaptive spike distance
184 (False for legacy SPIKE distance)
185 Out: Spike Distance at current time
186 """
187 cdef double meanISI = .5*(isi1+isi2)
188 cdef double limitedISI = max(MRTS, meanISI)
189 if RI:
190 return .5*(s1+s2)/limitedISI
191 else:
192 return .5*(s1*isi2 + s2*isi1)/(meanISI*limitedISI)
193
194 ############################################################
195 # spike_profile_cython
196 ############################################################
197 def spike_profile_cython(double[:] t1, double[:] t2,
198 double t_start, double t_end,
199 double MRTS=0., int RI=0):
200
201 cdef double[:] spike_events
202 cdef double[:] y_starts
203 cdef double[:] y_ends
204 cdef double[:] t_aux1 = np.empty(2)
205 cdef double[:] t_aux2 = np.empty(2)
206
207 cdef int N1, N2, index1, index2, index
208 cdef double t_p1, t_f1, t_p2, t_f2, dt_p1, dt_p2, dt_f1, dt_f2
209 cdef double isi1, isi2, s1, s2
210
211 N1 = len(t1)
212 N2 = len(t2)
213
214 # we can assume at least one spikes per spike train
215 assert N1 > 0
216 assert N2 > 0
217
218 spike_events = np.empty(N1+N2+2)
219
220 y_starts = np.empty(len(spike_events)-1)
221 y_ends = np.empty(len(spike_events)-1)
222
223 with nogil: # release the interpreter to allow multithreading
224 spike_events[0] = t_start
225 # t_p1 = t_start
226 # t_p2 = t_start
227 # auxiliary spikes for edge correction - consistent with first/last ISI
228 t_aux1[0] = fmin(t_start, 2*t1[0]-t1[1]) if N1 > 1 else t_start
229 t_aux1[1] = fmax(t_end, 2*t1[N1-1]-t1[N1-2]) if N1 > 1 else t_end
230 t_aux2[0] = fmin(t_start, 2*t2[0]-t2[1]) if N2 > 1 else t_start
231 t_aux2[1] = fmax(t_end, 2*t2[N2-1]-t2[N2-2]) if N2 > 1 else t_end
232 t_p1 = t_start if (t1[0] == t_start) else t_aux1[0]
233 t_p2 = t_start if (t2[0] == t_start) else t_aux2[0]
234 if t1[0] > t_start:
235 # dt_p1 = t2[0]-t_start
236 t_f1 = t1[0]
237 dt_f1 = get_min_dist_cython(t_f1, t2, N2, 0, t_aux2[0], t_aux2[1])
238 isi1 = fmax(t_f1-t_start, t1[1]-t1[0]) if N1 > 1 else t_f1-t_start
239 dt_p1 = dt_f1
240 # s1 = dt_p1*(t_f1-t_start)/isi1
241 s1 = dt_p1
242 index1 = -1
243 else:
244 t_f1 = t1[1] if N1 > 1 else t_end
245 dt_f1 = get_min_dist_cython(t_f1, t2, N2, 0, t_aux2[0], t_aux2[1])
246 dt_p1 = get_min_dist_cython(t_p1, t2, N2, 0, t_aux2[0], t_aux2[1])
247 isi1 = t_f1-t1[0]
248 s1 = dt_p1
249 index1 = 0
250 if t2[0] > t_start:
251 # dt_p1 = t2[0]-t_start
252 t_f2 = t2[0]
253 dt_f2 = get_min_dist_cython(t_f2, t1, N1, 0, t_aux1[0], t_aux1[1])
254 dt_p2 = dt_f2
255 isi2 = fmax(t_f2-t_start, t2[1]-t2[0]) if N2 > 1 else t_f2-t_start
256 # s2 = dt_p2*(t_f2-t_start)/isi2
257 s2 = dt_p2
258 index2 = -1
259 else:
260 t_f2 = t2[1] if N2 > 1 else t_end
261 dt_f2 = get_min_dist_cython(t_f2, t1, N1, 0, t_aux1[0], t_aux1[1])
262 dt_p2 = get_min_dist_cython(t_p2, t1, N1, 0, t_aux1[0], t_aux1[1])
263 isi2 = t_f2-t2[0]
264 s2 = dt_p2
265 index2 = 0
266
267 y_starts[0] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
268 index = 1
269
270 while index1+index2 < N1+N2-2:
271 # print(index, index1, index2)
272 if (index1 < N1-1) and (t_f1 < t_f2 or index2 == N2-1):
273 index1 += 1
274 # first calculate the previous interval end value
275 s1 = dt_f1*(t_f1-t_p1) / isi1
276 # the previous time now was the following time before:
277 dt_p1 = dt_f1
278 t_p1 = t_f1 # t_p1 contains the current time point
279 # get the next time
280 if index1 < N1-1:
281 t_f1 = t1[index1+1]
282 else:
283 t_f1 = t_aux1[1]
284 spike_events[index] = t_p1
285 s2 = (dt_p2*(t_f2-t_p1) + dt_f2*(t_p1-t_p2)) / isi2
286 y_ends[index-1] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
287 # now the next interval start value
288 if index1 < N1-1:
289 dt_f1 = get_min_dist_cython(t_f1, t2, N2, index2,
290 t_aux2[0], t_aux2[1])
291 isi1 = t_f1-t_p1
292 s1 = dt_p1
293 else:
294 dt_f1 = dt_p1
295 isi1 = fmax(t_end-t1[N1-1], t1[N1-1]-t1[N1-2]) if N1 > 1 \
296 else t_end-t1[N1-1]
297 # s1 needs adjustment due to change of isi1
298 # s1 = dt_p1*(t_end-t1[N1-1])/isi1
299 # Eero's correction: no adjustment
300 s1 = dt_p1
301 # s2 is the same as above, thus we can compute y2 immediately
302 y_starts[index] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
303 elif (index2 < N2-1) and (t_f1 > t_f2 or index1 == N1-1):
304 index2 += 1
305 # first calculate the previous interval end value
306 s2 = dt_f2*(t_f2-t_p2) / isi2
307 # the previous time now was the following time before:
308 dt_p2 = dt_f2
309 t_p2 = t_f2 # t_p2 contains the current time point
310 # get the next time
311 if index2 < N2-1:
312 t_f2 = t2[index2+1]
313 else:
314 t_f2 = t_aux2[1]
315 spike_events[index] = t_p2
316 s1 = (dt_p1*(t_f1-t_p2) + dt_f1*(t_p2-t_p1)) / isi1
317 y_ends[index-1] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
318 # now the next interval start value
319 if index2 < N2-1:
320 dt_f2 = get_min_dist_cython(t_f2, t1, N1, index1,
321 t_aux1[0], t_aux1[1])
322 isi2 = t_f2-t_p2
323 s2 = dt_p2
324 else:
325 dt_f2 = dt_p2
326 isi2 = fmax(t_end-t2[N2-1], t2[N2-1]-t2[N2-2]) if N2 > 1 \
327 else t_end-t2[N2-1]
328 # s2 needs adjustment due to change of isi2
329 # s2 = dt_p2*(t_end-t2[N2-1])/isi2
330 # Eero's correction: no adjustment
331 s2 = dt_p2
332 # s2 is the same as above, thus we can compute y2 immediately
333 y_starts[index] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
334 else: # t_f1 == t_f2 - generate only one event
335 index1 += 1
336 index2 += 1
337 t_p1 = t_f1
338 t_p2 = t_f2
339 dt_p1 = 0.0
340 dt_p2 = 0.0
341 spike_events[index] = t_f1
342 y_ends[index-1] = 0.0
343 y_starts[index] = 0.0
344 if index1 < N1-1:
345 t_f1 = t1[index1+1]
346 dt_f1 = get_min_dist_cython(t_f1, t2, N2, index2,
347 t_aux2[0], t_aux2[1])
348 isi1 = t_f1 - t_p1
349 else:
350 t_f1 = t_aux1[1]
351 dt_f1 = dt_p1
352 isi1 = fmax(t_end-t1[N1-1], t1[N1-1]-t1[N1-2]) if N1 > 1 \
353 else t_end-t1[N1-1]
354 if index2 < N2-1:
355 t_f2 = t2[index2+1]
356 dt_f2 = get_min_dist_cython(t_f2, t1, N1, index1,
357 t_aux1[0], t_aux1[1])
358 isi2 = t_f2 - t_p2
359 else:
360 t_f2 = t_aux2[1]
361 dt_f2 = dt_p2
362 isi2 = fmax(t_end-t2[N2-1], t2[N2-1]-t2[N2-2]) if N2 > 1 \
363 else t_end-t2[N2-1]
364 index += 1
365 # the last event is the interval end
366 if spike_events[index-1] == t_end:
367 index -= 1
368 else:
369 spike_events[index] = t_end
370 s1 = dt_f1
371 s2 = dt_f2
372 y_ends[index-1] = dist_at_t(isi1, isi2, s1, s2, MRTS, RI)
373 # end nogil
374
375 # use only the data added above
376 # could be less than original length due to equal spike times
377 return spike_events[:index+1], y_starts[:index], y_ends[:index]
378
379
380
381 ############################################################
382 # coincidence_profile_cython
383 ############################################################
384 def coincidence_profile_cython(double[:] spikes1, double[:] spikes2,
385 double t_start, double t_end, double max_tau, double MRTS=0):
386
387 cdef int N1 = len(spikes1)
388 cdef int N2 = len(spikes2)
389 cdef int i = -1
390 cdef int j = -1
391 cdef int n = 0
392 cdef double[:] st = np.zeros(N1 + N2 + 2) # spike times
393 cdef double[:] c = np.zeros(N1 + N2 + 2) # coincidences
394 cdef double[:] mp = np.ones(N1 + N2 + 2) # multiplicity
395 cdef double interval = t_end - t_start
396 cdef double tau
397
398 cdef double true_max = t_end - t_start
399 if max_tau > 0:
400 true_max = fmin(true_max, 2*max_tau)
401
402 while i + j < N1 + N2 - 2:
403 if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]):
404 i += 1
405 n += 1
406 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
407 st[n] = spikes1[i]
408 if j > -1 and spikes1[i]-spikes2[j] < tau:
409 # coincidence between the current spike and the previous spike
410 # both get marked with 1
411 c[n] = 1
412 c[n-1] = 1
413 elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]):
414 j += 1
415 n += 1
416 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
417 st[n] = spikes2[j]
418 if i > -1 and spikes2[j]-spikes1[i] < tau:
419 # coincidence between the current spike and the previous spike
420 # both get marked with 1
421 c[n] = 1
422 c[n-1] = 1
423 else: # spikes1[i+1] = spikes2[j+1]
424 # advance in both spike trains
425 j += 1
426 i += 1
427 n += 1
428 # add only one event, but with coincidence 2 and multiplicity 2
429 st[n] = spikes1[i]
430 c[n] = 2
431 mp[n] = 2
432
433 st = st[:n+2]
434 c = c[:n+2]
435 mp = mp[:n+2]
436
437 st[0] = t_start
438 st[len(st)-1] = t_end
439 if N1 + N2 > 0:
440 c[0] = c[1]
441 c[len(c)-1] = c[len(c)-2]
442 mp[0] = mp[1]
443 mp[len(mp)-1] = mp[len(mp)-2]
444 else:
445 c[0] = 1
446 c[1] = 1
447
448 return st, c, mp
449
450
451 ############################################################
452 # coincidence_single_profile_cython
453 ############################################################
454 def coincidence_single_profile_cython(double[:] spikes1, double[:] spikes2,
455 double t_start, double t_end, double max_tau, double MRTS=0.):
456
457 cdef int N1 = len(spikes1)
458 cdef int N2 = len(spikes2)
459 cdef int j = -1
460 cdef double[:] c = np.zeros(N1) # coincidences
461 cdef double interval = t_end - t_start
462 cdef double tau
463
464 cdef double true_max = t_end - t_start
465 if max_tau > 0:
466 true_max = fmin(true_max, 2*max_tau)
467
468 for i in xrange(N1):
469 while j < N2-1 and spikes2[j+1] < spikes1[i]:
470 # move forward until spikes2[j] is the last spike before spikes1[i]
471 # note that if spikes2[j] is after spikes1[i] we dont do anything
472 j += 1
473 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
474 if j > -1 and fabs(spikes1[i]-spikes2[j]) < tau:
475 # current spike in st1 is coincident
476 c[i] = 1
477 if j < N2-1 and (j < 0 or spikes2[j] < spikes1[i]):
478 # in case spikes2[j] is before spikes1[i] it has to be the one
479 # right before (see above), hence we move one forward and also
480 # check the next spike
481 j += 1
482 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
483 if fabs(spikes2[j]-spikes1[i]) < tau:
484 # current spike in st1 is coincident
485 c[i] = 1
486 return c