LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
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 <iomanip>
27
31#include "algorithm" // for copy
32
33/* TO DO:
34 think about imposing a maximum number of matches that may
35 be discarded in Cleanup.
36*/
37
38namespace lsst {
39namespace jointcal {
40
41static double sq(double x) { return x * x; }
42
43double StarMatch::computeChi2(const AstrometryTransform &transform) const {
44 FatPoint tr;
45 transform.transformPosAndErrors(point1, tr);
46 double vxx = tr.vx + point2.vx;
47 double vyy = tr.vy + point2.vy;
48 double vxy = tr.vxy + point2.vxy;
49 double det = vxx * vyy - vxy * vxy;
50 return (vyy * sq(tr.x - point2.x) + vxx * sq(tr.y - point2.y) -
51 2 * vxy * (tr.x - point2.x) * (tr.y - point2.y)) /
52 det;
53}
54
56 stream << match.point1.x << ' ' << match.point1.y << ' ' << match.point2.x << ' ' << match.point2.y << ' '
57 << match.distance << std::endl;
58 return stream;
59}
60
61std::ostream &operator<<(std::ostream &stream, const StarMatchList &starMatchList) {
62 stream << " number of elements " << starMatchList.size() << std::endl;
63 copy(starMatchList.begin(), starMatchList.end(), std::ostream_iterator<StarMatch>(stream));
64 return stream;
65}
66
67static std::vector<double> chi2_array(const StarMatchList &starMatchList,
68 const AstrometryTransform &transform) {
69 std::vector<double> result(starMatchList.size());
70 unsigned count = 0;
71 for (auto const &it : starMatchList) result[count++] = it.computeChi2(transform);
72 return result;
73}
74
75static unsigned chi2_cleanup(StarMatchList &starMatchList, const double chi2Cut,
76 const AstrometryTransform &transform) {
77 unsigned erased = starMatchList.removeAmbiguities(transform);
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
92void StarMatchList::refineTransform(double nSigmas) {
93 double cut;
94 unsigned nremoved;
95 if (!_transform) _transform.reset(new AstrometryTransformLinear);
96 do {
97 int nused = size();
98 if (nused <= 2) {
99 _chi2 = -1;
100 break;
101 }
102 _chi2 = _transform->fit(*this);
103 /* convention of the fitted routines :
104 - chi2 = 0 means zero degrees of freedom
105 (this was not enforced in AstrometryTransform{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 AstrometryTransformPolynomial::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::vector<double> chi2_array(npair);
118 unsigned count = 0;
119 for (auto &starMatch : *this)
120 chi2_array[count++] = starMatch.chi2 = starMatch.computeChi2(*_transform);
121
122 std::sort(chi2_array.begin(), chi2_array.end());
123 double median = (npair & 1) ? chi2_array[npair / 2]
124 : (chi2_array[npair / 2 - 1] + chi2_array[npair / 2]) * 0.5;
125
126 // discard outliers : the cut is understood as a "distance" cut
127 cut = sq(nSigmas) * median;
128 nremoved = chi2_cleanup(*this, cut, *_transform);
129 } while (nremoved);
130 _dist2 = computeDist2(*this, *_transform);
131}
132
133/* not very robust : assumes that we went through Refine just before... */
135 int deno = (2. * size() - _transform->getNpar());
136 return (deno > 0) ? sqrt(_dist2 / deno) : -1; // is -1 a good idea?
137}
138
140 for (auto &smi : *this) smi.setDistance(transform); // c'est compact
141}
142
143unsigned StarMatchList::removeAmbiguities(const AstrometryTransform &transform, int which) {
144 if (!which) return 0;
145 setDistance(transform);
146 int initial_count = size();
147 if (which & 1) {
150 }
151 if (which & 2) {
154 }
155 return (initial_count - size());
156}
157
159 if (order == 0)
160 setTransform(std::make_shared<AstrometryTransformLinearShift>());
161 else if (order == 1)
162 setTransform(std::make_shared<AstrometryTransformLinear>());
163 else
165 // might consider throwing if order does not make sense (e.g. >10)
166 _order = order;
167}
168
169/* This routine should operate on a copy : refineTransform
170 might shorten the list */
171/* it is not const although it tries not to change anything */
173 if (!_transform) return nullptr;
174
175 auto old_transform = _transform->clone();
176 double old_chi2 = _chi2;
177
178 swap();
179 setTransformOrder(_order);
180 refineTransform(3.); // keep same order
181 auto inverted_transform = _transform->clone();
182 setTransform(old_transform.get());
183 swap();
184 _chi2 = old_chi2;
185
186 return inverted_transform;
187}
188
189void StarMatchList::cutTail(int nKeep) {
190 iterator si;
191 int count = 0;
192 for (si = begin(); si != end() && count < nKeep; ++count, ++si)
193 ;
194 erase(si, end());
195}
196
198 for (auto &starMatch : *this) {
199 starMatch.swap();
200 }
201}
202
203int StarMatchList::recoveredNumber(double mindist) const {
204 int n = 0;
206 for (auto const &starMatch : *this) {
207 if (starMatch.computeDistance(identity) < mindist) n++;
208 }
209 return (n);
210}
211
212void StarMatchList::applyTransform(StarMatchList &transformed, const AstrometryTransform *priorTransform,
213 const AstrometryTransform *posteriorTransform) const {
214 transformed.clear();
216 const AstrometryTransform &T1 = (priorTransform) ? *priorTransform : id;
217 const AstrometryTransform &T2 = (posteriorTransform) ? *posteriorTransform : id;
218
219 for (auto const &starMatch : *this) {
220 FatPoint p1;
221 T1.transformPosAndErrors(starMatch.point1, p1);
222 FatPoint p2;
223 T2.transformPosAndErrors(starMatch.point2, p2);
224 transformed.push_back(StarMatch(p1, p2, starMatch.s1, starMatch.s2));
225 }
226}
227
229 stream << " ================================================================" << std::endl
230 << " Transformation between lists of order " << getTransformOrder() << std::endl
231 << *_transform //<< endl
232 << " Chi2 = " << getChi2() << " Residual = " << computeResidual() << std::endl
233 << " Number in the list = " << size() << std::endl
234 << " ================================================================" << std::endl;
235}
236
237double computeDist2(const StarMatchList &starMatchList, const AstrometryTransform &transform) {
238 double dist2 = 0;
239 for (auto const &starMatch : starMatchList)
240 dist2 += transform.apply(starMatch.point1).computeDist2(starMatch.point2);
241 return dist2;
242}
243
244double computeChi2(const StarMatchList &starMatchList, const AstrometryTransform &transform) {
245 auto chi2s(chi2_array(starMatchList, transform));
246 double chi2 = 0;
247 for (auto const &c : chi2s) chi2 += c;
248 return chi2;
249}
250} // namespace jointcal
251} // namespace lsst
py::object result
Definition _schema.cc:429
int end
table::Key< int > id
Definition Detector.cc:162
pairs of points
T begin(T... args)
a virtual (interface) class for geometric transformations.
virtual void transformPosAndErrors(const FatPoint &in, FatPoint &out) const
A do-nothing transformation. It anyway has dummy routines to mimick a AstrometryTransform.
implements the linear transformations (6 real coefficients).
A Point with uncertainties.
Definition FatPoint.h:34
double x
coordinate
Definition Point.h:42
A hanger for star associations.
Definition StarMatch.h:55
double computeChi2(const AstrometryTransform &transform) const
returns the chi2 (using errors in the FatPoint's)
Definition StarMatch.cc:43
FatPoint point2
2 points
Definition StarMatch.h:61
std::unique_ptr< AstrometryTransform > inverseTransform()
returns the inverse transform (swap, fit(refineTransform) , and swap).
Definition StarMatch.cc:172
void setDistance(const AstrometryTransform &transform)
Sets the distance (residual) field of all std::list elements. Mandatory before sorting on distances.
Definition StarMatch.cc:139
void printTransform(std::ostream &stream=std::cout) const
print the matching transformation quality (transform, chi2, residual)
Definition StarMatch.cc:228
void swap()
swaps elements 1 and 2 of each starmatch in std::list.
Definition StarMatch.cc:197
double getChi2() const
access to the chi2 of the last call to refineTransform.
Definition StarMatch.h:171
int getTransformOrder() const
returns the order of the used transform
Definition StarMatch.h:174
void setTransform(const AstrometryTransform *transform)
sets a transform between the 2 std::lists and deletes the previous or default one....
Definition StarMatch.h:188
int recoveredNumber(double mindist) const
count the number of elements for which distance is < mindist
Definition StarMatch.cc:203
void setTransformOrder(int order)
set transform according to the given order.
Definition StarMatch.cc:158
double computeResidual() const
returns the average 1d Residual (last call to refineTransform)
Definition StarMatch.cc:134
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:143
void cutTail(int nKeep)
deletes the tail of the match std::list
Definition StarMatch.cc:189
void applyTransform(StarMatchList &transformed, const AstrometryTransform *priorTransform, const AstrometryTransform *posteriorTransform=nullptr) const
enables to get a transformed StarMatchList.
Definition StarMatch.cc:212
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:92
T copy(T... args)
T count(T... args)
T end(T... args)
T endl(T... args)
StarMatch erase(StarMatch ... args)
bool compareStar1(const StarMatch &one, const StarMatch &two)
Definition StarMatch.h:116
bool sameStar2(const StarMatch &one, const StarMatch &two)
Definition StarMatch.h:126
bool sameStar1(const StarMatch &one, const StarMatch &two)
Definition StarMatch.h:120
bool compareStar2(const StarMatch &one, const StarMatch &two)
Definition StarMatch.h:122
double computeDist2(const StarMatchList &S, const AstrometryTransform &transform)
sum of distance squared
Definition StarMatch.cc:237
double computeChi2(const StarMatchList &L, const AstrometryTransform &transform)
the actual chi2
Definition StarMatch.cc:244
std::ostream & operator<<(std::ostream &stream, AstrometryMapping const &mapping)
T size(T... args)
T sort(T... args)
T sqrt(T... args)
StarMatch unique(StarMatch ... args)
table::Key< int > order