28#include <boost/math/tools/minima.hpp>
63 size_t nval = chi2List.
size();
64 if (nval == 0)
return 0;
66 double median = (nval & 1) ? chi2List[nval / 2].chi2
67 : 0.5 * (chi2List[nval / 2 - 1].chi2 + chi2List[nval / 2].chi2);
69 LOGLS_DEBUG(
_log,
"findOutliers chi2 stat: mean/median/sigma " << averageAndSigma.first <<
'/' << median
70 <<
'/' << averageAndSigma.second);
71 cut = averageAndSigma.first + nSigmaCut * averageAndSigma.second;
77 Eigen::VectorXi affectedParams(
_nTotal);
78 affectedParams.setZero();
82 for (
auto chi2 = chi2List.
rbegin(); chi2 != chi2List.
rend(); ++chi2) {
83 if (chi2->chi2 < cut)
break;
88 auto measuredStar = std::dynamic_pointer_cast<MeasuredStar>(chi2->star);
90 if (measuredStar ==
nullptr) {
92 fittedStar = std::dynamic_pointer_cast<FittedStar>(chi2->star);
93 if (fittedStar->getMeasurementCount() == 0) {
94 LOGLS_WARN(
_log,
"FittedStar with no measuredStars found as an outlier: "
95 << *fittedStar <<
" chi2: " << chi2->chi2);
100 "RefStar is outlier but not removed when not fitting FittedStar-RefStar values: "
101 << *(fittedStar->getRefStar()) <<
" chi2: " << chi2->chi2);
106 indices.
push_back(fittedStar->getIndexInMatrix());
107 LOGLS_TRACE(
_log,
"Removing refStar " << *(fittedStar->getRefStar()) <<
" chi2: " << chi2->chi2);
112 auto tempFittedStar = measuredStar->getFittedStar();
113 if (tempFittedStar->getMeasurementCount() == 1 && tempFittedStar->getRefStar() ==
nullptr) {
114 LOGLS_WARN(
_log,
"FittedStar with 1 measuredStar and no refStar found as an outlier: "
119 LOGLS_TRACE(
_log,
"Removing measStar " << *measuredStar <<
" chi2: " << chi2->chi2);
127 for (
auto const &i : indices) {
128 if (affectedParams(i) != 0) {
135 if (measuredStar ==
nullptr) {
143 for (
auto const &i : indices) {
150 << fsOutliers.
size() <<
" ref outliers ");
159 jacobian.setFromTriplets(tripletList.
begin(), tripletList.
end());
160 return jacobian * jacobian.transpose();
164void dumpMatrixAndGradient(
SparseMatrixD const &matrix, Eigen::VectorXd
const &grad,
167 Eigen::MatrixXd matrixDense(matrix);
168 std::string dumpMatrixPath = dumpFile +
"-mat" + ext;
171 std::string dumpGradPath = dumpFile +
"-grad" + ext;
174 LOGLS_INFO(_log,
"Dumped Hessian, gradient to: '" << dumpMatrixPath <<
"', '" << dumpGradPath <<
"'");
179 double sigmaRelativeTolerance,
bool doRankUpdate,
bool const doLineSearch,
205 << hessian.rows() <<
" non-zeros=" << hessian.nonZeros()
206 <<
" filling-frac = " << hessian.nonZeros() /
std::pow(hessian.rows(), 2));
208 if (dumpMatrixFile !=
"") {
209 if (hessian.rows() * hessian.cols() > 2e8) {
210 LOGLS_WARN(
_log,
"Hessian matrix is too big to dump to file, with rows, columns: "
211 << hessian.rows() <<
", " << hessian.cols());
213 dumpMatrixAndGradient(hessian, grad, dumpMatrixFile,
_log);
218 if (chol.info() != Eigen::Success) {
226 double oldSigmaCut = 0.;
227 double sigmaCut = 0.;
230 Eigen::VectorXd delta = chol.solve(grad);
232 scale = _lineSearch(delta);
238 LOGL_ERROR(
_log,
"chi2 is not finite. Aborting outlier rejection.");
242 if (currentChi2.
chi2 > oldChi2 && totalMeasOutliers + totalRefOutliers != 0) {
243 LOGL_WARN(
_log,
"chi2 went up, skipping outlier rejection loop");
247 oldChi2 = currentChi2.
chi2;
249 if (nSigmaCut == 0)
break;
254 double relChange = 0.;
255 if(oldSigmaCut!=0.) relChange = (1 - sigmaCut / oldSigmaCut);
257 LOGLS_DEBUG(
_log,
"findOutliers chi2 cut level: " << sigmaCut <<
", relative change: " << relChange);
260 if ((sigmaRelativeTolerance > 0) && (oldSigmaCut > 0 && relChange < sigmaRelativeTolerance)) {
261 LOGLS_INFO(
_log,
"Iterations stopped with chi2 cut at " << sigmaCut <<
" and relative change of "
265 totalMeasOutliers += msOutliers.
size();
266 totalRefOutliers += fsOutliers.
size();
267 oldSigmaCut = sigmaCut;
268 if (nOutliers == 0)
break;
279 H.setFromTriplets(outlierTriplets.
begin(), outlierTriplets.
end());
293 hessian = createHessian(
_nTotal, nextTripletList);
294 nextTripletList.
clear();
297 "Restarting factorization, hessian: dim="
298 << hessian.rows() <<
" non-zeros=" << hessian.nonZeros()
299 <<
" filling-frac = " << hessian.nonZeros() /
std::pow(hessian.rows(), 2));
300 chol.compute(hessian);
301 if (chol.info() != Eigen::Success) {
308 if (totalMeasOutliers + totalRefOutliers > 0) {
313 if (nSigmaCut != 0) {
314 LOGLS_INFO(
_log,
"Number of outliers (Measured + Reference = Total): "
315 << totalMeasOutliers <<
" + " << totalRefOutliers <<
" = "
316 << totalMeasOutliers + totalRefOutliers);
323 for (
auto &outlier : msOutliers) {
326 const CcdImage &ccdImage = outlier->getCcdImage();
333 for (
auto &measuredStar : outliers) {
334 auto fittedStar = measuredStar->getFittedStar();
335 measuredStar->setValid(
false);
336 fittedStar->getMeasurementCount()--;
341 for (
auto &fittedStar : outliers) {
342 fittedStar->setRefStar(
nullptr);
348 for (
auto const &ccdImage : ccdImageList) {
356 auto pos = baseName.
find(replaceStr);
358 measFilename.
replace(pos, replaceStr.
size(),
"-meas.csv");
360 refFilename.
replace(pos, replaceStr.
size(),
"-ref.csv");
365double FitterBase::_lineSearch(Eigen::VectorXd
const &delta) {
366 auto func = [
this, &delta](
double scale) {
367 auto offset =
scale * delta;
376 auto result = boost::math::tools::brent_find_minima(func, -1.0, 2.0, bits);
Eigen::SparseMatrix< double, 0, Eigen::Index > SparseMatrixD
LSST DM logging module built on log4cxx.
#define LOGLS_WARN(logger, message)
Log a warn-level message using an iostream-based interface.
#define LOGL_WARN(logger, message...)
Log a warn-level message using a varargs/printf style interface.
#define LOGLS_INFO(logger, message)
Log a info-level message using an iostream-based interface.
#define LOGLS_ERROR(logger, message)
Log a error-level message using an iostream-based interface.
#define LOGL_ERROR(logger, message...)
Log a error-level message using a varargs/printf style interface.
#define LOGLS_DEBUG(logger, message)
Log a debug-level message using an iostream-based interface.
#define LOGLS_TRACE(logger, message)
Log a trace-level message using an iostream-based interface.
void update(SparseMatrixD const &H, bool UpOrDown)
Handler of an actual image from a single CCD.
Structure to accumulate the chi2 contributions per each star (to help find outliers).
std::pair< double, double > computeAverageAndSigma()
Compute the average and std-deviation of these chisq values.
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.
Eigen::Index getNextFreeIndex() const
def scale(algorithm, min, max=None, frame=None)
MinimizeResult
Return value of minimize()
A base class for image defects.