source: tags/release-1.1.2/src/Cubes/CubicSearch.cc @ 1391

Last change on this file since 1391 was 393, checked in by MatthewWhiting, 17 years ago

Fixed up headers for trunk as well.

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