- Timestamp:
- 10/15/12 15:52:38 (12 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/asapfitter.py
r2541 r2666 3 3 from asap.logging import asaplog, asaplog_post_dec 4 4 from asap.utils import _n_bools, mask_and 5 5 from numpy import ndarray 6 6 7 7 class fitter: … … 26 26 self._selection = None 27 27 self.uselinear = False 28 self._constraints = [] 28 29 29 30 def set_data(self, xdat, ydat, mask=None): … … 73 74 Set the function to be fit. 74 75 Parameters: 75 poly: use a polynomial of the order given with nonlinear least squares fit 76 lpoly: use polynomial of the order given with linear least squares fit 76 poly: use a polynomial of the order given with nonlinear 77 least squares fit 78 lpoly: use polynomial of the order given with linear least 79 squares fit 77 80 gauss: fit the number of gaussian specified 78 81 lorentz: fit the number of lorentzian specified 79 82 sinusoid: fit the number of sinusoid specified 80 83 Example: 81 fitter.set_function(poly=3) # will fit a 3rd order polynomial via nonlinear method 82 fitter.set_function(lpoly=3) # will fit a 3rd order polynomial via linear method 84 fitter.set_function(poly=3) # will fit a 3rd order polynomial 85 # via nonlinear method 86 fitter.set_function(lpoly=3) # will fit a 3rd order polynomial 87 # via linear method 83 88 fitter.set_function(gauss=2) # will fit two gaussians 84 89 fitter.set_function(lorentz=2) # will fit two lorentzians … … 117 122 self.components = [ 3 for i in range(n) ] 118 123 self.uselinear = False 124 elif kwargs.has_key('expression'): 125 self.uselinear = False 126 raise RuntimeError("Not yet implemented") 119 127 else: 120 128 msg = "Invalid function type." … … 122 130 123 131 self.fitter.setexpression(self.fitfunc,n) 132 self._constraints = [] 124 133 self.fitted = False 125 134 return … … 147 156 raise RuntimeError(msg) 148 157 149 else: 150 if self.data is not None: 151 self.x = self.data._getabcissa(row) 152 self.y = self.data._getspectrum(row) 153 #self.mask = mask_and(self.mask, self.data._getmask(row)) 154 if len(self.x) == len(self.mask): 155 self.mask = mask_and(self.mask, self.data._getmask(row)) 156 else: 157 asaplog.push('lengths of data and mask are not the same. preset mask will be ignored') 158 asaplog.post('WARN','asapfit.fit') 159 self.mask=self.data._getmask(row) 160 asaplog.push("Fitting:") 161 i = row 162 out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (self.data.getscan(i), 163 self.data.getbeam(i), 164 self.data.getif(i), 165 self.data.getpol(i), 166 self.data.getcycle(i)) 167 asaplog.push(out,False) 158 if self.data is not None: 159 self.x = self.data._getabcissa(row) 160 self.y = self.data._getspectrum(row) 161 #self.mask = mask_and(self.mask, self.data._getmask(row)) 162 if len(self.x) == len(self.mask): 163 self.mask = mask_and(self.mask, self.data._getmask(row)) 164 else: 165 asaplog.push('lengths of data and mask are not the same. ' 166 'preset mask will be ignored') 167 asaplog.post('WARN','asapfit.fit') 168 self.mask=self.data._getmask(row) 169 asaplog.push("Fitting:") 170 i = row 171 out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % ( 172 self.data.getscan(i), 173 self.data.getbeam(i), 174 self.data.getif(i), 175 self.data.getpol(i), 176 self.data.getcycle(i)) 177 178 asaplog.push(out, False) 179 168 180 self.fitter.setdata(self.x, self.y, self.mask) 169 181 if self.fitfunc == 'gauss' or self.fitfunc == 'lorentz': … … 174 186 if len(fxdpar) and fxdpar.count(0) == 0: 175 187 raise RuntimeError,"No point fitting, if all parameters are fixed." 188 if self._constraints: 189 for c in self._constraints: 190 self.fitter.addconstraint(c[0]+[c[-1]]) 176 191 if self.uselinear: 177 192 converged = self.fitter.lfit() … … 234 249 msg = "Please specify a fitting function first." 235 250 raise RuntimeError(msg) 236 if (self.fitfunc == "gauss" or self.fitfunc == "lorentz" or self.fitfunc == "sinusoid") and component is not None: 251 if (self.fitfunc == "gauss" or self.fitfunc == "lorentz" 252 or self.fitfunc == "sinusoid") and component is not None: 237 253 if not self.fitted and sum(self.fitter.getparameters()) == 0: 238 254 pars = _n_bools(len(self.components)*3, False) … … 338 354 raise ValueError(msg) 339 355 356 357 def add_constraint(self, xpar, y): 358 """Add parameter constraints to the fit. This is done by setting up 359 linear equations for the related parameters. 360 361 For example a two component gaussian fit where the amplitudes are 362 constraint by amp1 = 2*amp2 363 needs a constraint 364 365 add_constraint([1, 0, 0, -2, 0, 0, 0], 0) 366 367 a velocity difference of v2-v1=17 368 369 add_constraint([0.,-1.,0.,0.,1.,0.], 17.) 370 371 """ 372 self._constraints.append((xpar, y)) 373 374 340 375 def get_area(self, component=None): 341 376 """ … … 381 416 cerrs = errs 382 417 if component is not None: 383 if self.fitfunc == "gauss" or self.fitfunc == "lorentz" or self.fitfunc == "sinusoid": 418 if self.fitfunc == "gauss" or self.fitfunc == "lorentz" \ 419 or self.fitfunc == "sinusoid": 384 420 i = 3*component 385 421 if i < len(errs): … … 463 499 out += "%s%s = %3.3f %s, " % (pnam[1], fix1, pars[i+1], aunit) 464 500 out += "%s%s = %3.3f %s\n" % (pnam[2], fix2, pars[i+2], aunit) 465 if len(area): out += " area = %3.3f %s %s\n" % (area[i], ounit, aunit) 501 if len(area): out += " area = %3.3f %s %s\n" % (area[i], 502 ounit, 503 aunit) 466 504 c+=1 467 505 i+=3 … … 571 609 ylab = self.data._get_ordinate_label() 572 610 573 colours = ["#777777","#dddddd","red","orange","purple","green","magenta", "cyan"] 611 colours = ["#777777","#dddddd","red","orange","purple","green", 612 "magenta", "cyan"] 574 613 nomask=True 575 614 for i in range(len(m)): … … 604 643 if isinstance(components,int): cs = [components] 605 644 if plotparms: 606 self._p.text(0.15,0.15,str(self.get_parameters()['formatted']),size=8) 645 self._p.text(0.15,0.15, 646 str(self.get_parameters()['formatted']),size=8) 607 647 n = len(self.components) 608 648 self._p.palette(3) … … 656 696 asaplog.push("Fitting:") 657 697 for r in rows: 658 out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (scan.getscan(r), 659 scan.getbeam(r), 660 scan.getif(r), 661 scan.getpol(r), 662 scan.getcycle(r)) 698 out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % ( 699 scan.getscan(r), 700 scan.getbeam(r), 701 scan.getif(r), 702 scan.getpol(r), 703 scan.getcycle(r) 704 ) 663 705 asaplog.push(out, False) 664 706 self.x = scan._getabcissa(r) … … 668 710 self.mask = mask_and(self.mask, self.data._getmask(row)) 669 711 else: 670 asaplog.push('lengths of data and mask are not the same. preset mask will be ignored') 712 asaplog.push('lengths of data and mask are not the same. ' 713 'preset mask will be ignored') 671 714 asaplog.post('WARN','asapfit.fit') 672 715 self.mask=self.data._getmask(row) -
trunk/src/STFitter.cpp
r2580 r2666 80 80 y_.resize(); 81 81 m_.resize(); 82 constraints_.clear(); 82 83 } 83 84 … … 294 295 } 295 296 297 void Fitter::addConstraint(const std::vector<float>& constraint) 298 { 299 if (funcs_.nelements() == 0) 300 throw (AipsError("Function not yet set.")); 301 constraints_.push_back(constraint); 302 303 } 304 305 void Fitter::applyConstraints(GenericL2Fit<Float>& fitter) 306 { 307 std::vector<std::vector<float> >::const_iterator it; 308 for (it = constraints_.begin(); it != constraints_.end(); ++it) { 309 Vector<Float> tmp(*it); 310 fitter.addConstraint(tmp(Slice(0,tmp.nelements()-1)), 311 tmp(tmp.nelements()-1)); 312 } 313 } 314 296 315 bool Fitter::setFixedParameters(std::vector<bool> fixed) 297 316 { … … 377 396 // Convergence criterium 378 397 fitter.setCriteria(0.001); 398 applyConstraints(fitter); 379 399 380 400 // Fit … … 397 417 chisquared_ = fitter.getChi2(); 398 418 399 // residual_.resize();400 // residual_ = y_;401 // fitter.residual(residual_,x_);402 419 // use fitter.residual(model=True) to get the model 403 420 thefit_.resize(x_.nelements()); 404 421 fitter.residual(thefit_,x_,True); 405 // residual = data - model406 422 residual_.resize(x_.nelements()); 407 423 residual_ = y_ - thefit_ ; … … 419 435 420 436 fitter.setFunction(func); 421 //fitter.setMaxIter(50+n*10); 422 // Convergence criterium 423 //fitter.setCriteria(0.001); 424 425 // Fit 426 // Vector<Float> sigma(x_.nelements()); 427 // sigma = 1.0; 437 applyConstraints(fitter); 428 438 429 439 parameters_.resize(); 430 // parameters_ = fitter.fit(x_, y_, sigma, &m_);431 440 parameters_ = fitter.fit(x_, y_, &m_); 432 441 std::vector<float> ps; … … 439 448 chisquared_ = fitter.getChi2(); 440 449 441 // residual_.resize();442 // residual_ = y_;443 // fitter.residual(residual_,x_);444 // use fitter.residual(model=True) to get the model445 450 thefit_.resize(x_.nelements()); 446 451 fitter.residual(thefit_,x_,True); 447 // residual = data - model448 452 residual_.resize(x_.nelements()); 449 453 residual_ = y_ - thefit_ ; -
trunk/src/STFitter.h
r1932 r2666 39 39 #include <scimath/Functionals/Function.h> 40 40 #include <scimath/Functionals/CompoundFunction.h> 41 #include <scimath/Fitting/GenericL2Fit.h> 42 41 43 42 44 #include "STFitEntry.h" 45 43 46 44 47 namespace asap { … … 55 58 bool setParameters(std::vector<float> params); 56 59 bool setFixedParameters(std::vector<bool> fixed); 60 void addConstraint(const std::vector<float>& constraint); 57 61 58 62 std::vector<float> getResidual() const; … … 76 80 private: 77 81 void clear(); 82 void applyConstraints(casa::GenericL2Fit<casa::Float>& fitter); 78 83 casa::Vector<casa::Float> x_; 79 84 casa::Vector<casa::Float> y_; … … 87 92 casa::Vector<casa::Float> parameters_; 88 93 casa::Vector<casa::Bool> fixedpar_; 94 std::vector<std::vector<float> > constraints_; 89 95 90 96 casa::Vector<casa::Float> error_; -
trunk/src/STLineFinder.h
r2580 r2666 48 48 #include "ScantableWrapper.h" 49 49 #include "Scantable.h" 50 #include "STFitter.h"51 50 52 51 namespace asap { -
trunk/src/Scantable.cpp
r2658 r2666 71 71 #include "STPolStokes.h" 72 72 #include "STUpgrade.h" 73 #include "STFitter.h" 73 74 #include "Scantable.h" 74 75 -
trunk/src/Scantable.h
r2658 r2666 43 43 #include "STFit.h" 44 44 #include "STFitEntry.h" 45 #include "STFitter.h"45 //#include "STFitter.h" 46 46 #include "STFocus.h" 47 47 #include "STFrequencies.h" … … 55 55 56 56 namespace asap { 57 58 class Fitter; 57 59 58 60 /** -
trunk/src/python_Fitter.cpp
r1391 r2666 49 49 .def("getparameters", &Fitter::getParameters) 50 50 .def("setparameters", &Fitter::setParameters) 51 .def("addconstraint", &Fitter::addConstraint) 51 52 .def("getestimate", &Fitter::getEstimate) 52 53 .def("estimate", &Fitter::computeEstimate) -
trunk/src/python_asap.cpp
r2658 r2666 124 124 casa::pyrap::register_convert_casa_valueholder(); 125 125 casa::pyrap::register_convert_casa_record(); 126 126 127 #endif 127 128 }
Note:
See TracChangeset
for help on using the changeset viewer.