1818import numpy as np
1919
2020from 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
2222from spikeinterface .core import SortingAnalyzer , get_noise_levels
2323from spikeinterface .core .template_tools import (
2424 get_template_extremum_channel ,
3939
4040def 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
112113def _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