Debian Codegraph @

pyspike/isi_distance.py

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