source: tags/release-0.9/Cubes/cubicSearch.cc @ 813

Last change on this file since 813 was 13, checked in by Matthew Whiting, 18 years ago

Introduced a new parameter to the Image class : minSize, the minimum size
of a detection for it to be accepted.
Added code in the searching algorithms to distinguish between minPix and
minChannels for the 2D and 1D searches.
Set default value of minPix to 2.

File size: 6.7 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <fstream>
4#include <vector>
5#include <Cubes/cubes.hh>
6#include <Utils/utils.hh>
7
8void Cube::SimpleSearch3D()
9{
10  /**
11   * Cube::SimpleSearch3D()
12   *  A front end to the cubic searching routine that does not
13   *  involve any wavelet reconstruction.
14   *  Although if baseline-removal is required that is done prior to searching.
15   *  Once searching is complete, the detection map is updated and
16   *  the intermediate detections are logged in the log file.
17   */
18
19  this->objectList = cubicSearch(this->axisDim,this->array,this->par);
20   
21  this->updateDetectMap();
22  if(this->par.getFlagLog()) this->logDetectionList();
23
24}
25
26
27vector <Detection> cubicSearch(long *dim, float *Array, Param &par)
28{
29  /**
30   * cubicSearch
31   *  Takes a dimension array and data array as input (and Parameter set)
32   *  and searches for detections in a combination of 1D and 2D searches.
33   *  Returns a vector list of Detections.
34   *  No reconstruction is assumed to have taken place, so statistics are
35   *  calculated (using robust methods) from the data array itself.
36   */
37
38  vector <Detection> outputList;
39  int zdim = dim[2];
40  int xySize = dim[0] * dim[1];
41  int fullSize = zdim * xySize;
42  int num = 0;
43
44  float blankPixValue = par.getBlankPixVal();
45  bool *isGood = new bool[fullSize];
46  for(int pos=0;pos<fullSize;pos++)
47    isGood[pos] = !par.isBlank(Array[pos]);
48 
49  float dud;
50
51  // FIRST SEARCH --  IN EACH SPECTRUM.
52  // FIRST, GET STATS
53  if(zdim>1){
54    if(par.isVerbose()) std::cout << "  1D: |                    |" << std::flush;
55//     if(par.isVerbose()) std::cout << "Done  0%" << "\b\b\b\b\b\b\b\b" << std::flush;
56    float *specMedian = new float[xySize];
57    float *specSigma = new float[xySize];
58
59    for(int npix=0; npix<xySize; npix++){
60      float *spec = new float[zdim];
61      int goodSize=0;
62      for(int z=0;z<zdim;z++) if(isGood[z*xySize+npix]) spec[goodSize++] = Array[z*xySize+npix];
63      if(goodSize>0) findMedianStats(spec,goodSize,specMedian[npix],dud);
64      else specMedian[npix] = blankPixValue;
65      //     if(goodSize>0) findNormalStats(spec,goodSize,dud,specSigma[npix]);
66      if(goodSize>0){
67        findMedianStats(spec,goodSize,dud,specSigma[npix]);
68        specSigma[npix] /= correctionFactor;
69      }
70      else specSigma[npix] = 1.;
71      delete spec;
72    }
73    // NEXT, DO SOURCE FINDING
74    int numSearches = xySize + zdim;
75    for(int npix=0; npix<xySize; npix++){
76
77      if( par.isVerbose() && ((100*(npix+1)/xySize)%5 == 0) ){
78        std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
79        for(int i=0;i<(100*(npix+1)/xySize)/5;i++) std::cout << "#";
80        for(int i=(100*(npix+1)/xySize)/5;i<20;i++) std::cout << " ";
81        std::cout << "|" << std::flush;
82      }
83
84      float *spec = new float[zdim];
85      for(int z=0;z<zdim;z++) spec[z] = Array[z*xySize + npix];
86      long *specdim = new long[2];
87      specdim[0] = zdim; specdim[1]=1;
88      Image *spectrum = new Image(specdim);
89      spectrum->saveParam(par);
90      spectrum->pars().setBeamSize(2.); // for spectrum, only neighbouring channels correlated
91      spectrum->saveArray(spec,zdim);
92      spectrum->setStats(specMedian[npix],specSigma[npix],par.getCut());
93      if(par.getFlagFDR()) spectrum->setupFDR();
94      spectrum->setMinSize(par.getMinChannels());
95      spectrum->lutz_detect();
96      for(int obj=0;obj<spectrum->getNumObj();obj++){
97        Detection *object = new Detection;
98        *object = spectrum->getObject(obj);
99//      if(par.getFlagGrowth()) growObject(*object,*spectrum);
100        for(int pix=0;pix<object->getSize();pix++) {
101          // Fix up coordinates of each pixel to match original array
102          object->setZ(pix, object->getX(pix));
103          object->setX(pix, npix%dim[0]);
104          object->setY(pix, npix/dim[0]);
105        }
106        object->addOffsets(par);
107        object->calcParams();
108//      outputList.push_back(*object);
109        mergeIntoList(*object,outputList,par);
110        delete object;
111      }
112      delete spectrum;
113      delete spec;
114      delete specdim;
115    }
116
117    delete [] specMedian;
118    delete [] specSigma;
119
120    num = outputList.size();
121    if(par.isVerbose())
122      std::cout <<"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bFound " << num <<";" << std::flush;
123
124  }
125
126  // SECOND SEARCH --  IN EACH CHANNEL
127  // FIRST, GET STATS
128  if(par.isVerbose()) std::cout << "  2D: |                    |" << std::flush;
129//   if(par.isVerbose()) std::cout << "Done  0%" << "\b\b\b\b\b\b\b\b" << std::flush;
130  float *imageMedian = new float[zdim];
131  float *imageSigma = new float[zdim];
132  for(int z=0; z<zdim; z++){
133    float *image = new float[xySize];
134    int goodSize=0;
135    for(int npix=0; npix<xySize; npix++)
136      if(isGood[z*xySize + npix]) image[goodSize++] = Array[z*xySize + npix];
137    if(goodSize>0) findMedianStats(image,goodSize,imageMedian[z],dud);
138    else imageMedian[z] = blankPixValue;
139    if(goodSize>0) findNormalStats(image,goodSize,dud,imageSigma[z]);
140    else imageSigma[z] = 1.;
141    delete image;
142  }
143  // NEXT, DO SOURCE FINDING
144  bool *doChannel = new bool[zdim];
145  for(int z=0;z<zdim;z++)
146    doChannel[z] = !( par.getFlagMW() && (z>=par.getMinMW()) && (z<=par.getMaxMW()) );
147
148  for(int z=0; z<zdim; z++){
149
150    if( par.isVerbose() && ((100*(z+1)/zdim)%5 == 0) ){
151      std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
152      for(int i=0;i<(100*(z+1)/zdim)/5;i++) std::cout << "#";
153      for(int i=(100*(z+1)/zdim)/5;i<20;i++) std::cout << " ";
154      std::cout << "|" << std::flush;
155    }
156
157    if( doChannel[z] ){
158
159      float *image = new float[xySize];
160      for(int npix=0; npix<xySize; npix++) image[npix] = Array[z*xySize + npix];
161      long *imdim = new long[2];
162      imdim[0] = dim[0]; imdim[1] = dim[1];
163      Image *channelImage = new Image(imdim);
164      channelImage->saveParam(par);
165      channelImage->saveArray(image,xySize);
166      channelImage->setStats(imageMedian[z],imageSigma[z],par.getCut());
167      if(par.getFlagFDR()) channelImage->setupFDR();
168      channelImage->setMinSize(par.getMinPix());
169      channelImage->lutz_detect();
170      for(int obj=0;obj<channelImage->getNumObj();obj++){
171        Detection *object = new Detection;
172        *object = channelImage->getObject(obj);
173//      if(par.getFlagGrowth()) growObject(*object,*channelImage);
174        // Fix up coordinates of each pixel to match original array
175        for(int pix=0;pix<object->getSize();pix++) object->setZ(pix, z);
176        object->addOffsets(par);
177        object->calcParams();
178//      outputList.push_back(*object);
179        mergeIntoList(*object,outputList,par);
180        delete object;
181      }
182      delete image;
183      delete channelImage;
184      delete imdim;
185    }
186
187  }
188
189  if(par.isVerbose())
190    std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bFound " << outputList.size() - num
191              << ".                                           " << std::endl << std::flush;
192
193  delete [] imageMedian;
194  delete [] imageSigma;
195  delete [] isGood;
196  delete [] doChannel;
197
198  return outputList;
199}
Note: See TracBrowser for help on using the repository browser.