summaryrefslogtreecommitdiff
path: root/pyspike/distances.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyspike/distances.py')
-rw-r--r--pyspike/distances.py190
1 files changed, 112 insertions, 78 deletions
diff --git a/pyspike/distances.py b/pyspike/distances.py
index 76dcd83..7a85471 100644
--- a/pyspike/distances.py
+++ b/pyspike/distances.py
@@ -63,6 +63,116 @@ def isi_distance(spikes1, spikes2):
############################################################
+# spike_distance
+############################################################
+def spike_distance(spikes1, spikes2):
+ """ Computes the instantaneous spike-distance S_spike (t) of the two given
+ spike trains. The spike trains are expected to have auxiliary spikes at the
+ beginning and end of the interval. Use the function add_auxiliary_spikes to
+ add those spikes to the spike train.
+ Args:
+ - spikes1, spikes2: ordered arrays of spike times with auxiliary spikes.
+ Returns:
+ - PieceWiseLinFunc describing the spike-distance.
+ """
+ # check for auxiliary spikes - first and last spikes should be identical
+ assert spikes1[0]==spikes2[0], \
+ "Given spike trains seems not to have auxiliary spikes!"
+ assert spikes1[-1]==spikes2[-1], \
+ "Given spike trains seems not to have auxiliary spikes!"
+
+ # compile and load cython implementation
+ import pyximport
+ pyximport.install(setup_args={'include_dirs': [np.get_include()]})
+ from cython_distance import spike_distance_cython
+
+ times, y_starts, y_ends = spike_distance_cython(spikes1, spikes2)
+
+ return PieceWiseLinFunc(times, y_starts, y_ends)
+
+
+############################################################
+# multi_distance
+############################################################
+def multi_distance(spike_trains, pair_distance_func, indices=None):
+ """ Internal implementation detail, use isi_distance_multi or
+ spike_distance_multi.
+
+ Computes the multi-variate distance for a set of spike-trains using the
+ pair_dist_func to compute pair-wise distances. That is it computes the
+ average distance of all pairs of spike-trains:
+ S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j},
+ where the sum goes over all pairs <i,j>.
+ Args:
+ - spike_trains: list of spike trains
+ - pair_distance_func: function computing the distance of two spike trains
+ - indices: list of indices defining which spike trains to use,
+ if None all given spike trains are used (default=None)
+ Returns:
+ - The averaged multi-variate distance of all pairs
+ """
+ if indices==None:
+ indices = np.arange(len(spike_trains))
+ indices = np.array(indices)
+ # check validity of indices
+ assert (indices < len(spike_trains)).all() and (indices >= 0).all(), \
+ "Invalid index list."
+ # generate a list of possible index pairs
+ pairs = [(i,j) for i in indices for j in indices[i+1:]]
+ # start with first pair
+ (i,j) = pairs[0]
+ average_dist = pair_distance_func(spike_trains[i], spike_trains[j])
+ for (i,j) in pairs[1:]:
+ current_dist = pair_distance_func(spike_trains[i], spike_trains[j])
+ average_dist.add(current_dist) # add to the average
+ average_dist.mul_scalar(1.0/len(pairs)) # normalize
+ return average_dist
+
+
+############################################################
+# isi_distance_multi
+############################################################
+def isi_distance_multi(spike_trains, indices=None):
+ """ computes the multi-variate isi-distance for a set of spike-trains. That
+ is the average isi-distance of all pairs of spike-trains:
+ S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j},
+ where the sum goes over all pairs <i,j>
+ Args:
+ - spike_trains: list of spike trains
+ - indices: list of indices defining which spike trains to use,
+ if None all given spike trains are used (default=None)
+ Returns:
+ - A PieceWiseConstFunc representing the averaged isi distance S
+ """
+ return multi_distance(spike_trains, isi_distance, indices)
+
+
+############################################################
+# spike_distance_multi
+############################################################
+def spike_distance_multi(spike_trains, indices=None):
+ """ computes the multi-variate spike-distance for a set of spike-trains.
+ That is the average spike-distance of all pairs of spike-trains:
+ S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j},
+ where the sum goes over all pairs <i,j>
+ Args:
+ - spike_trains: list of spike trains
+ - indices: list of indices defining which spike trains to use,
+ if None all given spike trains are used (default=None)
+ Returns:
+ - A PieceWiseLinFunc representing the averaged spike distance S
+ """
+ return multi_distance(spike_trains, spike_distance, indices)
+
+
+############################################################
+############################################################
+# VANILLA PYTHON IMPLEMENTATIONS OF ISI AND SPIKE DISTANCE
+############################################################
+############################################################
+
+
+############################################################
# isi_distance_python
############################################################
def isi_distance_python(s1, s2):
@@ -134,9 +244,9 @@ def get_min_dist(spike_time, spike_train, start_index=0):
############################################################
-# spike_distance
+# spike_distance_python
############################################################
-def spike_distance(spikes1, spikes2):
+def spike_distance_python(spikes1, spikes2):
""" Computes the instantaneous spike-distance S_spike (t) of the two given
spike trains. The spike trains are expected to have auxiliary spikes at the
beginning and end of the interval. Use the function add_auxiliary_spikes to
@@ -235,79 +345,3 @@ def spike_distance(spikes1, spikes2):
# could be less than original length due to equal spike times
return PieceWiseLinFunc(spike_events[:index+1],
y_starts[:index], y_ends[:index])
-
-
-
-
-############################################################
-# multi_distance
-############################################################
-def multi_distance(spike_trains, pair_distance_func, indices=None):
- """ Internal implementation detail, use isi_distance_multi or
- spike_distance_multi.
-
- Computes the multi-variate distance for a set of spike-trains using the
- pair_dist_func to compute pair-wise distances. That is it computes the
- average distance of all pairs of spike-trains:
- S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j},
- where the sum goes over all pairs <i,j>.
- Args:
- - spike_trains: list of spike trains
- - pair_distance_func: function computing the distance of two spike trains
- - indices: list of indices defining which spike trains to use,
- if None all given spike trains are used (default=None)
- Returns:
- - The averaged multi-variate distance of all pairs
- """
- if indices==None:
- indices = np.arange(len(spike_trains))
- indices = np.array(indices)
- # check validity of indices
- assert (indices < len(spike_trains)).all() and (indices >= 0).all(), \
- "Invalid index list."
- # generate a list of possible index pairs
- pairs = [(i,j) for i in indices for j in indices[i+1:]]
- # start with first pair
- (i,j) = pairs[0]
- average_dist = pair_distance_func(spike_trains[i], spike_trains[j])
- for (i,j) in pairs[1:]:
- current_dist = pair_distance_func(spike_trains[i], spike_trains[j])
- average_dist.add(current_dist) # add to the average
- average_dist.mul_scalar(1.0/len(pairs)) # normalize
- return average_dist
-
-
-############################################################
-# isi_distance_multi
-############################################################
-def isi_distance_multi(spike_trains, indices=None):
- """ computes the multi-variate isi-distance for a set of spike-trains. That
- is the average isi-distance of all pairs of spike-trains:
- S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j},
- where the sum goes over all pairs <i,j>
- Args:
- - spike_trains: list of spike trains
- - indices: list of indices defining which spike trains to use,
- if None all given spike trains are used (default=None)
- Returns:
- - A PieceWiseConstFunc representing the averaged isi distance S
- """
- return multi_distance(spike_trains, isi_distance, indices)
-
-
-############################################################
-# spike_distance_multi
-############################################################
-def spike_distance_multi(spike_trains, indices=None):
- """ computes the multi-variate spike-distance for a set of spike-trains.
- That is the average spike-distance of all pairs of spike-trains:
- S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j},
- where the sum goes over all pairs <i,j>
- Args:
- - spike_trains: list of spike trains
- - indices: list of indices defining which spike trains to use,
- if None all given spike trains are used (default=None)
- Returns:
- - A PieceWiseLinFunc representing the averaged spike distance S
- """
- return multi_distance(spike_trains, spike_distance, indices)