From cc8ae1974454307de4c69d9bb2a860538f0adfef Mon Sep 17 00:00:00 2001 From: Mario Mulansky Date: Mon, 27 Apr 2015 17:27:24 +0200 Subject: updated docs --- pyspike/DiscreteFunc.py | 12 +++------ pyspike/PieceWiseConstFunc.py | 12 +++------ pyspike/PieceWiseLinFunc.py | 12 +++------ pyspike/SpikeTrain.py | 41 ++++++++++++++++++++++-------- pyspike/isi_distance.py | 59 +++++++++++++++++++++---------------------- pyspike/spike_distance.py | 58 +++++++++++++++++++++--------------------- pyspike/spike_sync.py | 30 ++++++++++------------ pyspike/spikes.py | 32 +++++++++-------------- 8 files changed, 127 insertions(+), 129 deletions(-) (limited to 'pyspike') diff --git a/pyspike/DiscreteFunc.py b/pyspike/DiscreteFunc.py index bd13e1f..33b7a81 100644 --- a/pyspike/DiscreteFunc.py +++ b/pyspike/DiscreteFunc.py @@ -1,11 +1,7 @@ -""" -Class representing discrete functions. +# Class representing discrete functions. +# Copyright 2014-2015, Mario Mulansky +# Distributed under the BSD License -Copyright 2014-2015, Mario Mulansky - -Distributed under the BSD License - -""" from __future__ import print_function import numpy as np @@ -174,7 +170,7 @@ class DiscreteFunc(object): def avrg(self, interval=None): """ Computes the average of the interval sequence: - :math:`a = 1/N sum f_n` where N is the number of intervals. + :math:`a = 1/N \\sum f_n` where N is the number of intervals. :param interval: averaging interval given as a pair of floats, a sequence of pairs for averaging multiple intervals, or diff --git a/pyspike/PieceWiseConstFunc.py b/pyspike/PieceWiseConstFunc.py index dc57ab1..41998ef 100644 --- a/pyspike/PieceWiseConstFunc.py +++ b/pyspike/PieceWiseConstFunc.py @@ -1,11 +1,7 @@ -""" -Class representing piece-wise constant functions. +# Class representing piece-wise constant functions. +# Copyright 2014-2015, Mario Mulansky +# Distributed under the BSD License -Copyright 2014-2015, Mario Mulansky - -Distributed under the BSD License - -""" from __future__ import print_function import numpy as np @@ -103,7 +99,7 @@ class PieceWiseConstFunc(object): def avrg(self, interval=None): """ Computes the average of the piece-wise const function: - :math:`a = 1/T int_0^T f(x) dx` where T is the length of the interval. + :math:`a = 1/T \int_0^T f(x) dx` where T is the length of the interval. :param interval: averaging interval given as a pair of floats, a sequence of pairs for averaging multiple intervals, or diff --git a/pyspike/PieceWiseLinFunc.py b/pyspike/PieceWiseLinFunc.py index bc0aa2a..f2442be 100644 --- a/pyspike/PieceWiseLinFunc.py +++ b/pyspike/PieceWiseLinFunc.py @@ -1,11 +1,7 @@ -""" -Class representing piece-wise linear functions. +# Class representing piece-wise linear functions. +# Copyright 2014-2015, Mario Mulansky +# Distributed under the BSD License -Copyright 2014-2015, Mario Mulansky - -Distributed under the BSD License - -""" from __future__ import print_function import numpy as np @@ -123,7 +119,7 @@ class PieceWiseLinFunc: def avrg(self, interval=None): """ Computes the average of the piece-wise linear function: - :math:`a = 1/T int_0^T f(x) dx` where T is the length of the interval. + :math:`a = 1/T \int_0^T f(x) dx` where T is the interval length. :param interval: averaging interval given as a pair of floats, a sequence of pairs for averaging multiple intervals, or diff --git a/pyspike/SpikeTrain.py b/pyspike/SpikeTrain.py index d586fe0..a02b7ab 100644 --- a/pyspike/SpikeTrain.py +++ b/pyspike/SpikeTrain.py @@ -1,9 +1,6 @@ -""" Module containing the class representing spike trains for PySpike. - -Copyright 2015, Mario Mulansky - -Distributed under the BSD License -""" +# Module containing the class representing spike trains for PySpike. +# Copyright 2015, Mario Mulansky +# Distributed under the BSD License import numpy as np @@ -11,15 +8,22 @@ import numpy as np class SpikeTrain(object): """ Class representing spike trains for the PySpike Module.""" - def __init__(self, spike_times, edges): - """ Constructs the SpikeTrain + def __init__(self, spike_times, edges, is_sorted=True): + """ Constructs the SpikeTrain. + :param spike_times: ordered array of spike times. :param 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. + (T0, T1) or a single float T1, where then T0=0 is + assumed. + :param is_sorted: If `False`, the spike times will sorted by `np.sort`. + """ # TODO: sanity checks - self.spikes = np.array(spike_times, dtype=float) + if is_sorted: + self.spikes = np.array(spike_times, dtype=float) + else: + self.spikes = np.sort(np.array(spike_times, dtype=float)) try: self.t_start = float(edges[0]) @@ -27,3 +31,20 @@ class SpikeTrain(object): except: self.t_start = 0.0 self.t_end = float(edges) + + def sort(self): + """ Sorts the spike times of this spike train using `np.sort` + """ + self.spikes = np.sort(self.spikes) + + def copy(self): + """ 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. + + :return: :class:`.SpikeTrain` copy of this spike train. + + """ + return SpikeTrain(self.spikes.copy(), [self.t_start, self.t_end]) diff --git a/pyspike/isi_distance.py b/pyspike/isi_distance.py index cb8ef54..aeab0df 100644 --- a/pyspike/isi_distance.py +++ b/pyspike/isi_distance.py @@ -1,11 +1,6 @@ -""" - -Module containing several functions to compute the ISI profiles and distances - -Copyright 2014-2015, Mario Mulansky - -Distributed under the BSD License -""" +# Module containing several functions to compute the ISI profiles and distances +# Copyright 2014-2015, Mario Mulansky +# Distributed under the BSD License from pyspike import PieceWiseConstFunc from pyspike.generic import _generic_profile_multi, _generic_distance_matrix @@ -15,16 +10,16 @@ from pyspike.generic import _generic_profile_multi, _generic_distance_matrix # isi_profile ############################################################ def isi_profile(spike_train1, spike_train2): - """ Computes the isi-distance profile :math:`S_{isi}(t)` of the two given - spike trains. Retruns the profile as a PieceWiseConstFunc object. The S_isi - values are defined positive S_isi(t)>=0. + """ Computes the isi-distance profile :math:`I(t)` of the two given + spike trains. Retruns the profile as a PieceWiseConstFunc object. The + ISI-values are defined positive :math:`I(t)>=0`. :param spike_train1: First spike train. - :type spike_train1: :class:`pyspike.SpikeTrain` + :type spike_train1: :class:`.SpikeTrain` :param spike_train2: Second spike train. - :type spike_train2: :class:`pyspike.SpikeTrain` - :returns: The isi-distance profile :math:`S_{isi}(t)` - :rtype: :class:`pyspike.function.PieceWiseConstFunc` + :type spike_train2: :class:`.SpikeTrain` + :returns: The isi-distance profile :math:`I(t)` + :rtype: :class:`.PieceWiseConstFunc` """ # check whether the spike trains are defined for the same interval @@ -54,20 +49,20 @@ Falling back to slow python backend.") # isi_distance ############################################################ def isi_distance(spike_train1, spike_train2, interval=None): - """ Computes the isi-distance I of the given spike trains. The + """ Computes the ISI-distance :math:`D_I` of the given spike trains. The isi-distance is the integral over the isi distance profile - :math:`S_{isi}(t)`: + :math:`I(t)`: - .. math:: I = \int_{T_0}^{T_1} S_{isi}(t) dt. + .. math:: D_I = \\int_{T_0}^{T_1} I(t) dt. :param spike_train1: First spike train. - :type spike_train1: :class:`pyspike.SpikeTrain` + :type spike_train1: :class:`.SpikeTrain` :param spike_train2: Second spike train. - :type spike_train2: :class:`pyspike.SpikeTrain` + :type spike_train2: :class:`.SpikeTrain` :param interval: averaging interval given as a pair of floats (T0, T1), if None the average over the whole function is computed. :type interval: Pair of floats or None. - :returns: The isi-distance I. + :returns: The isi-distance :math:`D_I`. :rtype: double """ return isi_profile(spike_train1, spike_train2).avrg(interval) @@ -79,15 +74,17 @@ def isi_distance(spike_train1, spike_train2, interval=None): def isi_profile_multi(spike_trains, indices=None): """ computes the multi-variate isi distance profile for a set of spike trains. That is the average isi-distance of all pairs of spike-trains: - S_isi(t) = 2/((N(N-1)) sum_{} S_{isi}^{i,j}, + + .. math:: = \\frac{2}{N(N-1)} \\sum_{} I^{i,j}, + where the sum goes over all pairs - :param spike_trains: list of :class:`pyspike.SpikeTrain` + :param spike_trains: list of :class:`.SpikeTrain` :param indices: list of indices defining which spike trains to use, if None all given spike trains are used (default=None) :type state: list or None - :returns: The averaged isi profile :math:`(t)` - :rtype: :class:`pyspike.function.PieceWiseConstFunc` + :returns: The averaged isi profile :math:`` + :rtype: :class:`.PieceWiseConstFunc` """ average_dist, M = _generic_profile_multi(spike_trains, isi_profile, indices) @@ -101,16 +98,18 @@ def isi_profile_multi(spike_trains, indices=None): def isi_distance_multi(spike_trains, indices=None, interval=None): """ computes the multi-variate isi-distance for a set of spike-trains. That is the time average of the multi-variate spike profile: - I = \int_0^T 2/((N(N-1)) sum_{} S_{isi}^{i,j}, + + .. math:: D_I = \\int_0^T \\frac{2}{N(N-1)} \\sum_{} I^{i,j}, + where the sum goes over all pairs - :param spike_trains: list of spike trains + :param spike_trains: list of :class:`.SpikeTrain` :param indices: list of indices defining which spike trains to use, if None all given spike trains are used (default=None) :param interval: averaging interval given as a pair of floats, if None the average over the whole function is computed. :type interval: Pair of floats or None. - :returns: The time-averaged isi distance :math:`I` + :returns: The time-averaged multivariate ISI distance :math:`D_I` :rtype: double """ return isi_profile_multi(spike_trains, indices).avrg(interval) @@ -122,7 +121,7 @@ def isi_distance_multi(spike_trains, indices=None, interval=None): def isi_distance_matrix(spike_trains, indices=None, interval=None): """ Computes the time averaged isi-distance of all pairs of spike-trains. - :param spike_trains: list of :class:`pyspike.SpikeTrain` + :param spike_trains: list of :class:`.SpikeTrain` :param indices: list of indices defining which spike trains to use, if None all given spike trains are used (default=None) :type indices: list or None @@ -130,7 +129,7 @@ def isi_distance_matrix(spike_trains, indices=None, interval=None): the average over the whole function is computed. :type interval: Pair of floats or None. :returns: 2D array with the pair wise time average isi distances - :math:`I_{ij}` + :math:`D_{I}^{ij}` :rtype: np.array """ return _generic_distance_matrix(spike_trains, isi_distance, diff --git a/pyspike/spike_distance.py b/pyspike/spike_distance.py index 8d03d70..cc620d4 100644 --- a/pyspike/spike_distance.py +++ b/pyspike/spike_distance.py @@ -1,11 +1,6 @@ -""" - -Module containing several functions to compute SPIKE profiles and distances - -Copyright 2014-2015, Mario Mulansky - -Distributed under the BSD License -""" +# Module containing several functions to compute SPIKE profiles and distances +# Copyright 2014-2015, Mario Mulansky +# Distributed under the BSD License from pyspike import PieceWiseLinFunc from pyspike.generic import _generic_profile_multi, _generic_distance_matrix @@ -15,16 +10,16 @@ from pyspike.generic import _generic_profile_multi, _generic_distance_matrix # spike_profile ############################################################ def spike_profile(spike_train1, spike_train2): - """ Computes the spike-distance profile S_spike(t) of the two given spike - trains. Returns the profile as a PieceWiseLinFunc object. The S_spike - values are defined positive S_spike(t)>=0. + """ Computes the spike-distance profile :math:`S(t)` of the two given spike + trains. Returns the profile as a PieceWiseLinFunc object. The SPIKE-values + are defined positive :math:`S(t)>=0`. :param spike_train1: First spike train. - :type spike_train1: :class:`pyspike.SpikeTrain` + :type spike_train1: :class:`.SpikeTrain` :param spike_train2: Second spike train. - :type spike_train2: :class:`pyspike.SpikeTrain` - :returns: The spike-distance profile :math:`S_{spike}(t)`. - :rtype: :class:`pyspike.function.PieceWiseLinFunc` + :type spike_train2: :class:`.SpikeTrain` + :returns: The spike-distance profile :math:`S(t)`. + :rtype: :class:`.PieceWiseLinFunc` """ # check whether the spike trains are defined for the same interval @@ -56,15 +51,15 @@ Falling back to slow python backend.") # spike_distance ############################################################ def spike_distance(spike_train1, spike_train2, interval=None): - """ Computes the spike-distance S of the given spike trains. The - spike-distance is the integral over the isi distance profile S_spike(t): + """ Computes the spike-distance :math:`D_S` of the given spike trains. The + spike-distance is the integral over the isi distance profile :math:`S(t)`: - .. math:: S = \int_{T_0}^{T_1} S_{spike}(t) dt. + .. math:: D_S = \int_{T_0}^{T_1} S(t) dt. :param spike_train1: First spike train. - :type spike_train1: :class:`pyspike.SpikeTrain` + :type spike_train1: :class:`.SpikeTrain` :param spike_train2: Second spike train. - :type spike_train2: :class:`pyspike.SpikeTrain` + :type spike_train2: :class:`.SpikeTrain` :param interval: averaging interval given as a pair of floats (T0, T1), if None the average over the whole function is computed. :type interval: Pair of floats or None. @@ -81,15 +76,17 @@ def spike_distance(spike_train1, spike_train2, interval=None): def spike_profile_multi(spike_trains, indices=None): """ Computes the multi-variate spike distance profile for a set of spike trains. That is the average spike-distance of all pairs of spike-trains: - :math:`S_spike(t) = 2/((N(N-1)) sum_{} S_{spike}^{i, j}`, + + .. math:: = \\frac{2}{N(N-1)} \\sum_{} S^{i, j}`, + where the sum goes over all pairs - :param spike_trains: list of spike trains + :param spike_trains: list of :class:`.SpikeTrain` :param indices: list of indices defining which spike trains to use, if None all given spike trains are used (default=None) :type indices: list or None - :returns: The averaged spike profile :math:`(t)` - :rtype: :class:`pyspike.function.PieceWiseLinFunc` + :returns: The averaged spike profile :math:`(t)` + :rtype: :class:`.PieceWiseLinFunc` """ average_dist, M = _generic_profile_multi(spike_trains, spike_profile, @@ -104,17 +101,20 @@ def spike_profile_multi(spike_trains, indices=None): def spike_distance_multi(spike_trains, indices=None, interval=None): """ Computes the multi-variate spike distance for a set of spike trains. That is the time average of the multi-variate spike profile: - S_{spike} = \int_0^T 2/((N(N-1)) sum_{} S_{spike}^{i, j} dt + + .. math:: D_S = \\int_0^T \\frac{2}{N(N-1)} \\sum_{} + S^{i, j} dt + where the sum goes over all pairs - :param spike_trains: list of :class:`pyspike.SpikeTrain` + :param spike_trains: list of :class:`.SpikeTrain` :param indices: list of indices defining which spike trains to use, if None all given spike trains are used (default=None) :type indices: list or None :param interval: averaging interval given as a pair of floats, if None the average over the whole function is computed. :type interval: Pair of floats or None. - :returns: The averaged spike distance S. + :returns: The averaged multi-variate spike distance :math:`D_S`. :rtype: double """ return spike_profile_multi(spike_trains, indices).avrg(interval) @@ -126,7 +126,7 @@ def spike_distance_multi(spike_trains, indices=None, interval=None): def spike_distance_matrix(spike_trains, indices=None, interval=None): """ Computes the time averaged spike-distance of all pairs of spike-trains. - :param spike_trains: list of :class:`pyspike.SpikeTrain` + :param spike_trains: list of :class:`.SpikeTrain` :param indices: list of indices defining which spike trains to use, if None all given spike trains are used (default=None) :type indices: list or None @@ -134,7 +134,7 @@ def spike_distance_matrix(spike_trains, indices=None, interval=None): the average over the whole function is computed. :type interval: Pair of floats or None. :returns: 2D array with the pair wise time average spike distances - :math:`S_{ij}` + :math:`D_S^{ij}` :rtype: np.array """ return _generic_distance_matrix(spike_trains, spike_distance, diff --git a/pyspike/spike_sync.py b/pyspike/spike_sync.py index 8ddd32c..9d2e363 100644 --- a/pyspike/spike_sync.py +++ b/pyspike/spike_sync.py @@ -1,12 +1,7 @@ -""" - -Module containing several functions to compute SPIKE-Synchronization profiles -and distances - -Copyright 2014-2015, Mario Mulansky - -Distributed under the BSD License -""" +# Module containing several functions to compute SPIKE-Synchronization profiles +# and distances +# Copyright 2014-2015, Mario Mulansky +# Distributed under the BSD License from functools import partial from pyspike import DiscreteFunc @@ -27,7 +22,7 @@ def spike_sync_profile(spike_train1, spike_train2, max_tau=None): :param spike_train2: Second spike train. :type spike_train2: :class:`pyspike.SpikeTrain` :param max_tau: Maximum coincidence window size. If 0 or `None`, the - coincidence window has no upper bound. + coincidence window has no upper bound. :returns: The spike-distance profile :math:`S_{sync}(t)`. :rtype: :class:`pyspike.function.DiscreteFunction` @@ -77,12 +72,13 @@ def spike_sync(spike_train1, spike_train2, interval=None, max_tau=None): :param spike_train2: Second spike train. :type spike_train2: :class:`pyspike.SpikeTrain` :param interval: averaging interval given as a pair of floats (T0, T1), - if None the average over the whole function is computed. + if `None` the average over the whole function is computed. :type interval: Pair of floats or None. :param max_tau: Maximum coincidence window size. If 0 or `None`, the - coincidence window has no upper bound. + coincidence window has no upper bound. :returns: The spike synchronization value. - :rtype: double + :rtype: `double` + """ return spike_sync_profile(spike_train1, spike_train2, max_tau).avrg(interval) @@ -103,7 +99,7 @@ def spike_sync_profile_multi(spike_trains, indices=None, max_tau=None): if None all given spike trains are used (default=None) :type indices: list or None :param max_tau: Maximum coincidence window size. If 0 or `None`, the - coincidence window has no upper bound. + coincidence window has no upper bound. :returns: The multi-variate spike sync profile :math:`(t)` :rtype: :class:`pyspike.function.DiscreteFunction` @@ -130,9 +126,10 @@ def spike_sync_multi(spike_trains, indices=None, interval=None, max_tau=None): the average over the whole function is computed. :type interval: Pair of floats or None. :param max_tau: Maximum coincidence window size. If 0 or `None`, the - coincidence window has no upper bound. + coincidence window has no upper bound. :returns: The multi-variate spike synchronization value SYNC. :rtype: double + """ return spike_sync_profile_multi(spike_trains, indices, max_tau).avrg(interval) @@ -153,10 +150,11 @@ def spike_sync_matrix(spike_trains, indices=None, interval=None, max_tau=None): the average over the whole function is computed. :type interval: Pair of floats or None. :param max_tau: Maximum coincidence window size. If 0 or `None`, the - coincidence window has no upper bound. + coincidence window has no upper bound. :returns: 2D array with the pair wise time spike synchronization values :math:`SYNC_{ij}` :rtype: np.array + """ dist_func = partial(spike_sync, max_tau=max_tau) return _generic_distance_matrix(spike_trains, dist_func, diff --git a/pyspike/spikes.py b/pyspike/spikes.py index 9401b6e..35d8533 100644 --- a/pyspike/spikes.py +++ b/pyspike/spikes.py @@ -1,11 +1,6 @@ -""" spikes.py - -Module containing several function to load and transform spike trains - -Copyright 2014, Mario Mulansky - -Distributed under the BSD License -""" +# Module containing several function to load and transform spike trains +# Copyright 2014, Mario Mulansky +# Distributed under the BSD License import numpy as np from pyspike import SpikeTrain @@ -15,21 +10,18 @@ from pyspike import SpikeTrain # spike_train_from_string ############################################################ def spike_train_from_string(s, edges, sep=' ', is_sorted=False): - """ Converts a string of times into a :class:`pyspike.SpikeTrain`. + """ Converts a string of times into a :class:`.SpikeTrain`. :param s: the string with (ordered) spike times. :param 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. + Given as a pair of floats (T0, T1) or a single float T1, + where T0=0 is assumed. :param sep: The separator between the time numbers, default=' '. :param is_sorted: if True, the spike times are not sorted after loading, if False, spike times are sorted with `np.sort` - :returns: :class:`pyspike.SpikeTrain` + :returns: :class:`.SpikeTrain` """ - if not(is_sorted): - return SpikeTrain(np.sort(np.fromstring(s, sep=sep)), edges) - else: - return SpikeTrain(np.fromstring(s, sep=sep), edges) + return SpikeTrain(np.fromstring(s, sep=sep), edges, is_sorted) ############################################################ @@ -40,7 +32,7 @@ def load_spike_trains_from_txt(file_name, edges, """ 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 `interval` represents the start and the end of the + neglected. The `edges` represents the start and the end of the spike trains. :param file_name: The name of the text file. @@ -51,7 +43,7 @@ def load_spike_trains_from_txt(file_name, edges, :param separator: The character used to seprate the values in the text file :param comment: Lines starting with this character are ignored. :param sort: If true, the spike times are order via `np.sort`, default=True - :returns: list of spike trains + :returns: list of :class:`.SpikeTrain` """ spike_trains = [] spike_file = open(file_name, 'r') @@ -70,7 +62,7 @@ def load_spike_trains_from_txt(file_name, edges, def merge_spike_trains(spike_trains): """ Merges a number of spike trains into a single spike train. - :param spike_trains: list of arrays of spike times + :param spike_trains: list of :class:`.SpikeTrain` :returns: spike train with the merged spike times """ # get the lengths of the spike trains @@ -110,7 +102,7 @@ def generate_poisson_spikes(rate, interval): to the spike train at the beginning and end of this interval, if they are not yet present. :type interval: pair of doubles or double - :returns: Poisson spike train as a :class:`pyspike.SpikeTrain` + :returns: Poisson spike train as a :class:`.SpikeTrain` """ try: T_start = interval[0] -- cgit v1.2.3