| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import |
| 6 |
|
| 7 |
import pyspike |
| 8 |
from pyspike import PieceWiseLinFunc |
| 9 |
from pyspike.generic import _generic_profile_multi, _generic_distance_multi, \ |
| 10 |
_generic_distance_matrix, resolve_keywords |
| 11 |
from pyspike.isi_lengths import default_thresh |
| 12 |
from pyspike.spikes import reconcile_spike_trains, reconcile_spike_trains_bi |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
def spike_profile(*args, **kwargs): |
| 19 |
""" Computes the spike-distance profile :math:`S(t)` of the given |
| 20 |
spike trains. Returns the profile as a PieceWiseConstLin object. The |
| 21 |
SPIKE-values are defined positive :math:`S(t)>=0`. |
| 22 |
|
| 23 |
Valid call structures:: |
| 24 |
|
| 25 |
spike_profile(st1, st2) # returns the bi-variate profile |
| 26 |
spike_profile(st1, st2, st3) # multi-variate profile of 3 spike trains |
| 27 |
|
| 28 |
spike_trains = [st1, st2, st3, st4] # list of spike trains |
| 29 |
spike_profile(spike_trains) # profile of the list of spike trains |
| 30 |
spike_profile(spike_trains, indices=[0, 1]) # use only the spike trains |
| 31 |
# given by the indices |
| 32 |
|
| 33 |
The multivariate spike-distance profile is defined as the average of all |
| 34 |
pairs of spike-trains: |
| 35 |
|
| 36 |
.. math:: <S(t)> = \\frac{2}{N(N-1)} \\sum_{<i,j>} S^{i, j}`, |
| 37 |
|
| 38 |
where the sum goes over all pairs <i,j> |
| 39 |
|
| 40 |
:returns: The spike-distance profile :math:`S(t)` |
| 41 |
:rtype: :class:`.PieceWiseConstLin` |
| 42 |
""" |
| 43 |
if len(args) == 1: |
| 44 |
return spike_profile_multi(args[0], **kwargs) |
| 45 |
elif len(args) == 2: |
| 46 |
return spike_profile_bi(args[0], args[1], **kwargs) |
| 47 |
else: |
| 48 |
return spike_profile_multi(args, **kwargs) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
def spike_profile_bi(spike_train1, spike_train2, **kwargs): |
| 55 |
""" Specific function to compute a bivariate SPIKE-profile. This is a |
| 56 |
deprecated function and should not be called directly. Use |
| 57 |
:func:`.spike_profile` to compute SPIKE-profiles. |
| 58 |
|
| 59 |
:param spike_train1: First spike train. |
| 60 |
:type spike_train1: :class:`.SpikeTrain` |
| 61 |
:param spike_train2: Second spike train. |
| 62 |
:type spike_train2: :class:`.SpikeTrain` |
| 63 |
:returns: The spike-distance profile :math:`S(t)`. |
| 64 |
:rtype: :class:`.PieceWiseLinFunc` |
| 65 |
|
| 66 |
""" |
| 67 |
if kwargs.get('Reconcile', True): |
| 68 |
spike_train1, spike_train2 = reconcile_spike_trains_bi(spike_train1, spike_train2) |
| 69 |
MRTS, RI = resolve_keywords(**kwargs) |
| 70 |
|
| 71 |
if isinstance(MRTS, str): |
| 72 |
MRTS = default_thresh([spike_train1, spike_train2]) |
| 73 |
|
| 74 |
|
| 75 |
try: |
| 76 |
from .cython.cython_profiles import spike_profile_cython \ |
| 77 |
as spike_profile_impl |
| 78 |
except ImportError: |
| 79 |
pyspike.NoCythonWarn() |
| 80 |
|
| 81 |
|
| 82 |
from .cython.python_backend import spike_distance_python \ |
| 83 |
as spike_profile_impl |
| 84 |
|
| 85 |
times, y_starts, y_ends = spike_profile_impl( |
| 86 |
spike_train1.get_spikes_non_empty(), |
| 87 |
spike_train2.get_spikes_non_empty(), |
| 88 |
spike_train1.t_start, spike_train1.t_end, |
| 89 |
MRTS, RI) |
| 90 |
|
| 91 |
return PieceWiseLinFunc(times, y_starts, y_ends) |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
def spike_profile_multi(spike_trains, indices=None, **kwargs): |
| 98 |
""" Specific function to compute a multivariate SPIKE-profile. This is a |
| 99 |
deprecated function and should not be called directly. Use |
| 100 |
:func:`.spike_profile` to compute SPIKE-profiles. |
| 101 |
|
| 102 |
:param spike_trains: list of :class:`.SpikeTrain` |
| 103 |
:param indices: list of indices defining which spike trains to use, |
| 104 |
if None all given spike trains are used (default=None) |
| 105 |
:type indices: list or None |
| 106 |
:returns: The averaged spike profile :math:`<S>(t)` |
| 107 |
:rtype: :class:`.PieceWiseLinFunc` |
| 108 |
|
| 109 |
""" |
| 110 |
average_dist, M = _generic_profile_multi(spike_trains, spike_profile_bi, |
| 111 |
indices, **kwargs) |
| 112 |
average_dist.mul_scalar(1.0/M) |
| 113 |
return average_dist |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
def spike_distance(*args, **kwargs): |
| 120 |
""" Computes the SPIKE-distance :math:`D_S` of the given spike trains. The |
| 121 |
spike-distance is the integral over the spike distance profile |
| 122 |
:math:`D(t)`: |
| 123 |
|
| 124 |
.. math:: D_S = \\int_{T_0}^{T_1} S(t) dt. |
| 125 |
|
| 126 |
|
| 127 |
Valid call structures:: |
| 128 |
|
| 129 |
spike_distance(st1, st2) # returns the bi-variate distance |
| 130 |
spike_distance(st1, st2, st3) # multi-variate distance of 3 spike trains |
| 131 |
|
| 132 |
spike_trains = [st1, st2, st3, st4] # list of spike trains |
| 133 |
spike_distance(spike_trains) # distance of the list of spike trains |
| 134 |
spike_distance(spike_trains, indices=[0, 1]) # use only the spike trains |
| 135 |
# given by the indices |
| 136 |
|
| 137 |
In the multivariate case, the spike distance is given as the integral over |
| 138 |
the multivariate profile, that is the average profile of all spike train |
| 139 |
pairs: |
| 140 |
|
| 141 |
.. math:: D_S = \\int_0^T \\frac{2}{N(N-1)} \\sum_{<i,j>} |
| 142 |
S^{i, j} dt |
| 143 |
|
| 144 |
:returns: The spike-distance :math:`D_S`. |
| 145 |
:rtype: double |
| 146 |
""" |
| 147 |
|
| 148 |
if len(args) == 1: |
| 149 |
return spike_distance_multi(args[0], **kwargs) |
| 150 |
elif len(args) == 2: |
| 151 |
return spike_distance_bi(args[0], args[1], **kwargs) |
| 152 |
else: |
| 153 |
return spike_distance_multi(args, **kwargs) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
def spike_distance_bi(spike_train1, spike_train2, interval=None, **kwargs): |
| 160 |
""" Specific function to compute a bivariate SPIKE-distance. This is a |
| 161 |
deprecated function and should not be called directly. Use |
| 162 |
:func:`.spike_distance` to compute SPIKE-distances. |
| 163 |
|
| 164 |
:param spike_train1: First spike train. |
| 165 |
:type spike_train1: :class:`.SpikeTrain` |
| 166 |
:param spike_train2: Second spike train. |
| 167 |
:type spike_train2: :class:`.SpikeTrain` |
| 168 |
:param interval: averaging interval given as a pair of floats (T0, T1), |
| 169 |
if None the average over the whole function is computed. |
| 170 |
:type interval: Pair of floats or None. |
| 171 |
:returns: The spike-distance. |
| 172 |
:rtype: double |
| 173 |
|
| 174 |
""" |
| 175 |
if kwargs.get('Reconcile', True): |
| 176 |
spike_train1, spike_train2 = reconcile_spike_trains_bi(spike_train1, spike_train2) |
| 177 |
kwargs['Reconcile'] = False |
| 178 |
MRTS, RI = resolve_keywords(**kwargs) |
| 179 |
if isinstance(MRTS, str): |
| 180 |
MRTS = default_thresh([spike_train1, spike_train2]) |
| 181 |
|
| 182 |
if interval is None: |
| 183 |
|
| 184 |
|
| 185 |
try: |
| 186 |
from .cython.cython_distances import spike_distance_cython \ |
| 187 |
as spike_distance_impl |
| 188 |
return spike_distance_impl(spike_train1.get_spikes_non_empty(), |
| 189 |
spike_train2.get_spikes_non_empty(), |
| 190 |
spike_train1.t_start, |
| 191 |
spike_train1.t_end, |
| 192 |
MRTS, RI) |
| 193 |
except ImportError: |
| 194 |
|
| 195 |
return spike_profile_bi(spike_train1, spike_train2, |
| 196 |
**kwargs).avrg(interval) |
| 197 |
else: |
| 198 |
|
| 199 |
return spike_profile_bi(spike_train1, spike_train2, |
| 200 |
**kwargs).avrg(interval) |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
def spike_distance_multi(spike_trains, indices=None, interval=None, **kwargs): |
| 207 |
""" Specific function to compute a multivariate SPIKE-distance. This is a |
| 208 |
deprecated function and should not be called directly. Use |
| 209 |
:func:`.spike_distance` to compute SPIKE-distances. |
| 210 |
|
| 211 |
:param spike_trains: list of :class:`.SpikeTrain` |
| 212 |
:param indices: list of indices defining which spike trains to use, |
| 213 |
if None all given spike trains are used (default=None) |
| 214 |
:type indices: list or None |
| 215 |
:param interval: averaging interval given as a pair of floats, if None |
| 216 |
the average over the whole function is computed. |
| 217 |
:type interval: Pair of floats or None. |
| 218 |
:returns: The averaged multi-variate spike distance :math:`D_S`. |
| 219 |
:rtype: double |
| 220 |
""" |
| 221 |
return _generic_distance_multi(spike_trains, spike_distance_bi, indices, |
| 222 |
interval, **kwargs) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
def spike_distance_matrix(spike_trains, indices=None, interval=None, **kwargs): |
| 229 |
""" Computes the time averaged spike-distance of all pairs of spike-trains. |
| 230 |
|
| 231 |
:param spike_trains: list of :class:`.SpikeTrain` |
| 232 |
:param indices: list of indices defining which spike trains to use, |
| 233 |
if None all given spike trains are used (default=None) |
| 234 |
:type indices: list or None |
| 235 |
:param interval: averaging interval given as a pair of floats, if None |
| 236 |
the average over the whole function is computed. |
| 237 |
:type interval: Pair of floats or None. |
| 238 |
:returns: 2D array with the pair wise time average spike distances |
| 239 |
:math:`D_S^{ij}` |
| 240 |
:rtype: np.array |
| 241 |
""" |
| 242 |
return _generic_distance_matrix(spike_trains, spike_distance_bi, |
| 243 |
indices, interval, **kwargs) |