Skip to content

Commit 8382fbf

Browse files
authored
Merge pull request #105 from AllenNeuralDynamics/kh-skewness
Adding skewness estimation function to snr_kurtosis
2 parents 1b87340 + ef10ac2 commit 8382fbf

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

src/aind_dynamic_foraging_basic_analysis/metrics/snr_kurtosis.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- :func:`estimate_snr` — an SNR estimator using a derivative-based noise
66
estimate and peak-based signal estimate.
77
- :func:`estimate_kurtosis` — excess kurtosis of the trace distribution.
8+
- :func:`estimate_skewness` — skewness of the trace distribution.
89
910
Notes
1011
-----
@@ -27,6 +28,8 @@
2728
True
2829
>>> isinstance(estimate_kurtosis(y), float)
2930
True
31+
>>> isinstance(estimate_skewness(y), float)
32+
True
3033
"""
3134

3235
from __future__ import annotations
@@ -37,9 +40,9 @@
3740
import numpy as np
3841
from numpy.typing import NDArray
3942
from scipy.signal import find_peaks
40-
from scipy.stats import kurtosis
43+
from scipy.stats import kurtosis, skew
4144

42-
__all__ = ["estimate_snr", "estimate_kurtosis"]
45+
__all__ = ["estimate_snr", "estimate_kurtosis", "estimate_skewness"]
4346

4447

4548
def estimate_snr(
@@ -132,3 +135,27 @@ def estimate_kurtosis(trace: NDArray[np.floating]) -> float:
132135

133136
# Excess kurtosis (normal distribution = 0)
134137
return float(kurtosis(trace, fisher=True, bias=False))
138+
139+
140+
def estimate_skewness(trace: NDArray[np.floating]) -> float:
141+
"""
142+
Compute the **skewness** of a 1D trace distribution.
143+
144+
Parameters
145+
----------
146+
trace : numpy.ndarray
147+
1D input trace. NaNs will be replaced with the median of ``trace``.
148+
149+
Returns
150+
-------
151+
float
152+
Skewness of the distribution:
153+
- Symmetric distribution → 0.0
154+
- Right-skewed (positive skew) → positive
155+
- Left-skewed (negative skew) → negative
156+
"""
157+
# Replace NaNs with the median of the trace
158+
trace = np.nan_to_num(trace, nan=np.nanmedian(trace))
159+
160+
# Skewness
161+
return float(skew(trace, bias=False))

0 commit comments

Comments
 (0)