Skip to content

Commit 373cf64

Browse files
committed
reply to review
1 parent 7b107e9 commit 373cf64

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

doc/modules/qualitymetrics/noise_cutoff.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ instead, it directly compares counts in the low‐amplitude bins to counts in hi
1515
1. **Build a histogram**
1616

1717
For each unit, divide all amplitudes into `n_bins` equally spaced bins over the range of the amplitude.
18+
If the number of spikes is large, you may consider using a larger `n_bins`. For a small number of spikes, consider a smaller `n_bins`.
1819
Let `n_i` denote the count in the :math:`i`-th bin.
1920

2021
2. **Identify the “low” region**
@@ -28,7 +29,7 @@ instead, it directly compares counts in the low‐amplitude bins to counts in hi
2829
2930
3. **Identify the “high” region**
3031

31-
- Compute the amplitude value at the specified `high_quantile` (for example, 0.25 = top 25 percentile), denoted as :math:`\text{amp}_{high}`.
32+
- Compute the amplitude value at the specified `high_quantile` (for example, 0.25 = top 25th percentile), denoted as :math:`\text{amp}_{high}`.
3233
- Find all histogram bins whose lower edge is greater than that quantile value. These bins form the “high‐quantile region.”
3334
- Compute
3435

@@ -50,7 +51,7 @@ instead, it directly compares counts in the low‐amplitude bins to counts in hi
5051

5152
5. **Compute the low-to-peak ratio**
5253

53-
- Let :math:`M = \max_i\,n_i` be the height of the peak bin in the histogram.
54+
- Let :math:`M = \max_i\,n_i` be the height of the largest bin in the histogram.
5455
- Define
5556

5657
.. math::
@@ -101,7 +102,7 @@ Reference
101102
Examples with plots
102103
-------------------
103104

104-
Here is shown the histogram of two units, with the vertical lines separating low- and high-amplitude region.
105+
Here is shown the histogram of two units, with the vertical lines separating low- and high-amplitude regions.
105106

106107
- On the left, we have a unit with no truncation at the left end, and the cutoff and ratio are small.
107108
- On the right, we have a unit with truncation at -1, and the cutoff and ratio are much larger.
@@ -114,7 +115,7 @@ Links to original implementations
114115

115116
* From `IBL implementation <https://github.com/int-brain-lab/ibllib/blob/2e1f91c622ba8dbd04fc53946c185c99451ce5d6/brainbox/metrics/single_units.py>`_
116117

117-
Note: Compared to the original implementation, we have added a comparison between the low-amplitude bins to the peak bin (`noise_ratio`).
118+
Note: Compared to the original implementation, we have added a comparison between the low-amplitude bins to the largest bin (`noise_ratio`).
118119
The selection of low-amplitude bins is based on the `low_quantile` rather than the number of bins.
119120

120121
Literature

src/spikeinterface/qualitymetrics/misc_metrics.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919

2020
from spikeinterface.core.job_tools import fix_job_kwargs, split_job_kwargs
21-
from spikeinterface.postprocessing import correlogram_for_one_segment
21+
from spikeinterface.postprocessing import correlogpram_for_one_segment
2222
from spikeinterface.core import SortingAnalyzer, get_noise_levels
2323
from spikeinterface.core.template_tools import (
2424
get_template_extremum_channel,
@@ -39,9 +39,8 @@
3939

4040
def compute_noise_cutoffs(sorting_analyzer, high_quantile=0.25, low_quantile=0.1, n_bins=100, unit_ids=None):
4141
"""
42-
A metric to determine if a unit's amplitude distribution is cut off, without assuming a Gaussian distribution.
42+
A metric to determine if a unit's amplitude distribution is cut off as it approaches zero, without assuming a Gaussian distribution.
4343
44-
The function flips the sign if 'peak_sign' == 'neg' when computing the amplitude.
4544
Based on the histogram of the (transformed) amplitude:
4645
4746
1. This method compares counts in the lower-amplitude bins to counts in the top 'high_quantile' of the amplitude range.
@@ -88,14 +87,16 @@ def compute_noise_cutoffs(sorting_analyzer, high_quantile=0.25, low_quantile=0.1
8887

8988
amplitude_extension = sorting_analyzer.get_extension("spike_amplitudes")
9089
peak_sign = amplitude_extension.params["peak_sign"]
91-
amplitudes_by_units = _get_amplitudes_by_units(sorting_analyzer, unit_ids, peak_sign)
92-
9390
if peak_sign == "both":
94-
raise TypeError('peak_sign should either be "pos" or "neg"!')
91+
raise TypeError('`peak_sign` should either be "pos" or "neg". You can set `peak_sign` as an argument when you compute spike_amplitudes.')
92+
93+
amplitudes_by_units = _get_amplitudes_by_units(sorting_analyzer, unit_ids, peak_sign)
9594

9695
for unit_id in unit_ids:
9796
amplitudes = amplitudes_by_units[unit_id]
9897

98+
# We assume the noise (zero values) is on the lower tail of the amplitude distribution.
99+
# But if peak_sign == 'neg', the noise will be on the higher tail, so we flip the distribution.
99100
if peak_sign == "neg":
100101
amplitudes = -amplitudes
101102

@@ -111,9 +112,8 @@ def compute_noise_cutoffs(sorting_analyzer, high_quantile=0.25, low_quantile=0.1
111112

112113
def _noise_cutoff(amps, high_quantile=0.25, low_quantile=0.1, n_bins=100):
113114
"""
114-
A metric to determine if a unit's amplitude distribution is cut off, without assuming a Gaussian distribution.
115+
A metric to determine if a unit's amplitude distribution is cut off as it approaches zero, without assuming a Gaussian distribution.
115116
116-
The function flips the sign if 'peak_sign' == 'neg' when computing the amplitude.
117117
Based on the histogram of the (transformed) amplitude:
118118
119119
1. This method compares counts in the lower-amplitude bins to counts in the higher_amplitude bins.
@@ -143,7 +143,7 @@ def _noise_cutoff(amps, high_quantile=0.25, low_quantile=0.1, n_bins=100):
143143
"""
144144
n_per_bin, bin_edges = np.histogram(amps, bins=n_bins)
145145

146-
peak = np.max(n_per_bin)
146+
maximum_bin_height = np.max(n_per_bin)
147147

148148
low_quantile_value = np.quantile(amps, q=low_quantile)
149149

@@ -156,16 +156,21 @@ def _noise_cutoff(amps, high_quantile=0.25, low_quantile=0.1, n_bins=100):
156156
high_indices = np.where(bin_edges[:-1] >= high_quantile_value)[0]
157157

158158
if len(low_indices) == 0:
159-
warnings.warn("no bin is selected to test cutoff. please increase low_quantile.")
159+
warnings.warn("No bin is selected to test cutoff. Please increase low_quantile.")
160160
return np.nan, np.nan
161161

162-
# compute ratio between low-amplitude bins and the peak bin
162+
# compute ratio between low-amplitude bins and the largest bin
163163
low_counts = n_per_bin[low_indices]
164164
mean_low_counts = np.mean(low_counts)
165-
ratio = mean_low_counts / peak
165+
ratio = mean_low_counts / maximum_bin_height
166166

167167
if len(high_indices) == 0:
168-
warnings.warn("no bin is selected as the reference region. please increase high_quantile.")
168+
warnings.warn("No bin is selected as the reference region. Please increase high_quantile.")
169+
return np.nan, ratio
170+
171+
if len(high_indices) == 1:
172+
warnings.warn("Only one bin is selected as the reference region, and thus the standard deviation cannot be computed. " \
173+
"Please increase high_quantile.")
169174
return np.nan, ratio
170175

171176
# compute cutoff from low-amplitude and high-amplitude bins
@@ -174,7 +179,7 @@ def _noise_cutoff(amps, high_quantile=0.25, low_quantile=0.1, n_bins=100):
174179
std_high_counts = np.std(high_counts)
175180
if std_high_counts == 0:
176181
warnings.warn(
177-
"only one bin is selected as the reference region, and thus the standard deviation cannot be computed. please increase high_quantile."
182+
"All the high-amplitude bins have the same size. Please consider changing n_bins."
178183
)
179184
return np.nan, ratio
180185

0 commit comments

Comments
 (0)