[299] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // CubicSearch.cc: Searching a 3-dimensional Cube. |
---|
| 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 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
| 29 | #include <iomanip> |
---|
| 30 | #include <fstream> |
---|
| 31 | #include <vector> |
---|
[393] | 32 | #include <duchamp/param.hh> |
---|
| 33 | #include <duchamp/PixelMap/Object3D.hh> |
---|
| 34 | #include <duchamp/Cubes/cubes.hh> |
---|
| 35 | #include <duchamp/Utils/utils.hh> |
---|
| 36 | #include <duchamp/Utils/feedback.hh> |
---|
| 37 | #include <duchamp/Utils/Statistics.hh> |
---|
[3] | 38 | |
---|
[232] | 39 | using std::vector; |
---|
[258] | 40 | using namespace PixelInfo; |
---|
[274] | 41 | using namespace Statistics; |
---|
[232] | 42 | |
---|
[378] | 43 | namespace duchamp |
---|
| 44 | { |
---|
| 45 | |
---|
[103] | 46 | void Cube::CubicSearch() |
---|
[3] | 47 | { |
---|
[528] | 48 | /// @details |
---|
| 49 | /// A front end to the cubic searching routine that does not |
---|
| 50 | /// involve any wavelet reconstruction. |
---|
| 51 | /// The statistics of the cube are calculated first of all. |
---|
| 52 | /// If baseline-removal is required that is done prior to searching. |
---|
| 53 | /// Once searching is complete, the detection map is updated and |
---|
| 54 | /// the intermediate detections are logged in the log file. |
---|
[3] | 55 | |
---|
[219] | 56 | if(this->par.isVerbose()) std::cout << " "; |
---|
[258] | 57 | |
---|
[189] | 58 | this->setCubeStats(); |
---|
| 59 | |
---|
[258] | 60 | if(this->par.isVerbose()) std::cout << " Searching... " << std::flush; |
---|
| 61 | |
---|
[686] | 62 | *this->objectList = search3DArray(this->axisDim,this->array, |
---|
| 63 | this->par,this->Stats); |
---|
[189] | 64 | |
---|
[258] | 65 | if(this->par.isVerbose()) std::cout << " Updating detection map... " |
---|
| 66 | << std::flush; |
---|
[3] | 67 | this->updateDetectMap(); |
---|
[258] | 68 | if(this->par.isVerbose()) std::cout << "Done.\n"; |
---|
[3] | 69 | |
---|
[258] | 70 | if(this->par.getFlagLog()){ |
---|
| 71 | if(this->par.isVerbose()) |
---|
| 72 | std::cout << " Logging intermediate detections... " << std::flush; |
---|
| 73 | this->logDetectionList(); |
---|
| 74 | if(this->par.isVerbose()) std::cout << "Done.\n"; |
---|
| 75 | } |
---|
| 76 | |
---|
[3] | 77 | } |
---|
[263] | 78 | //--------------------------------------------------------------- |
---|
[3] | 79 | |
---|
[275] | 80 | std::vector <Detection> search3DArray(long *dim, float *Array, Param &par, |
---|
| 81 | StatsContainer<float> &stats) |
---|
[3] | 82 | { |
---|
[686] | 83 | |
---|
| 84 | if(par.getSearchType()=="spectral") |
---|
| 85 | return search3DArraySpectral(dim,Array,par,stats); |
---|
| 86 | else if(par.getSearchType()=="spatial") |
---|
| 87 | return search3DArraySpatial(dim,Array,par,stats); |
---|
| 88 | else{ |
---|
| 89 | std::stringstream ss; |
---|
| 90 | ss << "Unknown search type : " << par.getSearchType(); |
---|
| 91 | duchampError("search3DArray",ss.str()); |
---|
| 92 | return std::vector<Detection>(0); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | //--------------------------------------------------------------- |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | std::vector <Detection> search3DArraySpectral(long *dim, float *Array, Param &par, |
---|
| 99 | StatsContainer<float> &stats) |
---|
| 100 | { |
---|
[528] | 101 | /// @details |
---|
| 102 | /// Takes a dimension array and data array as input (and Parameter set) |
---|
[686] | 103 | /// and searches for detections in just the 1D spectra. |
---|
[528] | 104 | /// Returns a vector list of Detections. |
---|
| 105 | /// No reconstruction is assumed to have taken place, so statistics are |
---|
| 106 | /// calculated (using robust methods) from the data array itself. |
---|
| 107 | /// \param dim Array of dimension sizes for the data array. |
---|
| 108 | /// \param Array Array of data. |
---|
| 109 | /// \param par Param set defining how to do detection, and what a |
---|
| 110 | /// BLANK pixel is etc. |
---|
| 111 | /// \param stats The statistics that define what a detection is. |
---|
| 112 | /// \return Vector of detected objects. |
---|
[3] | 113 | |
---|
[275] | 114 | std::vector <Detection> outputList; |
---|
[103] | 115 | long zdim = dim[2]; |
---|
| 116 | long xySize = dim[0] * dim[1]; |
---|
[3] | 117 | int num = 0; |
---|
| 118 | |
---|
| 119 | if(zdim>1){ |
---|
[691] | 120 | |
---|
| 121 | ProgressBar bar; |
---|
| 122 | if(par.isVerbose()) bar.init(xySize); |
---|
[3] | 123 | |
---|
[258] | 124 | bool *doPixel = new bool[xySize]; |
---|
[3] | 125 | for(int npix=0; npix<xySize; npix++){ |
---|
[258] | 126 | doPixel[npix] = false; |
---|
| 127 | for(int z=0;z<zdim;z++){ |
---|
| 128 | doPixel[npix] = doPixel[npix] || |
---|
| 129 | (!par.isBlank(Array[npix]) && !par.isInMW(z)); |
---|
| 130 | } |
---|
| 131 | // doPixel[i] is false only when there are no good pixels in spectrum |
---|
| 132 | // of pixel #i. |
---|
| 133 | } |
---|
[86] | 134 | |
---|
[258] | 135 | long *specdim = new long[2]; |
---|
| 136 | specdim[0] = zdim; specdim[1]=1; |
---|
| 137 | Image *spectrum = new Image(specdim); |
---|
| 138 | delete [] specdim; |
---|
| 139 | spectrum->saveParam(par); |
---|
| 140 | spectrum->saveStats(stats); |
---|
[691] | 141 | // spectrum->setMinSize(par.getMinChannels()); |
---|
| 142 | spectrum->setMinSize(1); |
---|
[3] | 143 | |
---|
[258] | 144 | for(int y=0; y<dim[1]; y++){ |
---|
| 145 | for(int x=0; x<dim[0]; x++){ |
---|
[103] | 146 | |
---|
[258] | 147 | int npix = y*dim[0] + x; |
---|
| 148 | if( par.isVerbose() ) bar.update(npix+1); |
---|
| 149 | |
---|
| 150 | if(doPixel[npix]){ |
---|
| 151 | spectrum->extractSpectrum(Array,dim,npix); |
---|
| 152 | spectrum->removeMW(); // only works if flagMW is true |
---|
[582] | 153 | std::vector<Scan> objlist = spectrum->findSources1D(); |
---|
[623] | 154 | std::vector<Scan>::iterator obj; |
---|
[258] | 155 | num += objlist.size(); |
---|
[623] | 156 | for(obj=objlist.begin();obj<objlist.end();obj++){ |
---|
[258] | 157 | Detection newObject; |
---|
[103] | 158 | // Fix up coordinates of each pixel to match original array |
---|
[623] | 159 | for(int z=obj->getX();z<=obj->getXmax();z++) { |
---|
[570] | 160 | newObject.addPixel(x,y,z); |
---|
[258] | 161 | } |
---|
| 162 | newObject.setOffsets(par); |
---|
[691] | 163 | if(par.getFlagTwoStageMerging()) mergeIntoList(newObject,outputList,par); |
---|
| 164 | else outputList.push_back(newObject); |
---|
[103] | 165 | } |
---|
[3] | 166 | } |
---|
| 167 | } |
---|
[103] | 168 | } |
---|
[3] | 169 | |
---|
[258] | 170 | delete spectrum; |
---|
| 171 | delete [] doPixel; |
---|
| 172 | |
---|
[691] | 173 | |
---|
| 174 | if(par.isVerbose()){ |
---|
| 175 | bar.remove(); |
---|
| 176 | std::cout << "Found " << num << ".\n"; |
---|
[175] | 177 | } |
---|
[3] | 178 | |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | return outputList; |
---|
| 182 | } |
---|
[263] | 183 | //--------------------------------------------------------------- |
---|
[258] | 184 | |
---|
[686] | 185 | std::vector <Detection> search3DArraySpatial(long *dim, float *Array, |
---|
| 186 | Param &par, |
---|
| 187 | StatsContainer<float> &stats) |
---|
[263] | 188 | { |
---|
[528] | 189 | /// @details |
---|
| 190 | /// Takes a dimension array and data array as input (and Parameter |
---|
| 191 | /// set) and searches for detections just in the channel maps -- no |
---|
| 192 | /// 1D searches are done. |
---|
| 193 | /// Returns a vector list of Detections. |
---|
| 194 | /// No reconstruction is assumed to have taken place, so only the base |
---|
| 195 | /// data array is searched. |
---|
| 196 | /// \param dim Array of dimension sizes for the data array. |
---|
| 197 | /// \param Array Array of data. |
---|
| 198 | /// \param par Param set defining how to do detection, and what a |
---|
| 199 | /// BLANK pixel is etc. |
---|
| 200 | /// \param stats The statistics that define what a detection is. |
---|
| 201 | /// \return A std::vector of detected objects. |
---|
[258] | 202 | |
---|
[275] | 203 | std::vector <Detection> outputList; |
---|
[263] | 204 | long zdim = dim[2]; |
---|
| 205 | int num = 0; |
---|
| 206 | |
---|
| 207 | ProgressBar bar; |
---|
[377] | 208 | bool useBar = (zdim>1); |
---|
| 209 | if(useBar && par.isVerbose()) bar.init(zdim); |
---|
[263] | 210 | |
---|
| 211 | long *imdim = new long[2]; |
---|
| 212 | imdim[0] = dim[0]; imdim[1] = dim[1]; |
---|
| 213 | Image *channelImage = new Image(imdim); |
---|
| 214 | delete [] imdim; |
---|
| 215 | channelImage->saveParam(par); |
---|
| 216 | channelImage->saveStats(stats); |
---|
| 217 | channelImage->setMinSize(1); |
---|
| 218 | |
---|
| 219 | for(int z=0; z<zdim; z++){ |
---|
| 220 | |
---|
[377] | 221 | if( par.isVerbose() && useBar ) bar.update(z+1); |
---|
[263] | 222 | |
---|
| 223 | if(!par.isInMW(z)){ |
---|
| 224 | |
---|
| 225 | channelImage->extractImage(Array,dim,z); |
---|
[582] | 226 | std::vector<Object2D> objlist = channelImage->findSources2D(); |
---|
[623] | 227 | std::vector<Object2D>::iterator obj; |
---|
[263] | 228 | num += objlist.size(); |
---|
[623] | 229 | for(obj=objlist.begin();obj!=objlist.end();obj++){ |
---|
[263] | 230 | Detection newObject; |
---|
[623] | 231 | newObject.addChannel(z,*obj); |
---|
[263] | 232 | newObject.setOffsets(par); |
---|
[691] | 233 | if(par.getFlagTwoStageMerging()) mergeIntoList(newObject,outputList,par); |
---|
| 234 | else outputList.push_back(newObject); |
---|
[263] | 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | delete channelImage; |
---|
| 241 | |
---|
| 242 | if(par.isVerbose()){ |
---|
[377] | 243 | if(useBar) bar.remove(); |
---|
| 244 | std::cout << "Found " << num << ".\n"; |
---|
[263] | 245 | } |
---|
| 246 | |
---|
| 247 | return outputList; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
[378] | 251 | } |
---|