source: trunk/src/Cubes/cubes.hh @ 191

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

Large commit. The addition of a new Statistics namespace & class, plus a number of changes to the code to make the cube-wide statistics calculation work. The FDR calculations are incorporated into the new class, and a number of functions have been made into templates to ease the calculations. Details follow:

  • New namespace and class (StatsContainer? -- templated) in Statistics.hh, that holds mean,median,stddev & madfm, and provide accessor and calculator functions for these. It also holds the threshold values for sigma-clipping and FDR methods, as well as the PValue evaluation functions
  • The correctionFactor incorporated into the namespace, and given a conversion function that other functions can call (eg. the atrous_Xd_reconstruct functions).
  • Templated the statistics functions in getStats.cc.
  • Templated the sort functions, and made swap an inline one defined in utils.hh.
  • A number of changes to cubes.cc and cubes.hh:
    • DataArray? gains a StatsContainer? object, to store stats info.
    • Image has lost its pValue array (not needed as we calculate on the fly) and mask array (not used).
    • Image has also lost all its stats members, but keeps minPix.
    • Functions to go are Image::maskObject, Image::findStats. Removed calls to the former. Latter never used.
    • Cube::setCubeStats does the cube-wide stats calculations, including setupFDR (now a Cube function).
    • Cube loses the specMean etc arrays.
  • The Search functions (ReconSearch? and CubicSearch?) changed to accommodate the exchange of StatsContainer? objects. This changed the prototypes as well.
  • The growObject function incorporates the new StatsContainer? object.
  • Minor change to Merger, in the preparation for calling growObject.
  • A new par introduced: flagUserThreshold -- set to true when the user enters a value for the threshold.
  • Removed thresholding_functions.cc and incorporated its functions into cubes.cc and cubes.hh.
File size: 13.1 KB
Line 
1#ifndef CUBES_H
2#define CUBES_H
3
4#include <iostream>
5#include <string>
6#include <vector>
7
8#ifndef PARAM_H
9#include <param.hh>
10#endif
11#ifndef DETECTION_H
12#include <Detection/detection.hh>
13#endif
14#ifndef COLUMNS_H
15#include <Detection/columns.hh>
16#endif
17#ifndef PLOTS_H
18#include <Cubes/plots.hh>
19#endif
20#ifndef STATS_H
21#include <Utils/Statistics.hh>
22#endif
23
24using std::string;
25using std::vector;
26using namespace Column;
27using namespace Statistics;
28
29/****************************************************************/
30/////////////////////////////////////////////////////////////
31//// Definition of an n-dimensional data array:
32////     array of pixel values, size & dimensions
33////     array of Detection objects
34/////////////////////////////////////////////////////////////
35
36
37class DataArray
38{
39public:
40  DataArray(){numDim=0; numPixels=0;};
41  DataArray(short int nDim){numDim=nDim; numPixels=0;};
42  DataArray(short int nDim, long size);
43  DataArray(short int nDim, long *dimensions);
44  virtual ~DataArray();
45  // Size and Dimension related
46  long                getDimX(){if(numDim>0) return axisDim[0];else return 0;};
47  long                getDimY(){if(numDim>1) return axisDim[1];else return 1;};
48  long                getDimZ(){if(numDim>2) return axisDim[2];else return 1;};
49  void                getDim(long &x, long &y, long &z);
50  long                getSize(){return numPixels;};
51  short int           getNumDim(){return numDim;};
52  // Related to the various arrays
53  void                   getDimArray(long *output);
54  void                   getArray(float *output);
55  virtual void           saveArray(float *input, long size);
56  float                  getPixValue(long pos){return array[pos];};
57  void                   setPixValue(long pos, float f){array[pos] = f;};
58  // Related to the object lists
59  Detection              getObject(long number){return objectList[number];};
60  void                   addObject(Detection object);   
61                  // adds a single detection to the object list
62  vector <Detection>     getObjectList(){return objectList;};
63  void                   addObjectList(vector <Detection> newlist); 
64                  // adds all objects in a detection list to the object list
65  void                   addObjectOffsets();
66  long                   getNumObj(){return objectList.size();};
67  void                   clearDetectionList(){this->objectList.clear();};
68  // Parameter list related.
69  int                    readParam(string paramfile){
70    return par.readParams(paramfile);};
71  void                   showParam(std::ostream &stream){stream << par;};
72  Param                  getParam(){return par;};
73  void                   saveParam(Param newpar){par = newpar;};
74  Param&                 pars(){Param &rpar = par; return rpar;};
75  bool                   isBlank(int vox){return par.isBlank(array[vox]);};
76  // Statistics
77  StatsContainer<float> getStats(){return Stats;};
78  StatsContainer<float>& stats(){
79    StatsContainer<float> &rstats = Stats;
80    return rstats;
81  };
82  void saveStats(StatsContainer<float> newStats){
83    Stats = newStats;
84  };
85
86  bool isDetection(float value){
87    if(par.isBlank(value)) return false;
88    else return Stats.isDetection(value);
89  }; 
90// bool isDetection(long voxel)
91// {
92//   if(isBlank(voxel)) return false;
93//   else return Stats.isDetection(this->array[voxel]);
94// }
95
96  friend std::ostream& operator<< ( std::ostream& theStream, DataArray &array);
97
98protected:
99  short int               numDim;     // number of dimensions.
100  long                   *axisDim;    // array of dimensions of cube
101                                      //     (ie. how large in each direction).
102  long                    numPixels;  // total number of pixels in cube
103  float                  *array;      // array of data
104  vector <Detection>      objectList; // the list of detected objects
105  Param                   par;        // a parameter list.
106  StatsContainer<float>   Stats;      // The statistics for the dataArray
107};
108
109
110/****************************************************************/
111/////////////////////////////////////////////////////////////
112//// Definition of an image object (2D):
113////     a DataArray object
114////     arrays for: probability values (for FDR)
115////                 mask image to indicate location of objects
116////                 detected objects
117////     statistics information
118/////////////////////////////////////////////////////////////
119
120class Cube;
121class Image : public DataArray
122{
123public:
124  Image(){
125    numPixels=0;
126    numDim=2;};
127  Image(long nPix);
128  Image(long *dimensions);
129  virtual ~Image();
130
131  // Defining the array
132  void      saveArray(float *input, long size);
133  void      extractSpectrum(float *Array, long *dim, long pixel);
134  void      extractImage(float *Array, long *dim, long channel);
135  void      extractSpectrum(Cube &cube, long pixel);
136  void      extractImage(Cube &cube, long channel);
137  // Accessing the data.
138  float     getPixValue(long x, long y){return array[y*axisDim[0] + x];};
139  float     getPixValue(long pos){return array[pos];};
140  // the next few should have checks against array overflow...
141  void      setPixValue(long x, long y, float f){array[y*axisDim[0] + x] = f;};
142  void      setPixValue(long pos, float f){array[pos] = f;};
143  bool      isBlank(int vox){return par.isBlank(array[vox]);};
144  bool      isBlank(long x,long y){return par.isBlank(array[y*axisDim[0]+x]);};
145
146  // Detection-related
147  void      lutz_detect();            // in Detection/lutz_detect.cc
148  void      spectrumDetect();         // in Detection/spectrumDetect.cc
149  int       getMinSize(){return minSize;};
150  void      setMinSize(int i){minSize=i;};
151  // the rest are in Detection/thresholding_functions.cc
152  bool      isDetection(long x, long y){
153    long voxel = y*axisDim[0] + x;
154    return DataArray::isDetection(array[voxel]);
155  };
156
157  void      removeMW();
158 
159private:
160  int        minSize;    // the minimum number of pixels for a detection
161                         //  to be accepted.
162};
163
164/****************************************************************/
165/////////////////////////////////////////////////////////////
166//// Definition of an data-cube object (3D):
167////     a DataArray object limited to dim=3
168/////////////////////////////////////////////////////////////
169
170class Cube : public DataArray
171{
172public:
173  Cube(){numPixels=0; numDim=3; reconExists = false;};
174  Cube(long nPix);
175  Cube(long *dimensions);
176  virtual ~Cube();
177
178  // additional accessor functions
179  //       -- in Cubes/cubes.cc unless otherwise specified.
180
181  int     getCube(string fname);
182  int     getCube(){ 
183    // a front-end to the getCube function, that does subsection checks
184    // assumes the Param is setup properly
185    string fname = par.getImageFile();
186    if(par.getFlagSubsection()) fname+=par.getSubsection();
187    return getCube(fname);
188  };
189  int     getFITSdata(string fname);         // in FitsIO/dataIO.cc
190  void    initialiseCube(long *dimensions);  // in cubes.cc
191  int     getopts(int argc, char ** argv);   // in cubes.cc
192  void    saveReconstructedCube();
193  int     readReconCube();
194
195  bool    isBlank(int vox){return par.isBlank(array[vox]);};
196  bool    isBlank(long x, long y, long z){
197    return par.isBlank(array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x]);};
198  float   getPixValue(long pos){return array[pos];};
199  float   getPixValue(long x, long y, long z){
200    return array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
201  short   getDetectMapValue(long pos){return detectMap[pos];};
202  short   getDetectMapValue(long x, long y){return detectMap[y*axisDim[0]+x];};
203  bool    isRecon(){return reconExists;};
204  float   getReconValue(long pos){return recon[pos];};
205  float   getReconValue(long x, long y, long z){
206    return recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
207  float   getBaselineValue(long pos){return baseline[pos];};
208  float   getBaselineValue(long x, long y, long z){
209    return baseline[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x];};
210  // these should have checks against array overflow...
211  void    setPixValue(long pos, float f){array[pos] = f;};
212  void    setPixValue(long x, long y, long z, float f){
213    array[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;};
214  void    setDetectMapValue(long pos, short f){detectMap[pos] = f;};
215  void    setDetectMapValue(long x, long y, short f){
216    detectMap[y*axisDim[0] + x] = f;};
217  void    setReconValue(long pos, float f){recon[pos] = f;};
218  void    setReconValue(long x, long y, long z, float f){
219    recon[z*axisDim[0]*axisDim[1] + y*axisDim[0] + x] = f;};
220  void    setReconFlag(bool f){reconExists = f;};
221  void    saveArray(float *input, long size);
222  void    saveRecon(float *input, long size);
223  void    getRecon(float *output);
224
225  vector<Col>  getLogCols(){return logCols;};
226  void         setLogCols(vector<Col> C){logCols=C;};
227  vector<Col>  getFullCols(){return fullCols;};
228  void         setFullCols(vector<Col> C){fullCols=C;};
229
230  // Statistics for cube
231  void    setCubeStats();    // in Cubes/cubes.cc
232  int     setupFDR();
233  bool    isDetection(long x, long y, long z){
234    long voxel = z*axisDim[0]*axisDim[1] + y*axisDim[0] + x;
235    return DataArray::isDetection(array[voxel]);
236  };
237
238  // Functions that act on the cube
239  void    removeMW();        // in Cubes/cubes.cc
240  void    trimCube();        // in Cubes/trimImage.cc
241  void    unTrimCube();      // in Cubes/trimImage.cc
242  void    removeBaseline();  // in ATrous/baselineSubtract.cc
243  void    replaceBaseline(); // in ATrous/baselineSubtract.cc
244  void    invert();          // in Cubes/invertCube.cc
245  void    reInvert();        // in Cubes/invertCube.cc
246
247  // Reconstruction and Searching functions
248  void    ReconSearch();     // in ATrous/ReconSearch.cc
249  void    ReconCube();       // in ATrous/ReconSearch.cc
250  void    ReconCube1D();     // in ATrous/ReconSearch.cc
251  void    ReconCube2D();     // in ATrous/ReconSearch.cc
252  void    ReconCube3D();     // in ATrous/ReconSearch.cc
253  void    CubicSearch();     // in Cubes/CubicSearch.cc
254
255  // Dealing with the WCS
256  FitsHeader getHead(){return head;};
257  void       setHead(FitsHeader F){head=F;};
258  FitsHeader& header(){FitsHeader &h = head; return h;};
259  int        wcsToPix(const double *world, double *pix){
260    return this->head.wcsToPix(world,pix);}
261  int        wcsToPix(const double *world, double *pix, const int npts){
262    return this->head.wcsToPix(world,pix,npts);}
263  int        pixToWCS(const double *pix, double *world){
264    return this->head.pixToWCS(pix,world);}
265  int        pixToWCS(const double *pix, double *world, const int npts){
266    return this->head.pixToWCS(pix,world,npts);}
267 
268  // Dealing with the detections
269  void    ObjectMerger();          // in Cubes/Merger.cc
270  void    calcObjectWCSparams();
271  void    sortDetections();
272  void    updateDetectMap();
273  void    updateDetectMap(Detection obj);
274  void    setObjectFlags();
275  float   enclosedFlux(Detection obj);
276  bool    objAtEdge(Detection obj);
277 
278  // Text outputting of detected objects.
279  //    the next five in Cubes/detectionIO.cc
280  void    outputDetectionsKarma(std::ostream &stream);     
281  void    outputDetectionsVOTable(std::ostream &stream);   
282  void    outputDetectionList();                           
283  void    logDetectionList();                             
284  void    logDetection(Detection obj, int counter);       
285  void    setupColumns(); // in Cubes/cubes.cc
286
287  // Graphical plotting of the cube and the detections.
288  void    plotBlankEdges();  // in Cubes/cubes.cc
289  //    the next three in Cubes/plotting.cc
290  void    plotDetectionMap(string pgDestination);         
291  void    plotMomentMap(string pgDestination);             
292  void    plotWCSaxes();                                   
293  //    the next two in Cubes/outputSpectra.cc
294  void    outputSpectra();
295  void    plotSpectrum(Detection obj,Plot::SpectralPlot &plot);
296  //    the next three in Cubes/drawMomentCutout.cc
297  void    drawMomentCutout(Detection &object);
298  void    drawScale(float xstart,float ystart,float channel);
299  void    drawFieldEdge();
300
301private:
302  float  *recon;           // reconstructed array -- used when doing a trous
303                           //   reconstruction.
304  bool    reconExists;     // flag saying whether there is a reconstruction
305  short  *detectMap;       // "moment map" -- x,y locations of detected pixels
306  float  *baseline;        // array of spectral baseline values.
307       
308  FitsHeader head;         // the WCS and other header information.
309  vector<Col> fullCols;    // the list of all columns as printed in the
310                           //  results file
311  vector<Col> logCols;     // the list of columns as printed in the log file
312
313};
314
315/****************************************************************/
316//////////////////////////////////////////////////////
317// Prototypes for functions that use above classes
318//////////////////////////////////////////////////////
319
320DataArray getImage(string fname, short int maxdim);
321Image getImage(string fname);
322
323void findSources(Image &image);
324void findSources(Image &image, float mean, float sigma);
325
326vector <Detection> searchReconArray(long *dim, float *originalArray,
327                                    float *reconArray, Param &par,
328                                    StatsContainer<float> &stats);
329vector <Detection> search3DArray(long *dim, float *Array, Param &par,
330                                 StatsContainer<float> &stats);
331
332void growObject(Detection &object, Cube &cube);
333
334#endif
Note: See TracBrowser for help on using the repository browser.