allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis module

class allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.StimulusAnalysis(ecephys_session, trial_duration=None, **kwargs)[source]

Bases: object

METRICS_COLUMNS
conditionwise_psth

For every unit and stimulus-condition construction a PSTH table. ie. the spike-counts at a each time-interval during a stimulus, averaged over all trials of the same stim condition.

Each PSTH will count and average spikes over a time-window as determined by class parameter ‘trial_duration’ which ideally be a similar value as the duration of each stimulus (in seconds). The length of each time-bin is determined by the class parameter ‘psth_resolution’ (in seconds).

Returns:
conditionwise_psth xarray.DataArray
An 3D table that contains the PSTH for every unit/condition, with the following coordinates
  • stimulus_condition_id
  • time_relative_to_stimulus_onset
  • unit_id
conditionwise_statistics

Create a table of spike statistics, averaged and indexed by every unit_id, stimulus_condition_id pair.

Returns:
conditionwise_statistics: pd.DataFrame

A dataframe indexed by unit_id and stimulus_condition containing spike_count, spike_mean, spike_sem, spike_std and stimulus_presentation_count information.

ecephys_session
empty_metrics_table(self)[source]
get_intrinsic_timescale(self, unit_ids)[source]

Calculates the intrinsic timescale for a subset of units

known_spontaneous_keys
classmethod known_stimulus_keys()[source]

Used for discovering the correct stimulus_name key for a given StimulusAnalysis subclass (when stimulus_key is not explicity set). Should return a list of “stimulus_name” strings.

metrics

Returns a pandas DataFrame of the stimulus response metrics for each unit.

metrics_dtypes
metrics_names
name

Return the stimulus name.

null_condition
plot_conditionwise_raster(self, unit_id)[source]

Plot a matrix of rasters for each condition (orientations x temporal frequencies)

plot_raster(self, condition, unit_id)[source]
presentationwise_spike_times

Constructs a table containing all the relevant spike_times plus the stimulus_presentation_id and unit_id for the given spike.

Returns:
presentationwise_spike_times : pd.DataFrame

Indexed by spike_time, each spike containing the corresponding stimulus_presentation_id and unit_id

presentationwise_statistics

Returns a table of the spike-counts, stimulus-conditions and running speed for every stimulus_presentation_id , unit_id pair.

Returns:
presentationwise_statistics: pd.DataFrame

MultiIndex : unit_id, stimulus_presentation_id Columns : spike_count, stimulus_condition_id, running_speed

running_speed

Construct a dataframe with the averaged running speed for each stimulus_presenation_id

spikes

Returns a dictionary of unit_id -> spike-times.

stim_table
stim_table_spontaneous

Returns a stimulus table with only ‘spontaneous’ stimulus selected.

stimulus_conditions

Returns a table of relevant stimulus_conditions.

Returns:
pd.DataFrame :

Index : stimulus_condition_id Columns : stimulus parameter types

total_presentations

Total nmber of presentations / trials

trial_duration
unit_count

Get the number of units.

unit_ids

Returns a list of unit IDs for which to apply the analysis

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.calculate_time_delayed_correlation(dataset)[source]
allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.deg2rad(arr)[source]

Converts array-like input from degrees to radians

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.dsi(orivals, tuning)[source]

Computes the direction selectivity of a cell. See Ringbach 2002, Van Hooser 2014

Parameters:
ori_vals : complex array of length N

Each value the oriention of the stimulus.

tuning : float array of length N

Each value the (averaged) response of the cell at a different orientation.

Returns:
osi : float

An N-dimensional array of the circular variance (scalar value, in radians) of the responses.

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.fano_factor(spike_counts)[source]

Computers the fano factor (var/mean) for the spike-counts across a series of trials.

Parameters:
spike_counts : array

The spike counts across a series of 2 or more trials

Returns:
fano_factor : float
allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.fit_exp(rsc_time_matrix)[source]
allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.get_fr(spikes, num_timestep_second=30, sweep_length=3.1, filter_width=0.1)[source]

Uses a gaussian convolution to convert the spike-times into a contiguous firing-rate series.

Parameters:
spikes : array

An array of spike times (shifted to start at 0)

num_timestep_second : float

The sampling frequency

sweep_length : float

The lenght of the returned array

filter_width: float

The window of the gaussian method

Returns:
firing_rate : float

A linear-spaced array of length num_timestep_second*sweep_length of the smoothed firing rates series.

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.lifetime_sparseness(responses)[source]

Computes the lifetime sparseness for one unit. See Olsen & Wilson 2008.

Parameters:
responses : array of floats

An array of a unit’s spike-counts over the duration of multiple trials within a given session

Returns:
lifetime_sparsness : float

The lifetime sparseness for one unit

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.osi(orivals, tuning)[source]

Computes the orientation selectivity of a cell. The calculation of the orientation is done using the normalized circular variance (CirVar) as described in Ringbach 2002

Parameters:
ori_vals : complex array of length N

Each value the oriention of the stimulus.

tuning : float array of length N

Each value the (averaged) response of the cell at a different orientation.

Returns:
osi : float

An N-dimensional array of the circular variance (scalar value, in radians) of the responses.

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.overall_firing_rate(start_times, stop_times, spike_times)[source]

Computes the global firing rate of a series of spikes, for only those values within the given start and stop times.

Parameters:
start_times : array of N floats

A series of stimulus block start times (seconds)

stop_times : array of N floats

Times when the stimulus block ends

spike_times : array of floats

A list of spikes for a given unit

Returns:
firing_rate : float
allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.reliability(unit_sweeps, padding=1.0, num_timestep_second=30, filter_width=0.1, window_beg=0, window_end=None)[source]

Computes the trial-to-trial reliability for a set of sweeps for a given cell

Parameters:
  • unit_sweeps
  • padding
Returns:

allensdk.brain_observatory.ecephys.stimulus_analysis.stimulus_analysis.running_modulation(spike_counts, running_speeds, speed_threshold=1.0)[source]

Given a series of trials that include the spike-counts and (averaged) running-speed, does a statistical comparison to see if there was any difference in spike firing while running and while stationary.

Requires at least 2 trials while the mouse is running and two when the mouse is stationary.

Parameters:
spike_counts : array of floats of size N.

The spike counts for each trial

running_speeds: array floats of size N.

The running velocities (cm/s) of each trial.

speed_threshold: float

The minimum threshold for which the animal can be considered running (default 1.0).

Returns:
p_value : float or Nan

T-test p-value between the running and stationary trials.

run_mod : float or Nan

Relative difference between running and stationary mean firing rates.