LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
StarMatch.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of jointcal.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #include <iostream>
26 #include <fstream>
27 #include <iomanip>
28 
31 #include "lsst/jointcal/BaseStar.h"
32 #include "algorithm" // for copy
33 
34 /* TO DO:
35  think about imposing a maximum number of matches that may
36  be discarded in Cleanup.
37 */
38 
39 namespace lsst {
40 namespace jointcal {
41 
42 static double sq(double x) { return x * x; }
43 
45  FatPoint tr;
46  transform.transformPosAndErrors(point1, tr);
47  double vxx = tr.vx + point2.vx;
48  double vyy = tr.vy + point2.vy;
49  double vxy = tr.vxy + point2.vxy;
50  double det = vxx * vyy - vxy * vxy;
51  return (vyy * sq(tr.x - point2.x) + vxx * sq(tr.y - point2.y) -
52  2 * vxy * (tr.x - point2.x) * (tr.y - point2.y)) /
53  det;
54 }
55 
57  stream << match.point1.x << ' ' << match.point1.y << ' ' << match.point2.x << ' ' << match.point2.y << ' '
58  << match.distance << std::endl;
59  return stream;
60 }
61 
63  stream << " number of elements " << starMatchList.size() << std::endl;
64  copy(starMatchList.begin(), starMatchList.end(), std::ostream_iterator<StarMatch>(stream));
65  return stream;
66 }
67 
68 static std::unique_ptr<double[]> chi2_array(const StarMatchList &starMatchList,
70  unsigned s = starMatchList.size();
71  auto res = std::unique_ptr<double[]>(new double[s]);
72  unsigned count = 0;
73  for (auto const &it : starMatchList) res[count++] = it.computeChi2(transform);
74  return res;
75 }
76 
77 static unsigned chi2_cleanup(StarMatchList &starMatchList, const double chi2Cut,
78  const AstrometryTransform &transform) {
79  unsigned erased = starMatchList.removeAmbiguities(transform);
80  for (auto smi = starMatchList.begin(); smi != starMatchList.end();) {
81  if (smi->chi2 > chi2Cut) {
82  smi = starMatchList.erase(smi);
83  erased++;
84  } else
85  ++smi;
86  }
87  return erased;
88 }
89 
94 void StarMatchList::refineTransform(double nSigmas) {
95  double cut;
96  unsigned nremoved;
97  if (!_transform) _transform.reset(new AstrometryTransformLinear);
98  do {
99  int nused = size();
100  if (nused <= 2) {
101  _chi2 = -1;
102  break;
103  }
104  _chi2 = _transform->fit(*this);
105  /* convention of the fitted routines :
106  - chi2 = 0 means zero degrees of freedom
107  (this was not enforced in AstrometryTransform{Lin,Quad,Cub} ...)
108  - chi2 = -1 means ndof <0 (and hence no possible fit)
109  --> in either case, refinement is over
110  The fact that chi2 = 0 was not enforced when necessary means
111  that in this (rare) case, we were discarding matches at random....
112  With AstrometryTransformPolynomial::fit, this is no longer the case.
113  */
114  if (_chi2 <= 0) return;
115  unsigned npair = int(size());
116  if (npair == 0) break; // should never happen
117 
118  // compute some chi2 statistics
119  std::unique_ptr<double[]> chi2_array(new double[npair]);
120  unsigned count = 0;
121  for (auto &starMatch : *this)
122  chi2_array[count++] = starMatch.chi2 = starMatch.computeChi2(*_transform);
123 
124  std::sort(chi2_array.get(), chi2_array.get() + npair);
125  double median = (npair & 1) ? chi2_array[npair / 2]
126  : (chi2_array[npair / 2 - 1] + chi2_array[npair / 2]) * 0.5;
127 
128  // discard outliers : the cut is understood as a "distance" cut
129  cut = sq(nSigmas) * median;
130  nremoved = chi2_cleanup(*this, cut, *_transform);
131  } while (nremoved);
132  _dist2 = computeDist2(*this, *_transform);
133 }
134 
135 /* not very robust : assumes that we went through Refine just before... */
137  int deno = (2. * size() - _transform->getNpar());
138  return (deno > 0) ? sqrt(_dist2 / deno) : -1; // is -1 a good idea?
139 }
140 
142  for (auto &smi : *this) smi.setDistance(transform); // c'est compact
143 }
144 
145 unsigned StarMatchList::removeAmbiguities(const AstrometryTransform &transform, int which) {
146  if (!which) return 0;
147  setDistance(transform);
148  int initial_count = size();
149  if (which & 1) {
151  unique(sameStar1);
152  }
153  if (which & 2) {
155  unique(sameStar2);
156  }
157  return (initial_count - size());
158 }
159 
161  if (order == 0)
162  setTransform(std::make_shared<AstrometryTransformLinearShift>());
163  else if (order == 1)
164  setTransform(std::make_shared<AstrometryTransformLinear>());
165  else
166  setTransform(AstrometryTransformPolynomial(order));
167  // might consider throwing if order does not make sense (e.g. >10)
168  _order = order;
169 }
170 
171 /* This routine should operate on a copy : refineTransform
172  might shorten the list */
173 /* it is not const although it tries not to change anything */
175  if (!_transform) return nullptr;
176 
177  auto old_transform = _transform->clone();
178  double old_chi2 = _chi2;
179 
180  swap();
181  setTransformOrder(_order);
182  refineTransform(3.); // keep same order
183  auto inverted_transform = _transform->clone();
184  setTransform(old_transform.get());
185  swap();
186  _chi2 = old_chi2;
187 
188  return inverted_transform;
189 }
190 
191 void StarMatchList::cutTail(int nKeep) {
192  iterator si;
193  int count = 0;
194  for (si = begin(); si != end() && count < nKeep; ++count, ++si)
195  ;
196  erase(si, end());
197 }
198 
200  for (auto &starMatch : *this) {
201  starMatch.swap();
202  }
203 }
204 
205 int StarMatchList::recoveredNumber(double mindist) const {
206  int n = 0;
208  for (auto const &starMatch : *this) {
209  if (starMatch.computeDistance(identity) < mindist) n++;
210  }
211  return (n);
212 }
213 
214 void StarMatchList::applyTransform(StarMatchList &transformed, const AstrometryTransform *priorTransform,
215  const AstrometryTransform *posteriorTransform) const {
216  transformed.clear();
218  const AstrometryTransform &T1 = (priorTransform) ? *priorTransform : id;
219  const AstrometryTransform &T2 = (posteriorTransform) ? *posteriorTransform : id;
220 
221  for (auto const &starMatch : *this) {
222  FatPoint p1;
223  T1.transformPosAndErrors(starMatch.point1, p1);
224  FatPoint p2;
225  T2.transformPosAndErrors(starMatch.point2, p2);
226  transformed.push_back(StarMatch(p1, p2, starMatch.s1, starMatch.s2));
227  }
228 }
229 
231  stream << " ================================================================" << std::endl
232  << " Transformation between lists of order " << getTransformOrder() << std::endl
233  << *_transform //<< endl
234  << " Chi2 = " << getChi2() << " Residual = " << computeResidual() << std::endl
235  << " Number in the list = " << size() << std::endl
236  << " ================================================================" << std::endl;
237 }
238 
239 double computeDist2(const StarMatchList &starMatchList, const AstrometryTransform &transform) {
240  double dist2 = 0;
241  for (auto const &starMatch : starMatchList)
242  dist2 += transform.apply(starMatch.point1).computeDist2(starMatch.point2);
243  return dist2;
244 }
245 
246 double computeChi2(const StarMatchList &starMatchList, const AstrometryTransform &transform) {
247  unsigned s = starMatchList.size();
248  std::unique_ptr<double[]> chi2s(chi2_array(starMatchList, transform));
249  double chi2 = 0;
250  for (unsigned k = 0; k < s; ++k) chi2 += chi2s[k];
251  return chi2;
252 }
253 } // namespace jointcal
254 } // namespace lsst
T copy(T... args)
A hanger for star associations.
Definition: StarMatch.h:54
implements the linear transformations (6 real coefficients).
def erase(frame=None)
Definition: ds9.py:97
void setTransformOrder(int order)
set transform according to the given order.
Definition: StarMatch.cc:160
FatPoint point2
2 points
Definition: StarMatch.h:60
T endl(T... args)
double computeChi2(const AstrometryTransform &transform) const
returns the chi2 (using errors in the FatPoint&#39;s)
Definition: StarMatch.cc:44
void setDistance(const AstrometryTransform &transform)
to be used before sorting on distances.
Definition: StarMatch.h:85
T end(T... args)
table::Key< int > id
Definition: Detector.cc:162
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
T unique(T... args)
int recoveredNumber(double mindist) const
count the number of elements for which distance is < mindist
Definition: StarMatch.cc:205
A Point with uncertainties.
Definition: FatPoint.h:34
double x
coordinate
Definition: Point.h:41
pairs of points
std::unique_ptr< AstrometryTransform > inverseTransform()
returns the inverse transform (swap, fit(refineTransform) , and swap).
Definition: StarMatch.cc:174
T push_back(T... args)
void refineTransform(double nSigmas)
removes pairs beyond nSigmas in distance (where the sigma scale is set by the fit) and iterates until...
Definition: StarMatch.cc:94
A base class for image defects.
friend bool sameStar2(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:125
unsigned removeAmbiguities(const AstrometryTransform &transform, int which=3)
cleans up the std::list of pairs for pairs that share one of their stars, keeping the closest one...
Definition: StarMatch.cc:145
friend bool sameStar1(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:119
void setDistance(const AstrometryTransform &transform)
Sets the distance (residual) field of all std::list elements. Mandatory before sorting on distances...
Definition: StarMatch.cc:141
T erase(T... args)
double computeResidual() const
returns the average 1d Residual (last call to refineTransform)
Definition: StarMatch.cc:136
T clear(T... args)
friend bool compareStar2(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:121
double x
T count(T... args)
void dumpTransform(std::ostream &stream=std::cout) const
print the matching transformation quality (transform, chi2, residual)
Definition: StarMatch.cc:230
A do-nothing transformation. It anyway has dummy routines to mimick a AstrometryTransform.
void applyTransform(StarMatchList &transformed, const AstrometryTransform *priorTransform, const AstrometryTransform *posteriorTransform=nullptr) const
enables to get a transformed StarMatchList.
Definition: StarMatch.cc:214
T get(T... args)
T size(T... args)
STL class.
a virtual (interface) class for geometric transformations.
T begin(T... args)
double computeDist2(const StarMatchList &S, const AstrometryTransform &transform)
sum of distance squared
Definition: StarMatch.cc:239
friend std::ostream & operator<<(std::ostream &stream, const StarMatch &Match)
Definition: StarMatch.cc:56
T sort(T... args)
friend bool compareStar1(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:115
T sqrt(T... args)
void swap()
swaps elements 1 and 2 of each starmatch in std::list.
Definition: StarMatch.cc:199
STL class.
void cutTail(int nKeep)
deletes the tail of the match std::list
Definition: StarMatch.cc:191
int end
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0