lsst.jointcal  16.0-23-gcb65559+3
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 
29 #include "lsst/jointcal/Gtransfo.h"
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  gtransfo.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 
56 std::ostream &operator<<(std::ostream &stream, const StarMatch &match) {
57  stream << match.point1.x << ' ' << match.point1.y << ' ' << match.point2.x << ' ' << match.point2.y << ' '
58  << match.distance << std::endl;
59  return stream;
60 }
61 
62 std::ostream &operator<<(std::ostream &stream, const StarMatchList &starMatchList) {
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, const Gtransfo &gtransfo) {
69  unsigned s = starMatchList.size();
70  auto res = std::unique_ptr<double[]>(new double[s]);
71  unsigned count = 0;
72  for (auto const &it : starMatchList) res[count++] = it.computeChi2(gtransfo);
73  return res;
74 }
75 
76 static unsigned chi2_cleanup(StarMatchList &starMatchList, const double chi2Cut, const Gtransfo &gtransfo) {
77  unsigned erased = starMatchList.removeAmbiguities(gtransfo);
78  for (auto smi = starMatchList.begin(); smi != starMatchList.end();) {
79  if (smi->chi2 > chi2Cut) {
80  smi = starMatchList.erase(smi);
81  erased++;
82  } else
83  ++smi;
84  }
85  return erased;
86 }
87 
92 void StarMatchList::refineTransfo(double nSigmas) {
93  double cut;
94  unsigned nremoved;
95  if (!_transfo) _transfo.reset(new GtransfoLin);
96  do {
97  int nused = size();
98  if (nused <= 2) {
99  _chi2 = -1;
100  break;
101  }
102  _chi2 = _transfo->fit(*this);
103  /* convention of the fitted routines :
104  - chi2 = 0 means zero degrees of freedom
105  (this was not enforced in Gtransfo{Lin,Quad,Cub} ...)
106  - chi2 = -1 means ndof <0 (and hence no possible fit)
107  --> in either case, refinement is over
108  The fact that chi2 = 0 was not enforced when necessary means
109  that in this (rare) case, we were discarding matches at random....
110  With GtransfoPoly::fit, this is no longer the case.
111  */
112  if (_chi2 <= 0) return;
113  unsigned npair = int(size());
114  if (npair == 0) break; // should never happen
115 
116  // compute some chi2 statistics
117  std::unique_ptr<double[]> chi2_array(new double[npair]);
118  unsigned count = 0;
119  for (auto &starMatch : *this) chi2_array[count++] = starMatch.chi2 = starMatch.computeChi2(*_transfo);
120 
121  std::sort(chi2_array.get(), chi2_array.get() + npair);
122  double median = (npair & 1) ? chi2_array[npair / 2]
123  : (chi2_array[npair / 2 - 1] + chi2_array[npair / 2]) * 0.5;
124 
125  // discard outliers : the cut is understood as a "distance" cut
126  cut = sq(nSigmas) * median;
127  nremoved = chi2_cleanup(*this, cut, *_transfo);
128  } while (nremoved);
129  _dist2 = computeDist2(*this, *_transfo);
130 }
131 
132 /* not very robust : assumes that we went through Refine just before... */
134  int deno = (2. * size() - _transfo->getNpar());
135  return (deno > 0) ? sqrt(_dist2 / deno) : -1; // is -1 a good idea?
136 }
137 
138 void StarMatchList::setDistance(const Gtransfo &gtransfo) {
139  for (auto &smi : *this) smi.setDistance(gtransfo); // c'est compact
140 }
141 
142 unsigned StarMatchList::removeAmbiguities(const Gtransfo &gtransfo, int which) {
143  if (!which) return 0;
144  setDistance(gtransfo);
145  int initial_count = size();
146  if (which & 1) {
148  unique(sameStar1);
149  }
150  if (which & 2) {
152  unique(sameStar2);
153  }
154  return (initial_count - size());
155 }
156 
158  if (order == 0)
159  setTransfo(std::make_shared<GtransfoLinShift>());
160  else if (order == 1)
161  setTransfo(std::make_shared<GtransfoLin>());
162  else
163  setTransfo(GtransfoPoly(order));
164  // might consider throwing if order does not make sense (e.g. >10)
165  _order = order;
166 }
167 
168 /* This routine should operate on a copy : refineTransfo
169  might shorten the list */
170 /* it is not const although it tries not to change anything */
172  if (!_transfo) return nullptr;
173 
174  auto old_transfo = _transfo->clone();
175  double old_chi2 = _chi2;
176 
177  swap();
178  setTransfoOrder(_order);
179  refineTransfo(3.); // keep same order
180  auto inverted_transfo = _transfo->clone();
181  setTransfo(old_transfo.get());
182  swap();
183  _chi2 = old_chi2;
184 
185  return inverted_transfo;
186 }
187 
188 void StarMatchList::cutTail(int nKeep) {
189  iterator si;
190  int count = 0;
191  for (si = begin(); si != end() && count < nKeep; ++count, ++si)
192  ;
193  erase(si, end());
194 }
195 
197  for (auto &starMatch : *this) {
198  starMatch.swap();
199  }
200 }
201 
202 int StarMatchList::recoveredNumber(double mindist) const {
203  int n = 0;
204  GtransfoIdentity identity;
205  for (auto const &starMatch : *this) {
206  if (starMatch.computeDistance(identity) < mindist) n++;
207  }
208  return (n);
209 }
210 
211 void StarMatchList::applyTransfo(StarMatchList &transformed, const Gtransfo *priorTransfo,
212  const Gtransfo *posteriorTransfo) const {
213  transformed.clear();
215  const Gtransfo &T1 = (priorTransfo) ? *priorTransfo : id;
216  const Gtransfo &T2 = (posteriorTransfo) ? *posteriorTransfo : id;
217 
218  for (auto const &starMatch : *this) {
219  FatPoint p1;
220  T1.transformPosAndErrors(starMatch.point1, p1);
221  FatPoint p2;
222  T2.transformPosAndErrors(starMatch.point2, p2);
223  transformed.push_back(StarMatch(p1, p2, starMatch.s1, starMatch.s2));
224  }
225 }
226 
228  stream << " ================================================================" << std::endl
229  << " Transformation between lists of order " << getTransfoOrder() << std::endl
230  << *_transfo //<< endl
231  << " Chi2 = " << getChi2() << " Residual = " << computeResidual() << std::endl
232  << " Number in the list = " << size() << std::endl
233  << " ================================================================" << std::endl;
234 }
235 
236 double computeDist2(const StarMatchList &starMatchList, const Gtransfo &gtransfo) {
237  double dist2 = 0;
238  for (auto const &starMatch : starMatchList)
239  dist2 += gtransfo.apply(starMatch.point1).computeDist2(starMatch.point2);
240  return dist2;
241 }
242 
243 double computeChi2(const StarMatchList &starMatchList, const Gtransfo &gtransfo) {
244  unsigned s = starMatchList.size();
245  std::unique_ptr<double[]> chi2s(chi2_array(starMatchList, gtransfo));
246  double chi2 = 0;
247  for (unsigned k = 0; k < s; ++k) chi2 += chi2s[k];
248  return chi2;
249 }
250 } // namespace jointcal
251 } // namespace lsst
implements the linear transformations (6 real coefficients).
Definition: Gtransfo.h:414
T copy(T... args)
A hanger for star associations.
Definition: StarMatch.h:54
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:138
FatPoint point2
2 points
Definition: StarMatch.h:60
T endl(T... args)
double computeChi2(const Gtransfo &gtransfo) const
returns the chi2 (using errors in the FatPoint&#39;s)
Definition: StarMatch.cc:44
void setTransfoOrder(int order)
set transfo according to the given order.
Definition: StarMatch.cc:157
std::unique_ptr< Gtransfo > inverseTransfo()
returns the inverse transfo (swap, fit(refineTransfo) , and swap).
Definition: StarMatch.cc:171
T end(T... args)
Polynomial transformation class.
Definition: Gtransfo.h:276
T unique(T... args)
int recoveredNumber(double mindist) const
count the number of elements for which distance is < mindist
Definition: StarMatch.cc:202
A Point with uncertainties.
Definition: FatPoint.h:34
double x
coordinate
Definition: Point.h:41
pairs of points
void setDistance(const Gtransfo &gtransfo)
to be used before sorting on distances.
Definition: StarMatch.h:85
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:227
int end
friend bool sameStar2(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:125
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:142
friend bool sameStar1(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:119
T erase(T... args)
double computeResidual() const
returns the average 1d Residual (last call to refineTransfo)
Definition: StarMatch.cc:133
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
Definition: Gtransfo.cc:140
T clear(T... args)
table::Key< int > id
friend bool compareStar2(const StarMatch &one, const StarMatch &two)
Definition: StarMatch.h:121
T count(T... args)
T get(T... args)
A do-nothing transformation. It anyway has dummy routines to mimick a Gtransfo.
Definition: Gtransfo.h:219
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:92
T begin(T... args)
friend std::ostream & operator<<(std::ostream &stream, const StarMatch &Match)
Definition: StarMatch.cc:56
double computeDist2(const StarMatchList &S, const Gtransfo &gtransfo)
sum of distance squared
Definition: StarMatch.cc:236
a virtual (interface) class for geometric transformations.
Definition: Gtransfo.h:65
double x
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:196
STL class.
void applyTransfo(StarMatchList &transformed, const Gtransfo *priorTransfo, const Gtransfo *posteriorTransfo=nullptr) const
enables to get a transformed StarMatchList.
Definition: StarMatch.cc:211
void cutTail(int nKeep)
deletes the tail of the match std::list
Definition: StarMatch.cc:188
virtual void apply(const double xIn, const double yIn, double &xOut, double &yOut) const =0