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