def st_convolve(train, kernel, sampling_rate, mode='same', binning_params=None, kernel_discretization_params=None): """ Convolves a :class:`neo.core.SpikeTrain` with a kernel. :param train: Spike train to convolve. :type train: :class:`neo.core.SpikeTrain` :param kernel: The kernel instance to convolve with. :type kernel: :class:`Kernel` :param sampling_rate: The sampling rate which will be used to bin the spike train. The unit will typically be a frequency unit. :type sampling_rate: Quantity scalar :param mode: * 'same': The default which returns an array covering the whole duration of the spike train `train`. * 'full': Returns an array with additional discretization bins in the beginning and end so that for each spike the whole discretized kernel is included. * 'valid': Returns only the discretization bins where the discretized kernel and spike train completely overlap. See also :func:`scipy.signal.convolve`. :type mode: {'same', 'full', 'valid'} :param dict binning_params: Additional discretization arguments which will be passed to :func:`.tools.bin_spike_trains`. :param dict kernel_discretization_params: Additional discretization arguments which will be passed to :func:`.discretize_kernel`. :returns: The convolved spike train, the boundaries of the discretization bins :rtype: (Quantity 1D, Quantity 1D with the inverse units of `sampling_rate`) """ if binning_params is None: binning_params = {} if kernel_discretization_params is None: kernel_discretization_params = {} binned, bins = tools.bin_spike_trains({0: [train]}, sampling_rate, **binning_params) binned = binned[0][0] #sampling_rate = binned.size / (bins[-1] - bins[0]) result = smooth(binned, kernel, sampling_rate, mode, **kernel_discretization_params) assert (result.size - binned.size) % 2 == 0 num_additional_bins = (result.size - binned.size) // 2 if len(binned): bins = sp.linspace(bins[0] - num_additional_bins / sampling_rate, bins[-1] + num_additional_bins / sampling_rate, result.size + 1) else: bins = [] * pq.s return result, bins
def st_convolve( train, kernel, sampling_rate, mode='same', binning_params=None, kernel_discretization_params=None): """ Convolves a :class:`neo.core.SpikeTrain` with a kernel. :param train: Spike train to convolve. :type train: :class:`neo.core.SpikeTrain` :param kernel: The kernel instance to convolve with. :type kernel: :class:`Kernel` :param sampling_rate: The sampling rate which will be used to bin the spike train. The unit will typically be a frequency unit. :type sampling_rate: Quantity scalar :param mode: * 'same': The default which returns an array covering the whole duration of the spike train `train`. * 'full': Returns an array with additional discretization bins in the beginning and end so that for each spike the whole discretized kernel is included. * 'valid': Returns only the discretization bins where the discretized kernel and spike train completely overlap. See also :func:`scipy.signal.convolve`. :type mode: {'same', 'full', 'valid'} :param dict binning_params: Additional discretization arguments which will be passed to :func:`.tools.bin_spike_trains`. :param dict kernel_discretization_params: Additional discretization arguments which will be passed to :func:`.discretize_kernel`. :returns: The convolved spike train, the boundaries of the discretization bins :rtype: (Quantity 1D, Quantity 1D with the inverse units of `sampling_rate`) """ if binning_params is None: binning_params = {} if kernel_discretization_params is None: kernel_discretization_params = {} binned, bins = tools.bin_spike_trains( {0: [train]}, sampling_rate, **binning_params) binned = binned[0][0] #sampling_rate = binned.size / (bins[-1] - bins[0]) result = smooth( binned, kernel, sampling_rate, mode, **kernel_discretization_params) assert (result.size - binned.size) % 2 == 0 num_additional_bins = (result.size - binned.size) // 2 if len(binned): bins = sp.linspace( bins[0] - num_additional_bins / sampling_rate, bins[-1] + num_additional_bins / sampling_rate, result.size + 1) else: bins = [] * pq.s return result, bins
def optimal_gauss_kernel_size(train, optimize_steps, progress=None): """ Return the optimal kernel size for a spike density estimation of a spike train for a gaussian kernel. This function takes a single spike train, which can be a superposition of multiple spike trains (created with :func:`collapsed_spike_trains`) that should be included in a spike density estimation. Implements the algorithm from (Shimazaki, Shinomoto. Journal of Computational Neuroscience. 2010). :param train: The spike train for which the kernel size should be optimized. :type train: :class:`neo.core.SpikeTrain` :param optimize_steps: Array of kernel sizes to try (the best of these sizes will be returned). :type optimize_steps: Quantity 1D :param progress: Set this parameter to report progress. Will be advanced by len(`optimize_steps`) steps. :type progress: :class:`.progress_indicator.ProgressIndicator` :returns: Best of the given kernel sizes :rtype: Quantity scalar """ if not progress: progress = ProgressIndicator() x = train.rescale(optimize_steps.units) N = len(train) C = {} sampling_rate = 1024.0 / (x.t_stop - x.t_start) dt = float(1.0 / sampling_rate) y_hist = tools.bin_spike_trains({0: [x]}, sampling_rate)[0][0][0] y_hist = sp.asfarray(y_hist) / N / dt for step in optimize_steps: s = float(step) yh = sigproc.smooth(y_hist, sigproc.GaussianKernel(2 * step), sampling_rate, num_bins=2048, ensure_unit_area=True) * optimize_steps.units # Equation from Matlab code, 7/2012 c = (sp.sum(yh**2) * dt - 2 * sp.sum(yh * y_hist) * dt + 2 * 1 / sp.sqrt(2 * sp.pi) / s / N) C[s] = c * N * N progress.step() # Return kernel size with smallest cost return min(C, key=C.get) * optimize_steps.units
def optimal_gauss_kernel_size(train, optimize_steps, progress=None): """ Return the optimal kernel size for a spike density estimation of a spike train for a gaussian kernel. This function takes a single spike train, which can be a superposition of multiple spike trains (created with :func:`collapsed_spike_trains`) that should be included in a spike density estimation. Implements the algorithm from (Shimazaki, Shinomoto. Journal of Computational Neuroscience. 2010). :param train: The spike train for which the kernel size should be optimized. :type train: :class:`neo.core.SpikeTrain` :param optimize_steps: Array of kernel sizes to try (the best of these sizes will be returned). :type optimize_steps: Quantity 1D :param progress: Set this parameter to report progress. Will be advanced by len(`optimize_steps`) steps. :type progress: :class:`.progress_indicator.ProgressIndicator` :returns: Best of the given kernel sizes :rtype: Quantity scalar """ if not progress: progress = ProgressIndicator() x = train.rescale(optimize_steps.units) N = len(train) C = {} sampling_rate = 1024.0 / (x.t_stop - x.t_start) dt = float(1.0 / sampling_rate) y_hist = tools.bin_spike_trains({0: [x]}, sampling_rate)[0][0][0] y_hist = sp.asfarray(y_hist) / N / dt for step in optimize_steps: s = float(step) yh = sigproc.smooth( y_hist, sigproc.GaussianKernel(2 * step), sampling_rate, num_bins=2048, ensure_unit_area=True) * optimize_steps.units # Equation from Matlab code, 7/2012 c = (sp.sum(yh ** 2) * dt - 2 * sp.sum(yh * y_hist) * dt + 2 * 1 / sp.sqrt(2 * sp.pi) / s / N) C[s] = c * N * N progress.step() # Return kernel size with smallest cost return min(C, key=C.get) * optimize_steps.units
def psth(trains, bin_size, rate_correction=True, start=0 * pq.ms, stop=sp.inf * pq.s): """ Return dictionary of peri stimulus time histograms for a dictionary of spike train lists. :param dict trains: A dictionary of lists of :class:`neo.core.SpikeTrain` objects. :param bin_size: The desired bin size (as a time quantity). :type bin_size: Quantity scalar :param bool rate_correction: Determines if a rates (``True``) or counts (``False``) are returned. :param start: The desired time for the start of the first bin. It will be recalculated if there are spike trains which start later than this time. :type start: Quantity scalar :param stop: The desired time for the end of the last bin. It will be recalculated if there are spike trains which end earlier than this time. :type stop: Quantity scalar :returns: A dictionary (with the same indices as ``trains``) of arrays containing counts (or rates if ``rate_correction`` is ``True``) and the bin borders. :rtype: dict, Quantity 1D """ if not trains: raise SpykeException('No spike trains for PSTH!') start, stop = tools.minimum_spike_train_interval(trains, start, stop) binned, bins = tools.bin_spike_trains(trains, 1.0 / bin_size, start, stop) cumulative = {} time_multiplier = 1.0 / float(bin_size.rescale(pq.s)) for u in binned: if not binned[u]: cumulative[u] = sp.array([]) else: if rate_correction: cumulative[u] = sp.mean(sp.array(binned[u]), 0) else: cumulative[u] = sp.sum(sp.array(binned[u]), 0) cumulative[u] *= time_multiplier return cumulative, bins
def psth( trains, bin_size, rate_correction=True, start=0 * pq.ms, stop=sp.inf * pq.s): """ Return dictionary of peri stimulus time histograms for a dictionary of spike train lists. :param dict trains: A dictionary of lists of :class:`neo.core.SpikeTrain` objects. :param bin_size: The desired bin size (as a time quantity). :type bin_size: Quantity scalar :param bool rate_correction: Determines if a rates (``True``) or counts (``False``) are returned. :param start: The desired time for the start of the first bin. It will be recalculated if there are spike trains which start later than this time. :type start: Quantity scalar :param stop: The desired time for the end of the last bin. It will be recalculated if there are spike trains which end earlier than this time. :type stop: Quantity scalar :returns: A dictionary (with the same indices as ``trains``) of arrays containing counts (or rates if ``rate_correction`` is ``True``) and the bin borders. :rtype: dict, Quantity 1D """ if not trains: raise SpykeException('No spike trains for PSTH!') start, stop = tools.minimum_spike_train_interval(trains, start, stop) binned, bins = tools.bin_spike_trains(trains, 1.0 / bin_size, start, stop) cumulative = {} time_multiplier = 1.0 / float(bin_size.rescale(pq.s)) for u in binned: if not binned[u]: cumulative[u] = sp.array([]) else: if rate_correction: cumulative[u] = sp.mean(sp.array(binned[u]), 0) else: cumulative[u] = sp.sum(sp.array(binned[u]), 0) cumulative[u] *= time_multiplier return cumulative, bins