source: tags/release-1.1.7/src/Cubes/CubicSearch.cc @ 1455

Last change on this file since 1455 was 536, checked in by MatthewWhiting, 15 years ago

Including the recent minor changes to 1.1.7.

File size: 8.2 KB
Line 
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// -----------------------------------------------------------------------
28#include <iostream>
29#include <iomanip>
30#include <fstream>
31#include <vector>
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>
38
39using std::vector;
40using namespace PixelInfo;
41using namespace Statistics;
42
43namespace duchamp
44{
45
46void Cube::CubicSearch()
47{
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.
55
56  if(this->par.isVerbose()) std::cout << "  ";
57
58  this->setCubeStats();
59   
60  if(this->par.isVerbose()) std::cout << "  Searching... " << std::flush;
61 
62//   this->objectList = search3DArray(this->axisDim,this->array,
63//                                 this->par,this->Stats);
64  *this->objectList = search3DArraySimple(this->axisDim,this->array,
65                                         this->par,this->Stats);
66
67  if(this->par.isVerbose()) std::cout << "  Updating detection map... "
68                                      << std::flush;
69  this->updateDetectMap();
70  if(this->par.isVerbose()) std::cout << "Done.\n";
71
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
79}
80//---------------------------------------------------------------
81
82
83std::vector <Detection> search3DArray(long *dim, float *Array, Param &par,
84                                      StatsContainer<float> &stats)
85{
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.
98
99  std::vector <Detection> outputList;
100  long zdim = dim[2];
101  long xySize = dim[0] * dim[1];
102  int num = 0;
103
104  ProgressBar bar;
105  // FIRST SEARCH --  IN EACH SPECTRUM.
106  if(zdim>1){
107    if(par.isVerbose()) {
108      std::cout << "  1D: ";
109      bar.init(xySize);
110    }
111
112    bool *doPixel = new bool[xySize];
113    for(int npix=0; npix<xySize; npix++){
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    }
122
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
132
133    for(int y=0; y<dim[1]; y++){
134      for(int x=0; x<dim[0]; x++){
135
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(uint obj=0;obj<objlist.size();obj++){
145            Detection newObject;
146            // Fix up coordinates of each pixel to match original array
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);
152          }
153        }
154      }
155    }
156
157    delete spectrum;
158    delete [] doPixel;
159 
160    if(par.isVerbose()) {
161      bar.fillSpace("Found ");
162      std::cout << num <<";" << std::flush;
163    }
164
165  }
166
167  // SECOND SEARCH --  IN EACH CHANNEL
168  if(par.isVerbose()){
169    std::cout << "  2D: ";
170    bar.init(zdim);
171  }
172 
173  num = 0;
174
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);
181  //  channelImage->setMinSize(par.getMinPix());
182  channelImage->setMinSize(1);
183
184  for(int z=0; z<zdim; z++){
185
186    if( par.isVerbose() ) bar.update(z+1);
187
188    if(!par.isInMW(z)){
189
190      channelImage->extractImage(Array,dim,z);
191      std::vector<Object2D> objlist = channelImage->lutz_detect();
192      num += objlist.size();
193      for(uint 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);
198      }
199    }
200
201  }
202
203  delete channelImage;
204
205  if(par.isVerbose()){
206    bar.fillSpace("Found ");
207    std::cout << num << ".\n";
208  }
209
210  return outputList;
211}
212//---------------------------------------------------------------
213
214std::vector <Detection> search3DArraySimple(long *dim, float *Array,
215                                            Param &par,
216                                            StatsContainer<float> &stats)
217{
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.
231
232  std::vector <Detection> outputList;
233  long zdim = dim[2];
234  int num = 0;
235
236  ProgressBar bar;
237  bool useBar = (zdim>1);
238  if(useBar && par.isVerbose()) bar.init(zdim);
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
252    if( par.isVerbose() && useBar ) bar.update(z+1);
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(uint 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()){
272    if(useBar) bar.remove();
273    std::cout << "Found " << num << ".\n";
274  }
275
276  return outputList;
277}
278
279
280}
Note: See TracBrowser for help on using the repository browser.