source: branches/hpc33/src/STFitter.cpp@ 2576

Last change on this file since 2576 was 2570, checked in by Takeshi Nakazato, 12 years ago

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Calculate residual as (data-model) instead to calculate model as
(data-residual) since the later changes result.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
RevLine 
[91]1//#---------------------------------------------------------------------------
[890]2//# Fitter.cc: A Fitter class for spectra
[91]3//#--------------------------------------------------------------------------
4//# Copyright (C) 2004
[125]5//# ATNF
[91]6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//# Internet email: Malte.Marquarding@csiro.au
23//# Postal address: Malte Marquarding,
24//# Australia Telescope National Facility,
25//# P.O. Box 76,
26//# Epping, NSW, 2121,
27//# AUSTRALIA
28//#
[891]29//# $Id: STFitter.cpp 2570 2012-06-14 05:22:52Z TakeshiNakazato $
[91]30//#---------------------------------------------------------------------------
[125]31#include <casa/aips.h>
[91]32#include <casa/Arrays/ArrayMath.h>
33#include <casa/Arrays/ArrayLogical.h>
[1819]34#include <casa/Logging/LogIO.h>
[91]35#include <scimath/Fitting.h>
36#include <scimath/Fitting/LinearFit.h>
37#include <scimath/Functionals/CompiledFunction.h>
38#include <scimath/Functionals/CompoundFunction.h>
39#include <scimath/Functionals/Gaussian1D.h>
[1819]40#include "Lorentzian1D.h"
[2047]41#include <scimath/Functionals/Sinusoid1D.h>
[91]42#include <scimath/Functionals/Polynomial.h>
43#include <scimath/Mathematics/AutoDiff.h>
44#include <scimath/Mathematics/AutoDiffMath.h>
45#include <scimath/Fitting/NonLinearFitLM.h>
46#include <components/SpectralComponents/SpectralEstimate.h>
47
[894]48#include "STFitter.h"
49
[91]50using namespace asap;
[125]51using namespace casa;
[91]52
[890]53Fitter::Fitter()
[91]54{
55}
56
[890]57Fitter::~Fitter()
[91]58{
[517]59 reset();
[91]60}
61
[890]62void Fitter::clear()
[91]63{
[517]64 for (uInt i=0;i< funcs_.nelements();++i) {
65 delete funcs_[i]; funcs_[i] = 0;
66 }
[612]67 funcs_.resize(0,True);
[517]68 parameters_.resize();
[1232]69 fixedpar_.resize();
[517]70 error_.resize();
71 thefit_.resize();
72 estimate_.resize();
73 chisquared_ = 0.0;
[91]74}
[517]75
[890]76void Fitter::reset()
[91]77{
[517]78 clear();
79 x_.resize();
80 y_.resize();
81 m_.resize();
[91]82}
83
84
[890]85bool Fitter::computeEstimate() {
[517]86 if (x_.nelements() == 0 || y_.nelements() == 0)
87 throw (AipsError("No x/y data specified."));
[91]88
[517]89 if (dynamic_cast<Gaussian1D<Float>* >(funcs_[0]) == 0)
90 return false;
91 uInt n = funcs_.nelements();
92 SpectralEstimate estimator(n);
93 estimator.setQ(5);
94 Int mn,mx;
95 mn = 0;
96 mx = m_.nelements()-1;
97 for (uInt i=0; i<m_.nelements();++i) {
98 if (m_[i]) {
99 mn = i;
100 break;
[108]101 }
[517]102 }
[2163]103 // use Int to suppress compiler warning
104 for (Int j=m_.nelements()-1; j>=0;--j) {
[517]105 if (m_[j]) {
106 mx = j;
107 break;
[108]108 }
[517]109 }
[1067]110 //mn = 0+x_.nelements()/10;
111 //mx = x_.nelements()-x_.nelements()/10;
[517]112 estimator.setRegion(mn,mx);
113 //estimator.setWindowing(True);
114 SpectralList listGauss = estimator.estimate(x_, y_);
115 parameters_.resize(n*3);
116 Gaussian1D<Float>* g = 0;
117 for (uInt i=0; i<n;i++) {
118 g = dynamic_cast<Gaussian1D<Float>* >(funcs_[i]);
119 if (g) {
[2394]120 const GaussianSpectralElement *gauss = dynamic_cast<const GaussianSpectralElement *>(listGauss[i]) ;
121 (*g)[0] = gauss->getAmpl();
122 (*g)[1] = gauss->getCenter();
123 (*g)[2] = gauss->getFWHM();
124// (*g)[0] = listGauss[i].getAmpl();
125// (*g)[1] = listGauss[i].getCenter();
126// (*g)[2] = listGauss[i].getFWHM();
[91]127 }
[517]128 }
129 estimate_.resize();
130 listGauss.evaluate(estimate_,x_);
131 return true;
[91]132}
133
[890]134std::vector<float> Fitter::getEstimate() const
[91]135{
[517]136 if (estimate_.nelements() == 0)
137 throw (AipsError("No estimate set."));
138 std::vector<float> stlout;
139 estimate_.tovector(stlout);
140 return stlout;
[91]141}
142
143
[890]144bool Fitter::setExpression(const std::string& expr, int ncomp)
[91]145{
[517]146 clear();
147 if (expr == "gauss") {
148 if (ncomp < 1) throw (AipsError("Need at least one gaussian to fit."));
149 funcs_.resize(ncomp);
[1932]150 funcnames_.clear();
151 funccomponents_.clear();
[517]152 for (Int k=0; k<ncomp; ++k) {
153 funcs_[k] = new Gaussian1D<Float>();
[1932]154 funcnames_.push_back(expr);
155 funccomponents_.push_back(3);
[517]156 }
[1819]157 } else if (expr == "lorentz") {
158 if (ncomp < 1) throw (AipsError("Need at least one lorentzian to fit."));
159 funcs_.resize(ncomp);
[1932]160 funcnames_.clear();
161 funccomponents_.clear();
[1819]162 for (Int k=0; k<ncomp; ++k) {
163 funcs_[k] = new Lorentzian1D<Float>();
[1932]164 funcnames_.push_back(expr);
165 funccomponents_.push_back(3);
[1819]166 }
[2047]167 } else if (expr == "sinusoid") {
168 if (ncomp < 1) throw (AipsError("Need at least one sinusoid to fit."));
169 funcs_.resize(ncomp);
170 funcnames_.clear();
171 funccomponents_.clear();
172 for (Int k=0; k<ncomp; ++k) {
173 funcs_[k] = new Sinusoid1D<Float>();
174 funcnames_.push_back(expr);
175 funccomponents_.push_back(3);
176 }
177 } else if (expr == "poly") {
178 funcs_.resize(1);
179 funcnames_.clear();
180 funccomponents_.clear();
181 funcs_[0] = new Polynomial<Float>(ncomp);
182 funcnames_.push_back(expr);
183 funccomponents_.push_back(ncomp);
[517]184 } else {
[1819]185 LogIO os( LogOrigin( "Fitter", "setExpression()", WHERE ) ) ;
186 os << LogIO::WARN << " compiled functions not yet implemented" << LogIO::POST;
[517]187 //funcs_.resize(1);
188 //funcs_[0] = new CompiledFunction<Float>();
189 //funcs_[0]->setFunction(String(expr));
190 return false;
191 }
192 return true;
[91]193}
194
[890]195bool Fitter::setData(std::vector<float> absc, std::vector<float> spec,
[91]196 std::vector<bool> mask)
197{
198 x_.resize();
199 y_.resize();
200 m_.resize();
201 // convert std::vector to casa Vector
202 Vector<Float> tmpx(absc);
203 Vector<Float> tmpy(spec);
204 Vector<Bool> tmpm(mask);
205 AlwaysAssert(tmpx.nelements() == tmpy.nelements(), AipsError);
206 x_ = tmpx;
207 y_ = tmpy;
208 m_ = tmpm;
209 return true;
210}
211
[890]212std::vector<float> Fitter::getResidual() const
[91]213{
214 if (residual_.nelements() == 0)
215 throw (AipsError("Function not yet fitted."));
216 std::vector<float> stlout;
217 residual_.tovector(stlout);
218 return stlout;
219}
220
[890]221std::vector<float> Fitter::getFit() const
[91]222{
223 Vector<Float> out = thefit_;
224 std::vector<float> stlout;
225 out.tovector(stlout);
226 return stlout;
227
228}
229
[890]230std::vector<float> Fitter::getErrors() const
[91]231{
232 Vector<Float> out = error_;
233 std::vector<float> stlout;
234 out.tovector(stlout);
235 return stlout;
236}
237
[890]238bool Fitter::setParameters(std::vector<float> params)
[91]239{
240 Vector<Float> tmppar(params);
241 if (funcs_.nelements() == 0)
242 throw (AipsError("Function not yet set."));
243 if (parameters_.nelements() > 0 && tmppar.nelements() != parameters_.nelements())
244 throw (AipsError("Number of parameters inconsistent with function."));
[1232]245 if (parameters_.nelements() == 0) {
[91]246 parameters_.resize(tmppar.nelements());
[1232]247 if (tmppar.nelements() != fixedpar_.nelements()) {
248 fixedpar_.resize(tmppar.nelements());
249 fixedpar_ = False;
250 }
251 }
[91]252 if (dynamic_cast<Gaussian1D<Float>* >(funcs_[0]) != 0) {
253 uInt count = 0;
254 for (uInt j=0; j < funcs_.nelements(); ++j) {
255 for (uInt i=0; i < funcs_[j]->nparameters(); ++i) {
256 (funcs_[j]->parameters())[i] = tmppar[count];
257 parameters_[count] = tmppar[count];
258 ++count;
259 }
260 }
[1819]261 } else if (dynamic_cast<Lorentzian1D<Float>* >(funcs_[0]) != 0) {
262 uInt count = 0;
263 for (uInt j=0; j < funcs_.nelements(); ++j) {
264 for (uInt i=0; i < funcs_[j]->nparameters(); ++i) {
265 (funcs_[j]->parameters())[i] = tmppar[count];
266 parameters_[count] = tmppar[count];
267 ++count;
268 }
269 }
[2047]270 } else if (dynamic_cast<Sinusoid1D<Float>* >(funcs_[0]) != 0) {
271 uInt count = 0;
272 for (uInt j=0; j < funcs_.nelements(); ++j) {
273 for (uInt i=0; i < funcs_[j]->nparameters(); ++i) {
274 (funcs_[j]->parameters())[i] = tmppar[count];
275 parameters_[count] = tmppar[count];
276 ++count;
277 }
278 }
279 } else if (dynamic_cast<Polynomial<Float>* >(funcs_[0]) != 0) {
280 for (uInt i=0; i < funcs_[0]->nparameters(); ++i) {
281 parameters_[i] = tmppar[i];
282 (funcs_[0]->parameters())[i] = tmppar[i];
283 }
[91]284 }
[1232]285 // reset
286 if (params.size() == 0) {
287 parameters_.resize();
288 fixedpar_.resize();
289 }
[91]290 return true;
291}
292
[890]293bool Fitter::setFixedParameters(std::vector<bool> fixed)
[91]294{
295 if (funcs_.nelements() == 0)
296 throw (AipsError("Function not yet set."));
[1232]297 if (fixedpar_.nelements() > 0 && fixed.size() != fixedpar_.nelements())
[91]298 throw (AipsError("Number of mask elements inconsistent with function."));
[1232]299 if (fixedpar_.nelements() == 0) {
300 fixedpar_.resize(parameters_.nelements());
301 fixedpar_ = False;
302 }
[91]303 if (dynamic_cast<Gaussian1D<Float>* >(funcs_[0]) != 0) {
304 uInt count = 0;
305 for (uInt j=0; j < funcs_.nelements(); ++j) {
306 for (uInt i=0; i < funcs_[j]->nparameters(); ++i) {
[1232]307 funcs_[j]->mask(i) = !fixed[count];
308 fixedpar_[count] = fixed[count];
[91]309 ++count;
310 }
311 }
[1819]312 } else if (dynamic_cast<Lorentzian1D<Float>* >(funcs_[0]) != 0) {
313 uInt count = 0;
314 for (uInt j=0; j < funcs_.nelements(); ++j) {
315 for (uInt i=0; i < funcs_[j]->nparameters(); ++i) {
316 funcs_[j]->mask(i) = !fixed[count];
317 fixedpar_[count] = fixed[count];
318 ++count;
319 }
320 }
[2047]321 } else if (dynamic_cast<Sinusoid1D<Float>* >(funcs_[0]) != 0) {
322 uInt count = 0;
323 for (uInt j=0; j < funcs_.nelements(); ++j) {
324 for (uInt i=0; i < funcs_[j]->nparameters(); ++i) {
325 funcs_[j]->mask(i) = !fixed[count];
326 fixedpar_[count] = fixed[count];
327 ++count;
328 }
329 }
330 } else if (dynamic_cast<Polynomial<Float>* >(funcs_[0]) != 0) {
331 for (uInt i=0; i < funcs_[0]->nparameters(); ++i) {
332 fixedpar_[i] = fixed[i];
333 funcs_[0]->mask(i) = !fixed[i];
334 }
[91]335 }
336 return true;
337}
338
[890]339std::vector<float> Fitter::getParameters() const {
[91]340 Vector<Float> out = parameters_;
341 std::vector<float> stlout;
342 out.tovector(stlout);
343 return stlout;
344}
345
[890]346std::vector<bool> Fitter::getFixedParameters() const {
[108]347 Vector<Bool> out(parameters_.nelements());
348 if (fixedpar_.nelements() == 0) {
[1232]349 return std::vector<bool>();
[108]350 //throw (AipsError("No parameter mask set."));
351 } else {
352 out = fixedpar_;
353 }
354 std::vector<bool> stlout;
355 out.tovector(stlout);
356 return stlout;
[91]357}
358
[890]359float Fitter::getChisquared() const {
[91]360 return chisquared_;
361}
362
[890]363bool Fitter::fit() {
[517]364 NonLinearFitLM<Float> fitter;
365 CompoundFunction<Float> func;
[612]366
367 uInt n = funcs_.nelements();
[517]368 for (uInt i=0; i<n; ++i) {
369 func.addFunction(*funcs_[i]);
370 }
[612]371
[517]372 fitter.setFunction(func);
373 fitter.setMaxIter(50+n*10);
374 // Convergence criterium
375 fitter.setCriteria(0.001);
[612]376
[517]377 // Fit
[2569]378// Vector<Float> sigma(x_.nelements());
379// sigma = 1.0;
[890]380
[517]381 parameters_.resize();
[2569]382// parameters_ = fitter.fit(x_, y_, sigma, &m_);
383 parameters_ = fitter.fit(x_, y_, &m_);
[1067]384 if ( !fitter.converged() ) {
385 return false;
386 }
[517]387 std::vector<float> ps;
388 parameters_.tovector(ps);
389 setParameters(ps);
[612]390
[517]391 error_.resize();
392 error_ = fitter.errors();
[612]393
[517]394 chisquared_ = fitter.getChi2();
[890]395
[2570]396// residual_.resize();
397// residual_ = y_;
398// fitter.residual(residual_,x_);
[517]399 // use fitter.residual(model=True) to get the model
400 thefit_.resize(x_.nelements());
[2570]401 fitter.residual(thefit_,x_,True);
402 // residual = data - model
403 residual_.resize(x_.nelements());
404 residual_ = y_ - thefit_ ;
[517]405 return true;
406}
[483]407
[1391]408bool Fitter::lfit() {
409 LinearFit<Float> fitter;
410 CompoundFunction<Float> func;
[483]411
[1391]412 uInt n = funcs_.nelements();
413 for (uInt i=0; i<n; ++i) {
414 func.addFunction(*funcs_[i]);
415 }
416
417 fitter.setFunction(func);
418 //fitter.setMaxIter(50+n*10);
419 // Convergence criterium
420 //fitter.setCriteria(0.001);
421
422 // Fit
[2569]423// Vector<Float> sigma(x_.nelements());
424// sigma = 1.0;
[1391]425
426 parameters_.resize();
[2569]427// parameters_ = fitter.fit(x_, y_, sigma, &m_);
428 parameters_ = fitter.fit(x_, y_, &m_);
[1391]429 std::vector<float> ps;
430 parameters_.tovector(ps);
431 setParameters(ps);
432
433 error_.resize();
434 error_ = fitter.errors();
435
436 chisquared_ = fitter.getChi2();
437
[2570]438// residual_.resize();
439// residual_ = y_;
440// fitter.residual(residual_,x_);
[1391]441 // use fitter.residual(model=True) to get the model
442 thefit_.resize(x_.nelements());
[2570]443 fitter.residual(thefit_,x_,True);
444 // residual = data - model
445 residual_.resize(x_.nelements());
446 residual_ = y_ - thefit_ ;
[1391]447 return true;
448}
449
[890]450std::vector<float> Fitter::evaluate(int whichComp) const
451{
[517]452 std::vector<float> stlout;
[890]453 uInt idx = uInt(whichComp);
[517]454 Float y;
455 if ( idx < funcs_.nelements() ) {
456 for (uInt i=0; i<x_.nelements(); ++i) {
457 y = (*funcs_[idx])(x_[i]);
458 stlout.push_back(float(y));
459 }
460 }
461 return stlout;
462}
[483]463
[1932]464STFitEntry Fitter::getFitEntry() const
465{
466 STFitEntry fit;
467 fit.setParameters(getParameters());
468 fit.setErrors(getErrors());
469 fit.setComponents(funccomponents_);
470 fit.setFunctions(funcnames_);
471 fit.setParmasks(getFixedParameters());
472 return fit;
473}
Note: See TracBrowser for help on using the repository browser.