40 auto iter = pdf.
begin();
41 while (iter != pdf.
end()) {
42 xs.push_back((*iter).first);
43 ys.push_back((*iter).second);
54 for (
auto iter = pdf.
begin(); iter != pdf.
end(); ++iter) {
67 double peak_value = pdf[center_index];
68 double threshold = (1.0 - merge_ratio) * peak_value;
70 size_t max_x = pdf.
size() - 1;
72 for (
size_t index = 0; index <= center_index; ++index) {
73 size_t position = center_index - index;
74 if (position == 0 || pdf[position] == 0) {
77 if ((pdf[position] < threshold && pdf[position - 1] >= pdf[position])) {
84 for (
size_t position = center_index; position <= pdf.
size(); ++position) {
85 if (position == pdf.
size() - 1 || pdf[position] == 0) {
88 if ((pdf[position] < threshold && pdf[position + 1] >= pdf[position])) {
104 for (
size_t index = min_x; index <= max_x; ++index) {
107 dx = pdf.first[index + 1] - pdf.first[index];
108 }
else if (index == pdf.first.size() - 1) {
109 dx = pdf.first[index] - pdf.first[index - 1];
111 dx = (pdf.first[index + 1] - pdf.first[index - 1]) / 2.0;
114 num += pdf.first[index] * pdf.second[index] * dx;
115 area += pdf.second[index] * dx;
128 if (x_index >= pdf.first.size()) {
135 }
else if (x_index + 1 == pdf.first.size()) {
139 double x0 = pdf.first[x_index - 1];
140 double y0 = pdf.second[x_index - 1];
142 double x1 = pdf.first[x_index];
143 double y1 = pdf.second[x_index];
145 double x2 = pdf.first[x_index + 1];
146 double y2 = pdf.second[x_index + 1];
148 double denom = 2 * ((x1 - x0) * y2 - (x2 - x0) * y1 + (x2 - x1) * y0);
149 double num = (x1 * x1 - x0 * x0) * y2 - (x2 * x2 - x0 * x0) * y1 + (x2 * x2 - x1 * x1) * y0;
158 if (x_max < pdf.first.front()) {
159 x_max = pdf.first.front();
160 }
else if (x_max > pdf.first.back()) {
161 x_max = pdf.first.back();
170 for (
size_t index = min_x; index <= max_x; ++index) {
171 flatterned[index] = value;
177 double merge_ratio,
size_t n) {
181 for (
size_t idx = 0; idx < n; ++idx) {
183 auto min_max =
catchPeak(pdf_xy.second, peak_index, merge_ratio);
184 auto mean_area =
avgArea(pdf_xy, min_max.first, min_max.second);
187 result.push_back(
ModeInfo(pdf_xy.first[peak_index], mean_area.first, max_inter, mean_area.second));
188 pdf_xy =
flatternPeak(pdf_xy, min_max.first, min_max.second, 0.0);
194 auto pdf_xy =
getXYs(pdf);
199 double merge_ratio,
size_t n) {
201 double total_area =
avgArea(pdf_xy, 0, x_sampling.
size() - 1).second;
210 auto min_max =
catchPeak(pdf_xy.second, peak_index, merge_ratio);
211 auto mean_area =
avgArea(pdf_xy, min_max.first, min_max.second);
212 auto max_sample = pdf_xy.first[peak_index];
214 auto new_mode =
ModeInfo(max_sample, mean_area.first, interp_max_value, mean_area.second);
216 auto iter_mode = result.begin();
217 while (iter_mode != result.end()) {
218 if ((*iter_mode).getModeArea() < mean_area.second) {
224 if (iter_mode != result.end()) {
225 result.insert(iter_mode, new_mode);
226 }
else if (result.size() < n) {
227 result.push_back(new_mode);
229 total_area -= mean_area.second;
230 out = result.size() >= n && (result.back().getModeArea() > total_area || loop > 3 * n);
232 pdf_xy =
flatternPeak(pdf_xy, min_max.first, min_max.second, 0.0);
240 auto pdf_xy =
getXYs(pdf);
Class for storing the information of a PDF mode.
This module provides an interface for accessing two dimensional datasets (pairs of (X,...
const_iterator begin() const
Returns a const iterator to the first pair of the dataset.
const_iterator end() const
Returns a const iterator to the one after last pair dataset.
std::pair< double, double > avgArea(std::pair< std::vector< double >, std::vector< double >> &pdf, size_t min_x, size_t max_x)
size_t findMaximumIndex(const std::vector< double > &pdf)
std::pair< std::vector< double >, std::vector< double > > getXYs(const XYDataset::XYDataset &pdf)
double getInterpolationAround(const std::pair< std::vector< double >, std::vector< double >> &pdf, size_t x_index)
std::pair< size_t, size_t > catchPeak(const std::vector< double > &pdf, size_t center_index, double merge_ratio)
std::pair< std::vector< double >, std::vector< double > > flatternPeak(const std::pair< std::vector< double >, std::vector< double >> &pdf, size_t min_x, size_t max_x, double value)
std::vector< ModeInfo > extractNBigestModes(const XYDataset::XYDataset &pdf, double merge_ratio, size_t n)
std::vector< ModeInfo > extractNHighestModes(const XYDataset::XYDataset &pdf, double merge_ratio, size_t n)