Peaks
A module for peak detection and metric calculation.
Peak detection and peak metric extraction utilities.
PeakResult
dataclass
Container for a table of detected peak metrics.
one_peak_per_trial
property
Whether each trial represented in the table has exactly one peak.
filter
filter(on: str, how: Literal['lowest', 'highest', 'closest', 'between', 'equals'], value: None | str | float | tuple[float, float]) -> Self
Filter peaks globally or within each trial.
Parameters:
-
on(str) –Column used for filtering.
-
how(('lowest', 'highest', 'closest', 'between', 'equals'), default:'lowest') –Filtering strategy.
'lowest','highest', and'closest'select one row per trial;'between'and'equals'keep all matching rows. -
value(None, str, float, or tuple[float, float]) –Comparison value.
'between'requires a two-value tuple;'closest'requires a scalar numeric value.
Returns:
-
PeakResult–Filtered peak result.
Raises:
-
KeyError–If
onis not a column. -
ValueError–If
howis unknown orvalueis incompatible withhow.
Source code in PhoPro/analysis/peaks.py
to_csv
from_csv
classmethod
Read peak metrics from CSV.
Parameters:
-
path(str) –Input CSV path.
Returns:
-
PeakResult–Peak result loaded from disk.
Source code in PhoPro/analysis/peaks.py
SinglePeak
SinglePeak(trial_idx: int, bounds: tuple[int, int], signal: ndarray, time: ndarray, centers: ndarray = asarray(0.0), direction: Literal['positive', 'negative'] = 'positive')
Calculate metrics for one detected peak interval.
Initialize one peak and calculate its metrics.
Parameters:
-
trial_idx(int) –Trial index containing this peak.
-
bounds(tuple[int, int]) –Inclusive
(start_idx, stop_idx)bounds for the peak. -
signal(ndarray) –One-dimensional signal trace for the trial.
-
time(ndarray) –Time values aligned with
signal. -
centers(ndarray, default:np.asarray(0.0)) –Baseline or center trace aligned with
signal. -
direction(('positive', 'negative'), default:'positive') –Direction used to calculate signed peak metrics.
Source code in PhoPro/analysis/peaks.py
export_attrs
classmethod
Return attribute names exported for each peak.
Parameters:
-
full(bool, default:False) –If
True, include extra left and right prominence metrics.
Returns:
-
tuple[str, ...]–Exported attribute names.
Source code in PhoPro/analysis/peaks.py
n_export_attrs
classmethod
Return the number of exported attributes.
Parameters:
-
full(bool, default:False) –If
True, include extra left and right prominence metrics.
Returns:
-
int–Number of exported attributes.
Source code in PhoPro/analysis/peaks.py
export_columns
classmethod
Return exported attribute names as a list.
Parameters:
-
full(bool, default:False) –If
True, include extra left and right prominence metrics.
Returns:
-
list[str]–Exported column names.
Source code in PhoPro/analysis/peaks.py
calc_metrics
Calculate and store metrics for this peak.
Parameters:
-
y(ndarray) –Peak-window signal values.
-
x(ndarray) –Peak-window time values.
-
b(ndarray) –Peak-window baseline values.
Source code in PhoPro/analysis/peaks.py
width_at_half_max
Calculate peak width at half maximum.
Parameters:
-
y(ndarray) –Peak-window signal values.
-
x(ndarray) –Peak-window time values.
-
b(ndarray) –Peak-window baseline values.
-
peak_local_idx(int) –Peak index relative to the peak window.
Returns:
-
float–Width between the left and right half-maximum crossings.
Source code in PhoPro/analysis/peaks.py
export_as_array
Export peak metrics as an array.
Parameters:
-
full(bool, default:False) –If
True, include extra left and right prominence metrics.
Returns:
-
ndarray–One-dimensional array of exported metric values.
Source code in PhoPro/analysis/peaks.py
export_into_array
Write peak metrics into a preallocated array row.
Parameters:
-
target(ndarray) –Output array.
-
row(int) –Row in
targetto write into. -
full(bool, default:False) –If
True, include extra left and right prominence metrics.
Source code in PhoPro/analysis/peaks.py
export_dict
Export peak metrics as a dictionary.
Parameters:
-
full(bool, default:False) –If
True, include extra left and right prominence metrics.
Returns:
-
dict[str, Any]–Mapping from metric names to values.
Source code in PhoPro/analysis/peaks.py
PeakMask
Mutable two-dimensional boolean mask of detected peak samples.
Initialize a peak mask.
Parameters:
-
mask(ndarray) –Two-dimensional boolean-like array with shape
(n_trials, n_times).
Source code in PhoPro/analysis/peaks.py
merge_close
Merge peaks separated by short gaps.
Parameters:
-
min_distance(int or None) –Maximum gap length, in samples, to fill between adjacent peaks.
Noneand0leave the mask unchanged.
Source code in PhoPro/analysis/peaks.py
filter_size
Remove peak runs outside size bounds.
Parameters:
-
min_size(int or None, default:None) –Minimum peak size in samples.
-
max_size(int or None, default:None) –Maximum peak size in samples.
Source code in PhoPro/analysis/peaks.py
get_peak_intervals
Return inclusive peak intervals from the mask.
Returns:
-
trial_start(ndarray) –Trial index for each detected peak.
-
peak_intervals(ndarray) –Integer array with shape
(n_peaks, 2)containing inclusivestart_idxandstop_idxbounds.
Raises:
-
ValueError–If detected start and stop edges do not belong to the same trials.
Source code in PhoPro/analysis/peaks.py
PeakDetector
Bases: ABC
Base class for peak detectors.
handle_peaks_iterative
handle_peaks_iterative(signals: ndarray, time: ndarray, centers: ndarray, trial_idxs: ndarray, intervals: ndarray, direction: Literal['positive', 'negative'] = 'positive', full_output: bool = False) -> DataFrame
Calculate peak metrics for detected intervals.
Parameters:
-
signals(ndarray) –Trial-by-time signal matrix.
-
time(ndarray) –Time values for signal columns.
-
centers(ndarray) –Trial-by-time baseline or center matrix.
-
trial_idxs(ndarray) –Trial index for each peak interval.
-
intervals(ndarray) –Inclusive peak intervals with shape
(n_peaks, 2). -
direction(('positive', 'negative'), default:'positive') –Peak direction used for signed metrics.
-
full_output(bool, default:False) –If
True, include extra peak metrics.
Returns:
-
DataFrame–Table of peak metrics.
Source code in PhoPro/analysis/peaks.py
process_peak_mask
process_peak_mask(peak_mask: PeakMask, frequency: float, min_distance_sec: float | None, min_duration_sec: float | None, max_duration_sec: float | None) -> PeakMask
Merge and size-filter a peak mask.
Parameters:
-
peak_mask(PeakMask) –Peak mask to process in place.
-
frequency(float) –Sampling frequency in Hz.
-
min_distance_sec(float or None) –Minimum distance between peaks, in seconds.
-
min_duration_sec(float or None) –Minimum peak duration, in seconds.
-
max_duration_sec(float or None) –Maximum peak duration, in seconds.
Returns:
-
PeakMask–Processed peak mask.
Source code in PhoPro/analysis/peaks.py
detect
abstractmethod
detect(signals: ndarray, time: ndarray, frequency: float, baselines: ndarray | None = None, min_distance_sec: float | None = None, min_duration_sec: float | None = None, max_duration_sec: float | None = None, direction: Literal['positive', 'negative', 'both'] = 'both', detailed: bool = False) -> PeakResult
Detect peaks in trial-by-time signals.
Parameters:
-
signals(ndarray) –Trial-by-time signal matrix.
-
time(ndarray) –Time values for signal columns.
-
frequency(float) –Sampling frequency in Hz.
-
baselines(ndarray or None, default:None) –Optional baseline signals.
-
min_distance_sec(float or None, default:None) –Minimum distance between peaks, in seconds.
-
min_duration_sec(float or None, default:None) –Minimum peak duration, in seconds.
-
max_duration_sec(float or None, default:None) –Maximum peak duration, in seconds.
-
direction(('positive', 'negative', 'both'), default:'positive') –Peak direction to detect.
-
detailed(bool, default:False) –If
True, include extra peak metrics.
Returns:
-
PeakResult–Detected peaks and metrics.
Source code in PhoPro/analysis/peaks.py
ThresholdDetector
ThresholdDetector(center_method: Literal['median', 'mean', 'zeros'] | Callable = 'median', scale_method: Literal['mad', 'std', 'ones'] | Callable = 'mad', test_magnitude: float = 3.0)
Bases: PeakDetector
Base class for threshold-based peak detectors.
Initialize a threshold detector.
Parameters:
-
center_method(('median', 'mean', 'zeros'), default:'median') –Method used to estimate baseline center.
-
scale_method(('mad', 'std', 'ones'), default:'mad') –Method used to estimate baseline scale.
-
test_magnitude(float, default:3.0) –Scale multiplier used to set the detection threshold.
Source code in PhoPro/analysis/peaks.py
StaticThresholdDetector
StaticThresholdDetector(center_method: Literal['median', 'mean', 'zeros'] | Callable = 'median', scale_method: Literal['mad', 'std', 'ones'] | Callable = 'mad', test_magnitude: float = 3.0)
Bases: ThresholdDetector
Detect peaks using one static threshold per trial.
Initialize a static threshold detector.
Parameters:
-
center_method(('median', 'mean', 'zeros'), default:'median') –Method used to estimate each trial's baseline center.
-
scale_method(('mad', 'std', 'ones'), default:'mad') –Method used to estimate each trial's baseline scale.
-
test_magnitude(float, default:3.0) –Scale multiplier used to set the detection threshold.
Source code in PhoPro/analysis/peaks.py
detect
detect(signals: ndarray, time: ndarray, frequency: float, baselines: ndarray | None = None, min_distance_sec: float | None = None, min_duration_sec: float | None = None, max_duration_sec: float | None = None, direction: Literal['positive', 'negative', 'both'] = 'both', detailed: bool = False) -> PeakResult
Detect peaks using static baseline-derived thresholds.
Parameters:
-
signals(ndarray) –Trial-by-time signal matrix.
-
time(ndarray) –Time values for signal columns.
-
frequency(float) –Sampling frequency in Hz.
-
baselines(ndarray or None, default:None) –Baseline signals used to estimate threshold centers and scales. If
None,signalsare used. -
min_distance_sec(float or None, default:None) –Minimum distance between peaks, in seconds.
-
min_duration_sec(float or None, default:None) –Minimum peak duration, in seconds.
-
max_duration_sec(float or None, default:None) –Maximum peak duration, in seconds.
-
direction(('positive', 'negative', 'both'), default:'positive') –Peak direction to detect.
-
detailed(bool, default:False) –If
True, include extra peak metrics.
Returns:
-
PeakResult–Detected peaks and metrics.
Source code in PhoPro/analysis/peaks.py
RollingThresholdDetector
RollingThresholdDetector(window_width_sec: float = 5.0, center_method: Literal['median', 'mean', 'zeros'] | Callable = 'median', scale_method: Literal['mad', 'std', 'ones'] | Callable = 'mad', test_magnitude: float = 3.0)
Bases: ThresholdDetector
Detect peaks using rolling threshold estimates.
Initialize a rolling threshold detector.
Parameters:
-
window_width_sec(float, default:5.0) –Rolling window width, in seconds.
-
center_method(('median', 'mean', 'zeros'), default:'median') –Method used to estimate rolling center.
-
scale_method(('mad', 'std', 'ones'), default:'mad') –Method used to estimate rolling scale.
-
test_magnitude(float, default:3.0) –Scale multiplier used to set the detection threshold.
Source code in PhoPro/analysis/peaks.py
detect
detect(signals: ndarray, time: ndarray, frequency: float, baselines: ndarray | None = None, min_distance_sec: float | None = None, min_duration_sec: float | None = None, max_duration_sec: float | None = None, direction: Literal['positive', 'negative', 'both'] = 'both', detailed: bool = False) -> PeakResult
Detect peaks using rolling signal-derived thresholds.
Parameters:
-
signals(ndarray) –Trial-by-time signal matrix.
-
time(ndarray) –Time values for signal columns.
-
frequency(float) –Sampling frequency in Hz.
-
baselines(ndarray or None, default:None) –Accepted for API compatibility but not used.
-
min_distance_sec(float or None, default:None) –Minimum distance between peaks, in seconds.
-
min_duration_sec(float or None, default:None) –Minimum peak duration, in seconds.
-
max_duration_sec(float or None, default:None) –Maximum peak duration, in seconds.
-
direction(('positive', 'negative', 'both'), default:'positive') –Peak direction to detect.
-
detailed(bool, default:False) –If
True, include extra peak metrics.
Returns:
-
PeakResult–Detected peaks and metrics.
Source code in PhoPro/analysis/peaks.py
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 | |
TemplateMatchingDetector
nanmad
Calculate median absolute deviation while ignoring NaNs.
Parameters:
-
x(ndarray) –Input values.
-
axis(int) –Axis along which to calculate the statistic.
Returns:
-
ndarray–NaN-robust MAD values scaled by
1.4826.
Source code in PhoPro/analysis/peaks.py
one_scale
Return ones with the input shape reduced over axis.
Parameters:
-
x(ndarray) –Input values.
-
axis(int) –Axis removed from the output shape.
Returns:
-
ndarray–Array of ones with the reduced shape.
Source code in PhoPro/analysis/peaks.py
zero_center
Return zeros with the input shape reduced over axis.
Parameters:
-
x(ndarray) –Input values.
-
axis(int) –Axis removed from the output shape.
Returns:
-
ndarray–Array of zeros with the reduced shape.