[301] | 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 | // ----------------------------------------------------------------------- |
---|
[571] | 28 | #include <iostream> |
---|
| 29 | #include <sstream> |
---|
[3] | 30 | #include <vector> |
---|
[212] | 31 | #include <algorithm> |
---|
[954] | 32 | #include <map> |
---|
[571] | 33 | #include <duchamp/duchamp.hh> |
---|
| 34 | #include <duchamp/param.hh> |
---|
[393] | 35 | #include <duchamp/Detection/detection.hh> |
---|
[3] | 36 | |
---|
| 37 | |
---|
[528] | 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... |
---|
[212] | 48 | class Pair |
---|
| 49 | { |
---|
| 50 | public: |
---|
| 51 | Pair(){}; |
---|
| 52 | virtual ~Pair(){}; |
---|
| 53 | friend bool operator< (const Pair& lhs, const Pair& rhs){ |
---|
[528] | 54 | /// A comparison operator for pairs. Compare the primary elements |
---|
| 55 | /// of the two pairs, using the basic < operator. |
---|
[212] | 56 | return (lhs.primary < rhs.primary); |
---|
| 57 | }; |
---|
[221] | 58 | void define(float p, float m){ |
---|
[528] | 59 | /// Basic assignment function. |
---|
[221] | 60 | primary=p; matching=m; |
---|
| 61 | }; |
---|
[212] | 62 | float get1(){return primary;}; |
---|
| 63 | float get2(){return matching;}; |
---|
| 64 | private: |
---|
[258] | 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. |
---|
[212] | 70 | }; |
---|
| 71 | |
---|
[221] | 72 | //====================================================================== |
---|
| 73 | |
---|
[378] | 74 | namespace duchamp |
---|
[3] | 75 | { |
---|
| 76 | |
---|
[571] | 77 | void SortByZ(std::vector <Detection> &inputList) |
---|
[378] | 78 | { |
---|
[528] | 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. |
---|
[378] | 87 | |
---|
[954] | 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 | } |
---|
[3] | 96 | |
---|
[954] | 97 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 98 | sorted.push_back(inputList[comp->second]); |
---|
[3] | 99 | |
---|
[378] | 100 | inputList.clear(); |
---|
[954] | 101 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
[378] | 102 | sorted.clear(); |
---|
[954] | 103 | |
---|
[378] | 104 | } |
---|
[3] | 105 | |
---|
[378] | 106 | //====================================================================== |
---|
[301] | 107 | |
---|
[571] | 108 | void SortByVel(std::vector <Detection> &inputList) |
---|
[378] | 109 | { |
---|
[528] | 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. |
---|
[3] | 124 | |
---|
[378] | 125 | bool isGood = true; |
---|
[623] | 126 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
[3] | 127 | |
---|
[378] | 128 | if(isGood){ |
---|
[3] | 129 | |
---|
[954] | 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 | } |
---|
[3] | 138 | |
---|
[954] | 139 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 140 | sorted.push_back(inputList[comp->second]); |
---|
[3] | 141 | |
---|
[378] | 142 | inputList.clear(); |
---|
[954] | 143 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
[378] | 144 | sorted.clear(); |
---|
[954] | 145 | |
---|
| 146 | |
---|
[378] | 147 | } |
---|
[3] | 148 | |
---|
[378] | 149 | } |
---|
| 150 | |
---|
[571] | 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 | /// |
---|
[955] | 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. |
---|
[571] | 163 | /// |
---|
| 164 | /// \param inputList List of Detections to be sorted. |
---|
| 165 | /// \param parameter The name of the parameter to be sorted |
---|
[955] | 166 | /// on. Options are listed in the param.hh file |
---|
[571] | 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 | |
---|
[955] | 171 | bool OK = false, reverseSort=(parameter[0]=='-'); |
---|
| 172 | std::string checkParam; |
---|
| 173 | if(reverseSort) checkParam = parameter.substr(1); |
---|
| 174 | else checkParam = parameter; |
---|
[571] | 175 | for(int i=0;i<numSortingParamOptions;i++) |
---|
[955] | 176 | OK = OK || (checkParam == sortingParamOptions[i]); |
---|
[571] | 177 | if(!OK){ |
---|
[913] | 178 | DUCHAMPERROR("SortDetections", "Invalid sorting parameter: " << parameter << " -- Not doing any sorting."); |
---|
[571] | 179 | return; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | bool isGood = true; |
---|
[955] | 183 | if(checkParam!="zvalue" && checkParam!="pflux" && checkParam!="snr" && checkParam!="xvalue" && checkParam!="yvalue"){ |
---|
[623] | 184 | for(size_t i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS(); |
---|
[571] | 185 | } |
---|
| 186 | |
---|
| 187 | if(isGood){ |
---|
| 188 | |
---|
[954] | 189 | std::vector<Detection> sorted; |
---|
| 190 | std::vector<Detection>::iterator det; |
---|
| 191 | |
---|
[955] | 192 | if(checkParam=="xvalue" || checkParam=="yvalue" || checkParam=="zvalue" || checkParam=="iflux" || checkParam=="pflux" || checkParam=="snr"){ |
---|
[954] | 193 | |
---|
| 194 | std::multimap<float, size_t> complist; |
---|
| 195 | std::multimap<float, size_t>::iterator comp; |
---|
| 196 | size_t ct=0; |
---|
[955] | 197 | float reverse = reverseSort ? -1. : 1.; |
---|
[954] | 198 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
[955] | 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++)); |
---|
[954] | 205 | } |
---|
| 206 | |
---|
| 207 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 208 | sorted.push_back(inputList[comp->second]); |
---|
| 209 | |
---|
[571] | 210 | } |
---|
[955] | 211 | else if(checkParam=="ra" || checkParam=="dec" || checkParam=="vel" || checkParam=="w50"){ |
---|
[571] | 212 | |
---|
[954] | 213 | std::multimap<double, size_t> complist; |
---|
| 214 | std::multimap<double, size_t>::iterator comp; |
---|
| 215 | size_t ct=0; |
---|
[955] | 216 | double reverse = reverseSort ? -1. : 1.; |
---|
[954] | 217 | for (det=inputList.begin();det<inputList.end();det++){ |
---|
[955] | 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++)); |
---|
[954] | 222 | } |
---|
| 223 | |
---|
| 224 | for (comp = complist.begin(); comp != complist.end(); comp++) |
---|
| 225 | sorted.push_back(inputList[comp->second]); |
---|
| 226 | |
---|
| 227 | } |
---|
[571] | 228 | |
---|
| 229 | inputList.clear(); |
---|
[954] | 230 | for (det=sorted.begin();det<sorted.end();det++) inputList.push_back( *det ); |
---|
[571] | 231 | sorted.clear(); |
---|
[954] | 232 | |
---|
[571] | 233 | } |
---|
| 234 | |
---|
| 235 | } |
---|
| 236 | |
---|
[378] | 237 | } |
---|