# pyspike package¶

## Classes¶

### SpikeTrain¶

class pyspike.SpikeTrain.SpikeTrain(spike_times, edges, is_sorted=True)[source]

Bases: object

Class representing spike trains for the PySpike Module.

__init__(spike_times, edges, is_sorted=True)[source]

Constructs the SpikeTrain.

Parameters: spike_times – ordered array of spike times. edges – The edges of the spike train. Given as a pair of floats (T0, T1) or a single float T1, where then T0=0 is assumed. is_sorted – If False, the spike times will sorted by np.sort.
copy()[source]

Returns a copy of this spike train. Use this function if you want to create a real (deep) copy of this spike train. Simple assignment t2 = t1 does not create a copy of the spike train data, but a reference as numpy.array is used for storing the data.

Returns: SpikeTrain copy of this spike train.
get_spikes_non_empty()[source]

Returns the spikes of this spike train with auxiliary spikes in case of empty spike trains.

sort()[source]

Sorts the spike times of this spike train using np.sort

### PieceWiseConstFunc¶

class pyspike.PieceWiseConstFunc.PieceWiseConstFunc(x, y)[source]

Bases: object

A class representing a piece-wise constant function.

__init__(x, y)[source]

Constructs the piece-wise const function.

Parameters: x – array of length N+1 defining the edges of the intervals of the pwc function. y – array of length N defining the function values at the intervals.
add(f)[source]

Adds another PieceWiseConst function to this function. Note: only functions defined on the same interval can be summed.

Parameters: f – PieceWiseConstFunc function to be added. None
almost_equal(other, decimal=14)[source]

Checks if the function is equal to another function up to decimal precision.

Parameters: other – another PieceWiseConstFunc True if the two functions are equal up to decimal decimals, False otherwise bool
avrg(interval=None)[source]

Computes the average of the piece-wise const function: $$a = 1/T \int_0^T f(x) dx$$ where T is the length of the interval.

Parameters: interval (Pair, sequence of pairs, or None.) – averaging interval given as a pair of floats, a sequence of pairs for averaging multiple intervals, or None, if None the average over the whole function is computed. the average a. float
copy()[source]

Returns a copy of itself

Return type: PieceWiseConstFunc
get_plottable_data()[source]

Returns two arrays containing x- and y-coordinates for immeditate plotting of the piece-wise function.

Returns: (x_plot, y_plot) containing plottable data pair of np.array

Example:

x, y = f.get_plottable_data()
plt.plot(x, y, '-o', label="Piece-wise const function")

integral(interval=None)[source]

Returns the integral over the given interval.

Parameters: interval (Pair of floats or None.) – integration interval given as a pair of floats, if None the integral over the whole function is computed. the integral float
mul_scalar(fac)[source]

Multiplies the function with a scalar value

Parameters: fac (double) – Value to multiply None

### PieceWiseLinFunc¶

class pyspike.PieceWiseLinFunc.PieceWiseLinFunc(x, y1, y2)[source]

A class representing a piece-wise linear function.

__init__(x, y1, y2)[source]

Constructs the piece-wise linear function.

Parameters: x – array of length N+1 defining the edges of the intervals of the pwc function. y1 – array of length N defining the function values at the left of the intervals. y2 – array of length N defining the function values at the right of the intervals.
add(f)[source]

Adds another PieceWiseLin function to this function. Note: only functions defined on the same interval can be summed.

Parameters: f – PieceWiseLinFunc function to be added. None
almost_equal(other, decimal=14)[source]

Checks if the function is equal to another function up to decimal precision.

Parameters: other – another PieceWiseLinFunc True if the two functions are equal up to decimal decimals, False otherwise bool
avrg(interval=None)[source]

Computes the average of the piece-wise linear function: $$a = 1/T \int_0^T f(x) dx$$ where T is the interval length.

Parameters: interval (Pair, sequence of pairs, or None.) – averaging interval given as a pair of floats, a sequence of pairs for averaging multiple intervals, or None, if None the average over the whole function is computed. the average a. float
copy()[source]

Returns a copy of itself

Return type: PieceWiseLinFunc
get_plottable_data()[source]

Returns two arrays containing x- and y-coordinates for immeditate plotting of the piece-wise function.

Returns: (x_plot, y_plot) containing plottable data pair of np.array

Example:

x, y = f.get_plottable_data()
plt.plot(x, y, '-o', label="Piece-wise const function")

integral(interval=None)[source]

Returns the integral over the given interval.

Parameters: interval (Pair of floats or None.) – integration interval given as a pair of floats, if None the integral over the whole function is computed. the integral float
mul_scalar(fac)[source]

Multiplies the function with a scalar value

Parameters: fac (double) – Value to multiply None

### DiscreteFunc¶

class pyspike.DiscreteFunc.DiscreteFunc(x, y, multiplicity)[source]

Bases: object

A class representing values defined on a discrete set of points.

__init__(x, y, multiplicity)[source]

Constructs the discrete function.

Parameters: x – array of length N defining the points at which the values are defined. y – array of length N degining the values at the points x. multiplicity – array of length N defining the multiplicity of the values.
add(f)[source]

Adds another DiscreteFunc function to this function. Note: only functions defined on the same interval can be summed.

Parameters: f – DiscreteFunc function to be added. None
almost_equal(other, decimal=14)[source]

Checks if the function is equal to another function up to decimal precision.

Parameters: other – another DiscreteFunc True if the two functions are equal up to decimal decimals, False otherwise bool
avrg(interval=None, normalize=True)[source]

Computes the average of the interval sequence: $$a = 1/N \sum f_n$$ where N is the number of intervals.

Parameters: interval (Pair, sequence of pairs, or None.) – averaging interval given as a pair of floats, a sequence of pairs for averaging multiple intervals, or None, if None the average over the whole function is computed. the average a. float
copy()[source]

Returns a copy of itself

Return type: DiscreteFunc
get_plottable_data(averaging_window_size=0)[source]

Returns two arrays containing x- and y-coordinates for plotting the interval sequence. The optional parameter averaging_window_size determines the size of an averaging window to smoothen the profile. If this value is 0, no averaging is performed.

Parameters: averaging_window_size – size of the averaging window, default=0. (x_plot, y_plot) containing plottable data pair of np.array

Example:

x, y = f.get_plottable_data()
plt.plot(x, y, '-o', label="Discrete function")

integral(interval=None)[source]

Returns the integral over the given interval. For the discrete function, this amounts to two values: the sum over all values and the sum over all multiplicities.

Parameters: interval (Pair, sequence of pairs, or None.) – integration interval given as a pair of floats, or a sequence of pairs in case of multiple intervals, if None the integral over the whole function is computed. the summed values and the summed multiplicity pair of float
mul_scalar(fac)[source]

Multiplies the function with a scalar value

Parameters: fac (double) – Value to multiply None
pyspike.DiscreteFunc.average_profile(profiles)[source]

Computes the average profile from the given ISI- or SPIKE-profiles.

Parameters: profiles – list of PieceWiseConstFunc or PieceWiseLinFunc representing ISI- or SPIKE-profiles to be averaged. the averages profile  or . PieceWiseConstFunc or PieceWiseLinFunc

## Functions¶

### ISI-distance¶

pyspike.isi_distance.isi_distance(*args, **kwargs)[source]

Computes the ISI-distance $$D_I$$ of the given spike trains. The isi-distance is the integral over the isi distance profile $$I(t)$$:

$D_I = \int_{T_0}^{T_1} I(t) dt.$

In the multivariate case it is the integral over the multivariate ISI-profile, i.e. the average profile over all spike train pairs:

$\begin{split}D_I = \int_0^T \frac{2}{N(N-1)} \sum_{<i,j>} I^{i,j},\end{split}$

where the sum goes over all pairs <i,j>

Valid call structures:

isi_distance(st1, st2)  # returns the bi-variate distance
isi_distance(st1, st2, st3)  # multi-variate distance of 3 spike trains

spike_trains = [st1, st2, st3, st4]  # list of spike trains
isi_distance(spike_trains)  # distance of the list of spike trains
isi_distance(spike_trains, indices=[0, 1])  # use only the spike trains
# given by the indices

Returns: The isi-distance $$D_I$$. double
pyspike.isi_distance.isi_distance_bi(spike_train1, spike_train2, interval=None)[source]

Specific function to compute the bivariate ISI-distance. This is a deprecated function and should not be called directly. Use isi_distance() to compute ISI-distances.

Parameters: spike_train1 (SpikeTrain) – First spike train. spike_train2 (SpikeTrain) – Second spike train. interval (Pair of floats or None.) – averaging interval given as a pair of floats (T0, T1), if None the average over the whole function is computed. The isi-distance $$D_I$$. double
pyspike.isi_distance.isi_distance_matrix(spike_trains, indices=None, interval=None)[source]

Computes the time averaged isi-distance of all pairs of spike-trains.

Parameters: spike_trains – list of SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) interval (Pair of floats or None.) – averaging interval given as a pair of floats, if None the average over the whole function is computed. 2D array with the pair wise time average isi distances $$D_{I}^{ij}$$ np.array
pyspike.isi_distance.isi_distance_multi(spike_trains, indices=None, interval=None)[source]

Specific function to compute the multivariate ISI-distance. This is a deprecfated function and should not be called directly. Use isi_distance() to compute ISI-distances.

Parameters: spike_trains – list of SpikeTrain indices – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) interval (Pair of floats or None.) – averaging interval given as a pair of floats, if None the average over the whole function is computed. The time-averaged multivariate ISI distance $$D_I$$ double
pyspike.isi_distance.isi_profile(*args, **kwargs)[source]

Computes the isi-distance profile $$I(t)$$ of the given spike trains. Returns the profile as a PieceWiseConstFunc object. The ISI-values are defined positive $$I(t)>=0$$.

Valid call structures:

isi_profile(st1, st2)       # returns the bi-variate profile
isi_profile(st1, st2, st3)  # multi-variate profile of 3 spike trains

spike_trains = [st1, st2, st3, st4]  # list of spike trains
isi_profile(spike_trains)   # profile of the list of spike trains
isi_profile(spike_trains, indices=[0, 1])  # use only the spike trains
# given by the indices


The multivariate ISI distance profile for a set of spike trains is defined as the average ISI-profile of all pairs of spike-trains:

$\begin{split}<I(t)> = \frac{2}{N(N-1)} \sum_{<i,j>} I^{i,j},\end{split}$

where the sum goes over all pairs <i,j>

Returns: The isi-distance profile $$I(t)$$ PieceWiseConstFunc
pyspike.isi_distance.isi_profile_bi(spike_train1, spike_train2)[source]

Specific function to compute a bivariate ISI-profile. This is a deprecated function and should not be called directly. Use isi_profile() to compute ISI-profiles.

Parameters: spike_train1 (SpikeTrain) – First spike train. spike_train2 (SpikeTrain) – Second spike train. The isi-distance profile $$I(t)$$ PieceWiseConstFunc
pyspike.isi_distance.isi_profile_multi(spike_trains, indices=None)[source]

Specific function to compute the multivariate ISI-profile for a set of spike trains. This is a deprecated function and should not be called directly. Use isi_profile() to compute ISI-profiles.

Parameters: spike_trains – list of SpikeTrain indices – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) The averaged isi profile  PieceWiseConstFunc

### SPIKE-distance¶

pyspike.spike_distance.spike_distance(*args, **kwargs)[source]

Computes the SPIKE-distance $$D_S$$ of the given spike trains. The spike-distance is the integral over the spike distance profile $$D(t)$$:

$D_S = \int_{T_0}^{T_1} S(t) dt.$

Valid call structures:

spike_distance(st1, st2)  # returns the bi-variate distance
spike_distance(st1, st2, st3)  # multi-variate distance of 3 spike trains

spike_trains = [st1, st2, st3, st4]  # list of spike trains
spike_distance(spike_trains)  # distance of the list of spike trains
spike_distance(spike_trains, indices=[0, 1])  # use only the spike trains
# given by the indices


In the multivariate case, the spike distance is given as the integral over the multivariate profile, that is the average profile of all spike train pairs:

$\begin{split}D_S = \int_0^T \frac{2}{N(N-1)} \sum_{<i,j>} S^{i, j} dt\end{split}$
Returns: The spike-distance $$D_S$$. double
pyspike.spike_distance.spike_distance_bi(spike_train1, spike_train2, interval=None)[source]

Specific function to compute a bivariate SPIKE-distance. This is a deprecated function and should not be called directly. Use spike_distance() to compute SPIKE-distances.

Parameters: spike_train1 (SpikeTrain) – First spike train. spike_train2 (SpikeTrain) – Second spike train. interval (Pair of floats or None.) – averaging interval given as a pair of floats (T0, T1), if None the average over the whole function is computed. The spike-distance. double
pyspike.spike_distance.spike_distance_matrix(spike_trains, indices=None, interval=None)[source]

Computes the time averaged spike-distance of all pairs of spike-trains.

Parameters: spike_trains – list of SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) interval (Pair of floats or None.) – averaging interval given as a pair of floats, if None the average over the whole function is computed. 2D array with the pair wise time average spike distances $$D_S^{ij}$$ np.array
pyspike.spike_distance.spike_distance_multi(spike_trains, indices=None, interval=None)[source]

Specific function to compute a multivariate SPIKE-distance. This is a deprecated function and should not be called directly. Use spike_distance() to compute SPIKE-distances.

Parameters: spike_trains – list of SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) interval (Pair of floats or None.) – averaging interval given as a pair of floats, if None the average over the whole function is computed. The averaged multi-variate spike distance $$D_S$$. double
pyspike.spike_distance.spike_profile(*args, **kwargs)[source]

Computes the spike-distance profile $$S(t)$$ of the given spike trains. Returns the profile as a PieceWiseConstLin object. The SPIKE-values are defined positive $$S(t)>=0$$.

Valid call structures:

spike_profile(st1, st2)  # returns the bi-variate profile
spike_profile(st1, st2, st3)  # multi-variate profile of 3 spike trains

spike_trains = [st1, st2, st3, st4]  # list of spike trains
spike_profile(spike_trains)  # profile of the list of spike trains
spike_profile(spike_trains, indices=[0, 1])  # use only the spike trains
# given by the indices


The multivariate spike-distance profile is defined as the average of all pairs of spike-trains:

$\begin{split}<S(t)> = \frac{2}{N(N-1)} \sum_{<i,j>} S^{i, j},\end{split}$

where the sum goes over all pairs <i,j>

Returns: The spike-distance profile $$S(t)$$ PieceWiseConstLin
pyspike.spike_distance.spike_profile_bi(spike_train1, spike_train2)[source]

Specific function to compute a bivariate SPIKE-profile. This is a deprecated function and should not be called directly. Use spike_profile() to compute SPIKE-profiles.

Parameters: spike_train1 (SpikeTrain) – First spike train. spike_train2 (SpikeTrain) – Second spike train. The spike-distance profile $$S(t)$$. PieceWiseLinFunc
pyspike.spike_distance.spike_profile_multi(spike_trains, indices=None)[source]

Specific function to compute a multivariate SPIKE-profile. This is a deprecated function and should not be called directly. Use spike_profile() to compute SPIKE-profiles.

Parameters: spike_trains – list of SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) The averaged spike profile $$(t)$$ PieceWiseLinFunc

### SPIKE-synchronization¶

pyspike.spike_sync.spike_sync(*args, **kwargs)[source]

Computes the spike synchronization value SYNC of the given spike trains. The spike synchronization value is the computed as the total number of coincidences divided by the total number of spikes:

$SYNC = \sum_n C_n / N.$

Valid call structures:

spike_sync(st1, st2)  # returns the bi-variate spike synchronization
spike_sync(st1, st2, st3)  # multi-variate result for 3 spike trains

spike_trains = [st1, st2, st3, st4]  # list of spike trains
spike_sync(spike_trains)  # spike-sync of the list of spike trains
spike_sync(spike_trains, indices=[0, 1])  # use only the spike trains
# given by the indices


The multivariate SPIKE-Sync is again defined as the overall ratio of all coincidence values divided by the total number of spikes.

Returns: The spike synchronization value. double
pyspike.spike_sync.spike_sync_bi(spike_train1, spike_train2, interval=None, max_tau=None)[source]

Specific function to compute a bivariate SPIKE-Sync value. This is a deprecated function and should not be called directly. Use spike_sync() to compute SPIKE-Sync values.

Parameters: spike_train1 (pyspike.SpikeTrain) – First spike train. spike_train2 (pyspike.SpikeTrain) – Second spike train. interval (Pair of floats or None.) – averaging interval given as a pair of floats (T0, T1), if None the average over the whole function is computed. max_tau – Maximum coincidence window size. If 0 or None, the coincidence window has no upper bound. The spike synchronization value. double
pyspike.spike_sync.spike_sync_matrix(spike_trains, indices=None, interval=None, max_tau=None)[source]

Computes the overall spike-synchronization value of all pairs of spike-trains.

Parameters: spike_trains – list of pyspike.SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) interval (Pair of floats or None.) – averaging interval given as a pair of floats, if None the average over the whole function is computed. max_tau – Maximum coincidence window size. If 0 or None, the coincidence window has no upper bound. 2D array with the pair wise time spike synchronization values $$SYNC_{ij}$$ np.array
pyspike.spike_sync.spike_sync_multi(spike_trains, indices=None, interval=None, max_tau=None)[source]

Specific function to compute a multivariate SPIKE-Sync value. This is a deprecated function and should not be called directly. Use spike_sync() to compute SPIKE-Sync values.

Parameters: spike_trains – list of pyspike.SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) interval (Pair of floats or None.) – averaging interval given as a pair of floats, if None the average over the whole function is computed. max_tau – Maximum coincidence window size. If 0 or None, the coincidence window has no upper bound. The multi-variate spike synchronization value SYNC. double
pyspike.spike_sync.spike_sync_profile(*args, **kwargs)[source]

Computes the spike-synchronization profile S_sync(t) of the given spike trains. Returns the profile as a DiscreteFunction object. In the bivariate case, he S_sync values are either 1 or 0, indicating the presence or absence of a coincidence. For multi-variate cases, each spike in the set of spike trains, the profile is defined as the number of coincidences divided by the number of spike trains pairs involving the spike train of containing this spike, which is the number of spike trains minus one (N-1).

Valid call structures:

spike_sync_profile(st1, st2)  # returns the bi-variate profile
spike_sync_profile(st1, st2, st3)  # multi-variate profile of 3 sts

sts = [st1, st2, st3, st4]  # list of spike trains
spike_sync_profile(sts)  # profile of the list of spike trains
spike_sync_profile(sts, indices=[0, 1])  # use only the spike trains
# given by the indices


In the multivariate case, the profile is defined as the number of coincidences for each spike in the set of spike trains divided by the number of spike trains pairs involving the spike train of containing this spike, which is the number of spike trains minus one (N-1).

Returns: The spike-sync profile $$S_{sync}(t)$$. pyspike.function.DiscreteFunction
pyspike.spike_sync.spike_sync_profile_bi(spike_train1, spike_train2, max_tau=None)[source]

Specific function to compute a bivariate SPIKE-Sync-profile. This is a deprecated function and should not be called directly. Use spike_sync_profile() to compute SPIKE-Sync-profiles.

Parameters: spike_train1 (pyspike.SpikeTrain) – First spike train. spike_train2 (pyspike.SpikeTrain) – Second spike train. max_tau – Maximum coincidence window size. If 0 or None, the coincidence window has no upper bound. The spike-sync profile $$S_{sync}(t)$$. pyspike.function.DiscreteFunction
pyspike.spike_sync.spike_sync_profile_multi(spike_trains, indices=None, max_tau=None)[source]

Specific function to compute a multivariate SPIKE-Sync-profile. This is a deprecated function and should not be called directly. Use spike_sync_profile() to compute SPIKE-Sync-profiles.

Parameters: spike_trains – list of pyspike.SpikeTrain indices (list or None) – list of indices defining which spike trains to use, if None all given spike trains are used (default=None) max_tau – Maximum coincidence window size. If 0 or None, the coincidence window has no upper bound. The multi-variate spike sync profile $$(t)$$ pyspike.function.DiscreteFunction

### PSTH¶

pyspike.psth.psth(spike_trains, bin_size)[source]

Computes the peri-stimulus time histogram of a set of SpikeTrain. The PSTH is simply the histogram of merged spike events. The bin_size defines the width of the histogram bins.

Parameters: spike_trains – list of SpikeTrain bin_size – width of the histogram bins. The PSTH as a PieceWiseConstFunc

### Helper functions¶

pyspike.spikes.generate_poisson_spikes(rate, interval)[source]

Generates a Poisson spike train with the given rate in the given time interval

Parameters: rate – The rate of the spike trains interval (pair of doubles or double) – A pair (T_start, T_end) of values representing the start and end time of the spike train measurement or a single value representing the end time, the T_start is then assuemd as 0. Auxiliary spikes will be added to the spike train at the beginning and end of this interval, if they are not yet present. Poisson spike train as a SpikeTrain
pyspike.spikes.load_spike_trains_from_txt(file_name, edges, separator=' ', comment='#', is_sorted=False, ignore_empty_lines=True)[source]

Loads a number of spike trains from a text file. Each line of the text file should contain one spike train as a sequence of spike times separated by separator. Empty lines as well as lines starting with comment are neglected. The edges represents the start and the end of the spike trains.

Parameters: file_name – The name of the text file. edges – A pair (T_start, T_end) of values representing the start and end time of the spike train measurement or a single value representing the end time, the T_start is then assuemd as 0. separator – The character used to seprate the values in the text file comment – Lines starting with this character are ignored. sort – If true, the spike times are order via np.sort, default=True list of SpikeTrain
pyspike.spikes.merge_spike_trains(spike_trains)[source]

Merges a number of spike trains into a single spike train.

Parameters: spike_trains – list of SpikeTrain spike train with the merged spike times
pyspike.spikes.spike_train_from_string(s, edges, sep=' ', is_sorted=False)[source]

Converts a string of times into a SpikeTrain.

Parameters: s – the string with (ordered) spike times. edges – interval defining the edges of the spike train. Given as a pair of floats (T0, T1) or a single float T1, where T0=0 is assumed. sep – The separator between the time numbers, default=’ ‘. is_sorted – if True, the spike times are not sorted after loading, if False, spike times are sorted with np.sort SpikeTrain`