[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 1228 2006-08-31 07:09:19Z mar637 $ |
---|
[91] | 30 | //#--------------------------------------------------------------------------- |
---|
[125] | 31 | #include <casa/aips.h> |
---|
[91] | 32 | #include <casa/Arrays/ArrayMath.h> |
---|
| 33 | #include <casa/Arrays/ArrayLogical.h> |
---|
| 34 | #include <scimath/Fitting.h> |
---|
| 35 | #include <scimath/Fitting/LinearFit.h> |
---|
| 36 | #include <scimath/Functionals/CompiledFunction.h> |
---|
| 37 | #include <scimath/Functionals/CompoundFunction.h> |
---|
| 38 | #include <scimath/Functionals/Gaussian1D.h> |
---|
| 39 | #include <scimath/Functionals/Polynomial.h> |
---|
| 40 | #include <scimath/Mathematics/AutoDiff.h> |
---|
| 41 | #include <scimath/Mathematics/AutoDiffMath.h> |
---|
| 42 | #include <scimath/Fitting/NonLinearFitLM.h> |
---|
| 43 | #include <components/SpectralComponents/SpectralEstimate.h> |
---|
| 44 | |
---|
[894] | 45 | #include "STFitter.h" |
---|
| 46 | |
---|
[91] | 47 | using namespace asap; |
---|
[125] | 48 | using namespace casa; |
---|
[91] | 49 | |
---|
[890] | 50 | Fitter::Fitter() |
---|
[91] | 51 | { |
---|
| 52 | } |
---|
| 53 | |
---|
[890] | 54 | Fitter::~Fitter() |
---|
[91] | 55 | { |
---|
[517] | 56 | reset(); |
---|
[91] | 57 | } |
---|
| 58 | |
---|
[890] | 59 | void Fitter::clear() |
---|
[91] | 60 | { |
---|
[517] | 61 | for (uInt i=0;i< funcs_.nelements();++i) { |
---|
| 62 | delete funcs_[i]; funcs_[i] = 0; |
---|
| 63 | } |
---|
[612] | 64 | funcs_.resize(0,True); |
---|
[517] | 65 | parameters_.resize(); |
---|
[1228] | 66 | fixedpar_.resize(); |
---|
[517] | 67 | error_.resize(); |
---|
| 68 | thefit_.resize(); |
---|
| 69 | estimate_.resize(); |
---|
| 70 | chisquared_ = 0.0; |
---|
[91] | 71 | } |
---|
[517] | 72 | |
---|
[890] | 73 | void Fitter::reset() |
---|
[91] | 74 | { |
---|
[517] | 75 | clear(); |
---|
| 76 | x_.resize(); |
---|
| 77 | y_.resize(); |
---|
| 78 | m_.resize(); |
---|
[91] | 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
[890] | 82 | bool Fitter::computeEstimate() { |
---|
[517] | 83 | if (x_.nelements() == 0 || y_.nelements() == 0) |
---|
| 84 | throw (AipsError("No x/y data specified.")); |
---|
[91] | 85 | |
---|
[517] | 86 | if (dynamic_cast<Gaussian1D<Float>* >(funcs_[0]) == 0) |
---|
| 87 | return false; |
---|
| 88 | uInt n = funcs_.nelements(); |
---|
| 89 | SpectralEstimate estimator(n); |
---|
| 90 | estimator.setQ(5); |
---|
| 91 | Int mn,mx; |
---|
| 92 | mn = 0; |
---|
| 93 | mx = m_.nelements()-1; |
---|
| 94 | for (uInt i=0; i<m_.nelements();++i) { |
---|
| 95 | if (m_[i]) { |
---|
| 96 | mn = i; |
---|
| 97 | break; |
---|
[108] | 98 | } |
---|
[517] | 99 | } |
---|
| 100 | for (uInt j=m_.nelements()-1; j>=0;--j) { |
---|
| 101 | if (m_[j]) { |
---|
| 102 | mx = j; |
---|
| 103 | break; |
---|
[108] | 104 | } |
---|
[517] | 105 | } |
---|
[1067] | 106 | //mn = 0+x_.nelements()/10; |
---|
| 107 | //mx = x_.nelements()-x_.nelements()/10; |
---|
[517] | 108 | estimator.setRegion(mn,mx); |
---|
| 109 | //estimator.setWindowing(True); |
---|
| 110 | SpectralList listGauss = estimator.estimate(x_, y_); |
---|
| 111 | parameters_.resize(n*3); |
---|
| 112 | Gaussian1D<Float>* g = 0; |
---|
| 113 | for (uInt i=0; i<n;i++) { |
---|
| 114 | g = dynamic_cast<Gaussian1D<Float>* >(funcs_[i]); |
---|
| 115 | if (g) { |
---|
| 116 | (*g)[0] = listGauss[i].getAmpl(); |
---|
| 117 | (*g)[1] = listGauss[i].getCenter(); |
---|
| 118 | (*g)[2] = listGauss[i].getFWHM(); |
---|
[91] | 119 | } |
---|
[517] | 120 | } |
---|
| 121 | estimate_.resize(); |
---|
| 122 | listGauss.evaluate(estimate_,x_); |
---|
| 123 | return true; |
---|
[91] | 124 | } |
---|
| 125 | |
---|
[890] | 126 | std::vector<float> Fitter::getEstimate() const |
---|
[91] | 127 | { |
---|
[517] | 128 | if (estimate_.nelements() == 0) |
---|
| 129 | throw (AipsError("No estimate set.")); |
---|
| 130 | std::vector<float> stlout; |
---|
| 131 | estimate_.tovector(stlout); |
---|
| 132 | return stlout; |
---|
[91] | 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
[890] | 136 | bool Fitter::setExpression(const std::string& expr, int ncomp) |
---|
[91] | 137 | { |
---|
[517] | 138 | clear(); |
---|
| 139 | if (expr == "gauss") { |
---|
| 140 | if (ncomp < 1) throw (AipsError("Need at least one gaussian to fit.")); |
---|
| 141 | funcs_.resize(ncomp); |
---|
| 142 | for (Int k=0; k<ncomp; ++k) { |
---|
| 143 | funcs_[k] = new Gaussian1D<Float>(); |
---|
| 144 | } |
---|
| 145 | } else if (expr == "poly") { |
---|
| 146 | funcs_.resize(1); |
---|
| 147 | funcs_[0] = new Polynomial<Float>(ncomp); |
---|
| 148 | } else { |
---|
| 149 | cerr << " compiled functions not yet implemented" << endl; |
---|
| 150 | //funcs_.resize(1); |
---|
| 151 | //funcs_[0] = new CompiledFunction<Float>(); |
---|
| 152 | //funcs_[0]->setFunction(String(expr)); |
---|
| 153 | return false; |
---|
| 154 | } |
---|
| 155 | return true; |
---|
[91] | 156 | } |
---|
| 157 | |
---|
[890] | 158 | bool Fitter::setData(std::vector<float> absc, std::vector<float> spec, |
---|
[91] | 159 | std::vector<bool> mask) |
---|
| 160 | { |
---|
| 161 | x_.resize(); |
---|
| 162 | y_.resize(); |
---|
| 163 | m_.resize(); |
---|
| 164 | // convert std::vector to casa Vector |
---|
| 165 | Vector<Float> tmpx(absc); |
---|
| 166 | Vector<Float> tmpy(spec); |
---|
| 167 | Vector<Bool> tmpm(mask); |
---|
| 168 | AlwaysAssert(tmpx.nelements() == tmpy.nelements(), AipsError); |
---|
| 169 | x_ = tmpx; |
---|
| 170 | y_ = tmpy; |
---|
| 171 | m_ = tmpm; |
---|
| 172 | return true; |
---|
| 173 | } |
---|
| 174 | |
---|
[890] | 175 | std::vector<float> Fitter::getResidual() const |
---|
[91] | 176 | { |
---|
| 177 | if (residual_.nelements() == 0) |
---|
| 178 | throw (AipsError("Function not yet fitted.")); |
---|
| 179 | std::vector<float> stlout; |
---|
| 180 | residual_.tovector(stlout); |
---|
| 181 | return stlout; |
---|
| 182 | } |
---|
| 183 | |
---|
[890] | 184 | std::vector<float> Fitter::getFit() const |
---|
[91] | 185 | { |
---|
| 186 | Vector<Float> out = thefit_; |
---|
| 187 | std::vector<float> stlout; |
---|
| 188 | out.tovector(stlout); |
---|
| 189 | return stlout; |
---|
| 190 | |
---|
| 191 | } |
---|
| 192 | |
---|
[890] | 193 | std::vector<float> Fitter::getErrors() const |
---|
[91] | 194 | { |
---|
| 195 | Vector<Float> out = error_; |
---|
| 196 | std::vector<float> stlout; |
---|
| 197 | out.tovector(stlout); |
---|
| 198 | return stlout; |
---|
| 199 | } |
---|
| 200 | |
---|
[890] | 201 | bool Fitter::setParameters(std::vector<float> params) |
---|
[91] | 202 | { |
---|
| 203 | Vector<Float> tmppar(params); |
---|
| 204 | if (funcs_.nelements() == 0) |
---|
| 205 | throw (AipsError("Function not yet set.")); |
---|
| 206 | if (parameters_.nelements() > 0 && tmppar.nelements() != parameters_.nelements()) |
---|
| 207 | throw (AipsError("Number of parameters inconsistent with function.")); |
---|
[1221] | 208 | if (parameters_.nelements() == 0) { |
---|
[91] | 209 | parameters_.resize(tmppar.nelements()); |
---|
[1221] | 210 | if (tmppar.nelements() != fixedpar_.nelements()) { |
---|
[1228] | 211 | fixedpar_.resize(tmppar.nelements()); |
---|
| 212 | fixedpar_ = False; |
---|
| 213 | } |
---|
[1221] | 214 | } |
---|
[91] | 215 | if (dynamic_cast<Gaussian1D<Float>* >(funcs_[0]) != 0) { |
---|
| 216 | uInt count = 0; |
---|
| 217 | for (uInt j=0; j < funcs_.nelements(); ++j) { |
---|
| 218 | for (uInt i=0; i < funcs_[j]->nparameters(); ++i) { |
---|
| 219 | (funcs_[j]->parameters())[i] = tmppar[count]; |
---|
| 220 | parameters_[count] = tmppar[count]; |
---|
| 221 | ++count; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } else if (dynamic_cast<Polynomial<Float>* >(funcs_[0]) != 0) { |
---|
| 225 | for (uInt i=0; i < funcs_[0]->nparameters(); ++i) { |
---|
| 226 | parameters_[i] = tmppar[i]; |
---|
| 227 | (funcs_[0]->parameters())[i] = tmppar[i]; |
---|
| 228 | } |
---|
| 229 | } |
---|
[1228] | 230 | // reset |
---|
| 231 | if (params.size() == 0) { |
---|
| 232 | parameters_.resize(); |
---|
| 233 | fixedpar_.resize(); |
---|
| 234 | } |
---|
[91] | 235 | return true; |
---|
| 236 | } |
---|
| 237 | |
---|
[890] | 238 | bool Fitter::setFixedParameters(std::vector<bool> fixed) |
---|
[91] | 239 | { |
---|
| 240 | if (funcs_.nelements() == 0) |
---|
| 241 | throw (AipsError("Function not yet set.")); |
---|
[1221] | 242 | if (fixedpar_.nelements() > 0 && fixed.size() != fixedpar_.nelements()) |
---|
[91] | 243 | throw (AipsError("Number of mask elements inconsistent with function.")); |
---|
[1228] | 244 | if (fixedpar_.nelements() == 0) { |
---|
| 245 | fixedpar_.resize(parameters_.nelements()); |
---|
| 246 | fixedpar_ = False; |
---|
| 247 | } |
---|
[91] | 248 | if (dynamic_cast<Gaussian1D<Float>* >(funcs_[0]) != 0) { |
---|
| 249 | uInt count = 0; |
---|
| 250 | for (uInt j=0; j < funcs_.nelements(); ++j) { |
---|
| 251 | for (uInt i=0; i < funcs_[j]->nparameters(); ++i) { |
---|
[1221] | 252 | funcs_[j]->mask(i) = !fixed[count]; |
---|
| 253 | fixedpar_[count] = fixed[count]; |
---|
[91] | 254 | ++count; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } else if (dynamic_cast<Polynomial<Float>* >(funcs_[0]) != 0) { |
---|
| 258 | for (uInt i=0; i < funcs_[0]->nparameters(); ++i) { |
---|
[1221] | 259 | fixedpar_[i] = fixed[i]; |
---|
| 260 | funcs_[0]->mask(i) = !fixed[i]; |
---|
[91] | 261 | } |
---|
| 262 | } |
---|
| 263 | return true; |
---|
| 264 | } |
---|
| 265 | |
---|
[890] | 266 | std::vector<float> Fitter::getParameters() const { |
---|
[91] | 267 | Vector<Float> out = parameters_; |
---|
| 268 | std::vector<float> stlout; |
---|
| 269 | out.tovector(stlout); |
---|
| 270 | return stlout; |
---|
| 271 | } |
---|
| 272 | |
---|
[890] | 273 | std::vector<bool> Fitter::getFixedParameters() const { |
---|
[108] | 274 | Vector<Bool> out(parameters_.nelements()); |
---|
| 275 | if (fixedpar_.nelements() == 0) { |
---|
[1228] | 276 | return std::vector<bool>(); |
---|
[108] | 277 | //throw (AipsError("No parameter mask set.")); |
---|
| 278 | } else { |
---|
| 279 | out = fixedpar_; |
---|
| 280 | } |
---|
| 281 | std::vector<bool> stlout; |
---|
| 282 | out.tovector(stlout); |
---|
| 283 | return stlout; |
---|
[91] | 284 | } |
---|
| 285 | |
---|
[890] | 286 | float Fitter::getChisquared() const { |
---|
[91] | 287 | return chisquared_; |
---|
| 288 | } |
---|
| 289 | |
---|
[890] | 290 | bool Fitter::fit() { |
---|
[517] | 291 | NonLinearFitLM<Float> fitter; |
---|
| 292 | CompoundFunction<Float> func; |
---|
[612] | 293 | |
---|
| 294 | uInt n = funcs_.nelements(); |
---|
[517] | 295 | for (uInt i=0; i<n; ++i) { |
---|
| 296 | func.addFunction(*funcs_[i]); |
---|
| 297 | } |
---|
[612] | 298 | |
---|
[517] | 299 | fitter.setFunction(func); |
---|
| 300 | fitter.setMaxIter(50+n*10); |
---|
| 301 | // Convergence criterium |
---|
| 302 | fitter.setCriteria(0.001); |
---|
[612] | 303 | |
---|
[517] | 304 | // Fit |
---|
| 305 | Vector<Float> sigma(x_.nelements()); |
---|
| 306 | sigma = 1.0; |
---|
[890] | 307 | |
---|
[517] | 308 | parameters_.resize(); |
---|
| 309 | parameters_ = fitter.fit(x_, y_, sigma, &m_); |
---|
[1067] | 310 | if ( !fitter.converged() ) { |
---|
| 311 | return false; |
---|
| 312 | } |
---|
[517] | 313 | std::vector<float> ps; |
---|
| 314 | parameters_.tovector(ps); |
---|
| 315 | setParameters(ps); |
---|
[612] | 316 | |
---|
[517] | 317 | error_.resize(); |
---|
| 318 | error_ = fitter.errors(); |
---|
[612] | 319 | |
---|
[517] | 320 | chisquared_ = fitter.getChi2(); |
---|
[890] | 321 | |
---|
[517] | 322 | residual_.resize(); |
---|
| 323 | residual_ = y_; |
---|
| 324 | fitter.residual(residual_,x_); |
---|
| 325 | // use fitter.residual(model=True) to get the model |
---|
| 326 | thefit_.resize(x_.nelements()); |
---|
| 327 | fitter.residual(thefit_,x_,True); |
---|
| 328 | return true; |
---|
| 329 | } |
---|
[483] | 330 | |
---|
| 331 | |
---|
[890] | 332 | std::vector<float> Fitter::evaluate(int whichComp) const |
---|
| 333 | { |
---|
[517] | 334 | std::vector<float> stlout; |
---|
[890] | 335 | uInt idx = uInt(whichComp); |
---|
[517] | 336 | Float y; |
---|
| 337 | if ( idx < funcs_.nelements() ) { |
---|
| 338 | for (uInt i=0; i<x_.nelements(); ++i) { |
---|
| 339 | y = (*funcs_[idx])(x_[i]); |
---|
| 340 | stlout.push_back(float(y)); |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | return stlout; |
---|
| 344 | } |
---|
[483] | 345 | |
---|