Stimfit @PACKAGE_VERSION@
Loading...
Searching...
No Matches
stfnum.h
Go to the documentation of this file.
1// Header file for the stimfit namespace
2// General-purpose routines
3// last revision: 08-08-2006
4// C. Schmidt-Hieber, christsc@gmx.de
5
6// This program is free software; you can redistribute it and/or
7// modify it under the terms of the GNU General Public License
8// as published by the Free Software Foundation; either version 2
9// of the License, or (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
25
26#ifndef _STFNUM_H
27#define _STFNUM_H
28
29#ifdef _WINDOWS
30#pragma warning( disable : 4251 ) // Disable warning messages
31#endif
32
34#if defined(_WINDOWS) && !defined(__MINGW32__)
35 #ifdef STFNUMDLL
36 #define StfnumDll __declspec( dllexport )
37 #else
38 #define StfnumDll __declspec( dllimport )
39 #endif
40#else
41 #define StfnumDll
42#endif
43
44#include <vector>
45#include <complex>
46#include <deque>
47
48#if (__cplusplus < 201103)
49# include <boost/function.hpp>
50#else
51# include <algorithm>
52# include <cassert>
53# include <functional>
54#endif
55
56#ifdef _OPENMP
57#include <omp.h>
58#endif
59#include <fftw3.h>
60
61#ifdef _MSC_VER
62#ifndef INFINITY
63#define INFINITY (DBL_MAX+DBL_MAX)
64#endif
65#ifndef NAN
66 static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
67 #define NAN (*(const float *) __nan)
68#endif
69#endif
70
71#include "../libstfio/stfio.h"
72#include "./spline.h"
73
74namespace stfnum {
75
79
81
85#if (__cplusplus < 201103)
86
87typedef boost::function<double(double, const Vector_double&)> Func;
88
90typedef boost::function<Vector_double(double, const Vector_double&)> Jac;
91
93typedef boost::function<double(double, double, double, double, double)> Scale;
94
95#else
96
97typedef std::function<double(double, const Vector_double&)> Func;
98
100typedef std::function<Vector_double(double, const Vector_double&)> Jac;
101
103typedef std::function<double(double, double, double, double, double)> Scale;
104
105#endif
107Vector_double nojac( double x, const Vector_double& p);
108
110double noscale(double param, double xscale, double xoff, double yscale, double yoff);
111
113
118struct parInfo {
121 : desc(""),toFit(true), constrained(false), constr_lb(0), constr_ub(0), scale(noscale), unscale(noscale) {}
122
124
133 parInfo( const std::string& desc_, bool toFit_, bool constrained_ = false,
134 double constr_lb_ = 0, double constr_ub_ = 0, Scale scale_ = noscale, Scale unscale_ = noscale)
135 : desc(desc_),toFit(toFit_),
136 constrained(constrained_), constr_lb(constr_lb_), constr_ub(constr_ub_),
137 scale(scale_), unscale(unscale_)
138 {}
139
140 std::string desc;
141 bool toFit;
143 double constr_lb;
144 double constr_ub;
147};
148
150
153public:
155
158 Table(std::size_t nRows,std::size_t nCols);
159
161
163 Table(const std::map< std::string, double >& map);
164
166
170 double at(std::size_t row,std::size_t col) const;
171
173
177 double& at(std::size_t row,std::size_t col);
178
180
184 bool IsEmpty(std::size_t row,std::size_t col) const;
185
187
191 void SetEmpty(std::size_t row,std::size_t col,bool value=true);
192
194
197 void SetRowLabel(std::size_t row,const std::string& label);
198
200
203 void SetColLabel(std::size_t col,const std::string& label);
204
206
209 const std::string& GetRowLabel(std::size_t row) const;
210
212
215 const std::string& GetColLabel(std::size_t col) const;
216
218
220 std::size_t nRows() const { return rowLabels.size(); }
221
223
225 std::size_t nCols() const { return colLabels.size(); }
226
228
230 void AppendRows(std::size_t nRows);
231
232private:
233 // row major order:
234 std::vector< std::vector<double> > values;
235 std::vector< std::deque< bool > > empty;
236 std::vector< std::string > rowLabels;
237 std::vector< std::string > colLabels;
238};
239
240#if (__cplusplus < 201103)
242typedef boost::function<Table(const Vector_double&,const std::vector<stfnum::parInfo>,double)> Output;
243
246 const std::vector<parInfo>& parsInfo,
247 double chisqr);
248
250typedef boost::function<void(const Vector_double&, double, double, double, double, double, Vector_double&)> Init;
251#else
253typedef std::function<Table(const Vector_double&,const std::vector<stfnum::parInfo>,double)> Output;
254
257 const std::vector<parInfo>& parsInfo,
258 double chisqr);
259
261typedef std::function<void(const Vector_double&, double, double, double, double, double, Vector_double&)> Init;
262#endif
263
265
271
273
281 storedFunc( const std::string& name_, const std::vector<parInfo>& pInfo_,
282 const Func& func_, const Init& init_, const Jac& jac_, bool hasJac_ = true,
283 const Output& output_ = defaultOutput /*,
284 bool hasId_ = true*/
285 ) : name(name_),pInfo(pInfo_),func(func_),init(init_),jac(jac_),hasJac(hasJac_),output(output_) /*, hasId(hasId_)*/
286 {
287/* if (hasId) {
288 id = NextId();
289 std::string new_name;
290 new_name << id << ": " << name;
291 name = new_name;
292 } else
293 id = 0;
294*/ }
295
298
299// static int n_funcs; /*!< Static function counter */
300// int id; /*!< Function id; set automatically upon construction, so don't touch. */
301 std::string name;
302 std::vector<parInfo> pInfo;
306 bool hasJac;
308// bool hasId; /*!< Determines whether a function should have an id. */
309
310};
311
313
316template <typename T>
317T SQR (T a);
318
320
331 const Vector_double& toFilter,
332 std::size_t filter_start,
333 std::size_t filter_end,
334 const Vector_double &a,
335 int SR,
336 stfnum::Func func,
337 bool inverse = false
338);
339
341
345std::map<double, int>
346histogram(const Vector_double& data, int nbins=-1);
347
349
357deconvolve(const Vector_double& data, const Vector_double& templ,
358 int SR, double hipass, double lopass, stfio::ProgressInfo& progDlg);
359
361
366template <class T>
367std::vector<T>
369 const std::vector<T>& y,
370 T oldF,
371 T newF
372);
373
375/* \param input The valarray to be differentiated.
376 * \param x_scale The sampling interval.
377 * \return The result of the differentiation.
378 */
379template <class T>
380std::vector<T> diff(const std::vector<T>& input, T x_scale);
381
383
391 const Vector_double& input,
392 std::size_t a,
393 std::size_t b,
394 double x_scale
395);
396
398
406 const Vector_double& input,
407 std::size_t a,
408 std::size_t b,
409 double x_scale
410);
411
413
425int
427 int m,
428 int n,
429 int nrhs,
430 Vector_double& A,
432);
433
435
441quad(const Vector_double& data, std::size_t begin, std::size_t end);
442
443
445
451 const Vector_double& data,
452 const Vector_double& templ,
453 stfio::ProgressInfo& progDlg
454);
455
456// TODO: Add negative-going peaks.
458
463StfnumDll std::vector<int> peakIndices(const Vector_double& data, double threshold, int minDistance);
464
466
471
473
482double fgaussColqu(double x, const Vector_double& p);
483
485
492double fboltz(double x, const Vector_double& p);
493
495
502double fbessel(double x, int n);
503
505
515double fbessel4(double x, const Vector_double& p);
516
518
521int fac(int arg);
522
524
527int pow2(int arg);
528
536
542
544
545}
546
547typedef std::vector< stfnum::storedFunc >::const_iterator c_stfunc_it;
548
549inline int stfnum::pow2(int arg) {return 1<<arg;}
550
552
555template <typename T>
556void SWAP(T s1, T s2) {
557 T aux=s1;
558 s1=s2;
559 s2=aux;
560}
561
562template <class T>
563std::vector<T>
564stfnum::cubicSpline(const std::vector<T>& y,
565 T oldF,
566 T newF)
567{
568 double factor_i=newF/oldF;
569 int size=(int)y.size();
570 // size of interpolated data:
571 int size_i=(int)(size*factor_i);
573 Vector_double y_d(size);
574 for (int n_p=0; n_p < size; ++n_p) {
575 x[n_p]=n_p;
576 y_d[n_p]=y[n_p];
577 }
578 Vector_double y_i(stfnum::spline_cubic_set(x,y_d,0,0,0,0));
579
580 std::vector<T> y_if(size_i);
581 Vector_double x_i(size_i);
582
583 //Cubic spline interpolation:
584 for (int n_i=0; n_i < size_i; ++n_i) {
585 x_i[n_i]=(double)n_i * (double)size/(double)size_i;
586 double yp, ypp;
587 y_if[n_i]=(T)stfnum::spline_cubic_val(x,x_i[n_i],y_d,y_i,yp,ypp);
588 }
589 return y_if;
590}
591
592template <class T>
593std::vector<T> stfnum::diff(const std::vector<T>& input, T x_scale) {
594 std::vector<T> diffVA(input.size()-1);
595 for (unsigned n=0;n<diffVA.size();++n) {
596 diffVA[n]=(input[n+1]-input[n])/x_scale;
597 }
598 return diffVA;
599}
600
601template <typename T>
602inline T stfnum::SQR(T a) {return a*a;}
603
604#endif
WORD TpMarker int n
Definition Son.h:353
WORD TSTime TpMarker WORD size
Definition Son.h:337
ProgressInfo class.
Definition stfio.h:69
A table used for printing information.
Definition stfnum.h:152
std::vector< double > Vector_double
Definition core.h:55
Vector_double nojac(double x, const Vector_double &p)
Dummy function, serves as a placeholder to initialize functions without a Jacobian.
StfnumDll double fbessel4(double x, const Vector_double &p)
Computes a 4th-order Bessel polynomial that can be used as a filter kernel.
int linsolv(int m, int n, int nrhs, Vector_double &A, Vector_double &B)
Solves a linear equation system using LAPACK.
~storedFunc()
Destructor.
Definition stfnum.h:297
std::size_t nRows() const
Retrieves the number of rows.
Definition stfnum.h:220
bool constrained
Definition stfnum.h:142
Table(const std::map< std::string, double > &map)
Constructor.
StfnumDll double integrate_simpson(const Vector_double &input, std::size_t a, std::size_t b, double x_scale)
Integration using Simpson's rule.
direction
The direction of peak calculations.
Definition stfnum.h:530
double & at(std::size_t row, std::size_t col)
Range-checked access. Returns a reference. Throws std::out_of_range if out of range.
std::map< double, int > histogram(const Vector_double &data, int nbins=-1)
Computes a histogram.
int pow2(int arg)
Computes . Uses the bitwise-shift operator (<<).
Definition stfnum.h:549
bool IsEmpty(std::size_t row, std::size_t col) const
Check whether a cell is empty.
std::string name
Definition stfnum.h:301
boost::function< void(const Vector_double &, double, double, double, double, double, Vector_double &)> Init
Initialising function for the parameters in stfnum::Func to start a fit.
Definition stfnum.h:250
StfnumDll Vector_double quad(const Vector_double &data, std::size_t begin, std::size_t end)
Solve quadratic equations for 3 adjacent sampling points.
std::vector< T > diff(const std::vector< T > &input, T x_scale)
Differentiate data.
Definition stfnum.h:593
StfnumDll std::vector< int > peakIndices(const Vector_double &data, double threshold, int minDistance)
Searches for positive-going peaks.
StfnumDll Vector_double linCorr(const Vector_double &va1, const Vector_double &va2, stfio::ProgressInfo &progDlg)
Computes the linear correlation between two arrays.
boost::function< Vector_double(double, const Vector_double &)> Jac
The jacobian of a stfnum::Func.
Definition stfnum.h:90
StfnumDll Vector_double filter(const Vector_double &toFilter, std::size_t filter_start, std::size_t filter_end, const Vector_double &a, int SR, stfnum::Func func, bool inverse=false)
Convolves a data set with a filter function.
StfnumDll double fgaussColqu(double x, const Vector_double &p)
Computes a Gaussian that can be used as a filter kernel.
parInfo()
Default constructor.
Definition stfnum.h:120
Scale unscale
Definition stfnum.h:146
boost::function< double(double, const Vector_double &)> Func
A function taking a double and a vector and returning a double.
Definition stfnum.h:87
boost::function< Table(const Vector_double &, const std::vector< stfnum::parInfo >, double)> Output
Print the output of a fit into a stfnum::Table.
Definition stfnum.h:242
Table defaultOutput(const Vector_double &pars, const std::vector< parInfo > &parsInfo, double chisqr)
Default fit output function, constructing a stfnum::Table from the parameters, their description and ...
const std::string & GetRowLabel(std::size_t row) const
Retrieves the label of a row.
StfnumDll double integrate_trapezium(const Vector_double &input, std::size_t a, std::size_t b, double x_scale)
Integration using the trapezium rule.
StfnumDll double threshold(const std::vector< double > &data, std::size_t llp, std::size_t ulp, double slope, double &thrT, std::size_t windowLength)
Find the value within data between llp and ulp at which slope is exceeded.
parInfo(const std::string &desc_, bool toFit_, bool constrained_=false, double constr_lb_=0, double constr_ub_=0, Scale scale_=noscale, Scale unscale_=noscale)
Constructor.
Definition stfnum.h:133
void AppendRows(std::size_t nRows)
Appends rows to the table.
std::vector< parInfo > pInfo
Definition stfnum.h:302
StfnumDll Vector_double deconvolve(const Vector_double &data, const Vector_double &templ, int SR, double hipass, double lopass, stfio::ProgressInfo &progDlg)
Deconvolves a template from a signal.
double constr_ub
Definition stfnum.h:144
T SQR(T a)
Calculates the square of a number.
Definition stfnum.h:602
void SetRowLabel(std::size_t row, const std::string &label)
Sets the label of a row.
baseline_method
Methods for Baseline computation.
Definition stfnum.h:538
void SetColLabel(std::size_t col, const std::string &label)
Sets the label of a column.
const std::string & GetColLabel(std::size_t col) const
Retrieves the label of a column.
std::string desc
Definition stfnum.h:140
double at(std::size_t row, std::size_t col) const
Range-checked access. Returns a copy. Throws std::out_of_range if out of range.
StfnumDll Vector_double detectionCriterion(const Vector_double &data, const Vector_double &templ, stfio::ProgressInfo &progDlg)
Computes the event detection criterion according to Clements & Bekkers (1997).
double constr_lb
Definition stfnum.h:143
double xscale(double param, double xscale, double xoff, double yscale, double yoff)
Scales a parameter that linearly depends on x.
std::vector< T > cubicSpline(const std::vector< T > &y, T oldF, T newF)
Interpolates a dataset using cubic splines.
Definition stfnum.h:564
double noscale(double param, double xscale, double xoff, double yscale, double yoff)
Dummy function, serves as a placeholder to initialize parameters without a scaling function.
storedFunc(const std::string &name_, const std::vector< parInfo > &pInfo_, const Func &func_, const Init &init_, const Jac &jac_, bool hasJac_=true, const Output &output_=defaultOutput)
Constructor.
Definition stfnum.h:281
double fboltz(double x, const Vector_double &p)
Computes a Boltzmann function.
Table(std::size_t nRows, std::size_t nCols)
Constructor.
std::size_t nCols() const
Retrieves the number of columns.
Definition stfnum.h:225
double fbessel(double x, int n)
Computes a Bessel polynomial.
int fac(int arg)
Computes the faculty of an integer.
boost::function< double(double, double, double, double, double)> Scale
Scaling function for fit parameters.
Definition stfnum.h:93
double yscale(double param, double xscale, double xoff, double yscale, double yoff)
Scales a parameter that linearly depends on y.
void SetEmpty(std::size_t row, std::size_t col, bool value=true)
Empties or un-empties a cell.
@ down
Definition stfnum.h:532
@ both
Definition stfnum.h:533
@ undefined_direction
Definition stfnum.h:534
@ up
Definition stfnum.h:531
@ median_iqr
Definition stfnum.h:540
@ mean_sd
Definition stfnum.h:539
Definition fit.h:32
double spline_cubic_val(const Vector_double &t, double tval, const Vector_double &y, const Vector_double &ypp, double &ypval, double &yppval)
Vector_double spline_cubic_set(const Vector_double &t, const Vector_double &y, int ibcbeg, double ybcbeg, int ibcend, double ybcend)
Cubic spline interpolation.
header file for libstfio
void SWAP(T s1, T s2)
Swaps s1 and s2.
Definition stfnum.h:556
std::vector< stfnum::storedFunc >::const_iterator c_stfunc_it
Definition stfnum.h:547
#define StfnumDll
Defines dll export or import functions for libstfnum on Windows.
Definition stfnum.h:38