lsst.jointcal  14.0-17-ge3cc87b
StarMatch.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <iomanip>
4 
8 #include "algorithm" // for copy
9 
10 /* TO DO:
11  think about imposing a maximum number of matches that may
12  be discarded in Cleanup.
13 */
14 
15 namespace lsst {
16 namespace jointcal {
17 
18 static double sq(double x) { return x * x; }
19 
21  FatPoint tr;
22  gtransfo.transformPosAndErrors(point1, tr);
23  double vxx = tr.vx + point2.vx;
24  double vyy = tr.vy + point2.vy;
25  double vxy = tr.vxy + point2.vxy;
26  double det = vxx * vyy - vxy * vxy;
27  return (vyy * sq(tr.x - point2.x) + vxx * sq(tr.y - point2.y) -
28  2 * vxy * (tr.x - point2.x) * (tr.y - point2.y)) /
29  det;
30 }
31 
32 std::ostream &operator<<(std::ostream &stream, const StarMatch &match) {
33  stream << match.point1.x << ' ' << match.point1.y << ' ' << match.point2.x << ' ' << match.point2.y << ' '
34  << match.distance << std::endl;
35  return stream;
36 }
37 
38 std::ostream &operator<<(std::ostream &stream, const StarMatchList &starMatchList) {
39  stream << " number of elements " << starMatchList.size() << std::endl;
40  copy(starMatchList.begin(), starMatchList.end(), std::ostream_iterator<StarMatch>(stream));
41  return stream;
42 }
43 
44 static std::unique_ptr<double[]> chi2_array(const StarMatchList &starMatchList, const Gtransfo &gtransfo) {
45  unsigned s = starMatchList.size();
46  auto res = std::unique_ptr<double[]>(new double[s]);
47  unsigned count = 0;
48  for (auto const &it : starMatchList) res[count++] = it.computeChi2(gtransfo);
49  return res;
50 }
51 
52 static unsigned chi2_cleanup(StarMatchList &starMatchList, const double chi2Cut, const Gtransfo &gtransfo) {
53  unsigned erased = starMatchList.removeAmbiguities(gtransfo);
54  for (auto smi = starMatchList.begin(); smi != starMatchList.end();) {
55  if (smi->chi2 > chi2Cut) {
56  smi = starMatchList.erase(smi);
57  erased++;
58  } else
59  ++smi;
60  }
61  return erased;
62 }
63 
68 void StarMatchList::refineTransfo(double nSigmas) {
69  double cut;
70  unsigned nremoved;
71  if (!_transfo) _transfo.reset(new GtransfoLin);
72  do {
73  int nused = size();
74  if (nused <= 2) {
75  _chi2 = -1;
76  break;
77  }
78  _chi2 = _transfo->fit(*this);
79  /* convention of the fitted routines :
80  - chi2 = 0 means zero degrees of freedom
81  (this was not enforced in Gtransfo{Lin,Quad,Cub} ...)
82  - chi2 = -1 means ndof <0 (and hence no possible fit)
83  --> in either case, refinement is over
84  The fact that chi2 = 0 was not enforced when necessary means
85  that in this (rare) case, we were discarding matches at random....
86  With GtransfoPoly::fit, this is no longer the case.
87  */
88  if (_chi2 <= 0) return;
89  unsigned npair = int(size());
90  if (npair == 0) break; // should never happen
91 
92  // compute some chi2 statistics
93  std::unique_ptr<double[]> chi2_array(new double[npair]);
94  unsigned count = 0;
95  for (auto &starMatch : *this) chi2_array[count++] = starMatch.chi2 = starMatch.computeChi2(*_transfo);
96 
97  std::sort(chi2_array.get(), chi2_array.get() + npair);
98  double median = (npair & 1) ? chi2_array[npair / 2]
99  : (chi2_array[npair / 2 - 1] + chi2_array[npair / 2]) * 0.5;
100 
101  // discard outliers : the cut is understood as a "distance" cut
102  cut = sq(nSigmas) * median;
103  nremoved = chi2_cleanup(*this, cut, *_transfo);
104  } while (nremoved);
105  _dist2 = computeDist2(*this, *_transfo);
106 }
107 
108 /* not very robust : assumes that we went through Refine just before... */
110  int deno = (2. * size() - _transfo->getNpar());
111  return (deno > 0) ? sqrt(_dist2 / deno) : -1; // is -1 a good idea?
112 }
113 
114 void StarMatchList::setDistance(const Gtransfo &gtransfo) {
115  for (auto &smi : *this) smi.setDistance(gtransfo); // c'est compact
116 }
117 
118 unsigned StarMatchList::removeAmbiguities(const Gtransfo &gtransfo, int which) {
119  if (!which) return 0;
120  setDistance(gtransfo);
121  int initial_count = size();
122  if (which & 1) {
124  unique(sameStar1);
125  }
126  if (which & 2) {
128  unique(sameStar2);
129  }
130  return (initial_count - size());
131 }
132 
134  if (order == 0)
135  setTransfo(std::make_shared<GtransfoLinShift>());
136  else if (order == 1)
137  setTransfo(std::make_shared<GtransfoLin>());
138  else
139  setTransfo(GtransfoPoly(order));
140  // might consider throwing if order does not make sense (e.g. >10)
141  _order = order;
142 }
143 
144 /* This routine should operate on a copy : refineTransfo
145  might shorten the list */
146 /* it is not const although it tries not to change anything */
148  if (!_transfo) return nullptr;
149 
150  auto old_transfo = _transfo->clone();
151  double old_chi2 = _chi2;
152 
153  swap();
154  setTransfoOrder(_order);
155  refineTransfo(3.); // keep same order
156  auto inverted_transfo = _transfo->clone();
157  setTransfo(old_transfo.get());
158  swap();
159  _chi2 = old_chi2;
160 
161  return inverted_transfo;
162 }
163 
164 void StarMatchList::cutTail(int nKeep) {
165  iterator si;
166  int count = 0;
167  for (si = begin(); si != end() && count < nKeep; ++count, ++si)
168  ;
169  erase(si, end());
170 }
171 
173  for (auto &starMatch : *this) {
174  starMatch.swap();
175  }
176 }
177 
178 int StarMatchList::recoveredNumber(double mindist) const {
179  int n = 0;
180  GtransfoIdentity identity;
181  for (auto const &starMatch : *this) {
182  if (starMatch.computeDistance(identity) < mindist) n++;
183  }
184  return (n);
185 }
186 
187 void StarMatchList::applyTransfo(StarMatchList &transformed, const Gtransfo *priorTransfo,
188  const Gtransfo *posteriorTransfo) const {
189  transformed.clear();
191  const Gtransfo &T1 = (priorTransfo) ? *priorTransfo : id;
192  const Gtransfo &T2 = (posteriorTransfo) ? *posteriorTransfo : id;
193 
194  for (auto const &starMatch : *this) {
195  FatPoint p1;
196  T1.transformPosAndErrors(starMatch.point1, p1);
197  FatPoint p2;
198  T2.transformPosAndErrors(starMatch.point2, p2);
199  transformed.push_back(StarMatch(p1, p2, starMatch.s1, starMatch.s2));
200  }
201 }
202 
204  stream << " ================================================================" << std::endl
205  << " Transformation between lists of order " << getTransfoOrder() << std::endl
206  << *_transfo //<< endl
207  << " Chi2 = " << getChi2() << " Residual = " << computeResidual() << std::endl
208  << " Number in the list = " << size() << std::endl
209  << " ================================================================" << std::endl;
210 }
211 
212 double computeDist2(const StarMatchList &starMatchList, const Gtransfo &gtransfo) {
213  double dist2 = 0;
214  for (auto const &starMatch : starMatchList)
215  dist2 += gtransfo.apply(starMatch.point1).computeDist2(starMatch.point2);
216  return dist2;
217 }
218 
219 double computeChi2(const StarMatchList &starMatchList, const Gtransfo &gtransfo) {
220  unsigned s = starMatchList.size();
221  std::unique_ptr<double[]> chi2s(chi2_array(starMatchList, gtransfo));
222  double chi2 = 0;
223  for (unsigned k = 0; k < s; ++k) chi2 += chi2s[k];
224  return chi2;
225 }
226 } // namespace jointcal
227 } // namespace lsst
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:292
T copy(T... args)
A hanger for star associations.
Definition: StarMatch.h:31
def erase(frame=None)
void setDistance(const Gtransfo &gtransfo)
Sets the distance (residual) field of all std::list elements. Mandatory before sorting on distances...
Definition: StarMatch.cc:114
FatPoint point2
2 points
Definition: StarMatch.h:37
T endl(T... args)
double computeChi2(const Gtransfo &gtransfo) const
returns the chi2 (using errors in the FatPoint&#39;s)
Definition: StarMatch.cc:20
void setTransfoOrder(int order)
set transfo according to the given order.
Definition: StarMatch.cc:133
std::unique_ptr< Gtransfo > inverseTransfo()
returns the inverse transfo (swap, fit(refineTransfo) , and swap).
Definition: StarMatch.cc:147
T end(T... args)
Polynomial transformation class.
Definition: Gtransfo.h:193
T unique(T... args)
int recoveredNumber(double mindist) const
count the number of elements for which distance is < mindist
Definition: StarMatch.cc:178
A Point with uncertainties.
Definition: FatPoint.h:11
double x
coordinate
Definition: Point.h:18
pairs of points
void setDistance(const Gtransfo &gtransfo)
to be used before sorting on distances.
Definition: StarMatch.h:62
T push_back(T... args)
Class for a simple mapping implementing a generic Gtransfo.
void dumpTransfo(std::ostream &stream=std::cout) const
print the matching transformation quality (transfo, chi2, residual)
Definition: StarMatch.cc:203
int end
friend bool sameStar2(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:102
unsigned removeAmbiguities(const Gtransfo &gtransfo, 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:118
friend bool sameStar1(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:96
T erase(T... args)
double computeResidual() const
returns the average 1d Residual (last call to refineTransfo)
Definition: StarMatch.cc:109
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:99
T clear(T... args)
friend bool compareStar2(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:98
T count(T... args)
T get(T... args)
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
Definition: Gtransfo.h:150
T size(T... args)
STL class.
void refineTransfo(double nSigmas)
removes pairs beyond nSigmas in distance (where the sigma scale is set by the fit) and iterates until...
Definition: StarMatch.cc:68
T begin(T... args)
friend std::ostream & operator<<(std::ostream &stream, const StarMatch &Match)
Definition: StarMatch.cc:32
double computeDist2(const StarMatchList &S, const Gtransfo &gtransfo)
sum of distance squared
Definition: StarMatch.cc:212
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:39
int id
double x
T sort(T... args)
friend bool compareStar1(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:92
T sqrt(T... args)
void swap()
swaps elements 1 and 2 of each starmatch in std::list.
Definition: StarMatch.cc:172
STL class.
void applyTransfo(StarMatchList &transformed, const Gtransfo *priorTransfo, const Gtransfo *posteriorTransfo=nullptr) const
enables to get a transformed StarMatchList.
Definition: StarMatch.cc:187
void cutTail(int nKeep)
deletes the tail of the match std::list
Definition: StarMatch.cc:164
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0