cibrrig.analysis.population

Attributes

_log

Classes

Population

A class for analyzing and visualizing population-level neural activity.

Functions

rasterize(spike_times, spike_clusters, binsize[, FR])

Convert spike times and cluster IDs into a 2D array of spike counts per time bin.

smooth_raster(raster, binsize, sigma)

Apply Gaussian smoothing to a spike raster.

scale_raster(raster[, transform])

Apply a scaling transformation to the raster data.

preprocess_pca(spike_times, spike_clusters, binsize, sigma)

Preprocess spike data for PCA analysis.

compute_projection_speed(X[, ndims])

Compute the euclidean speed through PCA space

compute_path_lengths(X, time_bins, t0, tf[, ndims])

Compute the euclidean distance traveled between time t0 and tf

project_pca(raster, tbins[, ndims, t0, tf])

Project spiking data into PCA space. Will fit to the interval [t0,tf] and apply PCA to

_subset_raster(raster, tbins, t0, tf)

get_good_spikes(spikes, clusters)

Convinience function to return only good spikes

Module Contents

cibrrig.analysis.population._log[source]
cibrrig.analysis.population.rasterize(spike_times, spike_clusters, binsize, FR=False)[source]

Convert spike times and cluster IDs into a 2D array of spike counts per time bin.

Parameters:
  • spike_times (array-like) – Array of spike times.

  • spike_clusters (array-like) – Array of cluster IDs corresponding to each spike time.

  • binsize (float) – Time bin size in seconds.

  • FR (bool, optional) – If True, convert spike counts to firing rates. Defaults to False.

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.

Return type:

  • raster (np.ndarray)

cibrrig.analysis.population.smooth_raster(raster, binsize, sigma)[source]

Apply Gaussian smoothing to a spike raster.

Parameters:
  • raster (np.ndarray) – 2D array of spike counts or firing rates [cells x time].

  • binsize (float) – Time bin size in seconds.

  • sigma (float) – Standard deviation of the Gaussian kernel in seconds.

Returns:

Smoothed raster with the same shape as the input.

Return type:

np.ndarray

cibrrig.analysis.population.scale_raster(raster, transform='sqrt')[source]

Apply a scaling transformation to the raster data.

Parameters:
  • raster (np.ndarray) – 2D array of spike counts or firing rates [cells x time].

  • transform (str, optional) – Transformation to apply. Defaults to “sqrt”.

Returns:

Scaled raster with the same shape as the input.

Return type:

np.ndarray

cibrrig.analysis.population.preprocess_pca(spike_times, spike_clusters, binsize, sigma, transform='sqrt')[source]

Preprocess spike data for PCA analysis.

Parameters:
  • spike_times (array-like) – Array of spike times.

  • spike_clusters (array-like) – Array of cluster IDs corresponding to each spike time.

  • binsize (float) – Time bin size in seconds.

  • sigma (float) – Standard deviation for Gaussian smoothing in seconds.

  • transform (str, optional) – Transformation to apply to the smoothed raster. Defaults to “sqrt”.

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

Return type:

dict

cibrrig.analysis.population.compute_projection_speed(X, ndims=3)[source]

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

cibrrig.analysis.population.compute_path_lengths(X, time_bins, t0, tf, ndims=3)[source]

Compute the euclidean distance traveled between time t0 and tf in the projection space. Computes the distance along ndims

Parameters:
  • t0 (list or array) – start time of the path

  • tf (list or array) – end time of the path

  • ndims (int, optional) – _description_. Defaults to 3.

cibrrig.analysis.population.project_pca(raster, tbins, ndims=20, t0=None, tf=None)[source]

Project spiking data into PCA space. Will fit to the interval [t0,tf] and apply PCA to all times

Parameters:
  • raster (np.ndarray) – 2D array of preprocessed spike data [cells x time].

  • tbins (np.ndarray) – Array of time bin edges.

  • ndims (int, optional) – Number of PCA dimensions to compute. Defaults to 20.

  • t0 (float or array-like, optional) – Start time(s) for fitting. Defaults to None (use all data).

  • tf (float or array-like, optional) – End time(s) for fitting. Defaults to None (use all data).

Returns:

PCA projections of the input data [time x ndims]. - pca (sklearn.decomposition.PCA): Fitted PCA object.

Return type:

  • projected (np.ndarray)

cibrrig.analysis.population._subset_raster(raster, tbins, t0, tf)[source]
cibrrig.analysis.population.get_good_spikes(spikes, clusters)[source]

Convinience function to return only good spikes

class cibrrig.analysis.population.Population(spike_times, spike_clusters, ndims=None, binsize=0.005, sigma=0.01, t0=None, tf=None)[source]

A class for analyzing and visualizing population-level neural activity.

spike_times[source]

Array of spike times.

Type:

array-like

spike_clusters[source]

Array of cluster IDs corresponding to each spike time.

Type:

array-like

ndims[source]

Number of dimensions for PCA projection.

Type:

int

binsize[source]

Time bin size in seconds.

Type:

float

sigma[source]

Standard deviation for Gaussian smoothing in seconds.

Type:

float

t0[source]

Start time for analysis.

Type:

float

tf[source]

End time for analysis.

Type:

float

raster[source]

Rasterized spike data.

Type:

np.ndarray

raster_smoothed[source]

Smoothed raster data.

Type:

np.ndarray

cbins[source]

Array of unique cluster IDs.

Type:

np.ndarray

tbins[source]

Array of time bin edges.

Type:

np.ndarray

projection[source]

PCA projection of the data.

Type:

np.ndarray

pca[source]

Fitted PCA object.

Type:

sklearn.decomposition.PCA

transform[source]

Transformation applied to the raster.

Type:

str

projection_speed[source]

Speed of movement through PCA space.

Type:

np.ndarray

has_ssm[source]

Whether a state-space model has been loaded.

Type:

bool

spike_times[source]
spike_clusters[source]
ndims = None[source]
binsize = 0.005[source]
sigma = 0.01[source]
t0 = None[source]
tf = None[source]
raster = None[source]
raster_smoothed = None[source]
cbins = None[source]
tbins = None[source]
projection = None[source]
pca = None[source]
transform = None[source]
projection_speed = None[source]
has_ssm = False[source]
compute_projection()[source]

Compute the PCA projection of the population activity.

This method preprocesses the spike data, computes the raster, and projects it into PCA space.

compute_projection_speed(ndims=3)[source]

Compute the speed of movement through PCA space.

Parameters:

ndims (int, optional) – Number of dimensions to use for speed calculation. Defaults to 3.

plot_by_speed(dims=[0, 1, 2], t0=None, tf=None, **kwargs)[source]

Plot the PCA projection colored by movement speed.

Parameters:
  • dims (list, optional) – Dimensions to plot. Defaults to [0, 1, 2].

  • t0 (float, optional) – Start time for plotting. Defaults to None.

  • tf (float, optional) – End time for plotting. Defaults to None.

  • **kwargs – Additional keyword arguments to pass to the plotting function.

Returns:

The axes object containing the plot.

Return type:

matplotlib.axes.Axes

plot_projection(dims=[0, 1, 2], t0=None, tf=None, cvar=None, **kwargs)[source]

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”

Parameters:
  • dims (list, optional) – Which dimensions of the projection to plot. Can be 2 or 3 elements long. Defaults to [0, 1, 2].

  • t0 (float, optional) – start time of the plot in seconds. Defaults to None.

  • tf (float_, optional) – end time of the plot in seconds. Defaults to None.

  • cvar (np.ndarray, optional) – Variable to map to the color of the points. Defaults to None.

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

Return type:

matplotlib.pyplot.axes

plot_projection_line(dims=[0, 1, 2], t0=None, tf=None, cvar=None, ax=None, **kwargs)[source]

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 Arguments:
  • intervals (np.ndarray,optional) – Start and end times for each condition.

  • color (str, optional) – Color of the line if cvar is not provided. Default is “k” (black).

  • stim_color (str, optional) – Color of the line during intervals (if provided)

  • alpha (float, optional) – The alpha blending value, between 0 (transparent) and 1 (opaque). Default is 0.5.

  • lw (float, optional) – The line width. Default is 0.5.

  • vmin (float, optional) – Minimum of the colormap range. If not provided, it’s inferred from cvar.

  • vmax (float, optional) – Maximum of the colormap range. If not provided, 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) – The x, y, (and z for 3D) limits of the plot as [min, max]. Only used in 3D plots. Default is [-4, 4].

  • pane_color (color, optional) – Color of the panes in 3D plots. If None, default matplotlib style is used.

  • plot_colorbar (bool, optional) – Whether to plot the colorbar. Only used in 3D plots. Default is True.

Returns:

The axes object containing the plot.

Return type:

Axes

sync_var(x, x_t)[source]

Resample an exogenous signal with the projection

Parameters:
  • x (np.ndarray) – exogenous signal to resample

  • x_t (np.ndarray) – sample times for x in seconds

Returns:

y - resampled signal

Return type:

np.ndarray

compute_path_lengths(t0, tf, ndims=3)[source]

Compute the euclidean distance traveled between time t0 and tf in the projection space. Computes the distance along ndims

Parameters:
  • t0 (list or array) – start time of the path

  • tf (list or array) – end time of the path

  • ndims (int, optional) – number of dimensions to include in computation. Defaults to 3.

compute_raster(binsize=0.005, t0=None, tf=None, FR=False, cluster_ids=None)[source]

Convert spike times and cluster IDs into a 2D array of spike counts per time bin.

Parameters:
  • t0 (float) – start time of raster in seconds

  • tf (float) – stop time of raster in seconds

  • binsize (float) – Time bin size in seconds.

  • FR (bool, optional) – If True, convert spike counts to firing rates. Defaults to False.

load_rslds(ssm_fn, fit_on_load=False)[source]

Load a precomputed Linderman RSLDS model from a pickle file

Parameters:
  • ssm_fn (Path) – filename of the computed rslds model

  • fit_on_load (bool, optional) – Not yet implemented. Defaults to False.

sim_rslds(duration)[source]

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

Parameters:

duration (float) – Time duration to simulate in seconds

#TODO: pass initial state

transform_rslds(t0, tf)[source]

Compute the latent for a given interval [t0,tf] First checks if the latent already exists.

Parameters:
  • t0 (float) – Start time in seconds

  • tf (float) – End time in seconds

Returns:

ELBOS estimate of fit quality (slds.posterior): slds posterior object (np.ndarray): latent – continuous states of the posterior

Return type:

(np.ndarray)

plot_vectorfield(t0=None, tf=None, ax=None, nxpts=20, nypts=20, alpha=0.8, colors=None, xlim=None, ylim=None, zval=None, **kwargs)[source]

Plot the flow field of the dynamics. Wrapper to linderman Lab code in plot.py TODO: maybe include ability to plot 3D vector field?