[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 | |
---|
[263] | 62 | // this->objectList = search3DArray(this->axisDim,this->array, |
---|
| 63 | // this->par,this->Stats); |
---|
[291] | 64 | *this->objectList = search3DArraySimple(this->axisDim,this->array, |
---|
[263] | 65 | this->par,this->Stats); |
---|
[189] | 66 | |
---|
[258] | 67 | if(this->par.isVerbose()) std::cout << " Updating detection map... " |
---|
| 68 | << std::flush; |
---|
[3] | 69 | this->updateDetectMap(); |
---|
[258] | 70 | if(this->par.isVerbose()) std::cout << "Done.\n"; |
---|
[3] | 71 | |
---|
[258] | 72 | if(this->par.getFlagLog()){ |
---|
| 73 | if(this->par.isVerbose()) |
---|
| 74 | std::cout << " Logging intermediate detections... " << std::flush; |
---|
| 75 | this->logDetectionList(); |
---|
| 76 | if(this->par.isVerbose()) std::cout << "Done.\n"; |
---|
| 77 | } |
---|
| 78 | |
---|
[3] | 79 | } |
---|
[263] | 80 | //--------------------------------------------------------------- |
---|
[3] | 81 | |
---|
| 82 | |
---|
[275] | 83 | std::vector <Detection> search3DArray(long *dim, float *Array, Param &par, |
---|
| 84 | StatsContainer<float> &stats) |
---|
[3] | 85 | { |
---|
[528] | 86 | /// @details |
---|
| 87 | /// Takes a dimension array and data array as input (and Parameter set) |
---|
| 88 | /// and searches for detections in a combination of 1D and 2D searches. |
---|
| 89 | /// Returns a vector list of Detections. |
---|
| 90 | /// No reconstruction is assumed to have taken place, so statistics are |
---|
| 91 | /// calculated (using robust methods) from the data array itself. |
---|
| 92 | /// \param dim Array of dimension sizes for the data array. |
---|
| 93 | /// \param Array Array of data. |
---|
| 94 | /// \param par Param set defining how to do detection, and what a |
---|
| 95 | /// BLANK pixel is etc. |
---|
| 96 | /// \param stats The statistics that define what a detection is. |
---|
| 97 | /// \return Vector of detected objects. |
---|
[3] | 98 | |
---|
[275] | 99 | std::vector <Detection> outputList; |
---|
[103] | 100 | long zdim = dim[2]; |
---|
| 101 | long xySize = dim[0] * dim[1]; |
---|
[3] | 102 | int num = 0; |
---|
| 103 | |
---|
[187] | 104 | ProgressBar bar; |
---|
[3] | 105 | // FIRST SEARCH -- IN EACH SPECTRUM. |
---|
| 106 | if(zdim>1){ |
---|
[175] | 107 | if(par.isVerbose()) { |
---|
| 108 | std::cout << " 1D: "; |
---|
[187] | 109 | bar.init(xySize); |
---|
[175] | 110 | } |
---|
[3] | 111 | |
---|
[258] | 112 | bool *doPixel = new bool[xySize]; |
---|
[3] | 113 | for(int npix=0; npix<xySize; npix++){ |
---|
[258] | 114 | doPixel[npix] = false; |
---|
| 115 | for(int z=0;z<zdim;z++){ |
---|
| 116 | doPixel[npix] = doPixel[npix] || |
---|
| 117 | (!par.isBlank(Array[npix]) && !par.isInMW(z)); |
---|
| 118 | } |
---|
| 119 | // doPixel[i] is false only when there are no good pixels in spectrum |
---|
| 120 | // of pixel #i. |
---|
| 121 | } |
---|
[86] | 122 | |
---|
[258] | 123 | long *specdim = new long[2]; |
---|
| 124 | specdim[0] = zdim; specdim[1]=1; |
---|
| 125 | Image *spectrum = new Image(specdim); |
---|
| 126 | delete [] specdim; |
---|
| 127 | spectrum->saveParam(par); |
---|
| 128 | spectrum->saveStats(stats); |
---|
| 129 | spectrum->setMinSize(par.getMinChannels()); |
---|
| 130 | spectrum->pars().setBeamSize(2.); |
---|
| 131 | // beam size: for spectrum, only neighbouring channels correlated |
---|
[3] | 132 | |
---|
[258] | 133 | for(int y=0; y<dim[1]; y++){ |
---|
| 134 | for(int x=0; x<dim[0]; x++){ |
---|
[103] | 135 | |
---|
[258] | 136 | int npix = y*dim[0] + x; |
---|
| 137 | if( par.isVerbose() ) bar.update(npix+1); |
---|
| 138 | |
---|
| 139 | if(doPixel[npix]){ |
---|
| 140 | spectrum->extractSpectrum(Array,dim,npix); |
---|
| 141 | spectrum->removeMW(); // only works if flagMW is true |
---|
| 142 | std::vector<Scan> objlist = spectrum->spectrumDetect(); |
---|
| 143 | num += objlist.size(); |
---|
| 144 | for(int obj=0;obj<objlist.size();obj++){ |
---|
| 145 | Detection newObject; |
---|
[103] | 146 | // Fix up coordinates of each pixel to match original array |
---|
[258] | 147 | for(int z=objlist[obj].getX();z<=objlist[obj].getXmax();z++) { |
---|
| 148 | newObject.pixels().addPixel(x,y,z); |
---|
| 149 | } |
---|
| 150 | newObject.setOffsets(par); |
---|
| 151 | mergeIntoList(newObject,outputList,par); |
---|
[103] | 152 | } |
---|
[3] | 153 | } |
---|
| 154 | } |
---|
[103] | 155 | } |
---|
[3] | 156 | |
---|
[258] | 157 | delete spectrum; |
---|
| 158 | delete [] doPixel; |
---|
| 159 | |
---|
[175] | 160 | if(par.isVerbose()) { |
---|
[219] | 161 | bar.fillSpace("Found "); |
---|
| 162 | std::cout << num <<";" << std::flush; |
---|
[175] | 163 | } |
---|
[3] | 164 | |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | // SECOND SEARCH -- IN EACH CHANNEL |
---|
[175] | 168 | if(par.isVerbose()){ |
---|
| 169 | std::cout << " 2D: "; |
---|
[187] | 170 | bar.init(zdim); |
---|
[175] | 171 | } |
---|
| 172 | |
---|
[146] | 173 | num = 0; |
---|
[3] | 174 | |
---|
[258] | 175 | long *imdim = new long[2]; |
---|
| 176 | imdim[0] = dim[0]; imdim[1] = dim[1]; |
---|
| 177 | Image *channelImage = new Image(imdim); |
---|
| 178 | delete [] imdim; |
---|
| 179 | channelImage->saveParam(par); |
---|
| 180 | channelImage->saveStats(stats); |
---|
[263] | 181 | // channelImage->setMinSize(par.getMinPix()); |
---|
| 182 | channelImage->setMinSize(1); |
---|
[258] | 183 | |
---|
[3] | 184 | for(int z=0; z<zdim; z++){ |
---|
| 185 | |
---|
[187] | 186 | if( par.isVerbose() ) bar.update(z+1); |
---|
[3] | 187 | |
---|
[103] | 188 | if(!par.isInMW(z)){ |
---|
[3] | 189 | |
---|
[53] | 190 | channelImage->extractImage(Array,dim,z); |
---|
[258] | 191 | std::vector<Object2D> objlist = channelImage->lutz_detect(); |
---|
| 192 | num += objlist.size(); |
---|
| 193 | for(int obj=0;obj<objlist.size();obj++){ |
---|
| 194 | Detection newObject; |
---|
| 195 | newObject.pixels().addChannel(z,objlist[obj]); |
---|
| 196 | newObject.setOffsets(par); |
---|
| 197 | mergeIntoList(newObject,outputList,par); |
---|
[3] | 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | } |
---|
| 202 | |
---|
[258] | 203 | delete channelImage; |
---|
| 204 | |
---|
[175] | 205 | if(par.isVerbose()){ |
---|
[219] | 206 | bar.fillSpace("Found "); |
---|
| 207 | std::cout << num << ".\n"; |
---|
[175] | 208 | } |
---|
[3] | 209 | |
---|
| 210 | return outputList; |
---|
| 211 | } |
---|
[263] | 212 | //--------------------------------------------------------------- |
---|
[258] | 213 | |
---|
[275] | 214 | std::vector <Detection> search3DArraySimple(long *dim, float *Array, |
---|
| 215 | Param &par, |
---|
| 216 | StatsContainer<float> &stats) |
---|
[263] | 217 | { |
---|
[528] | 218 | /// @details |
---|
| 219 | /// Takes a dimension array and data array as input (and Parameter |
---|
| 220 | /// set) and searches for detections just in the channel maps -- no |
---|
| 221 | /// 1D searches are done. |
---|
| 222 | /// Returns a vector list of Detections. |
---|
| 223 | /// No reconstruction is assumed to have taken place, so only the base |
---|
| 224 | /// data array is searched. |
---|
| 225 | /// \param dim Array of dimension sizes for the data array. |
---|
| 226 | /// \param Array Array of data. |
---|
| 227 | /// \param par Param set defining how to do detection, and what a |
---|
| 228 | /// BLANK pixel is etc. |
---|
| 229 | /// \param stats The statistics that define what a detection is. |
---|
| 230 | /// \return A std::vector of detected objects. |
---|
[258] | 231 | |
---|
[275] | 232 | std::vector <Detection> outputList; |
---|
[263] | 233 | long zdim = dim[2]; |
---|
| 234 | int num = 0; |
---|
| 235 | |
---|
| 236 | ProgressBar bar; |
---|
[377] | 237 | bool useBar = (zdim>1); |
---|
| 238 | if(useBar && par.isVerbose()) bar.init(zdim); |
---|
[263] | 239 | |
---|
| 240 | num = 0; |
---|
| 241 | |
---|
| 242 | long *imdim = new long[2]; |
---|
| 243 | imdim[0] = dim[0]; imdim[1] = dim[1]; |
---|
| 244 | Image *channelImage = new Image(imdim); |
---|
| 245 | delete [] imdim; |
---|
| 246 | channelImage->saveParam(par); |
---|
| 247 | channelImage->saveStats(stats); |
---|
| 248 | channelImage->setMinSize(1); |
---|
| 249 | |
---|
| 250 | for(int z=0; z<zdim; z++){ |
---|
| 251 | |
---|
[377] | 252 | if( par.isVerbose() && useBar ) bar.update(z+1); |
---|
[263] | 253 | |
---|
| 254 | if(!par.isInMW(z)){ |
---|
| 255 | |
---|
| 256 | channelImage->extractImage(Array,dim,z); |
---|
| 257 | std::vector<Object2D> objlist = channelImage->lutz_detect(); |
---|
| 258 | num += objlist.size(); |
---|
| 259 | for(int obj=0;obj<objlist.size();obj++){ |
---|
| 260 | Detection newObject; |
---|
| 261 | newObject.pixels().addChannel(z,objlist[obj]); |
---|
| 262 | newObject.setOffsets(par); |
---|
| 263 | mergeIntoList(newObject,outputList,par); |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | delete channelImage; |
---|
| 270 | |
---|
| 271 | if(par.isVerbose()){ |
---|
[377] | 272 | if(useBar) bar.remove(); |
---|
| 273 | std::cout << "Found " << num << ".\n"; |
---|
[263] | 274 | } |
---|
| 275 | |
---|
| 276 | return outputList; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | |
---|
[378] | 280 | } |
---|