allensdk.ephys.ephys_features module

exception allensdk.ephys.ephys_features.FeatureError[source]

Bases: exceptions.Exception

Generic Python-exception-derived object raised by feature detection functions.

allensdk.ephys.ephys_features.adaptation_index(isis)[source]

Calculate adaptation index of isis.

allensdk.ephys.ephys_features.analyze_trough_details(v, t, spike_indexes, peak_indexes, clipped=None, end=None, filter=10.0, heavy_filter=1.0, term_frac=0.01, adp_thresh=0.5, tol=0.5, flat_interval=0.002, adp_max_delta_t=0.005, adp_max_delta_v=10.0, dvdt=None)[source]

Analyze trough to determine if an ADP exists and whether the reset is a ‘detour’ or ‘direct’

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of spike indexes
peak_indexes : numpy array of spike peak indexes
end : end of time window (optional)
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (default 1)
heavy_filter : lower cutoff frequency for 4-pole low-pass Bessel filter in kHz (default 1)
thresh_frac : fraction of average upstroke for threshold calculation (optional, default 0.05)
adp_thresh: minimum dV/dt in V/s to exceed to be considered to have an ADP (optional, default 1.5)
tol : tolerance for evaluating whether Vm drops appreciably further after end of spike (default 1.0 mV)
flat_interval: if the trace is flat for this duration, stop looking for an ADP (default 0.002 s)
adp_max_delta_t: max possible ADP delta t (default 0.005 s)
adp_max_delta_v: max possible ADP delta v (default 10 mV)
dvdt : pre-calculated time-derivative of voltage (optional)
Returns:
isi_types : numpy array of isi reset types (direct or detour)
fast_trough_indexes : numpy array of indexes at the start of the trough (i.e. end of the spike)
adp_indexes : numpy array of adp indexes (np.nan if there was no ADP in that ISI
slow_trough_indexes : numpy array of indexes at the minimum of the slow phase of the trough

(if there wasn’t just a fast phase)

allensdk.ephys.ephys_features.average_rate(t, spikes, start, end)[source]

Calculate average firing rate during interval between start and end.

Parameters:
t : numpy array of times in seconds
spikes : numpy array of spike indexes
start : start of time window for spike detection
end : end of time window for spike detection
Returns:
avg_rate : average firing rate in spikes/sec
allensdk.ephys.ephys_features.average_voltage(v, t, start=None, end=None)[source]

Calculate average voltage between start and end.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
start : start of time window for spike detection (optional, default None)
end : end of time window for spike detection (optional, default None)
Returns:
v_avg : average voltage
allensdk.ephys.ephys_features.calculate_dvdt(v, t, filter=None)[source]

Low-pass filters (if requested) and differentiates voltage by time.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default None)
Returns:
dvdt : numpy array of time-derivative of voltage (V/s = mV/ms)
allensdk.ephys.ephys_features.check_thresholds_and_peaks(v, t, spike_indexes, peak_indexes, upstroke_indexes, end=None, max_interval=0.005, thresh_frac=0.05, filter=10.0, dvdt=None, tol=1.0)[source]

Validate thresholds and peaks for set of spikes

Check that peaks and thresholds for consecutive spikes do not overlap Spikes with overlapping thresholds and peaks will be merged.

Check that peaks and thresholds for a given spike are not too far apart.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of spike indexes
peak_indexes : numpy array of indexes of spike peaks
upstroke_indexes : numpy array of indexes of spike upstrokes
max_interval : maximum allowed time between start of spike and time of peak in sec (default 0.005)
thresh_frac : fraction of average upstroke for threshold calculation (optional, default 0.05)
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default 10)
dvdt : pre-calculated time-derivative of voltage (optional)
tol : tolerance for returning to threshold in mV (optional, default 1)
Returns:
spike_indexes : numpy array of modified spike indexes
peak_indexes : numpy array of modified spike peak indexes
upstroke_indexes : numpy array of modified spike upstroke indexes
clipped : numpy array of clipped status of spikes
allensdk.ephys.ephys_features.detect_bursts(isis, isi_types, fast_tr_v, fast_tr_t, slow_tr_v, slow_tr_t, thr_v, tol=0.5, pause_cost=1.0)[source]

Detect bursts in spike train.

Parameters:
isis : numpy array of n interspike intervals
isi_types : numpy array of n interspike interval types
fast_tr_v : numpy array of fast trough voltages for the n + 1 spikes of the train
fast_tr_t : numpy array of fast trough times for the n + 1 spikes of the train
slow_tr_v : numpy array of slow trough voltages for the n + 1 spikes of the train
slow_tr_t : numpy array of slow trough times for the n + 1 spikes of the train
thr_v : numpy array of threshold voltages for the n + 1 spikes of the train
tol : tolerance for the difference in slow trough voltages and thresholds (default 0.5 mV)

Used to identify “delay” interspike intervals that occur within a burst

Returns:
bursts : list of bursts

Each item in list is a tuple of the form (burst_index, start, end) where burst_index is a comparison index between the highest instantaneous rate within the burst vs the highest instantaneous rate outside the burst. start is the index of the first ISI of the burst, and end is the ISI index immediately following the burst.

allensdk.ephys.ephys_features.detect_pauses(isis, isi_types, cost_weight=1.0)[source]

Determine which ISIs are “pauses” in ongoing firing.

Pauses are unusually long ISIs with a “detour reset” among “direct resets”.

Parameters:
isis : numpy array of interspike intervals
isi_types : numpy array of interspike interval types (‘direct’ or ‘detour’)
cost_weight : weight for cost function for calling an ISI a pause

Higher cost weights lead to fewer ISIs identified as pauses. The cost function also depends on the difference between the duration of the “pause” ISIs and the average duration and standard deviation of “non-pause” ISIs.

Returns:
pauses : numpy array of indices corresponding to pauses in isis
allensdk.ephys.ephys_features.detect_putative_spikes(v, t, start=None, end=None, filter=10.0, dv_cutoff=20.0)[source]

Perform initial detection of spikes and return their indexes.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
start : start of time window for spike detection (optional)
end : end of time window for spike detection (optional)
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default 10)
dv_cutoff : minimum dV/dt to qualify as a spike in V/s (optional, default 20)
dvdt : pre-calculated time-derivative of voltage (optional)
Returns:
putative_spikes : numpy array of preliminary spike indexes
allensdk.ephys.ephys_features.estimate_adjusted_detection_parameters(v_set, t_set, interval_start, interval_end, filter=10)[source]

Estimate adjusted values for spike detection by analyzing a period when the voltage changes quickly but passively (due to strong current stimulation), which can result in spurious spike detection results.

Parameters:
v_set : list of numpy arrays of voltage time series in mV
t_set : list of numpy arrays of times in seconds
interval_start : start of analysis interval (sec)
interval_end : end of analysis interval (sec)
Returns:
new_dv_cutoff : adjusted dv/dt cutoff (V/s)
new_thresh_frac : adjusted fraction of avg upstroke to find threshold
allensdk.ephys.ephys_features.filter_putative_spikes(v, t, spike_indexes, peak_indexes, min_height=2.0, min_peak=-30.0, filter=10.0, dvdt=None)[source]
Filter out events that are unlikely to be spikes based on:
  • Voltage failing to go down between peak and the next spike’s threshold
  • Height (threshold to peak)
  • Absolute peak level
Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of preliminary spike indexes
peak_indexes : numpy array of indexes of spike peaks
min_height : minimum acceptable height from threshold to peak in mV (optional, default 2)
min_peak : minimum acceptable absolute peak level in mV (optional, default -30)
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default 10)
dvdt : pre-calculated time-derivative of voltage (optional)
Returns:
spike_indexes : numpy array of threshold indexes
peak_indexes : numpy array of peak indexes
allensdk.ephys.ephys_features.find_downstroke_indexes(v, t, peak_indexes, trough_indexes, clipped=None, filter=10.0, dvdt=None)[source]

Find indexes of minimum voltage (troughs) between spikes.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
peak_indexes : numpy array of spike peak indexes
trough_indexes : numpy array of threshold indexes
clipped: boolean array - False if spike not clipped by edge of window
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default 10)
dvdt : pre-calculated time-derivative of voltage (optional)
Returns:
downstroke_indexes : numpy array of downstroke indexes
allensdk.ephys.ephys_features.find_peak_indexes(v, t, spike_indexes, end=None)[source]

Find indexes of spike peaks.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of preliminary spike indexes
end : end of time window for spike detection (optional)
allensdk.ephys.ephys_features.find_time_index(t, t_0)[source]

Find the index value of a given time (t_0) in a time series (t).

allensdk.ephys.ephys_features.find_trough_indexes(v, t, spike_indexes, peak_indexes, clipped=None, end=None)[source]

Find indexes of minimum voltage (trough) between spikes.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of spike indexes
peak_indexes : numpy array of spike peak indexes
end : end of time window (optional)
Returns:
trough_indexes : numpy array of threshold indexes
allensdk.ephys.ephys_features.find_upstroke_indexes(v, t, spike_indexes, peak_indexes, filter=10.0, dvdt=None)[source]

Find indexes of maximum upstroke of spike.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of preliminary spike indexes
peak_indexes : numpy array of indexes of spike peaks
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default 10)
dvdt : pre-calculated time-derivative of voltage (optional)
Returns:
upstroke_indexes : numpy array of upstroke indexes
allensdk.ephys.ephys_features.find_widths(v, t, spike_indexes, peak_indexes, trough_indexes, clipped=None)[source]

Find widths at half-height for spikes.

Widths are only returned when heights are defined

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
spike_indexes : numpy array of spike indexes
peak_indexes : numpy array of spike peak indexes
trough_indexes : numpy array of trough indexes
Returns:
widths : numpy array of spike widths in sec
allensdk.ephys.ephys_features.fit_membrane_time_constant(v, t, start, end, min_rsme=0.0001)[source]

Fit an exponential to estimate membrane time constant between start and end

Parameters:
v : numpy array of voltages in mV
t : numpy array of times in seconds
start : start of time window for exponential fit
end : end of time window for exponential fit
min_rsme: minimal acceptable root mean square error (default 1e-4)
Returns:
a, inv_tau, y0 : Coeffients of equation y0 + a * exp(-inv_tau * x)
returns np.nan for values if fit fails
allensdk.ephys.ephys_features.fit_prespike_time_constant(v, t, start, spike_time, dv_limit=-0.001, tau_limit=0.3)[source]

Finds the dominant time constant of the pre-spike rise in voltage

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
start : start of voltage rise (seconds)
spike_time : time of first spike (seconds)
dv_limit : dV/dt cutoff (default -0.001)

Shortens fit window if rate of voltage drop exceeds this limit

tau_limit : upper bound for slow time constant (seconds, default 0.3)

If the slower time constant of a double-exponential fit is twice that of the faster and exceeds this limit, the faster one will be considered the dominant one

Returns:
tau : dominant time constant (seconds)
allensdk.ephys.ephys_features.get_isis(t, spikes)[source]

Find interspike intervals in sec between spikes (as indexes).

allensdk.ephys.ephys_features.has_fixed_dt(t)[source]

Check that all time intervals are identical.

allensdk.ephys.ephys_features.latency(t, spikes, start)[source]

Calculate time to the first spike.

allensdk.ephys.ephys_features.norm_diff(a)[source]

Calculate average of (a[i] - a[i+1]) / (a[i] + a[i+1]).

allensdk.ephys.ephys_features.norm_sq_diff(a)[source]

Calculate average of (a[i] - a[i+1])^2 / (a[i] + a[i+1])^2.

allensdk.ephys.ephys_features.refine_threshold_indexes(v, t, upstroke_indexes, thresh_frac=0.05, filter=10.0, dvdt=None)[source]

Refine threshold detection of previously-found spikes.

Parameters:
v : numpy array of voltage time series in mV
t : numpy array of times in seconds
upstroke_indexes : numpy array of indexes of spike upstrokes (for threshold target calculation)
thresh_frac : fraction of average upstroke for threshold calculation (optional, default 0.05)
filter : cutoff frequency for 4-pole low-pass Bessel filter in kHz (optional, default 10)
dvdt : pre-calculated time-derivative of voltage (optional)
Returns:
threshold_indexes : numpy array of threshold indexes