| 1 |
""" |
| 2 |
|
| 3 |
Generic functions to compute multi-variate profiles and distance matrices. |
| 4 |
|
| 5 |
Copyright 2015, Mario Mulansky <[email protected]> |
| 6 |
|
| 7 |
Distributed under the BSD License |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import division |
| 11 |
from pyspike.isi_lengths import default_thresh |
| 12 |
from pyspike.spikes import reconcile_spike_trains, reconcile_spike_trains_bi |
| 13 |
import numpy as np |
| 14 |
|
| 15 |
def resolve_keywords(**kwargs): |
| 16 |
""" resolve keywords |
| 17 |
In: kwargs - dictionary of keywords |
| 18 |
out: MRTS - Minimum Relevant Time Scale, default 0. |
| 19 |
RI - Rate Independent Adaptive distance, default False |
| 20 |
""" |
| 21 |
if 'MRTS' in kwargs: |
| 22 |
MRTS = kwargs['MRTS'] |
| 23 |
else: |
| 24 |
MRTS = 0. |
| 25 |
if 'RI' in kwargs: |
| 26 |
RI = kwargs['RI'] |
| 27 |
else: |
| 28 |
RI = False |
| 29 |
return MRTS, RI |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
def _generic_profile_multi(spike_trains, pair_distance_func, indices=None, **kwargs): |
| 36 |
""" Internal implementation detail, don't call this function directly, |
| 37 |
use isi_profile_multi or spike_profile_multi instead. |
| 38 |
|
| 39 |
Computes the multi-variate distance for a set of spike-trains using the |
| 40 |
pair_dist_func to compute pair-wise distances. That is it computes the |
| 41 |
average distance of all pairs of spike-trains: |
| 42 |
:math:`S(t) = 2/((N(N-1)) sum_{<i,j>} S_{i,j}`, |
| 43 |
where the sum goes over all pairs <i,j>. |
| 44 |
Args: |
| 45 |
- spike_trains: list of spike trains |
| 46 |
- pair_distance_func: function computing the distance of two spike trains |
| 47 |
- indices: list of indices defining which spike trains to use, |
| 48 |
if None all given spike trains are used (default=None) |
| 49 |
Returns: |
| 50 |
- The averaged multi-variate distance of all pairs |
| 51 |
""" |
| 52 |
if kwargs.get('Reconcile', True): |
| 53 |
spike_trains = reconcile_spike_trains(spike_trains) |
| 54 |
kwargs['Reconcile'] = False |
| 55 |
|
| 56 |
MRTS, RI = resolve_keywords(**kwargs) |
| 57 |
if isinstance(MRTS, str): |
| 58 |
kwargs['MRTS'] = default_thresh(spike_trains) |
| 59 |
|
| 60 |
def divide_and_conquer(pairs1, pairs2): |
| 61 |
""" recursive calls by splitting the two lists in half. |
| 62 |
""" |
| 63 |
L1 = len(pairs1) |
| 64 |
if L1 > 1: |
| 65 |
dist_prof1 = divide_and_conquer(pairs1[:L1//2], |
| 66 |
pairs1[L1//2:]) |
| 67 |
else: |
| 68 |
dist_prof1 = pair_distance_func(spike_trains[pairs1[0][0]], |
| 69 |
spike_trains[pairs1[0][1]], |
| 70 |
**kwargs) |
| 71 |
L2 = len(pairs2) |
| 72 |
if L2 > 1: |
| 73 |
dist_prof2 = divide_and_conquer(pairs2[:L2//2], |
| 74 |
pairs2[L2//2:]) |
| 75 |
else: |
| 76 |
dist_prof2 = pair_distance_func(spike_trains[pairs2[0][0]], |
| 77 |
spike_trains[pairs2[0][1]], |
| 78 |
**kwargs) |
| 79 |
dist_prof1.add(dist_prof2) |
| 80 |
return dist_prof1 |
| 81 |
|
| 82 |
if indices is None: |
| 83 |
indices = np.arange(len(spike_trains)) |
| 84 |
indices = np.array(indices) |
| 85 |
|
| 86 |
assert (indices < len(spike_trains)).all() and (indices >= 0).all(), \ |
| 87 |
"Invalid index list." |
| 88 |
|
| 89 |
pairs = [(indices[i], j) for i in range(len(indices)) |
| 90 |
for j in indices[i+1:]] |
| 91 |
|
| 92 |
L = len(pairs) |
| 93 |
if L > 1: |
| 94 |
|
| 95 |
avrg_dist = divide_and_conquer(pairs[:len(pairs)//2], |
| 96 |
pairs[len(pairs)//2:]) |
| 97 |
else: |
| 98 |
avrg_dist = pair_distance_func(spike_trains[pairs[0][0]], |
| 99 |
spike_trains[pairs[0][1]], |
| 100 |
**kwargs) |
| 101 |
|
| 102 |
return avrg_dist, L |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
def _generic_distance_multi(spike_trains, pair_distance_func, |
| 109 |
indices=None, interval=None, **kwargs): |
| 110 |
""" Internal implementation detail, don't call this function directly, |
| 111 |
use isi_distance_multi or spike_distance_multi instead. |
| 112 |
|
| 113 |
Computes the multi-variate distance for a set of spike-trains using the |
| 114 |
pair_dist_func to compute pair-wise distances. That is it computes the |
| 115 |
average distance of all pairs of spike-trains: |
| 116 |
:math:`S(t) = 2/((N(N-1)) sum_{<i,j>} D_{i,j}`, |
| 117 |
where the sum goes over all pairs <i,j>. |
| 118 |
Args: |
| 119 |
- spike_trains: list of spike trains |
| 120 |
- pair_distance_func: function computing the distance of two spike trains |
| 121 |
- indices: list of indices defining which spike trains to use, |
| 122 |
if None all given spike trains are used (default=None) |
| 123 |
Returns: |
| 124 |
- The averaged multi-variate distance of all pairs |
| 125 |
""" |
| 126 |
if kwargs.get('Reconcile', True): |
| 127 |
spike_trains = reconcile_spike_trains(spike_trains) |
| 128 |
kwargs['Reconcile'] = False |
| 129 |
|
| 130 |
MRTS, RI = resolve_keywords(**kwargs) |
| 131 |
if isinstance(MRTS, str): |
| 132 |
kwargs['MRTS'] = default_thresh(spike_trains) |
| 133 |
|
| 134 |
if indices is None: |
| 135 |
indices = np.arange(len(spike_trains)) |
| 136 |
indices = np.array(indices) |
| 137 |
|
| 138 |
assert (indices < len(spike_trains)).all() and (indices >= 0).all(), \ |
| 139 |
"Invalid index list." |
| 140 |
|
| 141 |
pairs = [(indices[i], j) for i in range(len(indices)) |
| 142 |
for j in indices[i+1:]] |
| 143 |
|
| 144 |
avrg_dist = 0.0 |
| 145 |
for (i, j) in pairs: |
| 146 |
one_dist = pair_distance_func(spike_trains[i], spike_trains[j], |
| 147 |
interval, **kwargs) |
| 148 |
avrg_dist += one_dist |
| 149 |
|
| 150 |
return avrg_dist/len(pairs) |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
def _generic_distance_matrix(spike_trains, dist_function, |
| 157 |
indices=None, interval=None, **kwargs): |
| 158 |
""" Internal implementation detail. Don't use this function directly. |
| 159 |
Instead use isi_distance_matrix or spike_distance_matrix. |
| 160 |
Computes the time averaged distance of all pairs of spike-trains. |
| 161 |
Args: |
| 162 |
- spike_trains: list of spike trains |
| 163 |
- indices: list of indices defining which spike-trains to use |
| 164 |
if None all given spike-trains are used (default=None) |
| 165 |
Return: |
| 166 |
- a 2D array of size len(indices)*len(indices) containing the average |
| 167 |
pair-wise distance |
| 168 |
""" |
| 169 |
if kwargs.get('Reconcile', True): |
| 170 |
spike_trains = reconcile_spike_trains(spike_trains) |
| 171 |
kwargs['Reconcile'] = False |
| 172 |
|
| 173 |
MRTS, RI = resolve_keywords(**kwargs) |
| 174 |
if isinstance(MRTS, str): |
| 175 |
kwargs['MRTS'] = default_thresh(spike_trains) |
| 176 |
|
| 177 |
if indices is None: |
| 178 |
indices = np.arange(len(spike_trains)) |
| 179 |
indices = np.array(indices) |
| 180 |
|
| 181 |
assert (indices < len(spike_trains)).all() and (indices >= 0).all(), \ |
| 182 |
"Invalid index list." |
| 183 |
|
| 184 |
pairs = [(i, j) for i in range(len(indices)) |
| 185 |
for j in range(i+1, len(indices))] |
| 186 |
|
| 187 |
distance_matrix = np.zeros((len(indices), len(indices))) |
| 188 |
for i, j in pairs: |
| 189 |
d = dist_function(spike_trains[indices[i]], spike_trains[indices[j]], |
| 190 |
interval, **kwargs) |
| 191 |
distance_matrix[i, j] = d |
| 192 |
distance_matrix[j, i] = d |
| 193 |
return distance_matrix |