3 #include "lsst/log/Log.h"
12 static double sqr(
double x) {
return x * x; }
15 LOG_LOGGER _log = LOG_GET(
"jointcal.Fitter");
42 size_t nval = chi2List.size();
43 if (nval == 0)
return 0;
44 sort(chi2List.begin(), chi2List.end());
45 double median = (nval & 1) ? chi2List[nval / 2].chi2
46 : 0.5 * (chi2List[nval / 2 - 1].chi2 + chi2List[nval / 2].chi2);
48 LOGLS_DEBUG(_log,
"RemoveOutliers chi2 stat: mean/median/sigma " << averageAndSigma.first <<
'/' << median
49 <<
'/' << averageAndSigma.second);
50 double cut = averageAndSigma.first + nSigmaCut * averageAndSigma.second;
56 Eigen::VectorXi affectedParams(
_nParTot);
57 affectedParams.setZero();
59 unsigned nOutliers = 0;
61 for (
auto chi2 = chi2List.rbegin(); chi2 != chi2List.rend(); ++chi2) {
62 if (chi2->chi2 < cut)
break;
63 std::vector<unsigned> indices;
67 auto ms = std::dynamic_pointer_cast<
MeasuredStar>(chi2->star);
68 std::shared_ptr<FittedStar> fs;
71 fs = std::dynamic_pointer_cast<
FittedStar>(chi2->star);
72 indices.push_back(fs->getIndexInMatrix());
73 indices.push_back(fs->getIndexInMatrix() + 1);
85 for (
auto const &i : indices) {
86 if (affectedParams(i) != 0) {
95 msOutliers.push_back(ms);
98 fsOutliers.push_back(fs);
101 for (
auto const &i : indices) {
107 LOGLS_INFO(_log,
"findOutliers: found " << msOutliers.size() <<
" meas outliers and " << fsOutliers.size()
108 <<
" ref outliers ");
128 LOGLS_DEBUG(_log,
"End of triplet filling, ntrip = " << tripletList.size());
133 jacobian.setFromTriplets(tripletList.begin(), tripletList.end());
136 hessian = jacobian * jacobian.transpose();
139 LOGLS_DEBUG(_log,
"Starting factorization, hessian: dim="
140 << hessian.rows() <<
" non-zeros=" << hessian.nonZeros()
141 <<
" filling-frac = " << hessian.nonZeros() / sqr(hessian.rows()));
144 if (chol.info() != Eigen::Success) {
145 LOGLS_ERROR(_log,
"minimize: factorization failed ");
149 unsigned totalOutliers = 0;
153 Eigen::VectorXd delta = chol.solve(grad);
156 LOGLS_DEBUG(_log, currentChi2);
157 if (currentChi2.
chi2 > oldChi2) {
158 LOGL_WARN(_log,
"chi2 went up, skipping outlier rejection loop");
162 oldChi2 = currentChi2.
chi2;
164 if (nSigmaCut == 0)
break;
167 int nOutliers =
findOutliers(nSigmaCut, msOutliers, fsOutliers);
168 totalOutliers += nOutliers;
169 if (nOutliers == 0)
break;
179 H.setFromTriplets(tripletList.begin(), tripletList.end());
180 int update_status = chol.
update(H,
false );
181 LOGLS_DEBUG(_log,
"cholmod update_status " << update_status);
187 LOGLS_INFO(_log,
"Total number of outliers " << totalOutliers);
193 for (
auto &outlier : msOutliers) {
195 tmp.push_back(outlier);
196 const CcdImage &ccdImage = outlier->getCcdImage();
203 for (
auto &measuredStar : outliers) {
204 auto fittedStar = std::const_pointer_cast<
FittedStar>(measuredStar->getFittedStar());
205 measuredStar->setValid(
false);
211 for (
auto &fittedStar : outliers) {
212 fittedStar->setRefStar(
nullptr);
218 for (
auto const &ccdImage : ccdImageList) {
virtual void accumulateStatRefStars(Chi2Accumulator &accum) const =0
Compute the chi2 (per star or total, depending on which Chi2Accumulator is used) for RefStars...
virtual void leastSquareDerivativesReference(FittedStarList const &fittedStarList, TripletList &tripletList, Eigen::VectorXd &grad) const =0
Compute the derivatives of the reference terms.
Simple structure to accumulate chi2 and ndof.
int update(SpMat const &H, bool UpOrDown)
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...
virtual void getIndicesOfMeasuredStar(MeasuredStar const &measuredStar, std::vector< unsigned > &indices) const =0
Set the indices of a measured star from the full matrix, for outlier removal.
MinimizeResult minimize(std::string const &whatToFit, double nSigmaCut=0)
Does a 1 step minimization, assuming a linear model.
unsigned findOutliers(double nSigmaCut, MeasuredStarList &msOutliers, FittedStarList &fsOutliers) const
Find Measurements and references contributing more than a cut, computed as The outliers are NOT remo...
void removeMeasOutliers(MeasuredStarList &outliers)
Remove measuredStar outliers from the fit. No Refit done.
void leastSquareDerivatives(TripletList &tripletList, Eigen::VectorXd &grad) const
Evaluates the chI^2 derivatives (Jacobian and gradient) for the current whatToFit setting...
A list of MeasuredStar. They are usually filled in Associations::AddImage.
MinimizeResult
Return value of minimize()
Structure to accumulate the chi2 contributions per each star (to help find outliers).
Eigen::SparseMatrix< double > SpMat
A list of FittedStar s. Such a list is typically constructed by Associations.
void removeRefOutliers(FittedStarList &outliers)
Remove refStar outliers from the fit. No Refit done.
objects measured on actual images.
std::shared_ptr< Associations > _associations
virtual void assignIndices(std::string const &whatToFit)=0
Set parameters to fit and assign indices in the big matrix.
unsigned getNextFreeIndex() const
Chi2Statistic computeChi2() const
Returns the chi2 for the current state.
int getMeasurementCount() const
Handler of an actual image from a single CCD.
virtual void offsetParams(Eigen::VectorXd const &delta)=0
Offset the parameters by the requested quantities.
void outliersContributions(MeasuredStarList &msOutliers, FittedStarList &fsOutliers, TripletList &tripletList, Eigen::VectorXd &grad)
Contributions to derivatives from (presumably) outlier terms.
The objects which have been measured several times.
std::pair< double, double > computeAverageAndSigma()
Compute the average and std-deviation of these chisq values.
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.