source: trunk/ATrous/ReconSearch.cc @ 78

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

Changed the name of findSigma to findStddev

File size: 12.1 KB
Line 
1#include <fstream>
2#include <iostream>
3#include <iomanip>
4#include <vector>
5#include <Cubes/cubes.hh>
6#include <ATrous/atrous.hh>
7#include <Utils/utils.hh>
8
9using std::setw;
10
11//////////////////////////////////////////////////////////////////////////////////////
12/**
13 * Cube::ReconSearch1D()
14 *   This reconstructs a cube by performing a 1D a trous reconstruction
15 *   in the spectrum of each spatial pixel.
16 *   It then searches the cube using reconSearch (below).
17 *
18 *   The resulting object list is stored in the Cube.
19 */
20void Cube::ReconSearch1D()
21{
22  long xySize = axisDim[0] * axisDim[1];
23  long zdim = axisDim[2];
24
25  // Reconstruct the cube by 1d atrous transform in each spatial pixel
26  std::cout<<"Reconstructing... ";
27  for(int npix=0; npix<xySize; npix++){
28    if((100*npix/xySize)%5==0)
29      std::cout<<setw(3)<<100*npix/xySize<<"% done"<<"\b\b\b\b\b\b\b\b\b"<<std::flush;
30    float *spec = new float[zdim];
31    float *newSpec = new float[zdim];
32    for(int z=0;z<zdim;z++){
33      int cubepos = z*xySize + npix;
34      spec[z] = this->array[cubepos];
35    }
36    atrous1DReconstruct(axisDim[2],spec,newSpec,this->par);
37    for(int z=0;z<zdim;z++){
38      int cubepos = z*xySize + npix;
39      this->recon[cubepos] = newSpec[z];
40    }
41    delete spec;
42    delete newSpec;
43  }
44  this->reconExists = true;
45  std::cout<<"All Done. Searching... ";
46   
47  this->objectList = reconSearch(this->axisDim,this->array,this->recon,this->par);
48  this->updateDetectMap();
49  if(this->par.getFlagLog()) this->logDetectionList();
50
51}
52
53//////////////////////////////////////////////////////////////////////////////////////
54/**
55 * Cube::ReconSearch2D()
56 *   This reconstructs a cube by performing a 2D a trous reconstruction
57 *   in each spatial image of the cube.
58 *   It then searches the cube using reconSearch (below).
59 *
60 *   The resulting object list is stored in the Cube.
61 */
62void Cube::ReconSearch2D()
63{
64  long xySize = axisDim[0] * axisDim[1];
65
66  // RECONSTRUCT THE CUBE BY 2D ATROUS TRANSFORM IN EACH CHANNEL 
67  bool *doChannel = new bool[axisDim[2]];
68  for(int z=0;z<axisDim[2];z++)
69    doChannel[z] = !( this->par.getFlagMW() && (z>=this->par.getMinMW()) && (z<=this->par.getMaxMW()) );
70  std::cout<<"Reconstructing... ";
71  for(int z=0;z<axisDim[2];z++){
72    if((100*z/axisDim[2])%5==0)
73      std::cout<<setw(3)<<100*z/axisDim[2]<<"% done"<<"\b\b\b\b\b\b\b\b\b"<<std::flush;
74    if(doChannel[z]){
75      float *im = new float[xySize];
76      float *newIm = new float[xySize];
77      for(int npix=0; npix<xySize; npix++){
78        int cubepos = z*xySize + npix;
79        im[npix] = this->array[cubepos];
80      }
81      atrous2DReconstruct(axisDim[0],axisDim[1],im,newIm,this->par);
82      for(int npix=0; npix<xySize; npix++){
83        int cubepos = z*xySize + npix;
84        this->recon[cubepos] = newIm[npix];
85      }
86      delete im;
87      delete newIm;
88    }
89    else
90      for(int i=0; i<xySize; i++) this->recon[z*xySize+i] = this->array[z*xySize+i];
91  }
92  this->reconExists = true;
93  std::cout<<"All Done.                      \nSearching... ";
94
95  this->objectList = reconSearch(this->axisDim,this->array,this->recon,this->par);
96 
97  this->updateDetectMap();
98  if(this->par.getFlagLog()) this->logDetectionList();
99
100  delete [] doChannel;
101
102}
103
104//////////////////////////////////////////////////////////////////////////////////////
105/**
106 * Cube::ReconSearch3D()
107 *   This performs a full 3D a trous reconstruction of the cube
108 *   It then searches the cube using reconSearch (below).
109 *
110 *   The resulting object list is stored in the Cube.
111 */
112void Cube::ReconSearch3D()
113{
114  if(this->axisDim[2]==1) this->ReconSearch2D();
115  else {
116
117    if(!this->reconExists){
118      std::cout<<"  Reconstructing... "<<std::flush;
119      atrous3DReconstruct(this->axisDim[0],this->axisDim[1],this->axisDim[2],
120                          this->array,this->recon,this->par);
121      this->reconExists = true;
122      std::cout<<"All Done.                      \n  Searching... "<<std::flush;
123    }
124
125    this->objectList = reconSearch(this->axisDim,this->array,this->recon,this->par);
126
127    this->updateDetectMap();
128    if(this->par.getFlagLog()) this->logDetectionList();
129
130  }
131
132}
133
134
135//////////////////////////////////////////////////////////////////////////////////////
136/**
137 * reconSearch(long *dim, float *originalArray, float *reconArray, Param &par)
138 *   This searches for objects in a cube that has been reconstructed.
139 *
140 *   Inputs:   - dimension array
141 *             - original, un-reconstructed image array
142 *             - reconstructed image array
143 *             - parameters
144 *
145 *   Searches first in each spatial pixel (1D search),
146 *   then in each channel image (2D search).
147 *
148 *   Returns: vector of Detections resulting from the search.
149 */
150vector <Detection> reconSearch(long *dim, float *originalArray, float *reconArray, Param &par)
151{
152  vector <Detection> outputList;
153  int zdim = dim[2];
154  int xySize = dim[0] * dim[1];
155  int fullSize = zdim * xySize;
156  int num, goodSize;
157
158  //  bool flagBlank=par.getFlagBlankPix();
159  float blankPixValue = par.getBlankPixVal();
160  bool *isGood = new bool[fullSize];
161  for(int pos=0;pos<fullSize;pos++)
162    isGood[pos] = !par.isBlank(originalArray[pos]);
163    //    isGood[pos] = (!flagBlank) || (originalArray[pos]!=blankPixValue);
164 
165  float dud;
166
167  // First search --  in each spectrum.
168  // First, get stats
169  if(zdim > 1){
170    if(par.isVerbose()) std::cout << "1D: |                    |" << std::flush;
171
172    float *specMedian = new float[xySize];
173    float *specSigma = new float[xySize];
174    float *spec = new float[zdim];
175   
176    for(int npix=0; npix<xySize; npix++){
177      goodSize=0;
178      for(int z=0;z<zdim;z++)
179        if(isGood[z*xySize+npix]) spec[goodSize++] = originalArray[z*xySize+npix];
180//       if(goodSize>0) findMedianStats(spec,goodSize,specMedian[npix],dud);
181      if(goodSize>0) specMedian[npix] = findMedian(spec,goodSize);
182      else specMedian[npix] = blankPixValue;
183      goodSize=0;
184      for(int z=0;z<zdim;z++)
185        if(isGood[z*xySize+npix])
186          spec[goodSize++] = originalArray[z*xySize+npix]-reconArray[z*xySize+npix];
187//       if(goodSize>0) findNormalStats(spec,goodSize,dud,specSigma[npix]);
188      if(goodSize>0) specSigma[npix] = findStddev(spec,goodSize);
189      else specSigma[npix] = 1.;
190    }
191
192    // Next, do source finding.
193    long *specdim = new long[2];
194    specdim[0] = zdim; specdim[1]=1;
195    Image *spectrum = new Image(specdim);
196    spectrum->saveParam(par);
197    spectrum->pars().setBeamSize(2.); // for spectrum, only neighbouring channels correlated
198    for(int npix=0; npix<xySize; npix++){
199
200//       if(par.isVerbose() && ((1000*npix/xySize)%10==0) )
201//      std::cout << "Done " << setw(2) << 100*npix/xySize << "%\b\b\b\b\b\b\b\b" << std::flush;
202      if( par.isVerbose() && ((100*(npix+1)/xySize)%5 == 0) ){
203        std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
204        for(int i=0;i<(100*(npix+1)/xySize)/5;i++) std::cout << "#";
205        for(int i=(100*(npix+1)/xySize)/5;i<20;i++) std::cout << " ";
206        std::cout << "|" << std::flush;
207      }
208
209//       for(int z=0;z<zdim;z++) spec[z] = reconArray[z*xySize + npix];
210//       spectrum->saveArray(spec,zdim);
211      spectrum->extractSpectrum(reconArray,dim,npix);
212      spectrum->setStats(specMedian[npix],specSigma[npix],par.getCut());
213//       spectrum->findStats(10);
214//       float *resid = new float[zdim];
215//       goodSize=0;
216//       for(int z=0;z<zdim;z++)
217//      if(isGood[z*xySize+npix])
218//        resid[goodSize] = originalArray[z*xySize+npix]-reconArray[z*xySize+npix];
219//       spectrum->setSigma(findMADFM(resid,goodSize)/correctionFactor);
220//       delete [] resid;
221      if(par.getFlagFDR()) spectrum->setupFDR();
222      spectrum->setMinSize(par.getMinChannels());
223//       spectrum->lutz_detect();
224      spectrum->spectrumDetect();
225      for(int obj=0;obj<spectrum->getNumObj();obj++){
226        Detection *object = new Detection;
227        *object = spectrum->getObject(obj);
228//      if(par.getFlagGrowth()) growObject(*object,*spectrum);
229        for(int pix=0;pix<object->getSize();pix++) {
230          // Fix up coordinates of each pixel to match original array
231          object->setZ(pix, object->getX(pix));
232          object->setX(pix, npix%dim[0]);
233          object->setY(pix, npix/dim[0]);
234          object->setF(pix, originalArray[object->getX(pix)+object->getY(pix)*dim[0]+object->getZ(pix)*xySize]);
235          // NB: set F to the original value, not the recon value.
236        }
237        object->addOffsets(par);
238        object->calcParams();
239//      outputList.push_back(*object);
240        mergeIntoList(*object,outputList,par);
241        delete object;
242      }
243      spectrum->clearDetectionList();
244    }
245//     delete [] spec;
246    delete [] specdim;
247    delete spectrum;
248    delete [] specMedian;
249    delete [] specSigma;
250
251    num = outputList.size();
252    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;
253
254  } 
255
256  // Second search --  in each channel
257  if(par.isVerbose()) std::cout << "2D: |                    |" << std::flush;
258
259  float *imageMedian = new float[zdim];
260  float *imageSigma = new float[zdim];
261  float *image = new float[xySize];
262  //  First, get stats
263  for(int z=0; z<zdim; z++){
264    goodSize=0;
265    for(int npix=0; npix<xySize; npix++)
266      if(isGood[z*xySize + npix]) image[goodSize++] = originalArray[z*xySize + npix];
267//     if(goodSize>0) findMedianStats(image,goodSize,imageMedian[z],dud);
268    if(goodSize>0) imageMedian[z] = findMedian(image,goodSize);
269    else imageMedian[z] = blankPixValue;
270    goodSize=0;
271    for(int npix=0; npix<xySize; npix++)
272      if(isGood[z*xySize+npix])
273        image[goodSize++]=originalArray[z*xySize+npix]-reconArray[z*xySize+npix];
274//     if(goodSize>0) findNormalStats(image,goodSize,dud,imageSigma[z]);
275    if(goodSize>0) imageSigma[z] = findStddev(image,goodSize);
276    else imageSigma[z] = 1.;
277  }
278  // Next, do source finding.
279  long *imdim = new long[2];
280  imdim[0] = dim[0]; imdim[1] = dim[1];
281  Image *channelImage = new Image(imdim);
282  channelImage->saveParam(par);
283 
284  bool *doChannel = new bool[zdim];
285  // purpose of this is to ignore the Milky Way channels -- if we are flagging them...
286  for(int z=0;z<zdim;z++)
287    doChannel[z] = !( par.getFlagMW() && (z>=par.getMinMW()) && (z<=par.getMaxMW()) );
288
289  for(int z=0; z<zdim; z++){
290
291    if( par.isVerbose() && ((100*(z+1)/zdim)%5 == 0) ){
292      std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
293      for(int i=0;i<(100*(z+1)/zdim)/5;i++) std::cout << "#";
294      for(int i=(100*(z+1)/zdim)/5;i<20;i++) std::cout << " ";
295      std::cout << "|" << std::flush;
296    }
297//     if(par.isVerbose() && ((1000*z/zdim)%10==0) )
298//       std::cout << "Done " << setw(2) << 100*z/zdim << "%\b\b\b\b\b\b\b\b" << std::flush;
299
300    if( doChannel[z] ){
301//       for(int npix=0; npix<xySize; npix++) image[npix] = reconArray[z*xySize + npix];
302//       channelImage->saveArray(image,xySize);
303      channelImage->extractImage(reconArray,dim,z);
304      channelImage->setStats(imageMedian[z],imageSigma[z],par.getCut());
305//       channelImage->findStats(10);
306//       float *resid = new float[xySize];
307//       goodSize=0;
308//       for(int npix=0; npix<xySize; npix++)
309//      if(isGood[z*xySize+npix])
310//        resid[goodSize] = originalArray[z*xySize+npix]-reconArray[z*xySize+npix];
311//       channelImage->setSigma(findMADFM(resid,goodSize)/correctionFactor);
312//       delete [] resid;
313      if(par.getFlagFDR()) channelImage->setupFDR();
314      channelImage->setMinSize(par.getMinPix());
315      channelImage->lutz_detect();
316      for(int obj=0;obj<channelImage->getNumObj();obj++){
317        Detection *object = new Detection;
318        *object = channelImage->getObject(obj);
319//      if(par.getFlagGrowth()) growObject(*object,*channelImage);
320        // Fix up z coordinates of each pixel to match original array (x & y are fine)
321        for(int pix=0;pix<object->getSize();pix++){
322          object->setZ(pix, z);
323          object->setF(pix, originalArray[object->getX(pix)+object->getY(pix)*dim[0]+z*xySize]);
324          // NB: set F to the original value, not the recon value.
325        }
326        object->addOffsets(par);
327        object->calcParams();
328//      outputList.push_back(*object);
329        mergeIntoList(*object,outputList,par);
330        delete object;
331      }
332      channelImage->clearDetectionList();
333    }
334
335  }
336
337  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
338            << ".                                           "  << std::endl << std::flush;
339
340  delete [] image;
341  delete [] imdim;
342  delete channelImage;
343  delete [] doChannel;
344  delete [] imageMedian;
345  delete [] imageSigma;
346
347  delete [] isGood;
348
349  return outputList;
350}
Note: See TracBrowser for help on using the repository browser.