source: trunk/param.cc @ 16

Last change on this file since 16 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: 13.0 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <fstream>
4#include <sstream>
5#include <string>
6#include <math.h>
7#include <param.hh>
8
9using std::setw;
10using std::endl;
11
12/****************************************************************/
13///////////////////////////////////////////////////
14//// Accessor Functions for Parameter class:
15///////////////////////////////////////////////////
16Param::Param(){
17  /**
18   * Param()
19   *  Default intial values for the parameters.
20   * imageFile has no default value!
21   */
22  // Input files
23  this->imageFile       = "";
24  this->flagSubsection  = false;
25  this->subsection      = "[*,*,*]";
26  // Output files
27  this->flagLog         = true;
28  this->logFile         = "./duchamp-Logfile";
29  this->outFile         = "./duchamp-Results";
30  this->spectraFile     = "./duchamp-Spectra.ps";
31  this->flagOutputRecon = false;
32  this->flagOutputResid = false;
33  this->flagVOT         = false;
34  this->votFile         = "./duchamp-Results.xml";
35  this->flagMaps        = true;
36  this->detectionMap    = "./latest-detection-map.ps";
37  this->momentMap       = "./latest-moment-map.ps";
38  // Cube related parameters
39  this->flagBlankPix    = true;
40  this->blankPixValue   = -8.00061;
41  this->blankKeyword    = 1;
42  this->bscaleKeyword   = -8.00061;
43  this->bzeroKeyword    = 0.;
44  this->nanAsBlank      = false;
45  this->flagMW          = true;
46  this->maxMW           = 112;
47  this->minMW           = 75;
48  this->numPixBeam      = 0.;
49  // Trim-related         
50  this->flagTrimmed     = false;
51  this->borderLeft      = 0;
52  this->borderRight     = 0;
53  this->borderBottom    = 0;
54  this->borderTop       = 0;
55  // Subsection offsets
56  this->xSubOffset      = 0;
57  this->ySubOffset      = 0;
58  this->zSubOffset      = 0;
59  // Baseline related
60  this->flagBaseline    = false;
61  // Detection-related   
62  this->minPix          = 2;
63  // Object growth       
64  this->flagGrowth      = true;
65  this->growthCut       = 1.5;
66  // FDR analysis         
67  this->flagFDR         = false;
68  this->alphaFDR        = 0.01;
69  // Other detection     
70  this->snrCut          = 3.;
71  // A trous reconstruction parameters
72  this->flagATrous      = true;
73  this->scaleMin        = 1;
74  this->snrRecon        = 4.;
75  this->filterCode      = 2;
76  // Volume-merging parameters
77  this->flagAdjacent    = true;
78  this->threshSpatial   = 3.;
79  this->threshVelocity  = 7.;
80  this->minChannels     = 3;
81  // Input-Output related
82  this->borders         = true;
83  this->verbose         = true;
84};
85
86/****************************************************************/
87///////////////////////////////////////////////////
88//// Other Functions using the  Parameter class:
89///////////////////////////////////////////////////
90
91string makelower( string s )
92{
93  // "borrowed" from Matt Howlett's 'fred'
94  string out = "";
95  for( int i=0; i<s.size(); ++i ) {
96    out += tolower(s[i]);
97  }
98  return out;
99}
100
101void Param::readParams(string &paramfile)
102{
103
104  std::ifstream fin(paramfile.c_str());
105  string line;
106  string sval;
107  float fval;
108  int ival;
109  bool bval;
110  while( !std::getline(fin,line,'\n').eof()){
111
112    if(line[0]!='#'){
113      std::stringstream ss;
114      ss.str(line);
115      string arg;
116      ss >> arg;     
117      if(makelower(arg)=="imagefile"){       ss >> sval; this->imageFile = sval;}
118      if(makelower(arg)=="flaglog"){         ss >> bval; this->flagLog = bval; }
119      if(makelower(arg)=="logfile"){         ss >> sval; this->logFile = sval; }
120      if(makelower(arg)=="outfile"){         ss >> sval; this->outFile = sval; }
121      if(makelower(arg)=="spectrafile"){     ss >> sval; this->spectraFile = sval; }
122      if(makelower(arg)=="flagoutputrecon"){ ss >> bval; this->flagOutputRecon = bval; }
123      if(makelower(arg)=="flagoutputresid"){ ss >> bval; this->flagOutputResid = bval; }
124      if(makelower(arg)=="flagvot"){         ss >> bval; this->flagVOT = bval; }
125      if(makelower(arg)=="votfile"){         ss >> sval; this->votFile = sval; }
126      if(makelower(arg)=="flagmaps"){        ss >> bval; this->flagMaps = bval; }
127      if(makelower(arg)=="detectionmap"){    ss >> sval; this->detectionMap = sval; }
128      if(makelower(arg)=="momentmap"){       ss >> sval; this->momentMap = sval; }
129
130      if(makelower(arg)=="flagblankpix"){    ss >> bval; this->flagBlankPix = bval; }
131      if(makelower(arg)=="blankpixvalue"){   ss >> fval; this->blankPixValue = fval; }
132      if(makelower(arg)=="flagmw"){          ss >> bval; this->flagMW = bval; }
133      if(makelower(arg)=="maxmw"){           ss >> ival; this->maxMW = ival; }
134      if(makelower(arg)=="minmw"){           ss >> ival; this->minMW = ival; }
135      if(makelower(arg)=="minpix"){          ss >> ival; this->minPix = ival; }
136      if(makelower(arg)=="flagfdr"){         ss >> bval; this->flagFDR = bval; }
137      if(makelower(arg)=="alphafdr"){        ss >> fval; this->alphaFDR = fval; }
138      if(makelower(arg)=="flagbaseline"){    ss >> bval; this->flagBaseline = bval; }
139      if(makelower(arg)=="snrcut"){          ss >> fval; this->snrCut = fval; }
140      if(makelower(arg)=="flaggrowth"){      ss >> bval; this->flagGrowth = bval; }
141      if(makelower(arg)=="growthcut"){       ss >> fval; this->growthCut = fval; }
142      if(makelower(arg)=="flagatrous"){      ss >> bval; this->flagATrous = bval; }
143      if(makelower(arg)=="scalemin"){        ss >> ival; this->scaleMin = ival; }
144      if(makelower(arg)=="snrrecon"){        ss >> fval; this->snrRecon = fval; }
145      if(makelower(arg)=="filtercode"){      ss >> ival; this->filterCode = ival; }
146      if(makelower(arg)=="flagadjacent"){    ss >> bval; this->flagAdjacent = bval; }
147      if(makelower(arg)=="threshspatial"){   ss >> fval; this->threshSpatial = fval; }
148      if(makelower(arg)=="threshvelocity"){  ss >> fval; this->threshVelocity = fval; }
149      if(makelower(arg)=="minchannels"){     ss >> ival; this->minChannels = ival; }
150      if(makelower(arg)=="flagsubsection"){  ss >> bval; this->flagSubsection = bval; }
151      if(makelower(arg)=="subsection"){      ss >> sval; this->subsection = sval; }
152      if(makelower(arg)=="drawborders"){     ss >> bval; this->borders = bval; }
153      if(makelower(arg)=="verbose"){         ss >> bval; this->verbose = bval; }
154    }
155  }
156}
157
158
159std::ostream& operator<< ( std::ostream& theStream, Param& par)
160{
161  theStream.setf(std::ios::boolalpha);
162  theStream.setf(std::ios::left);
163  theStream  <<"---- Parameters ----"<<endl;
164  if(par.getFlagSubsection())
165    theStream<<setw(40)<<"Image to be analysed"                 <<"= "<<(par.getImageFile()+
166                                                                         par.getSubsection())   <<endl;
167  else
168    theStream<<setw(40)<<"Image to be analysed"                 <<"= "<<par.getImageFile()      <<endl;
169  theStream  <<setw(40)<<"Intermediate Logfile"                 <<"= "<<par.getLogFile()        <<endl;
170  theStream  <<setw(40)<<"Final Results file"                   <<"= "<<par.getOutFile()        <<endl;
171  theStream  <<setw(40)<<"Spectrum file"                        <<"= "<<par.getSpectraFile()    <<endl;
172  if(par.getFlagVOT()){
173    theStream<<setw(40)<<"VOTable file"                         <<"= "<<par.getVOTFile()        <<endl;
174  }
175  if(par.getFlagMaps()){
176    theStream<<setw(40)<<"1st Moment Map"                       <<"= "<<par.getMomentMap()      <<endl;
177    theStream<<setw(40)<<"Detection Map"                        <<"= "<<par.getDetectionMap()   <<endl;
178  }
179  if(par.getFlagATrous()){                             
180    theStream<<setw(40)<<"Saving reconstructed cube?"           <<"= "<<par.getFlagOutputRecon()<<endl;
181    theStream<<setw(40)<<"Saving residuals from reconstruction?"<<"= "<<par.getFlagOutputResid()<<endl;
182  }                                                   
183  theStream  <<"------"<<endl;                                 
184  theStream  <<setw(40)<<"Fixing Blank Pixels?"                 <<"= "<<par.getFlagBlankPix()   <<endl;
185  if(par.getFlagBlankPix()){
186    theStream<<setw(40)<<"Blank Pixel Value"                    <<"= "<<par.getBlankPixVal()    <<endl;
187  }
188  theStream  <<setw(40)<<"Removing Milky Way channels?"         <<"= "<<par.getFlagMW()         <<endl;
189  if(par.getFlagMW()){
190    theStream<<setw(40)<<"Milky Way Channels"                   <<"= "<<par.getMinMW()
191                       <<"-"                                          <<par.getMaxMW()          <<endl;
192  }
193  theStream  <<setw(40)<<"Beam Size (pixels)"                   <<"= "<<par.getBeamSize()       <<endl;
194  theStream  <<setw(40)<<"Removing baselines before search?"    <<"= "<<par.getFlagBaseline()   <<endl;
195  theStream  <<setw(40)<<"Minimum # Pixels in a detection"      <<"= "<<par.getMinPix()         <<endl;
196  theStream  <<setw(40)<<"Growing objects after detection?"     <<"= "<<par.getFlagGrowth()     <<endl;
197  if(par.getFlagGrowth()) {                           
198    theStream<<setw(40)<<"SNR Threshold for growth"             <<"= "<<par.getGrowthCut()      <<endl;
199  }
200  theStream  <<setw(40)<<"Using A Trous reconstruction?"        <<"= "<<par.getFlagATrous()     <<endl;
201  if(par.getFlagATrous()){                             
202    theStream<<setw(40)<<"Minimum scale in reconstruction"      <<"= "<<par.getMinScale()       <<endl;
203    theStream<<setw(40)<<"SNR Threshold within reconstruction"  <<"= "<<par.getAtrousCut()      <<endl;
204    theStream<<setw(40)<<"Filter being used for reconstruction" <<"= "<<par.getFilterName()     <<endl;
205  }                                                   
206  theStream  <<setw(40)<<"Using FDR analysis?"                  <<"= "<<par.getFlagFDR()        <<endl;
207  if(par.getFlagFDR()){                               
208    theStream<<setw(40)<<"Alpha value for FDR analysis"         <<"= "<<par.getAlpha()          <<endl;
209  }                                                   
210  else {
211    theStream<<setw(40)<<"SNR Threshold"                        <<"= "<<par.getCut()            <<endl;
212  }
213  theStream  <<setw(40)<<"Using Adjacent-pixel criterion?"      <<"= "<<par.getFlagAdjacent()   <<endl;
214  if(!par.getFlagAdjacent()){
215    theStream<<setw(40)<<"Max. spatial separation for merging"  <<"= "<<par.getThreshS()        <<endl;
216    theStream<<setw(40)<<"Max. velocity separation for merging" <<"= "<<par.getThreshV()        <<endl;
217  }
218  theStream  <<setw(40)<<"Min. # channels for merging"          <<"= "<<par.getMinChannels()    <<endl;
219  theStream  <<"--------------------"<<endl;
220  theStream.unsetf(std::ios::left);
221  theStream.unsetf(std::ios::boolalpha);
222}
223
224void Param::parseSubsection()
225{
226  std::stringstream ss;
227  ss.str(this->subsection);
228  bool removeStep = false;
229  string x,y,z;
230  getline(ss,x,'[');
231  getline(ss,x,',');
232  getline(ss,y,',');
233  getline(ss,z,']');
234  char *end;
235  if(x!="*") {
236    int x1,x2,dx;
237    int a = x.find(':');  // first occurence of ':' in x-subsection string
238    int b = x.find(':',a+1); // location of second ':' -- will be -1 if there is no second occurence.
239    x1 = atoi( x.substr(0,a).c_str() ); // minimum x in subsection
240    this->xSubOffset = x1 - 1;
241    if(b>0){ // there is a dx component
242      x2 = atoi( x.substr(a+1,b-a-1).c_str() ); // maximum x in subsection
243      dx = atoi( x.substr(b+1,x.size()).c_str() ); // delta x in subsection
244      x = x.substr(0,b); // rewrite x without the delta-x part.
245      removeStep = true;
246    }
247  }
248  if(y!="*") {
249    int y1,y2,dy;
250    int a = y.find(':');  // first occurence of ':' in y-subsection string
251    int b = y.find(':',a+1); // location of second ':' -- will be -1 if there is no second occurence.
252    y1 = atoi( y.substr(0,a).c_str() ); // minimum y in subsection
253    this->ySubOffset = y1 - 1;
254    if(b>0){ // there is a dy component
255      y2 = atoi( y.substr(a+1,b-a-1).c_str() ); // maximum y in subsection
256      dy = atoi( y.substr(b+1,y.size()).c_str() ); // delta y in subsection
257      y = y.substr(0,b); // rewrite y without the delta-y part.
258      removeStep = true;
259    }
260  }
261  if(z!="*") {
262    int z1,z2,dz;
263    int a = z.find(':');  // first occurence of ':' in z-subsection string
264    int b = z.find(':',a+1); // location of second ':' -- will be -1 if there is no second occurence.
265    z1 = atoi( z.substr(0,a).c_str() ); // minimum z in subsection
266    this->zSubOffset = z1 - 1;
267    if(b>0){ // there is a dz component
268      z2 = atoi( z.substr(a+1,b-a-1).c_str() ); // maximum z in subsection
269      dz = atoi( z.substr(b+1,z.size()).c_str() ); // delta z in subsection
270      z = z.substr(0,b); // rewrite z without the delta-z part.
271      removeStep = true;
272    }
273  }
274
275  if(removeStep){
276    std::cerr << "\a\tThe subsection given is " << this->subsection <<".\n";
277    std::cerr << "\tDuchamp is currently unable to deal with pixel steps in the subsection.\n";
278    std::cerr << "\tThese have been ignored, and so the subection used is ";   
279    this->subsection = "[" + x + "," + y + "," + z + "]";  // rewrite subsection without any step sizes.
280    std::cerr << this->subsection << std::endl;
281  }
282 
283//   if(x!="*") this->xSubOffset = strtol(x.c_str(),&end,10) - 1;   
284//   if(y!="*") this->ySubOffset = strtol(y.c_str(),&end,10) - 1;   
285//   if(z!="*") this->zSubOffset = strtol(z.c_str(),&end,10) - 1;   
286}
287
288bool Param::isBlank(float &val)
289{
290//   std::cerr << "val = " << val << " " << int((val-this->bzeroKeyword)/this->bscaleKeyword)
291//          <<" " <<this->blankKeyword << std::endl;
292  if(this->flagBlankPix){
293    if(this->nanAsBlank) return bool(isnan(val));
294    else//  return (val == this->blankPixValue);
295      return ( this->blankKeyword == int((val-this->bzeroKeyword)/this->bscaleKeyword) );
296  }
297  else return false;
298}
Note: See TracBrowser for help on using the repository browser.