source: branches/fitshead-branch/ATrous/ReconSearch.cc @ 95

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

Large commit, mostly removing dud commented code, and adding comments and
documentation to the existing code. No new features.

File size: 10.2 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=0, goodSize;
157
158  float blankPixValue = par.getBlankPixVal();
159  bool *isGood = new bool[fullSize];
160  for(int pos=0;pos<fullSize;pos++)
161    isGood[pos] = !par.isBlank(originalArray[pos]);
162 
163  float dud;
164
165  // First search --  in each spectrum.
166  // First, get stats
167  if(zdim > 1){
168    if(par.isVerbose()) std::cout << "1D: |                    |" << std::flush;
169
170    float *specMedian = new float[xySize];
171    float *specSigma = new float[xySize];
172    float *spec = new float[zdim];
173   
174    for(int npix=0; npix<xySize; npix++){
175      goodSize=0;
176      for(int z=0;z<zdim;z++)
177        if(isGood[z*xySize+npix]) spec[goodSize++] = originalArray[z*xySize+npix];
178      if(goodSize>0) specMedian[npix] = findMedian(spec,goodSize);
179      else specMedian[npix] = blankPixValue;
180      goodSize=0;
181      for(int z=0;z<zdim;z++)
182        if(isGood[z*xySize+npix])
183          spec[goodSize++] = originalArray[z*xySize+npix]-reconArray[z*xySize+npix];
184      if(goodSize>0) specSigma[npix] = findStddev(spec,goodSize);
185      else specSigma[npix] = 1.;
186    }
187
188    // Next, do source finding.
189    long *specdim = new long[2];
190    specdim[0] = zdim; specdim[1]=1;
191    Image *spectrum = new Image(specdim);
192    delete [] specdim;
193    spectrum->saveParam(par);
194    spectrum->pars().setBeamSize(2.); // for spectrum, only neighbouring channels correlated
195    for(int npix=0; npix<xySize; npix++){
196
197      if( par.isVerbose() && ((100*(npix+1)/xySize)%5 == 0) ){
198        std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
199        for(int i=0;i<(100*(npix+1)/xySize)/5;i++) std::cout << "#";
200        for(int i=(100*(npix+1)/xySize)/5;i<20;i++) std::cout << " ";
201        std::cout << "|" << std::flush;
202      }
203
204      spectrum->extractSpectrum(reconArray,dim,npix);
205      spectrum->setStats(specMedian[npix],specSigma[npix],par.getCut());
206      if(par.getFlagFDR()) spectrum->setupFDR();
207      spectrum->setMinSize(par.getMinChannels());
208      spectrum->spectrumDetect();
209      for(int obj=0;obj<spectrum->getNumObj();obj++){
210        Detection *object = new Detection;
211        *object = spectrum->getObject(obj);
212        for(int pix=0;pix<object->getSize();pix++) {
213          // Fix up coordinates of each pixel to match original array
214          object->setZ(pix, object->getX(pix));
215          object->setX(pix, npix%dim[0]);
216          object->setY(pix, npix/dim[0]);
217          object->setF(pix, originalArray[object->getX(pix)+object->getY(pix)*dim[0]+object->getZ(pix)*xySize]);
218          // NB: set F to the original value, not the recon value.
219        }
220        object->addOffsets(par);
221        object->calcParams();
222        mergeIntoList(*object,outputList,par);
223        delete object;
224      }
225      spectrum->clearDetectionList();
226    }
227    delete spectrum;
228    delete [] specMedian;
229    delete [] specSigma;
230
231    num = outputList.size();
232    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;
233
234  }
235
236  // Second search --  in each channel
237  if(par.isVerbose()) std::cout << "2D: |                    |" << std::flush;
238
239  float *imageMedian = new float[zdim];
240  float *imageSigma = new float[zdim];
241  float *image = new float[xySize];
242  //  First, get stats
243  for(int z=0; z<zdim; z++){
244    goodSize=0;
245    for(int npix=0; npix<xySize; npix++)
246      if(isGood[z*xySize + npix]) image[goodSize++] = originalArray[z*xySize + npix];
247    if(goodSize>0) imageMedian[z] = findMedian(image,goodSize);
248    else imageMedian[z] = blankPixValue;
249    goodSize=0;
250    for(int npix=0; npix<xySize; npix++)
251      if(isGood[z*xySize+npix])
252        image[goodSize++]=originalArray[z*xySize+npix]-reconArray[z*xySize+npix];
253    if(goodSize>0) imageSigma[z] = findStddev(image,goodSize);
254    else imageSigma[z] = 1.;
255  }
256  delete [] image;
257  // Next, do source finding.
258  long *imdim = new long[2];
259  imdim[0] = dim[0]; imdim[1] = dim[1];
260  Image *channelImage = new Image(imdim);
261  channelImage->saveParam(par);
262  delete [] imdim;
263 
264  for(int z=0; z<zdim; z++){  // loop over all channels
265
266    if( par.isVerbose() && ((100*(z+1)/zdim)%5 == 0) ){
267      std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b|";
268      for(int i=0;i<(100*(z+1)/zdim)/5;i++)  std::cout << "#";
269      for(int i=(100*(z+1)/zdim)/5;i<20;i++) std::cout << " ";
270      std::cout << "|" << std::flush;
271    }
272
273    if( !par.getFlagMW() || (z<par.getMinMW()) || (z>par.getMaxMW()) ){
274      // purpose of this is to ignore the Milky Way channels, but only if we are flagging them
275
276      channelImage->extractImage(reconArray,dim,z);
277      channelImage->setStats(imageMedian[z],imageSigma[z],par.getCut());
278      if(par.getFlagFDR()) channelImage->setupFDR();
279      channelImage->setMinSize(par.getMinPix());
280      channelImage->lutz_detect();
281      for(int obj=0;obj<channelImage->getNumObj();obj++){
282        Detection *object = new Detection;
283        *object = channelImage->getObject(obj);
284        // Fix up z coordinates of each pixel to match original array (x & y are fine)
285        for(int pix=0;pix<object->getSize();pix++){
286          object->setZ(pix, z);
287          object->setF(pix, originalArray[object->getX(pix)+object->getY(pix)*dim[0]+z*xySize]);
288          // NB: set F to the original value, not the recon value.
289        }
290        object->addOffsets(par);
291        object->calcParams();
292        mergeIntoList(*object,outputList,par);
293        delete object;
294      }
295      channelImage->clearDetectionList();
296
297    }
298
299  }
300  delete channelImage;
301  delete [] imageMedian;
302  delete [] imageSigma;
303
304
305  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
306            << ".                                           "  << std::endl << std::flush;
307
308  delete [] isGood;
309
310  return outputList;
311}
Note: See TracBrowser for help on using the repository browser.