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

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

This is the first full import of all working code to
the Duchamp repository.
Made three directories at top level:

branches/ tags/ trunk/

and trunk/ has the full set of code:
ATrous/ Cubes/ Detection/ InputComplete? InputExample? README Utils/ docs/ mainDuchamp.cc param.cc param.hh

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