13 LOG_LOGGER _log =
LOG_GET(
"jointcal.Fitter");
40 size_t nval = chi2List.
size();
41 if (nval == 0)
return 0;
43 double median = (nval & 1) ? chi2List[nval / 2].chi2
44 : 0.5 * (chi2List[nval / 2 - 1].chi2 + chi2List[nval / 2].chi2);
46 LOGLS_DEBUG(_log,
"RemoveOutliers chi2 stat: mean/median/sigma " << averageAndSigma.first <<
'/' << median
47 <<
'/' << averageAndSigma.second);
48 double cut = averageAndSigma.first + nSigmaCut * averageAndSigma.second;
54 Eigen::VectorXi affectedParams(
_nParTot);
55 affectedParams.setZero();
57 unsigned nOutliers = 0;
59 for (
auto chi2 = chi2List.
rbegin(); chi2 != chi2List.
rend(); ++chi2) {
60 if (chi2->chi2 < cut)
break;
72 indices.
push_back(fs->getIndexInMatrix());
84 for (
auto const &i : indices) {
85 if (affectedParams(i) != 0) {
100 for (
auto const &i : indices) {
106 LOGLS_INFO(_log,
"findOutliers: found " << msOutliers.
size() <<
" meas outliers and " << fsOutliers.
size()
107 <<
" ref outliers ");
127 LOGLS_DEBUG(_log,
"End of triplet filling, ntrip = " << tripletList.
size());
132 jacobian.setFromTriplets(tripletList.
begin(), tripletList.
end());
135 hessian = jacobian * jacobian.transpose();
138 LOGLS_DEBUG(_log,
"Starting factorization, hessian: dim=" 139 << hessian.rows() <<
" non-zeros=" << hessian.nonZeros()
140 <<
" filling-frac = " << hessian.nonZeros() /
std::pow(hessian.rows(), 2));
143 if (chol.info() != Eigen::Success) {
144 LOGLS_ERROR(_log,
"minimize: factorization failed ");
148 unsigned totalOutliers = 0;
152 Eigen::VectorXd delta = chol.solve(grad);
156 if (currentChi2.
chi2 > oldChi2 && totalOutliers != 0) {
157 LOGL_WARN(_log,
"chi2 went up, skipping outlier rejection loop");
161 oldChi2 = currentChi2.
chi2;
163 if (nSigmaCut == 0)
break;
166 int nOutliers =
findOutliers(nSigmaCut, msOutliers, fsOutliers);
167 totalOutliers += nOutliers;
168 if (nOutliers == 0)
break;
178 H.setFromTriplets(tripletList.
begin(), tripletList.
end());
179 int update_status = chol.
update(H,
false );
180 LOGLS_DEBUG(_log,
"cholmod update_status " << update_status);
186 LOGLS_INFO(_log,
"Total number of outliers " << totalOutliers);
192 for (
auto &outlier : msOutliers) {
202 for (
auto &measuredStar : outliers) {
204 measuredStar->setValid(
false);
210 for (
auto &fittedStar : outliers) {
211 fittedStar->setRefStar(
nullptr);
217 for (
auto const &
ccdImage : ccdImageList) {
227 size_t slash = baseName.
rfind(
'/');
228 if (dot == std::string::npos || (slash != std::string::npos && dot < slash)) dot = baseName.
size();
230 measTuple.
insert(dot,
"-meas");
233 refTuple.
insert(dot,
"-ref");
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...
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...
int getMeasurementCount() const
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.
void removeMeasOutliers(MeasuredStarList &outliers)
Remove measuredStar outliers from the fit. No Refit done.
A list of MeasuredStar. They are usually filled in Associations::AddImage.
virtual void saveChi2Contributions(std::string const &baseName) const
Save the full chi2 term per star that was used in the minimization, for debugging.
MinimizeResult
Return value of minimize()
#define LOGLS_INFO(logger, message)
def dot(symb, c, r, frame=None, size=2, ctype=None, origin=afwImage.PARENT, args, kwargs)
virtual void saveChi2MeasContributions(std::string const &baseName) const =0
Save a CSV file containing residuals of measurement terms.
Structure to accumulate the chi2 contributions per each star (to help find outliers).
Class for a simple mapping implementing a generic Gtransfo.
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.
T dynamic_pointer_cast(T... args)
virtual void saveChi2RefContributions(std::string const &baseName) const =0
Save a CSV file containing residuals of reference terms.
std::shared_ptr< Associations > _associations
virtual void assignIndices(std::string const &whatToFit)=0
Set parameters to fit and assign indices in the big matrix.
Chi2Statistic computeChi2() const
Returns the chi2 for the current state.
#define LOGLS_DEBUG(logger, message)
void leastSquareDerivatives(TripletList &tripletList, Eigen::VectorXd &grad) const
Evaluates the chI^2 derivatives (Jacobian and gradient) for the current whatToFit setting...
Handler of an actual image from a single CCD.
#define LOGL_WARN(logger, message...)
virtual void offsetParams(Eigen::VectorXd const &delta)=0
Offset the parameters by the requested quantities.
unsigned getNextFreeIndex() const
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.
#define LOGLS_ERROR(logger, message)
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.