36 size_t nval = chi2List.
size();
37 if (nval == 0)
return 0;
39 double median = (nval & 1) ? chi2List[nval / 2].chi2
40 : 0.5 * (chi2List[nval / 2 - 1].chi2 + chi2List[nval / 2].chi2);
42 LOGLS_DEBUG(
_log,
"RemoveOutliers chi2 stat: mean/median/sigma " << averageAndSigma.first <<
'/' << median
43 <<
'/' << averageAndSigma.second);
44 double cut = averageAndSigma.first + nSigmaCut * averageAndSigma.second;
50 Eigen::VectorXi affectedParams(
_nParTot);
51 affectedParams.setZero();
53 unsigned nOutliers = 0;
55 for (
auto chi2 = chi2List.
rbegin(); chi2 != chi2List.
rend(); ++chi2) {
56 if (chi2->chi2 < cut)
break;
63 if (measuredStar ==
nullptr) {
66 if (fittedStar->getMeasurementCount() == 0) {
67 LOGLS_WARN(
_log,
"FittedStar with no measuredStars found as an outlier: " << *fittedStar);
72 indices.
push_back(fittedStar->getIndexInMatrix());
77 auto tempFittedStar = measuredStar->getFittedStar();
78 if (tempFittedStar->getMeasurementCount() == 1 && tempFittedStar->getRefStar() ==
nullptr) {
79 LOGLS_WARN(
_log,
"FittedStar with 1 measuredStar and no refStar found as an outlier: " 91 for (
auto const &i : indices) {
92 if (affectedParams(i) != 0) {
99 if (measuredStar ==
nullptr) {
107 for (
auto const &i : indices) {
113 LOGLS_INFO(
_log,
"findOutliers: found " << msOutliers.
size() <<
" meas outliers and " << fsOutliers.
size()
114 <<
" ref outliers ");
139 jacobian.setFromTriplets(tripletList.
begin(), tripletList.
end());
142 hessian = jacobian * jacobian.transpose();
146 << hessian.rows() <<
" non-zeros=" << hessian.nonZeros()
147 <<
" filling-frac = " << hessian.nonZeros() /
std::pow(hessian.rows(), 2));
150 if (chol.info() != Eigen::Success) {
155 unsigned totalMeasOutliers = 0;
156 unsigned totalRefOutliers = 0;
160 Eigen::VectorXd delta = chol.solve(grad);
164 if (currentChi2.
chi2 > oldChi2 && totalMeasOutliers + totalRefOutliers != 0) {
165 LOGL_WARN(
_log,
"chi2 went up, skipping outlier rejection loop");
169 oldChi2 = currentChi2.
chi2;
171 if (nSigmaCut == 0)
break;
175 int nOutliers =
findOutliers(nSigmaCut, msOutliers, fsOutliers);
176 totalMeasOutliers += msOutliers.
size();
177 totalRefOutliers += fsOutliers.
size();
178 if (nOutliers == 0)
break;
188 H.setFromTriplets(tripletList.
begin(), tripletList.
end());
189 int update_status = chol.
update(H,
false );
197 if (nSigmaCut != 0) {
198 LOGLS_INFO(
_log,
"Number of outliers (Measured + Reference = Total): " 199 << totalMeasOutliers <<
" + " << totalRefOutliers <<
" = " 200 << totalMeasOutliers + totalRefOutliers);
207 for (
auto &outlier : msOutliers) {
217 for (
auto &measuredStar : outliers) {
218 auto fittedStar = measuredStar->getFittedStar();
219 measuredStar->setValid(
false);
220 fittedStar->getMeasurementCount()--;
225 for (
auto &fittedStar : outliers) {
226 fittedStar->setRefStar(
nullptr);
232 for (
auto const &
ccdImage : ccdImageList) {
242 size_t slash = baseName.
rfind(
'/');
243 if (dot == std::string::npos || (slash != std::string::npos && dot < slash)) dot = baseName.
size();
245 measTuple.
insert(dot,
"-meas");
248 refTuple.
insert(dot,
"-ref");
#define LOGLS_WARN(logger, message)
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...
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()
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).
#define LOGLS_DEBUG(logger, message)
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)
#define LOGL_WARN(logger, message...)
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.
#define LOGLS_INFO(logger, message)
Chi2Statistic computeChi2() const
Returns the chi2 for the current state.
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.
virtual void offsetParams(Eigen::VectorXd const &delta)=0
Offset the parameters by the requested quantities.
#define LOGLS_ERROR(logger, message)
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.
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.