From 3f810c231e661e9141c9c586ebd6d9d182488c92 Mon Sep 17 00:00:00 2001 From: Mario Mulansky Date: Thu, 16 Oct 2014 17:51:23 +0200 Subject: added sphinx doc generation --- pyspike/distances.py | 147 ++++++++++++++++++++++++++++----------------------- pyspike/function.py | 126 ++++++++++++++++++++++++------------------- pyspike/spikes.py | 58 ++++++++++---------- 3 files changed, 181 insertions(+), 150 deletions(-) (limited to 'pyspike') diff --git a/pyspike/distances.py b/pyspike/distances.py index 3e97b77..b0af24c 100644 --- a/pyspike/distances.py +++ b/pyspike/distances.py @@ -17,15 +17,17 @@ from pyspike import PieceWiseConstFunc, PieceWiseLinFunc # isi_profile ############################################################ def isi_profile(spikes1, spikes2): - """ Computes the isi-distance profile S_isi(t) of the two given spike - trains. Retruns the profile as a PieceWiseConstFunc object. The S_isi + """ 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. 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: - - PieceWiseConstFunc describing the isi-distance. + + :param spikes1: ordered array of spike times with auxiliary spikes. + :param spikes2: ordered array of spike times with auxiliary spikes. + :returns: The isi-distance profile :math:`S_{isi}(t)` + :rtype: :class:`pyspike.function.PieceWiseConstFunc` + """ # check for auxiliary spikes - first and last spikes should be identical assert spikes1[0] == spikes2[0], \ @@ -52,12 +54,15 @@ Falling back to slow python backend.") ############################################################ def isi_distance(spikes1, spikes2): """ Computes the isi-distance I of the given spike trains. The - isi-distance is the integral over the isi distance profile S_isi(t): - I = \int_^T S_isi(t) dt. - Args: - - spikes1, spikes2: ordered arrays of spike times with auxiliary spikes. - Returns: - - double value: The isi-distance I. + isi-distance is the integral over the isi distance profile + :math:`S_{isi}(t)`: + + .. math:: I = \int_0^T S_{isi}(t) dt. + + :param spikes1: ordered array of spike times with auxiliary spikes. + :param spikes2: ordered array of spike times with auxiliary spikes. + :returns: The isi-distance I. + :rtype: double """ return isi_profile(spikes1, spikes2).avrg() @@ -71,10 +76,12 @@ def spike_profile(spikes1, spikes2): values are defined positive S_spike(t)>=0. 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. + + :param spikes1: ordered array of spike times with auxiliary spikes. + :param spikes2: ordered array of spike times with auxiliary spikes. + :returns: The spike-distance profile :math:`S_{spike}(t). + :rtype: :class:`pyspike.function.PieceWiseLinFunc` + """ # check for auxiliary spikes - first and last spikes should be identical assert spikes1[0] == spikes2[0], \ @@ -104,18 +111,20 @@ def spike_distance(spikes1, spikes2): """ Computes the spike-distance S of the given spike trains. The spike-distance is the integral over the isi distance profile S_spike(t): S = \int_^T S_spike(t) dt. - Args: - - spikes1, spikes2: ordered arrays of spike times with auxiliary spikes. - Returns: - - double value: The spike-distance S. + + :param spikes1: ordered array of spike times with auxiliary spikes. + :param spikes2: ordered array of spike times with auxiliary spikes. + :returns: The spike-distance. + :rtype: double + """ return spike_profile(spikes1, spikes2).avrg() ############################################################ -# generic_profile_multi +# _generic_profile_multi ############################################################ -def generic_profile_multi(spike_trains, pair_distance_func, indices=None): +def _generic_profile_multi(spike_trains, pair_distance_func, indices=None): """ Internal implementation detail, don't call this function directly, use isi_profile_multi or spike_profile_multi instead. @@ -153,7 +162,7 @@ def generic_profile_multi(spike_trains, pair_distance_func, indices=None): ############################################################ # multi_distance_par ############################################################ -def multi_distance_par(spike_trains, pair_distance_func, indices=None): +def _multi_distance_par(spike_trains, pair_distance_func, indices=None): """ parallel implementation of the multi-distance. Not currently used as it does not improve the performance. """ @@ -210,14 +219,15 @@ def isi_profile_multi(spike_trains, indices=None): 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}, where the sum goes over all pairs - 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_isi(t) + + :param spike_trains: list of spike trains + :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` """ - return generic_profile_multi(spike_trains, isi_profile, indices) + return _generic_profile_multi(spike_trains, isi_profile, indices) ############################################################ @@ -226,14 +236,14 @@ def isi_profile_multi(spike_trains, indices=None): def isi_distance_multi(spike_trains, indices=None): """ computes the multi-variate isi-distance for a set of spike-trains. That is the time average of the multi-variate spike profile: - S_isi = \int_0^T 2/((N(N-1)) sum_{} S_{isi}^{i,j}, + I = \int_0^T 2/((N(N-1)) sum_{} S_{isi}^{i,j}, where the sum goes over all pairs - 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 double value representing the averaged isi distance S_isi + + :param spike_trains: list of spike trains + :param indices: list of indices defining which spike trains to use, + if None all given spike trains are used (default=None) + :returns: The time-averaged isi distance :math:`I` + :rtype: double """ return isi_profile_multi(spike_trains, indices).avrg() @@ -246,14 +256,14 @@ def spike_profile_multi(spike_trains, indices=None): trains. That is the average spike-distance of all pairs of spike-trains: S_spike(t) = 2/((N(N-1)) sum_{} S_{spike}^{i, j}, where the sum goes over all pairs - 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(t) + :param spike_trains: list of spike trains + :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` """ - return generic_profile_multi(spike_trains, spike_profile, indices) + return _generic_profile_multi(spike_trains, spike_profile, indices) ############################################################ @@ -264,12 +274,13 @@ def spike_distance_multi(spike_trains, indices=None): 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 where the sum goes over all pairs - 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 double value representing the averaged spike distance S + + :param spike_trains: list of spike trains + :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 distance S. + :rtype: double """ return spike_profile_multi(spike_trains, indices).avrg() @@ -277,7 +288,7 @@ def spike_distance_multi(spike_trains, indices=None): ############################################################ # generic_distance_matrix ############################################################ -def generic_distance_matrix(spike_trains, dist_function, indices=None): +def _generic_distance_matrix(spike_trains, dist_function, indices=None): """ Internal implementation detail. Don't use this function directly. Instead use isi_distance_matrix or spike_distance_matrix. Computes the time averaged distance of all pairs of spike-trains. @@ -311,15 +322,16 @@ def generic_distance_matrix(spike_trains, dist_function, indices=None): ############################################################ def isi_distance_matrix(spike_trains, indices=None): """ Computes the time averaged isi-distance of all pairs of spike-trains. - 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) - Return: - - a 2D array of size len(indices)*len(indices) containing the average - pair-wise isi-distance + + :param spike_trains: list of spike trains + :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: 2D array with the pair wise time average isi distances + :math:`I_{ij}` + :rtype: np.array """ - return generic_distance_matrix(spike_trains, isi_distance, indices) + return _generic_distance_matrix(spike_trains, isi_distance, indices) ############################################################ @@ -327,12 +339,13 @@ def isi_distance_matrix(spike_trains, indices=None): ############################################################ def spike_distance_matrix(spike_trains, indices=None): """ Computes the time averaged spike-distance of all pairs of spike-trains. - 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) - Return: - - a 2D array of size len(indices)*len(indices) containing the average - pair-wise spike-distance + + :param spike_trains: list of spike trains + :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: 2D array with the pair wise time average spike distances + :math:`S_{ij}` + :rtype: np.array """ - return generic_distance_matrix(spike_trains, spike_distance, indices) + return _generic_distance_matrix(spike_trains, spike_distance, indices) diff --git a/pyspike/function.py b/pyspike/function.py index b161034..ed47f27 100644 --- a/pyspike/function.py +++ b/pyspike/function.py @@ -16,15 +16,16 @@ import numpy as np ############################################################## # PieceWiseConstFunc ############################################################## -class PieceWiseConstFunc: +class PieceWiseConstFunc(object): """ A class representing a piece-wise constant function. """ def __init__(self, x, y): """ Constructs the piece-wise const function. - Args: - - 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. + + :param x: array of length N+1 defining the edges of the intervals of + the pwc function. + :param y: array of length N defining the function values at the + intervals. """ # convert parameters to arrays, also ensures copying self.x = np.array(x) @@ -32,19 +33,19 @@ class PieceWiseConstFunc: def copy(self): """ Returns a copy of itself - Returns: - - PieceWiseConstFunc copy + + :rtype: :class:`PieceWiseConstFunc` """ return PieceWiseConstFunc(self.x, self.y) def almost_equal(self, other, decimal=14): """ Checks if the function is equal to another function up to `decimal` precision. - Args: - - other: another PieceWiseConstFunc object - Returns: - True if the two functions are equal up to `decimal` decimals, - False otherwise + + :param: other: another :class:`PieceWiseConstFunc` + :returns: True if the two functions are equal up to `decimal` decimals, + False otherwise + :rtype: bool """ eps = 10.0**(-decimal) return np.allclose(self.x, other.x, atol=eps, rtol=0.0) and \ @@ -53,6 +54,14 @@ class PieceWiseConstFunc: def get_plottable_data(self): """ Returns two arrays containing x- and y-coordinates for immeditate plotting of the piece-wise function. + + :returns: (x_plot, y_plot) containing plottable data + :rtype: pair of np.array + + Example:: + + x, y = f.get_plottable_data() + plt.plot(x, y, '-o', label="Piece-wise const function") """ x_plot = np.empty(2*len(self.x)-2) @@ -67,9 +76,10 @@ class PieceWiseConstFunc: def avrg(self): """ Computes the average of the piece-wise const function: - a = 1/T int f(x) dx where T is the length of the interval. - Returns: - - the average a. + :math:`a = 1/T int_0^T f(x) dx` where T is the length of the interval. + + :returns: the average a. + :rtype: double """ return np.sum((self.x[1:]-self.x[:-1]) * self.y) / \ (self.x[-1]-self.x[0]) @@ -77,8 +87,9 @@ class PieceWiseConstFunc: def add(self, f): """ Adds another PieceWiseConst function to this function. Note: only functions defined on the same interval can be summed. - Args: - - f: PieceWiseConst function to be added. + + :param f: :class:`PieceWiseConstFunc` function to be added. + :rtype: None """ assert self.x[0] == f.x[0], "The functions have different intervals" assert self.x[-1] == f.x[-1], "The functions have different intervals" @@ -99,8 +110,10 @@ that PySpike is installed by running\n 'python setup.py build_ext --inplace'! \ def mul_scalar(self, fac): """ Multiplies the function with a scalar value - Args: - - fac: Value to multiply + + :param fac: Value to multiply + :type fac: double + :rtype: None """ self.y *= fac @@ -113,13 +126,13 @@ class PieceWiseLinFunc: def __init__(self, x, y1, y2): """ Constructs the piece-wise linear function. - Args: - - 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. + + :param x: array of length N+1 defining the edges of the intervals of + the pwc function. + :param y1: array of length N defining the function values at the left + of the intervals. + :param y2: array of length N defining the function values at the right + of the intervals. """ # convert to array, which also ensures copying self.x = np.array(x) @@ -128,19 +141,19 @@ class PieceWiseLinFunc: def copy(self): """ Returns a copy of itself - Returns: - - PieceWiseLinFunc copy + + :rtype: :class`PieceWiseLinFunc` """ return PieceWiseLinFunc(self.x, self.y1, self.y2) def almost_equal(self, other, decimal=14): """ Checks if the function is equal to another function up to `decimal` precision. - Args: - - other: another PieceWiseLinFunc object - Returns: - True if the two functions are equal up to `decimal` decimals, - False otherwise + + :param: other: another :class:`PieceWiseLinFunc` + :returns: True if the two functions are equal up to `decimal` decimals, + False otherwise + :rtype: bool """ eps = 10.0**(-decimal) return np.allclose(self.x, other.x, atol=eps, rtol=0.0) and \ @@ -150,6 +163,14 @@ class PieceWiseLinFunc: def get_plottable_data(self): """ Returns two arrays containing x- and y-coordinates for immeditate plotting of the piece-wise function. + + :returns: (x_plot, y_plot) containing plottable data + :rtype: pair of np.array + + Example:: + + x, y = f.get_plottable_data() + plt.plot(x, y, '-o', label="Piece-wise const function") """ x_plot = np.empty(2*len(self.x)-2) x_plot[0] = self.x[0] @@ -162,27 +183,20 @@ class PieceWiseLinFunc: def avrg(self): """ Computes the average of the piece-wise linear function: - a = 1/T int f(x) dx where T is the length of the interval. - Returns: - - the average a. + :math:`a = 1/T int_0^T f(x) dx` where T is the length of the interval. + + :returns: the average a. + :rtype: double """ return np.sum((self.x[1:]-self.x[:-1]) * 0.5*(self.y1+self.y2)) / \ (self.x[-1]-self.x[0]) - def abs_avrg(self): - """ Computes the absolute average of the piece-wise linear function: - a = 1/T int |f(x)| dx where T is the length of the interval. - Returns: - - the average a. - """ - return np.sum((self.x[1:]-self.x[:-1]) * 0.5 * - (np.abs(self.y1)+np.abs(self.y2)))/(self.x[-1]-self.x[0]) - def add(self, f): """ Adds another PieceWiseLin function to this function. Note: only functions defined on the same interval can be summed. - Args: - - f: PieceWiseLin function to be added. + + :param f: :class:`PieceWiseLinFunc` function to be added. + :rtype: None """ assert self.x[0] == f.x[0], "The functions have different intervals" assert self.x[-1] == f.x[-1], "The functions have different intervals" @@ -209,8 +223,10 @@ that PySpike is installed by running\n 'python setup.py build_ext --inplace'! \ def mul_scalar(self, fac): """ Multiplies the function with a scalar value - Args: - - fac: Value to multiply + + :param fac: Value to multiply + :type fac: double + :rtype: None """ self.y1 *= fac self.y2 *= fac @@ -218,12 +234,12 @@ that PySpike is installed by running\n 'python setup.py build_ext --inplace'! \ def average_profile(profiles): """ Computes the average profile from the given ISI- or SPIKE-profiles. - Args: - - profiles: list of PieceWiseConstFunc or PieceWiseLinFunc representing - ISI- or SPIKE-profiles to be averaged - Returns: - - avrg_profile: PieceWiseConstFunc or PieceWiseLinFunc containing the - average profile. + + :param profiles: list of :class:`PieceWiseConstFunc` or + :class:`PieceWiseLinFunc` representing ISI- or + SPIKE-profiles to be averaged. + :returns: the averages profile :math:`` or :math:``. + :rtype: :class:`PieceWiseConstFunc` or :class:`PieceWiseLinFunc` """ assert len(profiles) > 1 diff --git a/pyspike/spikes.py b/pyspike/spikes.py index 68c8bc1..6b6e2e7 100644 --- a/pyspike/spikes.py +++ b/pyspike/spikes.py @@ -15,15 +15,16 @@ import numpy as np ############################################################ def add_auxiliary_spikes(spike_train, time_interval): """ Adds spikes at the beginning and end of the given time interval. - Args: - - spike_train: ordered array of spike times - - time_interval: 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. - Returns: - - spike train with additional spikes at T_start and T_end. + + :param spike_train: ordered array of spike times + :param time_interval: 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. + :type time_interval: pair of doubles or double + :returns: spike train with additional spikes at T_start and T_end. """ try: @@ -49,12 +50,11 @@ def add_auxiliary_spikes(spike_train, time_interval): ############################################################ def spike_train_from_string(s, sep=' ', sort=True): """ Converts a string of times into an array of spike times. - Args: - - s: the string with (ordered) spike times - - sep: The separator between the time numbers, default=' '. - - sort: If True, the spike times are order via `np.sort`, default=True. - Returns: - - array of spike times + + :param s: the string with (ordered) spike times + :param sep: The separator between the time numbers, default=' '. + :param sort: If True, the spike times are order via `np.sort`, default=True + :returns: array of spike times """ if sort: return np.sort(np.fromstring(s, sep=sep)) @@ -75,15 +75,18 @@ def load_spike_trains_from_txt(file_name, time_interval=None, end of each spike train. However, if `time_interval == None`, no auxiliary spikes are added, but note that the Spike and ISI distance both require auxiliary spikes. - Args: - - file_name: The name of the text file. - - time_interval: 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. - - 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. + + :param file_name: The name of the text file. + :param time_interval: 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. + :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 """ spike_trains = [] spike_file = open(file_name, 'r') @@ -102,10 +105,9 @@ def load_spike_trains_from_txt(file_name, time_interval=None, ############################################################ def merge_spike_trains(spike_trains): """ Merges a number of spike trains into a single spike train. - Args: - - spike_trains: list of arrays of spike times - Returns: - - array with the merged spike times + + :param spike_trains: list of arrays of spike times + :returns: spike train with the merged spike times """ # get the lengths of the spike trains lens = np.array([len(st) for st in spike_trains]) -- cgit v1.2.3