Files
Jungfraujoch/image_analysis/indexing/FitProfileRadius.cpp
2025-09-08 20:28:59 +02:00

35 lines
1.1 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "FitProfileRadius.h"
#include <algorithm> // std::nth_element
#include <cmath> // std::fabs
std::optional<float> FitProfileRadius_MAD(const std::vector<SpotToSave>& xs) {
std::vector<float> absx;
absx.reserve(xs.size());
for (const auto &s: xs) {
if (s.indexed)
absx.push_back(std::fabs(s.dist_ewald_sphere));
}
if (absx.empty())
return std::nullopt;
std::nth_element(absx.begin(), absx.begin() + absx.size() / 2, absx.end());
float med;
if (absx.size() % 2 == 1) {
med = absx[absx.size() / 2];
} else {
auto it1 = absx.begin() + (absx.size() / 2 - 1);
auto it2 = absx.begin() + (absx.size() / 2);
float a = *it1;
float b = *it2;
med = 0.5f * (a + b);
}
// Normal consistency factor for MAD
return 1.4826f * med;
}