Skip to content

Commit

Permalink
Merge pull request #3066 from chrishalcrow/update-template-metric-docs
Browse files Browse the repository at this point in the history
Correct docs and docstrings for compute_template_metric units
  • Loading branch information
alejoe91 authored Jun 24, 2024
2 parents 772c85b + 227d91d commit d963aa2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
9 changes: 4 additions & 5 deletions doc/modules/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ All classes support:
* data on-demand (lazy loading)
* multiple segments, where each segment is a contiguous piece of data (recording, sorting, events).


.. _core-recording:
Recording
---------

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


.. _core-sorting-analyzer:
SortingAnalyzer
---------------

Expand All @@ -179,9 +179,8 @@ to perform further analysis, such as calculating :code:`waveforms` and :code:`te
Importantly, the :py:class:`~spikeinterface.core.SortingAnalyzer` handles the *sparsity* and the physical *scaling*.
Sparsity defines the channels on which waveforms and templates are calculated using, for example, a
physical distance from the channel with the largest peak amplitude (see the :ref:`Sparsity` section). Scaling, set by
the :code:`return_scaled` argument, says whether the data has been converted from integer values to physical units such as
Voltage (see the end of the :ref:`Recording` section).

the :code:`return_scaled` argument, determines whether the data is converted from integer values to :math:`\mu V` or not.
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.).

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

Expand Down
25 changes: 17 additions & 8 deletions doc/modules/postprocessing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,30 @@ template_metrics
This extension computes commonly used waveform/template metrics.
By default, the following metrics are computed:

* "peak_to_valley": duration between negative and positive peaks
* "halfwidth": duration in s at 50% of the amplitude
* "peak_to_valley": duration in :math:`s` between negative and positive peaks
* "halfwidth": duration in :math:`s` at 50% of the amplitude
* "peak_to_trough_ratio": ratio between negative and positive peaks
* "recovery_slope": speed in V/s to recover from the negative peak to 0
* "repolarization_slope": speed in V/s to repolarize from the positive peak to 0
* "recovery_slope": speed to recover from the negative peak to 0
* "repolarization_slope": speed to repolarize from the positive peak to 0
* "num_positive_peaks": the number of positive peaks
* "num_negative_peaks": the number of negative peaks

The units of :code:`recovery_slope` and :code:`repolarization_slope` depend on the
input. Voltages are based on the units of the template. By default this is :math:`\mu V`
but can be the raw output from the recording device (this depends on the
:code:`return_scaled` parameter, read more here: :ref:`core-sorting-analyzer`).
Distances are in :math:`\mu m` and times are in seconds. So, for example, if the
templates are in units of :math:`\mu V` then: :code:`repolarization_slope` is in
:math:`mV / s`; :code:`peak_to_trough_ratio` is in :math:`\mu m` and the
:code:`halfwidth` is in :math:`s`.

Optionally, the following multi-channel metrics can be computed by setting:
:code:`include_multi_channel_metrics=True`

* "velocity_above": the velocity above the max channel of the template
* "velocity_below": the velocity below the max channel of the template
* "exp_decay": the exponential decay of the template amplitude over distance
* "spread": the spread of the template amplitude over distance
* "velocity_above": the velocity in :math:`\mu m/s` above the max channel of the template
* "velocity_below": the velocity in :math:`\mu m/s` below the max channel of the template
* "exp_decay": the exponential decay in :math:`\mu m` of the template amplitude over distance
* "spread": the spread in :math:`\mu m` of the template amplitude over distance

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

Expand Down
32 changes: 22 additions & 10 deletions src/spikeinterface/postprocessing/template_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ def get_repolarization_slope(template_single, sampling_frequency, trough_idx=Non
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.
between trough and baseline. The returned slope is in units of (unit of template)
per second. By default traces are scaled to units of uV, controlled
by `sorting_analyzer.return_scaled`. In this case this function returns the slope
in uV/s.
Parameters
----------
Expand Down Expand Up @@ -454,11 +457,10 @@ def get_recovery_slope(template_single, sampling_frequency, peak_idx=None, **kwa
Return the recovery slope of input waveforms. After repolarization,
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.
Takes a numpy array of waveforms and returns an array with
recovery slopes per waveform.
in dV/dT. The returned slope is in units of (unit of template)
per second. By default traces are scaled to units of uV, controlled
by `sorting_analyzer.return_scaled`. In this case this function returns the slope
in uV/s. The slope is computed within a user-defined window after the peak.
Parameters
----------
Expand Down Expand Up @@ -619,7 +621,7 @@ def fit_velocity(peak_times, channel_dist):

def get_velocity_above(template, channel_locations, sampling_frequency, **kwargs):
"""
Compute the velocity above the max channel of the template.
Compute the velocity above the max channel of the template in units um/s.
Parameters
----------
Expand Down Expand Up @@ -697,7 +699,7 @@ def get_velocity_above(template, channel_locations, sampling_frequency, **kwargs

def get_velocity_below(template, channel_locations, sampling_frequency, **kwargs):
"""
Compute the velocity below the max channel of the template.
Compute the velocity below the max channel of the template in units um/s.
Parameters
----------
Expand Down Expand Up @@ -775,7 +777,7 @@ def get_velocity_below(template, channel_locations, sampling_frequency, **kwargs

def get_exp_decay(template, channel_locations, sampling_frequency=None, **kwargs):
"""
Compute the exponential decay of the template amplitude over distance.
Compute the exponential decay of the template amplitude over distance in units um/s.
Parameters
----------
Expand All @@ -788,6 +790,11 @@ def get_exp_decay(template, channel_locations, sampling_frequency=None, **kwargs
**kwargs: Required kwargs:
- exp_peak_function: the function to use to compute the peak amplitude for the exp decay ("ptp" or "min")
- min_r2_exp_decay: the minimum r2 to accept the exp decay fit
Returns
-------
exp_decay_value : float
The exponential decay of the template amplitude
"""
from scipy.optimize import curve_fit
from sklearn.metrics import r2_score
Expand Down Expand Up @@ -853,7 +860,7 @@ def exp_decay(x, decay, amp0, offset):

def get_spread(template, channel_locations, sampling_frequency, **kwargs):
"""
Compute the spread of the template amplitude over distance.
Compute the spread of the template amplitude over distance in units um/s.
Parameters
----------
Expand All @@ -867,6 +874,11 @@ def get_spread(template, channel_locations, sampling_frequency, **kwargs):
- depth_direction: the direction to compute velocity above and below ("x", "y", or "z")
- spread_threshold: the threshold to compute the spread
- column_range: the range in um in the x-direction to consider channels for velocity
Returns
-------
spread : float
Spread of the template amplitude
"""
assert "depth_direction" in kwargs, "depth_direction must be given as kwarg"
depth_direction = kwargs["depth_direction"]
Expand Down

0 comments on commit d963aa2

Please sign in to comment.