Skip to content

Commit d963aa2

Browse files
authored
Merge pull request #3066 from chrishalcrow/update-template-metric-docs
Correct docs and docstrings for compute_template_metric units
2 parents 772c85b + 227d91d commit d963aa2

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

doc/modules/core.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ All classes support:
2121
* data on-demand (lazy loading)
2222
* multiple segments, where each segment is a contiguous piece of data (recording, sorting, events).
2323

24-
24+
.. _core-recording:
2525
Recording
2626
---------
2727

@@ -162,7 +162,7 @@ Internally, any sorting object can construct 2 internal caches:
162162
2. a unique numpy.array with structured dtype aka "spikes vector". This is useful for processing by small chunks of
163163
time, like for extracting amplitudes from a recording.
164164

165-
165+
.. _core-sorting-analyzer:
166166
SortingAnalyzer
167167
---------------
168168

@@ -179,9 +179,8 @@ to perform further analysis, such as calculating :code:`waveforms` and :code:`te
179179
Importantly, the :py:class:`~spikeinterface.core.SortingAnalyzer` handles the *sparsity* and the physical *scaling*.
180180
Sparsity defines the channels on which waveforms and templates are calculated using, for example, a
181181
physical distance from the channel with the largest peak amplitude (see the :ref:`Sparsity` section). Scaling, set by
182-
the :code:`return_scaled` argument, says whether the data has been converted from integer values to physical units such as
183-
Voltage (see the end of the :ref:`Recording` section).
184-
182+
the :code:`return_scaled` argument, determines whether the data is converted from integer values to :math:`\mu V` or not.
183+
By default, :code:`return_scaled` is true and all processed data voltage values are in :math:`\mu V` (e.g., waveforms, templates, spike amplitudes, etc.).
185184

186185
Now we will create a :code:`SortingAnalyzer` called :code:`sorting_analyzer`.
187186

doc/modules/postprocessing.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,21 +301,30 @@ template_metrics
301301
This extension computes commonly used waveform/template metrics.
302302
By default, the following metrics are computed:
303303

304-
* "peak_to_valley": duration between negative and positive peaks
305-
* "halfwidth": duration in s at 50% of the amplitude
304+
* "peak_to_valley": duration in :math:`s` between negative and positive peaks
305+
* "halfwidth": duration in :math:`s` at 50% of the amplitude
306306
* "peak_to_trough_ratio": ratio between negative and positive peaks
307-
* "recovery_slope": speed in V/s to recover from the negative peak to 0
308-
* "repolarization_slope": speed in V/s to repolarize from the positive peak to 0
307+
* "recovery_slope": speed to recover from the negative peak to 0
308+
* "repolarization_slope": speed to repolarize from the positive peak to 0
309309
* "num_positive_peaks": the number of positive peaks
310310
* "num_negative_peaks": the number of negative peaks
311311

312+
The units of :code:`recovery_slope` and :code:`repolarization_slope` depend on the
313+
input. Voltages are based on the units of the template. By default this is :math:`\mu V`
314+
but can be the raw output from the recording device (this depends on the
315+
:code:`return_scaled` parameter, read more here: :ref:`core-sorting-analyzer`).
316+
Distances are in :math:`\mu m` and times are in seconds. So, for example, if the
317+
templates are in units of :math:`\mu V` then: :code:`repolarization_slope` is in
318+
:math:`mV / s`; :code:`peak_to_trough_ratio` is in :math:`\mu m` and the
319+
:code:`halfwidth` is in :math:`s`.
320+
312321
Optionally, the following multi-channel metrics can be computed by setting:
313322
:code:`include_multi_channel_metrics=True`
314323

315-
* "velocity_above": the velocity above the max channel of the template
316-
* "velocity_below": the velocity below the max channel of the template
317-
* "exp_decay": the exponential decay of the template amplitude over distance
318-
* "spread": the spread of the template amplitude over distance
324+
* "velocity_above": the velocity in :math:`\mu m/s` above the max channel of the template
325+
* "velocity_below": the velocity in :math:`\mu m/s` below the max channel of the template
326+
* "exp_decay": the exponential decay in :math:`\mu m` of the template amplitude over distance
327+
* "spread": the spread in :math:`\mu m` of the template amplitude over distance
319328

320329
.. figure:: ../images/1d_waveform_features.png
321330

src/spikeinterface/postprocessing/template_metrics.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ def get_repolarization_slope(template_single, sampling_frequency, trough_idx=Non
410410
411411
After reaching it's maximum polarization, the neuron potential will
412412
recover. The repolarization slope is defined as the dV/dT of the action potential
413-
between trough and baseline.
413+
between trough and baseline. The returned slope is in units of (unit of template)
414+
per second. By default traces are scaled to units of uV, controlled
415+
by `sorting_analyzer.return_scaled`. In this case this function returns the slope
416+
in uV/s.
414417
415418
Parameters
416419
----------
@@ -454,11 +457,10 @@ def get_recovery_slope(template_single, sampling_frequency, peak_idx=None, **kwa
454457
Return the recovery slope of input waveforms. After repolarization,
455458
the neuron hyperpolarizes until it peaks. The recovery slope is the
456459
slope of the action potential after the peak, returning to the baseline
457-
in dV/dT. The slope is computed within a user-defined window after
458-
the peak.
459-
460-
Takes a numpy array of waveforms and returns an array with
461-
recovery slopes per waveform.
460+
in dV/dT. The returned slope is in units of (unit of template)
461+
per second. By default traces are scaled to units of uV, controlled
462+
by `sorting_analyzer.return_scaled`. In this case this function returns the slope
463+
in uV/s. The slope is computed within a user-defined window after the peak.
462464
463465
Parameters
464466
----------
@@ -619,7 +621,7 @@ def fit_velocity(peak_times, channel_dist):
619621

620622
def get_velocity_above(template, channel_locations, sampling_frequency, **kwargs):
621623
"""
622-
Compute the velocity above the max channel of the template.
624+
Compute the velocity above the max channel of the template in units um/s.
623625
624626
Parameters
625627
----------
@@ -697,7 +699,7 @@ def get_velocity_above(template, channel_locations, sampling_frequency, **kwargs
697699

698700
def get_velocity_below(template, channel_locations, sampling_frequency, **kwargs):
699701
"""
700-
Compute the velocity below the max channel of the template.
702+
Compute the velocity below the max channel of the template in units um/s.
701703
702704
Parameters
703705
----------
@@ -775,7 +777,7 @@ def get_velocity_below(template, channel_locations, sampling_frequency, **kwargs
775777

776778
def get_exp_decay(template, channel_locations, sampling_frequency=None, **kwargs):
777779
"""
778-
Compute the exponential decay of the template amplitude over distance.
780+
Compute the exponential decay of the template amplitude over distance in units um/s.
779781
780782
Parameters
781783
----------
@@ -788,6 +790,11 @@ def get_exp_decay(template, channel_locations, sampling_frequency=None, **kwargs
788790
**kwargs: Required kwargs:
789791
- exp_peak_function: the function to use to compute the peak amplitude for the exp decay ("ptp" or "min")
790792
- min_r2_exp_decay: the minimum r2 to accept the exp decay fit
793+
794+
Returns
795+
-------
796+
exp_decay_value : float
797+
The exponential decay of the template amplitude
791798
"""
792799
from scipy.optimize import curve_fit
793800
from sklearn.metrics import r2_score
@@ -853,7 +860,7 @@ def exp_decay(x, decay, amp0, offset):
853860

854861
def get_spread(template, channel_locations, sampling_frequency, **kwargs):
855862
"""
856-
Compute the spread of the template amplitude over distance.
863+
Compute the spread of the template amplitude over distance in units um/s.
857864
858865
Parameters
859866
----------
@@ -867,6 +874,11 @@ def get_spread(template, channel_locations, sampling_frequency, **kwargs):
867874
- depth_direction: the direction to compute velocity above and below ("x", "y", or "z")
868875
- spread_threshold: the threshold to compute the spread
869876
- column_range: the range in um in the x-direction to consider channels for velocity
877+
878+
Returns
879+
-------
880+
spread : float
881+
Spread of the template amplitude
870882
"""
871883
assert "depth_direction" in kwargs, "depth_direction must be given as kwarg"
872884
depth_direction = kwargs["depth_direction"]

0 commit comments

Comments
 (0)