source: tags/release-1.1.5/src/Cubes/CubicSearch.cc @ 1441

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

Fixed up headers for trunk as well.

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  /**
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
57  if(this->par.isVerbose()) std::cout << "  ";
58
59  this->setCubeStats();
60   
61  if(this->par.isVerbose()) std::cout << "  Searching... " << std::flush;
62 
63//   this->objectList = search3DArray(this->axisDim,this->array,
64//                                 this->par,this->Stats);
65  *this->objectList = search3DArraySimple(this->axisDim,this->array,
66                                         this->par,this->Stats);
67
68  if(this->par.isVerbose()) std::cout << "  Updating detection map... "
69                                      << std::flush;
70  this->updateDetectMap();
71  if(this->par.isVerbose()) std::cout << "Done.\n";
72
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
80}
81//---------------------------------------------------------------
82
83
84std::vector <Detection> search3DArray(long *dim, float *Array, Param &par,
85                                      StatsContainer<float> &stats)
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.
93   * \param dim Array of dimension sizes for the data array.
94   * \param Array Array of data.
95   * \param par Param set defining how to do detection, and what a
96   * BLANK pixel is etc.
97   * \param stats The statistics that define what a detection is.
98   * \return Vector of detected objects.
99   */
100
101  std::vector <Detection> outputList;
102  long zdim = dim[2];
103  long xySize = dim[0] * dim[1];
104  int num = 0;
105
106  ProgressBar bar;
107  // FIRST SEARCH --  IN EACH SPECTRUM.
108  if(zdim>1){
109    if(par.isVerbose()) {
110      std::cout << "  1D: ";
111      bar.init(xySize);
112    }
113
114    bool *doPixel = new bool[xySize];
115    for(int npix=0; npix<xySize; npix++){
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    }
124
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
134
135    for(int y=0; y<dim[1]; y++){
136      for(int x=0; x<dim[0]; x++){
137
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;
148            // Fix up coordinates of each pixel to match original array
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);
154          }
155        }
156      }
157    }
158
159    delete spectrum;
160    delete [] doPixel;
161 
162    if(par.isVerbose()) {
163      bar.fillSpace("Found ");
164      std::cout << num <<";" << std::flush;
165    }
166
167  }
168
169  // SECOND SEARCH --  IN EACH CHANNEL
170  if(par.isVerbose()){
171    std::cout << "  2D: ";
172    bar.init(zdim);
173  }
174 
175  num = 0;
176
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);
183  //  channelImage->setMinSize(par.getMinPix());
184  channelImage->setMinSize(1);
185
186  for(int z=0; z<zdim; z++){
187
188    if( par.isVerbose() ) bar.update(z+1);
189
190    if(!par.isInMW(z)){
191
192      channelImage->extractImage(Array,dim,z);
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);
200      }
201    }
202
203  }
204
205  delete channelImage;
206
207  if(par.isVerbose()){
208    bar.fillSpace("Found ");
209    std::cout << num << ".\n";
210  }
211
212  return outputList;
213}
214//---------------------------------------------------------------
215
216std::vector <Detection> search3DArraySimple(long *dim, float *Array,
217                                            Param &par,
218                                            StatsContainer<float> &stats)
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
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.
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.
232   * \return A std::vector of detected objects.
233   */
234
235  std::vector <Detection> outputList;
236  long zdim = dim[2];
237  int num = 0;
238
239  ProgressBar bar;
240  bool useBar = (zdim>1);
241  if(useBar && par.isVerbose()) bar.init(zdim);
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
255    if( par.isVerbose() && useBar ) bar.update(z+1);
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()){
275    if(useBar) bar.remove();
276    std::cout << "Found " << num << ".\n";
277  }
278
279  return outputList;
280}
281
282
283}
Note: See TracBrowser for help on using the repository browser.