Debian Codegraph @

pyspike/cython/directionality_python_backend.py

raw symbols SCIP
1 """ directionality_python_backend.py
2
3 Collection of python functions that can be used instead of the cython
4 implementation.
5
6 Copyright 2015, Mario Mulansky <[email protected]>
7
8 Distributed under the BSD License
9
10 """
11
12 import numpy as np
13 from pyspike.cython.python_backend import get_tau
14
15 ############################################################
16 # spike_train_order_python
17 ############################################################
18 def spike_directionality_profile_python(spikes1, spikes2, t_start, t_end,
19 max_tau, MRTS=0.):
20 true_max = t_end - t_start
21 if max_tau > 0:
22 true_max = min(true_max, 2*max_tau)
23
24 N1 = len(spikes1)
25 N2 = len(spikes2)
26 i = -1
27 j = -1
28 d1 = np.zeros(N1) # directionality values
29 d2 = np.zeros(N2) # directionality values
30 while i + j < N1 + N2 - 2:
31 if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]):
32 i += 1
33 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
34 if j > -1 and spikes1[i]-spikes2[j] < tau:
35 # coincidence between the current spike and the previous spike
36 # spike in first spike train occurs after second
37 d1[i] = -1
38 d2[j] = +1
39 elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]):
40 j += 1
41 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
42 if i > -1 and spikes2[j]-spikes1[i] < tau:
43 # coincidence between the current spike and the previous spike
44 # spike in second spike train occurs after first
45 d1[i] = +1
46 d2[j] = -1
47 else: # spikes1[i+1] = spikes2[j+1]
48 # advance in both spike trains
49 j += 1
50 i += 1
51 d1[i] = 0
52 d2[j] = 0
53
54 return d1, d2
55
56
57 ############################################################
58 # spike_train_order_python
59 ############################################################
60 def spike_train_order_profile_python(spikes1, spikes2, t_start, t_end,
61 max_tau, MRTS=0.):
62 true_max = t_end - t_start
63 if max_tau > 0:
64 true_max = min(true_max, 2*max_tau)
65
66 N1 = len(spikes1)
67 N2 = len(spikes2)
68 i = -1
69 j = -1
70 n = 0
71 st = np.zeros(N1 + N2 + 2) # spike times
72 a = np.zeros(N1 + N2 + 2) # coincidences
73 mp = np.ones(N1 + N2 + 2) # multiplicity
74 while i + j < N1 + N2 - 2:
75 if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]):
76 i += 1
77 n += 1
78 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
79 st[n] = spikes1[i]
80 if j > -1 and spikes1[i]-spikes2[j] < tau:
81 # coincidence between the current spike and the previous spike
82 # both get marked with 1
83 a[n] = -1
84 a[n-1] = -1
85 elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]):
86 j += 1
87 n += 1
88 tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS)
89 st[n] = spikes2[j]
90 if i > -1 and spikes2[j]-spikes1[i] < tau:
91 # coincidence between the current spike and the previous spike
92 # both get marked with 1
93 a[n] = 1
94 a[n-1] = 1
95 else: # spikes1[i+1] = spikes2[j+1]
96 # advance in both spike trains
97 j += 1
98 i += 1
99 n += 1
100 # add only one event with zero asymmetry value and multiplicity 2
101 st[n] = spikes1[i]
102 a[n] = 0
103 mp[n] = 2
104
105 st = st[:n+2]
106 a = a[:n+2]
107 mp = mp[:n+2]
108
109 st[0] = t_start
110 st[len(st)-1] = t_end
111 if N1 + N2 > 0:
112 a[0] = a[1]
113 a[len(a)-1] = a[len(a)-2]
114 mp[0] = mp[1]
115 mp[len(mp)-1] = mp[len(mp)-2]
116 else:
117 a[0] = 1
118 a[1] = 1
119
120 return st, a, mp