From f7e8665d6f017d311027003852b78c165bc9c6d8 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Fri, 5 Apr 2024 18:39:04 +0200 Subject: [PATCH] fix bug in __AP_end_indices to avoid invalid min_element range --- efel/cppcore/LibV5.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/efel/cppcore/LibV5.cpp b/efel/cppcore/LibV5.cpp index 28a7f01c..c6b4908a 100644 --- a/efel/cppcore/LibV5.cpp +++ b/efel/cppcore/LibV5.cpp @@ -402,15 +402,22 @@ static int __AP_end_indices(const vector& t, const vector& v, peak_indices.push_back(v.size() - 1); for (size_t i = 0; i < peak_indices.size() - 1; i++) { - max_slope = - distance(dvdt.begin(), std::min_element(dvdt.begin() + peak_indices[i] + 1, - dvdt.begin() + peak_indices[i + 1])); + size_t start_index = peak_indices[i] + 1; + size_t end_index = peak_indices[i + 1]; + + if (start_index >= end_index || start_index >= dvdt.size() || end_index >= dvdt.size()) { + continue; + } + + auto min_element_it = std::min_element(dvdt.begin() + start_index, dvdt.begin() + end_index); + auto max_slope = std::distance(dvdt.begin(), min_element_it); // assure that the width of the slope is bigger than 4 - apei.push_back(distance(dvdt.begin(), find_if(dvdt.begin() + max_slope, - dvdt.begin() + peak_indices[i + 1], - [derivativethreshold](double x) { - return x >= derivativethreshold; - }))); + auto threshold_it = std::find_if(dvdt.begin() + max_slope, dvdt.begin() + end_index, + [derivativethreshold](double x) { return x >= derivativethreshold; }); + + if (threshold_it != dvdt.begin() + end_index) { + apei.push_back(std::distance(dvdt.begin(), threshold_it)); + } } return apei.size(); } @@ -1343,7 +1350,7 @@ double __decay_time_constant_after_stim(const vector& times, if (decayTimes.size() < 1 || decayValues.size() < 1) { throw FeatureComputationError("No data points to calculate decay_time_constant_after_stim"); - } + } linear_fit_result fit; fit = slope_straight_line_fit(decayTimes, decayValues);