29#include <boost/math/tools/minima.hpp>
65 if (
nval == 0)
return 0;
84 if (chi2->chi2 <
cut)
break;
95 LOGLS_WARN(
_log,
"FittedStar with no measuredStars found as an outlier: "
101 "RefStar is outlier but not removed when not fitting FittedStar-RefStar values: "
102 << *(
fittedStar->getRefStar()) <<
" chi2: " << chi2->chi2);
115 LOGLS_WARN(
_log,
"FittedStar with 1 measuredStar and no refStar found as an outlier: "
128 for (
auto const &
i : indices) {
144 for (
auto const &
i : indices) {
165void dumpMatrixAndGradient(
SparseMatrixD const &matrix, Eigen::VectorXd
const &grad,
168 Eigen::MatrixXd matrixDense(matrix);
169 std::string dumpMatrixPath = dumpFile +
"-mat" + ext;
172 std::string dumpGradPath = dumpFile +
"-grad" + ext;
175 LOGLS_INFO(_log,
"Dumped Hessian, gradient to: '" << dumpMatrixPath <<
"', '" << dumpGradPath <<
"'");
211 LOGLS_WARN(
_log,
"Hessian matrix is too big to dump to file, with rows, columns: "
219 if (
chol.info() != Eigen::Success) {
231 Eigen::VectorXd delta =
chol.solve(
grad);
233 scale = _lineSearch(delta);
239 LOGL_ERROR(
_log,
"chi2 is not finite. Aborting outlier rejection.");
244 LOGL_WARN(
_log,
"chi2 went up, skipping outlier rejection loop");
281 chol.update(
H,
false );
298 "Restarting factorization, hessian: dim="
302 if (
chol.info() != Eigen::Success) {
315 LOGLS_INFO(
_log,
"Number of outliers (Measured + Reference = Total): "
349 for (
auto const &
ccdImage : ccdImageList) {
366double FitterBase::_lineSearch(Eigen::VectorXd
const &delta) {
367 auto func = [
this, &delta](
double scale) {
368 auto offset = scale * delta;
377 auto result = boost::math::tools::brent_find_minima(func, -1.0, 2.0, bits);
#define LOGLS_WARN(logger, message)
#define LOGL_WARN(logger, message...)
#define LOGLS_INFO(logger, message)
#define LOGLS_ERROR(logger, message)
#define LOGL_ERROR(logger, message...)
#define LOGLS_DEBUG(logger, message)
#define LOGLS_TRACE(logger, message)
Eigen::SparseMatrix< double, 0, Eigen::Index > SparseMatrixD
Handler of an actual image from a single CCD.
Structure to accumulate the chi2 contributions per each star (to help find outliers).
Simple structure to accumulate chi2 and ndof.
A list of FittedStar s. Such a list is typically constructed by Associations.
void leastSquareDerivatives(TripletList &tripletList, Eigen::VectorXd &grad) const
Evaluates the chI^2 derivatives (Jacobian and gradient) for the current whatToFit setting.
void removeRefOutliers(FittedStarList &outliers)
Remove refStar outliers from the fit. No Refit done.
virtual void getIndicesOfMeasuredStar(MeasuredStar const &measuredStar, IndexVector &indices) const =0
Set the indices of a measured star from the full matrix, for outlier removal.
Chi2Statistic computeChi2() const
Returns the chi2 for the current state.
virtual void saveChi2MeasContributions(std::string const &filename) const =0
Save a CSV file containing residuals of measurement terms.
virtual void leastSquareDerivativesReference(FittedStarList const &fittedStarList, TripletList &tripletList, Eigen::VectorXd &grad) const =0
Compute the derivatives of the reference terms.
MinimizeResult minimize(std::string const &whatToFit, double nSigmaCut=0, double sigmaRelativeTolerance=0, bool doRankUpdate=true, bool doLineSearch=false, std::string const &dumpMatrixFile="")
Does a 1 step minimization, assuming a linear model.
virtual void saveChi2Contributions(std::string const &baseName) const
Save the full chi2 term per star that was used in the minimization, for debugging.
virtual void assignIndices(std::string const &whatToFit)=0
Set parameters to fit and assign indices in the big matrix.
virtual void offsetParams(Eigen::VectorXd const &delta)=0
Offset the parameters by the requested quantities.
std::shared_ptr< Associations > _associations
virtual void accumulateStatRefStars(Chi2Accumulator &accum) const =0
Compute the chi2 (per star or total, depending on which Chi2Accumulator is used) for RefStars.
Eigen::Index _nStarParams
virtual void saveChi2RefContributions(std::string const &filename) const =0
Save a CSV file containing residuals of reference terms.
virtual void leastSquareDerivativesMeasurement(CcdImage const &ccdImage, TripletList &tripletList, Eigen::VectorXd &grad, MeasuredStarList const *measuredStarList=nullptr) const =0
Compute the derivatives of the measured stars and model for one CcdImage.
void outliersContributions(MeasuredStarList &msOutliers, FittedStarList &fsOutliers, TripletList &tripletList, Eigen::VectorXd &grad)
Contributions to derivatives from (presumably) outlier terms.
std::size_t findOutliers(double nSigmaCut, MeasuredStarList &msOutliers, FittedStarList &fsOutliers, double &cut) const
Find Measurements and references contributing more than a cut, computed as.
virtual void accumulateStatImageList(CcdImageList const &ccdImageList, Chi2Accumulator &accum) const =0
Compute the chi2 (per star or total, depending on which Chi2Accumulator is used) for measurements.
void removeMeasOutliers(MeasuredStarList &outliers)
Remove measuredStar outliers from the fit. No Refit done.
A list of MeasuredStar. They are usually filled in Associations::createCcdImage.
MinimizeResult
Return value of minimize()