source: trunk/src/Cubes/CubicSearch.cc @ 913

Last change on this file since 913 was 913, checked in by MatthewWhiting, 12 years ago

A large swathe of changes aimed at improving warning/error/exception handling. Now make use of macros and streams. Also, there is now a distinction between DUCHAMPERROR and DUCHAMPTHROW.

File size: 7.8 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{
[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
[884]80std::vector <Detection> search3DArray(size_t *dim, float *Array, Param &par,
[275]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{
[913]89    DUCHAMPERROR("search3DArray","Unknown search type : " << par.getSearchType());
[686]90    return std::vector<Detection>(0);
91  }
92}
93//---------------------------------------------------------------
94
95
[884]96std::vector <Detection> search3DArraySpectral(size_t *dim, float *Array, Param &par,
[686]97                                              StatsContainer<float> &stats)
98{
[528]99  /// @details
100  ///  Takes a dimension array and data array as input (and Parameter set)
[686]101  ///  and searches for detections in just the 1D spectra.
[528]102  ///  Returns a vector list of Detections.
103  ///  No reconstruction is assumed to have taken place, so statistics are
104  ///  calculated (using robust methods) from the data array itself.
105  /// \param dim Array of dimension sizes for the data array.
106  /// \param Array Array of data.
107  /// \param par Param set defining how to do detection, and what a
108  /// BLANK pixel is etc.
109  /// \param stats The statistics that define what a detection is.
110  /// \return Vector of detected objects.
[3]111
[275]112  std::vector <Detection> outputList;
[884]113  size_t zdim = dim[2];
114  size_t xySize = dim[0] * dim[1];
[3]115  int num = 0;
116
117  if(zdim>1){
[691]118   
119    ProgressBar bar;
120    if(par.isVerbose()) bar.init(xySize);
[3]121
[258]122    bool *doPixel = new bool[xySize];
[894]123    for(size_t npix=0; npix<xySize; npix++){
[258]124      doPixel[npix] = false;
[894]125      for(size_t z=0;z<zdim;z++){
[258]126        doPixel[npix] = doPixel[npix] ||
127          (!par.isBlank(Array[npix]) && !par.isInMW(z));
128      }
129      // doPixel[i] is false only when there are no good pixels in spectrum
130      //  of pixel #i.
131    }
[86]132
[884]133    size_t *specdim = new size_t[2];
[258]134    specdim[0] = zdim; specdim[1]=1;
135    Image *spectrum = new Image(specdim);
136    delete [] specdim;
137    spectrum->saveParam(par);
138    spectrum->saveStats(stats);
[691]139    //    spectrum->setMinSize(par.getMinChannels());
140    spectrum->setMinSize(1);
[3]141
[894]142    for(size_t y=0; y<dim[1]; y++){
143      for(size_t x=0; x<dim[0]; x++){
[103]144
[894]145        size_t npix = y*dim[0] + x;
[258]146        if( par.isVerbose() ) bar.update(npix+1);
147       
148        if(doPixel[npix]){
149          spectrum->extractSpectrum(Array,dim,npix);
150          spectrum->removeMW(); // only works if flagMW is true
[582]151          std::vector<Scan> objlist = spectrum->findSources1D();
[623]152          std::vector<Scan>::iterator obj;
[258]153          num += objlist.size();
[623]154          for(obj=objlist.begin();obj<objlist.end();obj++){
[258]155            Detection newObject;
[103]156            // Fix up coordinates of each pixel to match original array
[623]157            for(int z=obj->getX();z<=obj->getXmax();z++) {
[570]158              newObject.addPixel(x,y,z);
[258]159            }
160            newObject.setOffsets(par);
[691]161            if(par.getFlagTwoStageMerging()) mergeIntoList(newObject,outputList,par);
162            else outputList.push_back(newObject);
[103]163          }
[3]164        }
165      }
[103]166    }
[3]167
[258]168    delete spectrum;
169    delete [] doPixel;
170 
[691]171
172    if(par.isVerbose()){
173      bar.remove();
174      std::cout << "Found " << num << ".\n";
[175]175    }
[3]176
177  }
178
179  return outputList;
180}
[263]181//---------------------------------------------------------------
[258]182
[884]183std::vector <Detection> search3DArraySpatial(size_t *dim, float *Array,
[686]184                                             Param &par,
185                                             StatsContainer<float> &stats)
[263]186{
[528]187  /// @details
188  ///  Takes a dimension array and data array as input (and Parameter
189  ///  set) and searches for detections just in the channel maps -- no
190  ///  1D searches are done. 
191  ///  Returns a vector list of Detections.
192  ///  No reconstruction is assumed to have taken place, so only the base
193  ///  data array is searched.
194  /// \param dim Array of dimension sizes for the data array.
195  /// \param Array Array of data.
196  /// \param par Param set defining how to do detection, and what a
197  ///              BLANK pixel is etc.
198  /// \param stats The statistics that define what a detection is.
199  /// \return A std::vector of detected objects.
[258]200
[275]201  std::vector <Detection> outputList;
[884]202  size_t zdim = dim[2];
[263]203  int num = 0;
204
205  ProgressBar bar;
[377]206  bool useBar = (zdim>1);
207  if(useBar && par.isVerbose()) bar.init(zdim);
[263]208 
[884]209  size_t *imdim = new size_t[2];
[263]210  imdim[0] = dim[0]; imdim[1] = dim[1];
211  Image *channelImage = new Image(imdim);
212  delete [] imdim;
213  channelImage->saveParam(par);
214  channelImage->saveStats(stats);
215  channelImage->setMinSize(1);
216
[894]217  for(size_t z=0; z<zdim; z++){
[263]218
[377]219    if( par.isVerbose() && useBar ) bar.update(z+1);
[263]220
221    if(!par.isInMW(z)){
222
223      channelImage->extractImage(Array,dim,z);
[582]224      std::vector<Object2D> objlist = channelImage->findSources2D();
[623]225      std::vector<Object2D>::iterator obj;
[263]226      num += objlist.size();
[623]227      for(obj=objlist.begin();obj!=objlist.end();obj++){
[263]228        Detection newObject;
[623]229        newObject.addChannel(z,*obj);
[263]230        newObject.setOffsets(par);
[691]231        if(par.getFlagTwoStageMerging()) mergeIntoList(newObject,outputList,par);
232        else outputList.push_back(newObject);
[263]233      }
234    }
235
236  }
237
238  delete channelImage;
239
240  if(par.isVerbose()){
[377]241    if(useBar) bar.remove();
242    std::cout << "Found " << num << ".\n";
[263]243  }
244
245  return outputList;
246}
247
248
[378]249}
Note: See TracBrowser for help on using the repository browser.