Files
Jungfraujoch/image_analysis/bragg_integration/Regression.h
T
leonarski_f bb9f5c715f
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 9m55s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 10m28s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 8m56s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 11m47s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 13m7s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 12m31s
Build Packages / build:rpm (rocky8) (push) Successful in 12m59s
Build Packages / build:rpm (rocky9) (push) Successful in 14m5s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 15m30s
Build Packages / Generate python client (push) Successful in 1m18s
Build Packages / Build documentation (push) Successful in 1m3s
Build Packages / Create release (push) Has been skipped
Build Packages / build:rpm (ubuntu2404) (push) Successful in 10m8s
Build Packages / XDS test (durin plugin) (push) Successful in 9m16s
Build Packages / XDS test (neggia plugin) (push) Successful in 7m59s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 9m12s
Build Packages / DIALS test (push) Successful in 11m44s
Build Packages / Unit tests (push) Successful in 1h23m8s
v1.0.0-rc.135 (#44)
This is an UNSTABLE release. The release has significant modifications and bug fixes, if things go wrong, it is better to revert to 1.0.0-rc.132.

* Multiple small bug fixes scattered across the whole code base. (detected with GPT-5.4)
* jfjoch_viewer: Improve image render performance

Reviewed-on: #44
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
Co-committed-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-04-16 11:59:59 +02:00

81 lines
2.2 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#ifndef JUNGFRAUJOCH_REGRESSION_H
#define JUNGFRAUJOCH_REGRESSION_H
#include <vector>
#include <utility>
#include <cmath>
#include "../../common/JFJochException.h"
struct RegressionResult {
float intercept;
float slope;
float r_square;
};
template <class fptype = float>
RegressionResult regression(std::vector<float> &x, std::vector<float> &y, const size_t N) {
if (N < 3)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Regression: minimum 3 points needed for any meaningful result");
if (x.size() < N || y.size() < N)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Regression: mismatch between input parameter size");
fptype x_sum = 0.0;
fptype y_sum = 0.0;
fptype xy_sum = 0.0;
fptype x2_sum = 0.0;
int64_t nelem = 0;
for (int i = 0; i < N; i++) {
if (std::isfinite(y[i]) && std::isfinite(x[i])) {
x_sum += x[i];
y_sum += y[i];
x2_sum += x[i] * x[i];
xy_sum += x[i] * y[i];
nelem++;
}
}
if (nelem < 3)
return {
.intercept = 0,
.slope = 0,
.r_square = 0
};
fptype denominator = nelem * x2_sum - x_sum * x_sum;
fptype intercept = (y_sum * x2_sum - x_sum * xy_sum) / denominator;
fptype slope = (nelem * xy_sum - x_sum * y_sum) / denominator;
fptype y_mean = y_sum / nelem;
fptype ss_tot = 0.0;
fptype ss_reg = 0.0;
for (int i = 0; i < nelem; i++) {
if (std::isfinite(y[i]) && std::isfinite(x[i])) {
fptype pred = slope * x[i] + intercept;
ss_tot += (y[i] - y_mean) * (y[i] - y_mean);
ss_reg += (pred - y_mean) * (pred - y_mean);
}
}
fptype r_square = 0.0;
if (ss_tot != 0.0)
r_square = ss_reg / ss_tot;
return {
.intercept = static_cast<float>(intercept),
.slope = static_cast<float>(slope),
.r_square = static_cast<float>(r_square)
};
};
#endif //JUNGFRAUJOCH_REGRESSION_H