cibrrig.analysis.population =========================== .. py:module:: cibrrig.analysis.population Attributes ---------- .. autoapisummary:: cibrrig.analysis.population._log Classes ------- .. autoapisummary:: cibrrig.analysis.population.Population Functions --------- .. autoapisummary:: cibrrig.analysis.population.rasterize cibrrig.analysis.population.smooth_raster cibrrig.analysis.population.scale_raster cibrrig.analysis.population.preprocess_pca cibrrig.analysis.population.compute_projection_speed cibrrig.analysis.population.compute_path_lengths cibrrig.analysis.population.project_pca cibrrig.analysis.population._subset_raster cibrrig.analysis.population.get_good_spikes Module Contents --------------- .. py:data:: _log .. py:function:: rasterize(spike_times, spike_clusters, binsize, FR=False) Convert spike times and cluster IDs into a 2D array of spike counts per time bin. :param spike_times: Array of spike times. :type spike_times: array-like :param spike_clusters: Array of cluster IDs corresponding to each spike time. :type spike_clusters: array-like :param binsize: Time bin size in seconds. :type binsize: float :param FR: If True, convert spike counts to firing rates. Defaults to False. :type FR: bool, optional :returns: 2D array of spike counts or firing rates [cells x time]. - tbins (np.ndarray): Array of time bin edges. - cbins (np.ndarray): Array of unique cluster IDs. :rtype: - raster (np.ndarray) .. py:function:: smooth_raster(raster, binsize, sigma) Apply Gaussian smoothing to a spike raster. :param raster: 2D array of spike counts or firing rates [cells x time]. :type raster: np.ndarray :param binsize: Time bin size in seconds. :type binsize: float :param sigma: Standard deviation of the Gaussian kernel in seconds. :type sigma: float :returns: Smoothed raster with the same shape as the input. :rtype: np.ndarray .. py:function:: scale_raster(raster, transform='sqrt') Apply a scaling transformation to the raster data. :param raster: 2D array of spike counts or firing rates [cells x time]. :type raster: np.ndarray :param transform: Transformation to apply. Defaults to "sqrt". :type transform: str, optional :returns: Scaled raster with the same shape as the input. :rtype: np.ndarray .. py:function:: preprocess_pca(spike_times, spike_clusters, binsize, sigma, transform='sqrt') Preprocess spike data for PCA analysis. :param spike_times: Array of spike times. :type spike_times: array-like :param spike_clusters: Array of cluster IDs corresponding to each spike time. :type spike_clusters: array-like :param binsize: Time bin size in seconds. :type binsize: float :param sigma: Standard deviation for Gaussian smoothing in seconds. :type sigma: float :param transform: Transformation to apply to the smoothed raster. Defaults to "sqrt". :type transform: str, optional :returns: A dictionary containing preprocessed data: - raster: Original rasterized data - raster_smoothed: Smoothed raster - raster_scaled: Scaled and smoothed raster - tbins: Time bin edges - cbins: Unique cluster IDs :rtype: dict .. py:function:: compute_projection_speed(X, ndims=3) Compute the euclidean speed through PCA space :param X: PCA decompositions (2D numpy array: N_timepoints x N_dims) :param n: number of dimensions to use (int) :return: X_speed - 1D numpy array of PCA speed .. py:function:: compute_path_lengths(X, time_bins, t0, tf, ndims=3) Compute the euclidean distance traveled between time t0 and tf in the projection space. Computes the distance along ndims :param t0: start time of the path :type t0: list or array :param tf: end time of the path :type tf: list or array :param ndims: _description_. Defaults to 3. :type ndims: int, optional .. py:function:: project_pca(raster, tbins, ndims=20, t0=None, tf=None) Project spiking data into PCA space. Will fit to the interval [t0,tf] and apply PCA to all times :param raster: 2D array of preprocessed spike data [cells x time]. :type raster: np.ndarray :param tbins: Array of time bin edges. :type tbins: np.ndarray :param ndims: Number of PCA dimensions to compute. Defaults to 20. :type ndims: int, optional :param t0: Start time(s) for fitting. Defaults to None (use all data). :type t0: float or array-like, optional :param tf: End time(s) for fitting. Defaults to None (use all data). :type tf: float or array-like, optional :returns: PCA projections of the input data [time x ndims]. - pca (sklearn.decomposition.PCA): Fitted PCA object. :rtype: - projected (np.ndarray) .. py:function:: _subset_raster(raster, tbins, t0, tf) .. py:function:: get_good_spikes(spikes, clusters) Convinience function to return only good spikes .. py:class:: Population(spike_times, spike_clusters, ndims=None, binsize=0.005, sigma=0.01, t0=None, tf=None) A class for analyzing and visualizing population-level neural activity. .. attribute:: spike_times Array of spike times. :type: array-like .. attribute:: spike_clusters Array of cluster IDs corresponding to each spike time. :type: array-like .. attribute:: ndims Number of dimensions for PCA projection. :type: int .. attribute:: binsize Time bin size in seconds. :type: float .. attribute:: sigma Standard deviation for Gaussian smoothing in seconds. :type: float .. attribute:: t0 Start time for analysis. :type: float .. attribute:: tf End time for analysis. :type: float .. attribute:: raster Rasterized spike data. :type: np.ndarray .. attribute:: raster_smoothed Smoothed raster data. :type: np.ndarray .. attribute:: cbins Array of unique cluster IDs. :type: np.ndarray .. attribute:: tbins Array of time bin edges. :type: np.ndarray .. attribute:: projection PCA projection of the data. :type: np.ndarray .. attribute:: pca Fitted PCA object. :type: sklearn.decomposition.PCA .. attribute:: transform Transformation applied to the raster. :type: str .. attribute:: projection_speed Speed of movement through PCA space. :type: np.ndarray .. attribute:: has_ssm Whether a state-space model has been loaded. :type: bool .. py:attribute:: spike_times .. py:attribute:: spike_clusters .. py:attribute:: ndims :value: None .. py:attribute:: binsize :value: 0.005 .. py:attribute:: sigma :value: 0.01 .. py:attribute:: t0 :value: None .. py:attribute:: tf :value: None .. py:attribute:: raster :value: None .. py:attribute:: raster_smoothed :value: None .. py:attribute:: cbins :value: None .. py:attribute:: tbins :value: None .. py:attribute:: projection :value: None .. py:attribute:: pca :value: None .. py:attribute:: transform :value: None .. py:attribute:: projection_speed :value: None .. py:attribute:: has_ssm :value: False .. py:method:: compute_projection() Compute the PCA projection of the population activity. This method preprocesses the spike data, computes the raster, and projects it into PCA space. .. py:method:: compute_projection_speed(ndims=3) Compute the speed of movement through PCA space. :param ndims: Number of dimensions to use for speed calculation. Defaults to 3. :type ndims: int, optional .. py:method:: plot_by_speed(dims=[0, 1, 2], t0=None, tf=None, **kwargs) Plot the PCA projection colored by movement speed. :param dims: Dimensions to plot. Defaults to [0, 1, 2]. :type dims: list, optional :param t0: Start time for plotting. Defaults to None. :type t0: float, optional :param tf: End time for plotting. Defaults to None. :type tf: float, optional :param \*\*kwargs: Additional keyword arguments to pass to the plotting function. :returns: The axes object containing the plot. :rtype: matplotlib.axes.Axes .. py:method:: plot_projection(dims=[0, 1, 2], t0=None, tf=None, cvar=None, **kwargs) Plot the low dimensional projection as a scatter Uses the known timebins of the projection to pass only a subset of the data to "plot_projection" :param dims: Which dimensions of the projection to plot. Can be 2 or 3 elements long. Defaults to [0, 1, 2]. :type dims: list, optional :param t0: start time of the plot in seconds. Defaults to None. :type t0: float, optional :param tf: end time of the plot in seconds. Defaults to None. :type tf: float_, optional :param cvar: Variable to map to the color of the points. Defaults to None. :type cvar: np.ndarray, optional Keyword Args passed to "plot_projection" from the plotting module. ax (matplotlib.axes.Axes, optional): The axes to plot on. If not provided, a new figure and axes will be created. color (str, optional): The color of the data points or lines. Default is "k" (black) if no color variable (`cvar`) is provided. alpha (float, optional): Transparency level for the points. Should be between 0 (fully transparent) and 1 (fully opaque). - Default is 0.5. lw (float, optional): Line width for plotting. Applies if plotting a line plot. Default is 0.5. vmin (float, optional): Minimum value for the colormap if a color variable (`cvar`) is used. If not specified, it's inferred from `cvar`. vmax (float, optional): Maximum value for the colormap if a color variable (`cvar`) is used. If not specified, it's inferred from `cvar`. colorbar_title (str, optional): Title for the colorbar. Default is an empty string. title (str, optional): Title for the plot. Only used in 3D plots. Default is an empty string. lims (list, optional): Axis limits for the plot. Should be a list of `[min, max]` values. - Default is [-4, 4] for 3D plots. pane_color (str, optional): Background color of the 3D panes in 3D plots. If `None`, default style is used. plot_colorbar (bool, optional): Whether to include a colorbar in the plot. Only applies to 3D plots when `cvar` is used. Default is True. s (float, optional): Size of the markers used in the scatter plot. Default is 1. cmap (str, optional): Colormap to use when plotting with a color variable (`cvar`). Default is "viridis". cvar (array-like, optional): An array of values used to color the data points. If provided, `cmap` is applied. s (float, optional): Size of the markers in the scatter plot. Default is 1. :returns: axes :rtype: matplotlib.pyplot.axes .. py:method:: plot_projection_line(dims=[0, 1, 2], t0=None, tf=None, cvar=None, ax=None, **kwargs) Plot the low dimensional projection as a line Uses the known timebins of the projection to pass only a subset of the data to "plot_projection_line" :param dims: _description_. Defaults to [0, 1, 2]. :type dims: list, optional :param t0: _description_. Defaults to None. :type t0: float, optional :param tf: _description_. Defaults to None. :type tf: float, optional :param cvar: _description_. Defaults to None. :type cvar: np.ndarray, optional :param ax: _description_. Defaults to None. :type ax: matplotlib.pyplot.axes, optional :keyword intervals: Start and end times for each condition. :kwtype intervals: np.ndarray,optional :keyword color: Color of the line if cvar is not provided. Default is "k" (black). :kwtype color: str, optional :keyword stim_color: Color of the line during intervals (if provided) :kwtype stim_color: str, optional :keyword alpha: The alpha blending value, between 0 (transparent) and 1 (opaque). Default is 0.5. :kwtype alpha: float, optional :keyword lw: The line width. Default is 0.5. :kwtype lw: float, optional :keyword vmin: Minimum of the colormap range. If not provided, it's inferred from cvar. :kwtype vmin: float, optional :keyword vmax: Maximum of the colormap range. If not provided, it's inferred from cvar. :kwtype vmax: float, optional :keyword colorbar_title: Title for the colorbar. Default is an empty string. :kwtype colorbar_title: str, optional :keyword title: Title for the plot. Only used in 3D plots. Default is an empty string. :kwtype title: str, optional :keyword lims: The x, y, (and z for 3D) limits of the plot as [min, max]. Only used in 3D plots. Default is [-4, 4]. :kwtype lims: list, optional :keyword pane_color: Color of the panes in 3D plots. If None, default matplotlib style is used. :kwtype pane_color: color, optional :keyword plot_colorbar: Whether to plot the colorbar. Only used in 3D plots. Default is True. :kwtype plot_colorbar: bool, optional :returns: The axes object containing the plot. :rtype: Axes .. py:method:: sync_var(x, x_t) Resample an exogenous signal with the projection :param x: exogenous signal to resample :type x: np.ndarray :param x_t: sample times for x in seconds :type x_t: np.ndarray :returns: y - resampled signal :rtype: np.ndarray .. py:method:: compute_path_lengths(t0, tf, ndims=3) Compute the euclidean distance traveled between time t0 and tf in the projection space. Computes the distance along ndims :param t0: start time of the path :type t0: list or array :param tf: end time of the path :type tf: list or array :param ndims: number of dimensions to include in computation. Defaults to 3. :type ndims: int, optional .. py:method:: compute_raster(binsize=0.005, t0=None, tf=None, FR=False, cluster_ids=None) Convert spike times and cluster IDs into a 2D array of spike counts per time bin. :param t0: start time of raster in seconds :type t0: float :param tf: stop time of raster in seconds :type tf: float :param binsize: Time bin size in seconds. :type binsize: float :param FR: If True, convert spike counts to firing rates. Defaults to False. :type FR: bool, optional .. py:method:: load_rslds(ssm_fn, fit_on_load=False) Load a precomputed Linderman RSLDS model from a pickle file :param ssm_fn: filename of the computed rslds model :type ssm_fn: Path :param fit_on_load: Not yet implemented. Defaults to False. :type fit_on_load: bool, optional .. py:method:: sim_rslds(duration) Simulate the latent for a given duration (in s) Adds a dictionary with keys ['discrete_states','continuous_states','emissions','sim_time'] to the population object :param duration: Time duration to simulate in seconds :type duration: float #TODO: pass initial state .. py:method:: transform_rslds(t0, tf) Compute the latent for a given interval [t0,tf] First checks if the latent already exists. :param t0: Start time in seconds :type t0: float :param tf: End time in seconds :type tf: float :returns: ELBOS estimate of fit quality (slds.posterior): slds posterior object (np.ndarray): latent -- continuous states of the posterior :rtype: (np.ndarray) .. py:method:: plot_vectorfield(t0=None, tf=None, ax=None, nxpts=20, nypts=20, alpha=0.8, colors=None, xlim=None, ylim=None, zval=None, **kwargs) Plot the flow field of the dynamics. Wrapper to linderman Lab code in plot.py TODO: maybe include ability to plot 3D vector field?