| 1 |
#cython: language_level=3 |
| 2 |
#cython: boundscheck=False |
| 3 |
#cython: wraparound=False |
| 4 |
#cython: cdivision=True |
| 5 |
|
| 6 |
""" |
| 7 |
cython_directionality.pyx |
| 8 |
|
| 9 |
cython implementation of the spike delay asymmetry measures |
| 10 |
|
| 11 |
Copyright 2015, Mario Mulansky <[email protected]> |
| 12 |
|
| 13 |
Distributed under the BSD License |
| 14 |
|
| 15 |
""" |
| 16 |
|
| 17 |
""" |
| 18 |
To test whether things can be optimized: remove all yellow stuff |
| 19 |
in the html output:: |
| 20 |
|
| 21 |
cython -a cython_directionality.pyx |
| 22 |
|
| 23 |
which gives:: |
| 24 |
|
| 25 |
cython_directionality.html |
| 26 |
|
| 27 |
""" |
| 28 |
|
| 29 |
import numpy as np |
| 30 |
cimport numpy as np |
| 31 |
|
| 32 |
from libc.math cimport fabs |
| 33 |
from libc.math cimport fmax |
| 34 |
from libc.math cimport fmin |
| 35 |
|
| 36 |
from pyspike.cython.cython_get_tau cimport get_tau |
| 37 |
|
| 38 |
#DTYPE = float |
| 39 |
#ctypedef np.float_t DTYPE_t |
| 40 |
|
| 41 |
############################################################ |
| 42 |
# spike_train_order_profile_cython |
| 43 |
############################################################ |
| 44 |
def spike_train_order_profile_cython(double[:] spikes1, double[:] spikes2, |
| 45 |
double t_start, double t_end, |
| 46 |
double max_tau, |
| 47 |
double MRTS = 0.): |
| 48 |
|
| 49 |
cdef int N1 = len(spikes1) |
| 50 |
cdef int N2 = len(spikes2) |
| 51 |
cdef int i = -1 |
| 52 |
cdef int j = -1 |
| 53 |
cdef int n = 0 |
| 54 |
cdef double[:] st = np.zeros(N1 + N2 + 2) # spike times |
| 55 |
cdef double[:] a = np.zeros(N1 + N2 + 2) # asymmetry values |
| 56 |
cdef double[:] mp = np.ones(N1 + N2 + 2) # multiplicity |
| 57 |
cdef double interval = t_end - t_start |
| 58 |
cdef double tau |
| 59 |
|
| 60 |
cdef double true_max = t_end - t_start |
| 61 |
if max_tau > 0: |
| 62 |
true_max = fmin(true_max, 2*max_tau) |
| 63 |
|
| 64 |
while i + j < N1 + N2 - 2: |
| 65 |
if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]): |
| 66 |
i += 1 |
| 67 |
n += 1 |
| 68 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 69 |
st[n] = spikes1[i] |
| 70 |
if j > -1 and spikes1[i]-spikes2[j] < tau: |
| 71 |
# coincidence between the current spike and the previous spike |
| 72 |
# spike from spike train 1 after spike train 2 |
| 73 |
# both get marked with -1 |
| 74 |
a[n] = -1 |
| 75 |
a[n-1] = -1 |
| 76 |
elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]): |
| 77 |
j += 1 |
| 78 |
n += 1 |
| 79 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 80 |
st[n] = spikes2[j] |
| 81 |
if i > -1 and spikes2[j]-spikes1[i] < tau: |
| 82 |
# coincidence between the current spike and the previous spike |
| 83 |
# spike from spike train 1 before spike train 2 |
| 84 |
# both get marked with 1 |
| 85 |
a[n] = 1 |
| 86 |
a[n-1] = 1 |
| 87 |
else: # spikes1[i+1] = spikes2[j+1] |
| 88 |
# advance in both spike trains |
| 89 |
j += 1 |
| 90 |
i += 1 |
| 91 |
n += 1 |
| 92 |
# add only one event with zero asymmetry value and multiplicity 2 |
| 93 |
st[n] = spikes1[i] |
| 94 |
a[n] = 0 |
| 95 |
mp[n] = 2 |
| 96 |
|
| 97 |
st = st[:n+2] |
| 98 |
a = a[:n+2] |
| 99 |
mp = mp[:n+2] |
| 100 |
|
| 101 |
st[0] = t_start |
| 102 |
st[len(st)-1] = t_end |
| 103 |
if N1 + N2 > 0: |
| 104 |
a[0] = a[1] |
| 105 |
a[len(a)-1] = a[len(a)-2] |
| 106 |
mp[0] = mp[1] |
| 107 |
mp[len(mp)-1] = mp[len(mp)-2] |
| 108 |
else: |
| 109 |
a[0] = 1 |
| 110 |
a[1] = 1 |
| 111 |
|
| 112 |
return st, a, mp |
| 113 |
|
| 114 |
|
| 115 |
############################################################ |
| 116 |
# spike_train_order_cython |
| 117 |
############################################################ |
| 118 |
def spike_train_order_cython(double[:] spikes1, double[:] spikes2, |
| 119 |
double t_start, double t_end, double max_tau, |
| 120 |
double MRTS = 0.): |
| 121 |
|
| 122 |
cdef int N1 = len(spikes1) |
| 123 |
cdef int N2 = len(spikes2) |
| 124 |
cdef int i = -1 |
| 125 |
cdef int j = -1 |
| 126 |
cdef int d = 0 |
| 127 |
cdef int mp = 0 |
| 128 |
cdef double interval = t_end - t_start |
| 129 |
cdef double tau |
| 130 |
|
| 131 |
cdef double true_max = t_end - t_start |
| 132 |
if max_tau > 0: |
| 133 |
true_max = fmin(true_max, 2*max_tau) |
| 134 |
|
| 135 |
while i + j < N1 + N2 - 2: |
| 136 |
if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]): |
| 137 |
i += 1 |
| 138 |
mp += 1 |
| 139 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 140 |
if j > -1 and spikes1[i]-spikes2[j] < tau: |
| 141 |
# coincidence between the current spike and the previous spike |
| 142 |
# spike in spike train 2 appeared before spike in spike train 1 |
| 143 |
# mark with -1 |
| 144 |
d -= 2 |
| 145 |
elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]): |
| 146 |
j += 1 |
| 147 |
mp += 1 |
| 148 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 149 |
if i > -1 and spikes2[j]-spikes1[i] < tau: |
| 150 |
# coincidence between the current spike and the previous spike |
| 151 |
# spike in spike train 1 appeared before spike in spike train 2 |
| 152 |
# mark with +1 |
| 153 |
d += 2 |
| 154 |
else: # spikes1[i+1] = spikes2[j+1] |
| 155 |
# advance in both spike trains |
| 156 |
j += 1 |
| 157 |
i += 1 |
| 158 |
# add only one event with multiplicity 2, but no asymmetry counting |
| 159 |
mp += 2 |
| 160 |
|
| 161 |
if d == 0 and mp == 0: |
| 162 |
# empty spike trains -> spike sync = 1 by definition |
| 163 |
d = 1 |
| 164 |
mp = 1 |
| 165 |
|
| 166 |
return d, mp |
| 167 |
|
| 168 |
|
| 169 |
############################################################ |
| 170 |
# spike_directionality_profiles_cython |
| 171 |
############################################################ |
| 172 |
def spike_directionality_profiles_cython(double[:] spikes1, |
| 173 |
double[:] spikes2, |
| 174 |
double t_start, double t_end, |
| 175 |
double max_tau, |
| 176 |
double MRTS = 0.): |
| 177 |
|
| 178 |
cdef int N1 = len(spikes1) |
| 179 |
cdef int N2 = len(spikes2) |
| 180 |
cdef int i = -1 |
| 181 |
cdef int j = -1 |
| 182 |
cdef double[:] d1 = np.zeros(N1) # directionality values |
| 183 |
cdef double[:] d2 = np.zeros(N2) # directionality values |
| 184 |
cdef double interval = t_end - t_start |
| 185 |
cdef double tau |
| 186 |
|
| 187 |
cdef double true_max = t_end - t_start |
| 188 |
if max_tau > 0: |
| 189 |
true_max = fmin(true_max, 2*max_tau) |
| 190 |
|
| 191 |
while i + j < N1 + N2 - 2: |
| 192 |
if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]): |
| 193 |
i += 1 |
| 194 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 195 |
if j > -1 and spikes1[i]-spikes2[j] < tau: |
| 196 |
# coincidence between the current spike and the previous spike |
| 197 |
# spike from spike train 1 after spike train 2 |
| 198 |
# leading spike gets +1, following spike -1 |
| 199 |
d1[i] = -1 |
| 200 |
d2[j] = +1 |
| 201 |
elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]): |
| 202 |
j += 1 |
| 203 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 204 |
if i > -1 and spikes2[j]-spikes1[i] < tau: |
| 205 |
# coincidence between the current spike and the previous spike |
| 206 |
# spike from spike train 1 before spike train 2 |
| 207 |
# leading spike gets +1, following spike -1 |
| 208 |
d1[i] = +1 |
| 209 |
d2[j] = -1 |
| 210 |
else: # spikes1[i+1] = spikes2[j+1] |
| 211 |
# advance in both spike trains |
| 212 |
j += 1 |
| 213 |
i += 1 |
| 214 |
# equal spike times: zero asymmetry value |
| 215 |
d1[i] = 0 |
| 216 |
d2[j] = 0 |
| 217 |
|
| 218 |
return d1, d2 |
| 219 |
|
| 220 |
|
| 221 |
############################################################ |
| 222 |
# spike_directionality_cython |
| 223 |
############################################################ |
| 224 |
def spike_directionality_cython(double[:] spikes1, |
| 225 |
double[:] spikes2, |
| 226 |
double t_start, double t_end, |
| 227 |
double max_tau, |
| 228 |
double MRTS = 0.): |
| 229 |
|
| 230 |
cdef int N1 = len(spikes1) |
| 231 |
cdef int N2 = len(spikes2) |
| 232 |
cdef int i = -1 |
| 233 |
cdef int j = -1 |
| 234 |
cdef int d = 0 # directionality value |
| 235 |
cdef double interval = t_end - t_start |
| 236 |
cdef double tau |
| 237 |
|
| 238 |
cdef double true_max = t_end - t_start |
| 239 |
if max_tau > 0: |
| 240 |
true_max = fmin(true_max, 2*max_tau) |
| 241 |
|
| 242 |
while i + j < N1 + N2 - 2: |
| 243 |
if (i < N1-1) and (j == N2-1 or spikes1[i+1] < spikes2[j+1]): |
| 244 |
i += 1 |
| 245 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 246 |
if j > -1 and spikes1[i]-spikes2[j] < tau: |
| 247 |
# coincidence between the current spike and the previous spike |
| 248 |
# spike from spike train 1 after spike train 2 |
| 249 |
# leading spike gets +1, following spike -1 |
| 250 |
d -= 1 |
| 251 |
elif (j < N2-1) and (i == N1-1 or spikes1[i+1] > spikes2[j+1]): |
| 252 |
j += 1 |
| 253 |
tau = get_tau(spikes1, spikes2, i, j, true_max, MRTS) |
| 254 |
if i > -1 and spikes2[j]-spikes1[i] < tau: |
| 255 |
# coincidence between the current spike and the previous spike |
| 256 |
# spike from spike train 1 before spike train 2 |
| 257 |
# leading spike gets +1, following spike -1 |
| 258 |
d += 1 |
| 259 |
else: # spikes1[i+1] = spikes2[j+1] |
| 260 |
# advance in both spike trains |
| 261 |
j += 1 |
| 262 |
i += 1 |
| 263 |
|
| 264 |
return d |