Analysis Utilities

Analysis functions

analysis_utils.bin_spike_ms(stell_spks_l: list, sim_dur: float) ndarray

Bin spike times into a binary matrix with millisecond resolution.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for each neuron.

  • sim_dur (float or int) – Duration of the simulation in milliseconds.

Returns:

A binary matrix of shape (number of cells, sim_dur) where each row corresponds to a cell and each column corresponds to a millisecond. A value of 1 indicates a spike at that millisecond, and 0 indicates no spike.

Return type:

ndarray

analysis_utils.build_and_return_matrix(sim_id: str = None, specs_file: str = None) ndarray

Builds and returns an connectivity matrix for a given simulation ID.

Used to analyze the connectivity of a simulation.

Parameters:
  • sim_id (str) – The ID of the simulation to load parameters for.

  • specs_file (str) – The path to a specifications file (not implemented).

Returns:

The adjacency matrix generated for the given simulation.

Return type:

np.ndarray

analysis_utils.calc_fft(x, T=2.5e-05)

Calculate the Fast Fourier Transform for a given signal.

Parameters:
  • x (np.ndarray) – The signal to calculate the FFT for.

  • T (float, optional) – The time step of the signal. Default is 0.000025s default dt for neuron.

Returns:

A tuple containing:
  • f (np.ndarray): The frequency array.

  • y (np.ndarray): The FFT of the signal.

  • power (np.ndarray): The power spectrum of the signal.

Return type:

tuple

analysis_utils.calc_firing_rates(spk_list, sim_dur)

Calculate the average firing rates of neurons.

Parameters:
  • spk_list (list of lists) – Spike times of each neuron

  • sim_dur (float) – The duration of the simulation in milliseconds.

Returns:

The average firing rate of the neurons in Hz.

Return type:

float

analysis_utils.calc_grid_field_sizes_time(stell_spikes_l, avg=True)

Calculate the sizes of grid fields along the time axis.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for each neuron.

  • avg (bool, optional) – Whether to return the average field size. Defaults to True.

Returns:

If avg is True, returns the median field size. Otherwise, returns a list of all field sizes.

Return type:

float or list

analysis_utils.calc_grid_scales_time(stell_spikes, avg=True)

Calculate the scale of grid fields along the time axis.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for each neuron.

  • avg (bool, optional) – Whether to return the average scale size. Defaults to True.

Returns:

If avg is True, returns the median scale size. Otherwise, returns a list of all scale sizes.

Return type:

float or list

analysis_utils.calc_speed_of_network(stell_spks_l, params, win_size=100)

Calculate the speed of the network.

This function calculates the speed of the network by decoding the position of the network and calculating the slope of the unwrapped position. It assumes the input DC to network was constant

Parameters:
  • stell_spks_l (list) – List of spike timings for stellate cells.

  • params (dict) – Dictionary containing the simulation parameters.

  • win_size (int, optional) – Window size for instantaneous rate calculation. Defaults to 100.

Returns:

The slope representing the speed of the network.

Return type:

float

analysis_utils.clean_spikes(stell_spikes_l, order=1)

Cleans spikes from the given spike trains using a threshold-based approach.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for each neuron.

  • order (float, optional) – Factor to determine the strictness of threshold.

Returns:

A list of spike trains with cleaned spikes, where each spike train is represented as a list of spike times.

Return type:

list

analysis_utils.decode_pos(stell_spikes_l, params, t_start=None, t_end=None, win_size=100)

Decode position for neuronal activity.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for each neuron.

  • params (dict) – Parameter dictionary of simulation parameters.

  • t_start (int, optional) – Start time for decoding. Default is 0.

  • t_end (int, optional) – End time for decoding. Default is None.

  • win_size (int, optional) – Window size for calculating the instantaneous rate. Default is 150.

Returns:

The decoded position

Return type:

np.ndarray

analysis_utils.generate_2d_video(sim_id, sheet_to_save=0, sim_num=0)

Generates a video for the 2D model

Video is saved as data/{sim_id}/{sim_id}_{sheet_to_save}.mp4

Parameters:
  • sim_id (str) – The simulation ID to load spikes and parameters.

  • sheet_to_save (int, optional) – The sheet index to save (0-4). Defaults to 0. Index 4 is the interneuron sheet.

Returns:

None

Example

generate_2d_video(“BaseModel2D”, sheet_to_save=2)

analysis_utils.grid_field_sizes_neurons(stell_spks, sim_dur, avg=True, win_size_t=1000, win_size_n=3)

Calculate the grid field sizes along the neuron axis.

Parameters:
  • stell_spks (list of lists) – Spike times for each neuron.

  • sim_dur (float) – Duration of the simulation in milliseconds.

  • avg (bool, optional) – If True, return the average grid field size. If False, return all grid field sizes. Defaults to True.

  • win_size_t (int, optional) – Time window size in milliseconds for analysis. Defaults to 1000.

  • win_size_n (int, optional) – Number of windows for analysis. Defaults to 3.

Returns:

Average grid field size if avg is True, otherwise a list of all grid field sizes.

Return type:

float or list

analysis_utils.grid_props_2D(inst_rates_reshaped, t=-1000)

Calculate 2D grid properties.

Calculate the grid score, grid scale, and grid size from the 2D autocorrelation of firing rate map.

Parameters:
  • inst_rates_reshaped (numpy.ndarray) – 3D array of reshaped instantaneous rates (cellxcellxtime).

  • t (int) – Time index to use for the calculation. Default is -1000.

Returns:

A tuple containing:
  • auto_corr (numpy.ndarray): Normalized 2D autocorrelation matrix.

  • grid_score (float): Grid score calculated from the autocorrelation matrix.

  • grid_scale (float): Median distance of the closest objects from the center.

  • grid_size (float): Median size of the grid cells.

Return type:

tuple

analysis_utils.grid_scale_neurons(stell_spikes_l, sim_dur, win_size=1000, stdev=3, avg=True)

Calculate the grid scale along the neuron axis.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for stellate neurons.

  • sim_dur (int) – Duration of the simulation in milliseconds.

  • win_size (int, optional) – Size of the window for periodic activity calculation in milliseconds. Defaults to 1000.

  • stdev (int, optional) – Standard deviation for smoothing the periodic activity. Defaults to 3.

  • avg (bool, optional) – If True, returns the average difference between peaks. If False, returns all differences. Defaults to True.

Returns:

If avg is True, returns the average difference between peaks as a float. If avg is False, returns a list of differences between peaks.

Return type:

float or list

analysis_utils.instant_rate(spike_train, sim_dur, stdev)

Calculate the instantaneous firing rate from a spike train using Gaussian convolution.

Parameters:
  • spike_train (list or array-like) – A list or array of spike times.

  • sim_dur (float) – The duration of the simulation in milliseconds.

  • stdev (float) – The standard deviation of the Gaussian window.

Returns:

The instantaneous firing rate as a numpy array.

Return type:

numpy.ndarray

analysis_utils.instant_rate_all(stell_spikes_l: list, sim_dur: float, stdev: float) ndarray

Calculate the instantaneous firing rate for all cells using Gaussian kernel convolution.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for each neuron.

  • sim_dur (float) – The duration of the simulation in milliseconds.

  • stdev (float) – The standard deviation of the Gaussian kernel used for convolution.

Returns:

A 2D array (cell X t_ms) where each row corresponds to the instantaneous firing rate of a cell.

Return type:

numpy.ndarray

analysis_utils.integrate_array(arr, dx=1, axis=-1)

Integrate an array using Simpson’s rule.

analysis_utils.periodic_activity_all(stell_spikes_l, sim_dur, window_t, stdev)

Instantaneous firing rate for all cells using Gaussian kernel convolution.

Parameters:
  • stell_spikes_l (list of lists) – Spike times for stellate cells.

  • sim_dur (int) – Total duration of the simulation in milliseconds.

  • window_t (int) – Time window for binning spikes in milliseconds.

  • stdev (float) – Standard deviation for the Gaussian kernel used in convolution.

Returns:

Filtered spike activity array with periodic activity highlighted.

Return type:

numpy.ndarray

analysis_utils.separate_fields(stell_spikes_l, order=1)

Separates spike trains into grid fields based on a threshold.

Parameters:

stell_spikes_l (list of lists) – Spike times for each neuron.

Returns:

A dictionary with keys as cell indices and values as lists of lists of separated grid fields,

Return type:

dict

analysis_utils.shift_fields_to_center(stell_spikes)

Shift the separated grid fields of spike trains to center them around zero.

Parameters:

stell_spikes (list of lists) – Spike times for each neuron.

Returns:

A dictionary with keys as cell indices and values as lists of shifted grid fields.

Return type:

dict

analysis_utils.spks_to_rate_reshaped(spks_l: list, params: dict, win_size: float = 200) ndarray

Convert spike times to firing rates and reshape based on cell position.

Parameters:
  • spks_l (list of lists) – Spike times for each neuron.

  • params (dict) – Parameter dictionary containing simulation parameters.

  • win_size (float, optional) – Window size for calculating the instantaneous rate. Default is 200.

Returns:

Reshaped matrix of instantaneous firing rates.

Return type:

np.ndarray