1 | // ----------------------------------------------------------------------- |
---|
2 | // sorting.cc: Sort the list of Detections by channel or velocity. |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <sstream> |
---|
30 | #include <vector> |
---|
31 | #include <algorithm> |
---|
32 | #include <map> |
---|
33 | #include <duchamp/duchamp.hh> |
---|
34 | #include <duchamp/param.hh> |
---|
35 | #include <duchamp/Detection/detection.hh> |
---|
36 | |
---|
37 | |
---|
38 | /// @brief |
---|
39 | /// A class to match things pair-wise (useful for sorting). |
---|
40 | /// |
---|
41 | /// @details This class is deigned to match two quantities to each |
---|
42 | /// other. It was devised to find a way of taking a pair of lists |
---|
43 | /// that are matched, and sorting one list while keeping the second |
---|
44 | /// matched pair-wise. |
---|
45 | /// |
---|
46 | /// The elements are currently just assumed to be floats. This could be |
---|
47 | /// extended by templating, but at this stage we don't need to... |
---|
48 | class Pair |
---|
49 | { |
---|
50 | public: |
---|
51 | Pair(){}; |
---|
52 | virtual ~Pair(){}; |
---|
53 | friend bool operator< (const Pair& lhs, const Pair& rhs){ |
---|
54 | /// A comparison operator for pairs. Compare the primary elements |
---|
55 | /// of the two pairs, using the basic < operator. |
---|
56 | return (lhs.primary < rhs.primary); |
---|
57 | }; |
---|
58 | void define(float p, float m){ |
---|
59 | /// Basic assignment function. |
---|
60 | primary=p; matching=m; |
---|
61 | }; |
---|
62 | float get1(){return primary;}; |
---|
63 | float get2(){return matching;}; |
---|
64 | private: |
---|
65 | float primary; ///< The main element -- this will be the one that |
---|
66 | ///can be compared. |
---|
67 | float matching; ///< The secondary element -- this cannot be |
---|
68 | ///compared with other objects, it just tracks the |
---|
69 | ///primary. |
---|
70 | }; |
---|
71 | |
---|
72 | //====================================================================== |
---|
73 | |
---|
74 | namespace duchamp |
---|
75 | { |
---|
76 | |
---|
77 | void SortByZ(std::vector <Detection> &inputList) |
---|
78 | { |
---|
79 | /// A Function that takes a list of Detections and sorts them in |
---|
80 | /// order of increasing z-pixel value. Upon return, the inputList |
---|
81 | /// is sorted. |
---|
82 | /// |
---|
83 | /// We use the std::stable_sort function, so that the order of |
---|
84 | /// objects with the same z-value is preserved. |
---|
85 | /// \param inputList List of Detections to be sorted. |
---|
86 | /// \return The inputList is returned with the elements sorted. |
---|
87 | |
---|
88 | std::multimap<float, size_t> complist; |
---|
89 | std::vector<Detection> sorted; |
---|
90 | std::multimap<float, size_t>::iterator comp; |
---|
91 | std::vector<Detection>::iterator det; |
---|
92 | size_t ct=0; |
---|
93 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
94 | complist.insert(std::pair<float, size_t>(det->getZcentre(), ct++)); |
---|
95 | } |
---|
96 | |
---|
97 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
98 | sorted.push_back(inputList[comp->second]); |
---|
99 | |
---|
100 | inputList.clear(); |
---|
101 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
102 | sorted.clear(); |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | //====================================================================== |
---|
107 | |
---|
108 | void SortByVel(std::vector <Detection> &inputList) |
---|
109 | { |
---|
110 | /// @details |
---|
111 | /// A Function that takes a list of Detections and sorts them in |
---|
112 | /// order of increasing velocity. |
---|
113 | /// Every member of the vector needs to have WCS defined, (and if so, |
---|
114 | /// then vel is assumed to be defined for all), otherwise no sorting |
---|
115 | /// is done. |
---|
116 | /// |
---|
117 | /// We use the std::stable_sort function, so that the order of |
---|
118 | /// objects with the same z-value is preserved. |
---|
119 | /// |
---|
120 | /// \param inputList List of Detections to be sorted. |
---|
121 | /// \return The inputList is returned with the elements sorted, |
---|
122 | /// unless the WCS is not good for at least one element, in which |
---|
123 | /// case it is returned unaltered. |
---|
124 | |
---|
125 | bool isGood = true; |
---|
126 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
127 | |
---|
128 | if(isGood){ |
---|
129 | |
---|
130 | std::multimap<double, size_t> complist; |
---|
131 | std::vector<Detection> sorted; |
---|
132 | std::multimap<double, size_t>::iterator comp; |
---|
133 | std::vector<Detection>::iterator det; |
---|
134 | size_t ct=0; |
---|
135 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
136 | complist.insert(std::pair<double, size_t>(det->getVel(), ct++)); |
---|
137 | } |
---|
138 | |
---|
139 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
140 | sorted.push_back(inputList[comp->second]); |
---|
141 | |
---|
142 | inputList.clear(); |
---|
143 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
144 | sorted.clear(); |
---|
145 | |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | //====================================================================== |
---|
152 | |
---|
153 | void SortDetections(std::vector <Detection> &inputList, std::string parameter) |
---|
154 | { |
---|
155 | /// @details |
---|
156 | /// A Function that takes a list of Detections and sorts them in |
---|
157 | /// order of increasing value of the parameter given. |
---|
158 | /// |
---|
159 | /// For parameters that need the WCS (iflux, vel, ra, dec, w50), a |
---|
160 | /// check is made that the WCS is valid, using |
---|
161 | /// Detection::isWCS(). If it is not, the list is returned |
---|
162 | /// unsorted. |
---|
163 | /// |
---|
164 | /// \param inputList List of Detections to be sorted. |
---|
165 | /// \param parameter The name of the parameter to be sorted |
---|
166 | /// on. Options are listed in the param.hh file |
---|
167 | /// \return The inputList is returned with the elements sorted, |
---|
168 | /// unless the WCS is not good for at least one element, |
---|
169 | /// in which case it is returned unaltered. |
---|
170 | |
---|
171 | bool OK = false, reverseSort=(parameter[0]=='-'); |
---|
172 | std::string checkParam; |
---|
173 | if(reverseSort) checkParam = parameter.substr(1); |
---|
174 | else checkParam = parameter; |
---|
175 | for(int i=0;i<numSortingParamOptions;i++) |
---|
176 | OK = OK || (checkParam == sortingParamOptions[i]); |
---|
177 | if(!OK){ |
---|
178 | DUCHAMPERROR("SortDetections", "Invalid sorting parameter: " << parameter << " -- Not doing any sorting."); |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | bool isGood = true; |
---|
183 | if(checkParam!="zvalue" && checkParam!="pflux" && checkParam!="snr" && checkParam!="xvalue" && checkParam!="yvalue"){ |
---|
184 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
185 | } |
---|
186 | |
---|
187 | if(isGood){ |
---|
188 | |
---|
189 | std::vector<Detection> sorted; |
---|
190 | std::vector<Detection>::iterator det; |
---|
191 | |
---|
192 | if(checkParam=="xvalue" || checkParam=="yvalue" || checkParam=="zvalue" || checkParam=="iflux" || checkParam=="pflux" || checkParam=="snr"){ |
---|
193 | |
---|
194 | std::multimap<float, size_t> complist; |
---|
195 | std::multimap<float, size_t>::iterator comp; |
---|
196 | size_t ct=0; |
---|
197 | float reverse = reverseSort ? -1. : 1.; |
---|
198 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
199 | if(checkParam=="xvalue") complist.insert(std::pair<float, size_t>(reverse*det->getXcentre(), ct++)); |
---|
200 | else if(checkParam=="yvalue") complist.insert(std::pair<float, size_t>(reverse*det->getYcentre(), ct++)); |
---|
201 | else if(checkParam=="zvalue") complist.insert(std::pair<float, size_t>(reverse*det->getZcentre(), ct++)); |
---|
202 | else if(checkParam=="iflux") complist.insert(std::pair<float, size_t>(reverse*det->getIntegFlux(), ct++)); |
---|
203 | else if(checkParam=="pflux") complist.insert(std::pair<float, size_t>(reverse*det->getPeakFlux(), ct++)); |
---|
204 | else if(checkParam=="snr") complist.insert(std::pair<float, size_t>(reverse*det->getPeakSNR(), ct++)); |
---|
205 | } |
---|
206 | |
---|
207 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
208 | sorted.push_back(inputList[comp->second]); |
---|
209 | |
---|
210 | } |
---|
211 | else if(checkParam=="ra" || checkParam=="dec" || checkParam=="vel" || checkParam=="w50"){ |
---|
212 | |
---|
213 | std::multimap<double, size_t> complist; |
---|
214 | std::multimap<double, size_t>::iterator comp; |
---|
215 | size_t ct=0; |
---|
216 | double reverse = reverseSort ? -1. : 1.; |
---|
217 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
218 | if(checkParam=="ra") complist.insert(std::pair<double, size_t>(reverse*det->getRA(), ct++)); |
---|
219 | else if(checkParam=="dec") complist.insert(std::pair<double, size_t>(reverse*det->getDec(), ct++)); |
---|
220 | else if(checkParam=="vel") complist.insert(std::pair<double, size_t>(reverse*det->getVel(), ct++)); |
---|
221 | else if(checkParam=="w50") complist.insert(std::pair<double, size_t>(reverse*det->getW50(), ct++)); |
---|
222 | } |
---|
223 | |
---|
224 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
225 | sorted.push_back(inputList[comp->second]); |
---|
226 | |
---|
227 | } |
---|
228 | |
---|
229 | inputList.clear(); |
---|
230 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
231 | sorted.clear(); |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|