Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in get_repolarization_slope with wrong index type #2432

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/spikeinterface/postprocessing/template_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def get_half_width(template_single, sampling_frequency, trough_idx=None, peak_id
return np.nan

trough_val = template_single[trough_idx]
# threshold is half of peak heigth (assuming baseline is 0)
# threshold is half of peak height (assuming baseline is 0)
threshold = 0.5 * trough_val

(cpre_idx,) = np.where(template_single[:trough_idx] < threshold)
Expand All @@ -451,13 +451,10 @@ def get_repolarization_slope(template_single, sampling_frequency, trough_idx=Non
"""
Return slope of repolarization period between trough and baseline

After reaching it's maxumum polarization, the neuron potential will
After reaching it's maximum polarization, the neuron potential will
recover. The repolarization slope is defined as the dV/dT of the action potential
between trough and baseline.

Optionally the function returns also the indices per waveform where the
potential crosses baseline.

Parameters
----------
template_single: numpy.ndarray
Expand All @@ -466,9 +463,14 @@ def get_repolarization_slope(template_single, sampling_frequency, trough_idx=Non
The sampling frequency of the template
trough_idx: int, default: None
The index of the trough

Returns
-------
slope: float
The repolarization slope
"""
if trough_idx is None:
trough_idx = get_trough_and_peak_idx(template_single)
trough_idx, _ = get_trough_and_peak_idx(template_single)

times = np.arange(template_single.shape[0]) / sampling_frequency

Expand All @@ -478,7 +480,7 @@ def get_repolarization_slope(template_single, sampling_frequency, trough_idx=Non
(rtrn_idx,) = np.nonzero(template_single[trough_idx:] >= 0)
if len(rtrn_idx) == 0:
return np.nan
# first time after trough, where template is at baseline
# first time after trough, where template is at baseline
return_to_base_idx = rtrn_idx[0] + trough_idx

if return_to_base_idx - trough_idx < 3:
Expand All @@ -493,8 +495,8 @@ def get_repolarization_slope(template_single, sampling_frequency, trough_idx=Non
def get_recovery_slope(template_single, sampling_frequency, peak_idx=None, **kwargs):
"""
Return the recovery slope of input waveforms. After repolarization,
the neuron hyperpolarizes untill it peaks. The recovery slope is the
slope of the actiopotential after the peak, returning to the baseline
the neuron hyperpolarizes until it peaks. The recovery slope is the
slope of the action potential after the peak, returning to the baseline
in dV/dT. The slope is computed within a user-defined window after
the peak.

Expand All @@ -511,6 +513,11 @@ def get_recovery_slope(template_single, sampling_frequency, peak_idx=None, **kwa
The index of the peak
**kwargs: Required kwargs:
- recovery_window_ms: the window in ms after the peak to compute the recovery_slope

Returns
-------
res.slope: float
The recovery slope
"""
import scipy.stats

Expand Down Expand Up @@ -543,6 +550,11 @@ def get_num_positive_peaks(template_single, sampling_frequency, **kwargs):
**kwargs: Required kwargs:
- peak_relative_threshold: the relative threshold to detect positive and negative peaks
- peak_width_ms: the width in samples to detect peaks

Returns
-------
number_positive_peaks: int
the number of positive peaks
"""
from scipy.signal import find_peaks

Expand Down Expand Up @@ -571,6 +583,11 @@ def get_num_negative_peaks(template_single, sampling_frequency, **kwargs):
**kwargs: Required kwargs:
- peak_relative_threshold: the relative threshold to detect positive and negative peaks
- peak_width_ms: the width in samples to detect peaks

Returns
-------
num_negative_peaks: int
the number of negative peaks
"""
from scipy.signal import find_peaks

Expand Down Expand Up @@ -628,7 +645,7 @@ def sort_template_and_locations(template, channel_locations, depth_direction="y"

def fit_velocity(peak_times, channel_dist):
"""
Fit velocity from peak times and channel distances using ribust Theilsen estimator.
Fit velocity from peak times and channel distances using robust Theilsen estimator.
"""
# from scipy.stats import linregress
# slope, intercept, _, _, _ = linregress(peak_times, channel_dist)
Expand Down
Loading