Xmipp  v3.23.11-Nereus
xmipp_funcs.h
Go to the documentation of this file.
1 /***************************************************************************
2 *
3 * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4 *
5 * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * 02111-1307 USA
21 *
22 * All comments concerning this program package may be sent to the
23 * e-mail address 'xmipp@cnb.csic.es'
24 ***************************************************************************/
25 
26 #ifndef CORE_XMIPP_FUNCS_H
27 #define CORE_XMIPP_FUNCS_H
28 
29 #include <complex>
30 #include "xmipp_macros.h"
31 #include <vector>
32 
33 class FileName;
34 
35 // For timing functions
36 // Uncomment next line timing functions are giving problems in your system
37 //#define _NO_TIME
38 #ifndef _NO_TIME
39 #include <unistd.h>
40 #ifndef __MINGW32__
41 #include <sys/times.h>
42 #endif
43 #ifdef _IRIX65
44 #include <sys/types.h>
45 #include <time.h>
46 #endif
47 #endif
48 
49 
50 
53 
54 
56 
68 class Tabsinc
69 {
70 public:
71 
72  double sampl;
73  double isampl;
74  int xmax;
75  int no_elem;
76  double* tabulatedsinc;
77 
78 public:
80  Tabsinc(const double dd, const int xx)
81  {
82  sampl = dd;
83  isampl = 1.0/sampl;
84  xmax = xx;
85  filltable();
86  }
87 
88  // Destructor
89  virtual ~Tabsinc()
90  {
91  delete tabulatedsinc;
92  }
93 
94 #define TSINCVALUE(Tsinc, x,y) \
95  { \
96  int TSINCVALUEaux=(int)(x * Tsinc.isampl); \
97  y=Tsinc.tabulatedsinc[ABS(TSINCVALUEaux)]; \
98  }
99 
100 
102  double operator()(double val) const
103  {
104  int aux=(int)(val * isampl);
105  return tabulatedsinc[ABS(aux)];
106  }
107 
109  void filltable()
110  {
111  no_elem = (int)(xmax / sampl);
112  tabulatedsinc = new double[no_elem];
113  tabulatedsinc[0] = 1;
114  for (int i = 1; i < no_elem; i++)
115  {
116  double xx = (double) i * sampl * PI;
117  tabulatedsinc[i] = sin(xx) / xx;
118  }
119  }
120 };
121 
150 {
151 protected:
152  double alpha, v, r;
153  int N;
154  int K;
155  double vtable;
156  int ntable;
157  std::vector<double> i0table;
158  double dtable;
159  double alphar;
160  double fac;
161  double vadjust;
162  double facadj;
163  void build_I0table();
164  double fltb;
165 
166 public:
168  KaiserBessel(double alpha_, int K, double r_,
169  double v_, int N_, double vtable_=0.,
170  int ntable_ = 5999);
171 
173  double I0table_maxerror();
174  std::vector<double> dump_table() const
175  {
176  return i0table;
177  }
178 
180  double sinhwin(double x) const;
181 
183  double i0win(double x) const;
184 
186  inline double i0win_tab(double x) const
187  {
188  double xt;
189  if(x<0.)
190  xt = -x*fltb+0.5;
191  else
192  xt = x*fltb+0.5;
193  return i0table[ (int) xt];
194  }
195 
197  int get_window_size() const
198  {
199  return K;
200  }
201 };
202 
203 #if defined(__APPLE__) || defined(__MINGW32__)
204 
207 void sin_cos(double angle, double * sine, double * cosine);
208 #endif
209 
219 int solve_2nd_degree_eq(double a,
220  double b,
221  double c,
222  double& x1,
223  double& x2,
224  double prec = XMIPP_EQUAL_ACCURACY);
225 
231 double gaussian1D(double x, double sigma, double mu = 0);
232 
238 double tstudent1D(double x, double df, double sigma, double mu = 0);
239 
247 double icdf_gauss(double p);
248 
254 double cdf_gauss(double x);
255 
262 double cdf_tstudent(int k, double t);
263 
270 double cdf_FSnedecor(int d1, int d2, double x);
271 
279 double icdf_FSnedecor(int d1, int d2, double p);
280 
288 double gaussian2D(double x,
289  double y,
290  double sigmaX,
291  double sigmaY,
292  double ang,
293  double muX = 0,
294  double muY = 0);
296 
298 
299 
308 size_t divide_equally(size_t N, size_t size, size_t rank, size_t &first, size_t &last);
309 
312 size_t divide_equally_group(size_t N, size_t size, size_t myself);
313 
316 template <class T>
317 void computeStats(const std::vector<T> &V, double& avg, double& stddev,
318  T& minval, T& maxval)
319 {
320  if (V.size()<= 0)
321  return;
322 
323  avg = 0;
324  stddev = 0;
325 
326  minval = maxval = V[0];
327 
328  size_t nmax=V.size();
329  const T* ptr=&V[0];
330  for(size_t n=0; n<nmax; ++n, ++ptr)
331  {
332  double val=*ptr;
333  avg += val;
334  stddev += val * val;
335 
336  if (val > maxval)
337  maxval = val;
338  else if (val < minval)
339  minval = val;
340  }
341  avg /= nmax;
342 
343  if (nmax > 1)
344  {
345  stddev = stddev / nmax - avg * avg;
346  stddev *= nmax / (nmax - 1);
347 
348  // Foreseeing numerical instabilities
349  stddev = sqrt(static_cast< double >(ABS(stddev)));
350  }
351  else
352  stddev = 0;
353 }
354 
357 template <class T>
358 void computeAvgStddev(const std::vector<T> &V, double& avg, double& stddev)
359 {
360  if (V.size()<= 0)
361  return;
362 
363  avg = 0;
364  stddev = 0;
365 
366  size_t nmax=V.size();
367  const T* ptr=&V[0];
368  for(size_t n=0; n<nmax; ++n, ++ptr)
369  {
370  double val=*ptr;
371  avg += val;
372  stddev += val * val;
373  }
374  avg /= nmax;
375 
376  if (nmax > 1)
377  {
378  stddev = stddev / nmax - avg * avg;
379  stddev *= nmax / (nmax - 1);
380 
381  // Foreseeing numerical instabilities
382  stddev = sqrt(static_cast< double >(ABS(stddev)));
383  }
384  else
385  stddev = 0;
386 }
387 
389 template <class T>
390 void initConstant(std::vector<T> &V, T &value)
391 {
392  const T* ptr=&V[0];
393  size_t nmax=V.size();
394  for(size_t n=0; n<nmax; ++n, ++ptr)
395  *ptr=value;
396 }
398 
400 
401 
407 template <typename T>
408 void RealImag2Complex(const T* _real,
409  const T* _imag,
410  std::complex< double >* _complex,
411  int length)
412 {
413  T* aux_real = (T*) _real;
414  T* aux_imag = (T*) _imag;
415  double* aux_complex = (double*) _complex;
416 
417  for (int i = 0; i < length; i++)
418  {
419  *aux_complex++ = (double)(*aux_real++);
420  *aux_complex++ = (double)(*aux_imag++);
421  }
422 }
423 
430 template <typename T>
431 void AmplPhase2Complex(const T* _ampl,
432  const T* _phase,
433  std::complex< double >* _complex,
434  int length)
435 {
436  T* aux_ampl = (T*) _ampl;
437  T* aux_phase = (T*) _phase;
438  double* aux_complex = (double*) _complex;
439 
440  for (int i = 0; i < length; i++)
441  {
442  double ampl = (double)(*aux_ampl++);
443  double phase = (double)(*aux_phase++);
444  *aux_complex++ = ampl * cos(phase);
445  *aux_complex++ = ampl * sin(phase);
446  }
447 }
448 
455 template <typename T>
456 void Complex2RealImag(const std::complex< double >* _complex,
457  T* _real,
458  T* _imag,
459  int length)
460 {
461  T* aux_real = (T*) _real;
462  T* aux_imag = (T*) _imag;
463  double* aux_complex = (double*) _complex;
464 
465  for (int i = 0; i < length; i++)
466  {
467  *aux_real++ = (T)(*aux_complex++);
468  *aux_imag++ = (T)(*aux_complex++);
469  }
470 }
471 
478 template <typename T>
479 void Complex2AmplPhase(const std::complex< double >* _complex,
480  T* _ampl,
481  T* _phase,
482  int length)
483 {
484  T* aux_ampl = (T*) _ampl;
485  T* aux_phase = (T*) _phase;
486  double* aux_complex = (double*) _complex;
487 
488  for (int i = 0; i < length; i++)
489  {
490  double re = *aux_complex++;
491  double im = *aux_complex++;
492  *aux_ampl++ = sqrt(re * re + im * im);
493  *aux_phase++ = atan2(im, re);
494  }
495 }
497 
529 void init_random_generator(int seed = -1);
530 
538 unsigned int randomize_random_generator();
539 
547 double rnd_unif();
548 
556 double rnd_unif(double a, double b);
557 
565 double rnd_student_t(double nu);
566 
574 double rnd_student_t(double nu, double a, double b);
575 
583 double rnd_gaus();
584 
592 double rnd_gaus(double a, double b);
593 
599 double gaus_within_x0(double x0, double mean = 0, double stddev = 1);
600 
606 double gaus_outside_x0(double x0, double mean = 0, double stddev = 1);
607 
613 double gaus_up_to_x0(double x0, double mean = 0, double stddev = 1);
614 
620 double gaus_from_x0(double x0, double mean = 0, double stddev = 1);
621 
627 double student_outside_probb(double p, double degrees_of_freedom);
628 
634 double student_within_t0(double t0, double degrees_of_freedom);
635 
641 double student_outside_t0(double t0, double degrees_of_freedom);
642 
648 double student_up_to_t0(double t0, double degrees_of_freedom);
649 
655 double student_from_t0(double t0, double degrees_of_freedom);
656 
662 double chi2_up_to_t0(double t0, double degrees_of_freedom);
663 
669 double chi2_from_t0(double t0, double degrees_of_freedom);
670 
680 double rnd_log(double a, double b);
682 
817 #if defined _NO_TIME || defined __MINGW32__
818 typedef int ProcessorTimeStamp; // Any other kind of data will do
819 typedef int TimeStamp; // Any other kind of data will do
820 struct tm* localtime_r (const time_t *clock, struct tm *result);
821 #else
822 typedef struct tms ProcessorTimeStamp; // Renaming of the time structure
823 typedef size_t TimeStamp; // Timestamp in miliseconds
824 #endif
825 
837 void time_config();
838 
839 #if !defined _NO_TIME && !defined __MINGW32__
840 
852 #endif
853 
865 void annotate_time(TimeStamp* time);
866 
867 #if !defined _NO_TIME && !defined __MINGW32__
868 
878 #endif
879 
901 double elapsed_time(ProcessorTimeStamp& time, bool _IN_SECS = true);
902 
903 #if !defined _NO_TIME && !defined __MINGW32__
904 
922 void print_elapsed_time(ProcessorTimeStamp& time, bool _IN_SECS = true);
923 #endif
924 
943 void print_elapsed_time(TimeStamp& time, bool _IN_SECS = true);
944 
948 class Timer
949 {
950 private:
951  struct timeval tv;
952  struct tm tm;
953  size_t tic_time;
954 
955 public:
956  size_t now();
957  size_t tic();
958  size_t toc(const char * msg=NULL, bool inSecs=true);
959  size_t elapsed();
960 };
961 
962 #if !defined _NO_TIME && !defined __MINGW32__
963 
971 double time_to_go(ProcessorTimeStamp& time, double fraction_done);
972 #endif
973 
988 void init_progress_bar(long total);
989 
1006 void progress_bar(long act_time);
1007 
1011 char * getCurrentTimeString();
1012 
1013 
1023 {
1024 public :
1026  BaseListener(): verbosity(0), cancel(false)
1027  {}
1028 
1030  virtual ~BaseListener() {}
1031 
1039  virtual void OnInitOperation(unsigned long _est_it) = 0;
1040 
1048  virtual void OnReportOperation(const std::string& _rsOp) = 0;
1049 
1061  virtual void OnProgress(unsigned long _it) = 0;
1062 
1065  virtual const unsigned& getVerbosity() const
1066  {
1067  return verbosity;
1068  }
1069 
1072  virtual unsigned& setVerbosity()
1073  {
1074  return verbosity;
1075  }
1076 
1083  virtual const bool& OnUserCancel() const
1084  {
1085  return cancel;
1086  }
1087 
1090  virtual bool& setCancel()
1091  {
1092  return cancel;
1093  }
1094 
1095 private:
1096  unsigned verbosity;
1097  bool cancel;
1098 };
1099 
1108 {
1109 public :
1110 
1118  virtual void OnInitOperation(unsigned long _est_it);
1119 
1128  virtual void OnProgress(unsigned long _it);
1129 
1134  virtual void OnReportOperation(const std::string& _rsOp);
1135 };
1136 
1149 void TimeMessage(const std::string &message);
1151 
1173 size_t xmippFREAD(void* dest, size_t size, size_t nitems, FILE*& fp,
1174  bool reverse = false);
1175 
1183 size_t xmippFWRITE(const void* src,
1184  size_t size,
1185  size_t nitems,
1186  FILE*& fp,
1187  bool reverse = false);
1188 
1194 void mapFile(const FileName &filename, char*&map,size_t &size, int &fileDescriptor, bool readOnly=true);
1195 
1197 void unmapFile(char *&map, size_t &size, int& fileDescriptor);
1198 
1203 #define little22bigendian(x) swapbytes((unsigned char*)& x,sizeof(x))
1204 
1209 void swapbytes(char* v, unsigned long n);
1210 
1213 bool IsBigEndian(void);
1214 
1216 //
1217 // process_mem_usage(double &, double &) - takes two doubles by reference,
1218 // attempts to read the system-dependent data for a process' virtual memory
1219 // size and resident set size, and return the results in KB.
1220 //
1221 // On failure, returns 0.0, 0.0
1222 void processMemUsage(double& vm_usage, double& resident_set);
1223 
1227 bool IsLittleEndian(void);
1228 
1231 void printMemoryUsed();
1233 
1235 bool compareTwoFiles(const FileName &fn1, const FileName &fn2, size_t offset = 0);
1236 
1237 
1238 
1240 #endif
double cdf_gauss(double x)
void init_progress_bar(long total)
void TimeMessage(const std::string &message)
int * nmax
double student_outside_probb(double p, double degrees_of_freedom)
double student_within_t0(double t0, double degrees_of_freedom)
Tabsinc(const double dd, const int xx)
Definition: xmipp_funcs.h:80
double gaus_up_to_x0(double x0, double mean=0, double stddev=1)
double cdf_tstudent(int k, double t)
double rnd_student_t(double nu)
virtual const bool & OnUserCancel() const
Definition: xmipp_funcs.h:1083
bool IsLittleEndian(void)
bool IsBigEndian(void)
void swapbytes(char *v, unsigned long n)
char * getCurrentTimeString()
double isampl
Definition: xmipp_funcs.h:73
double vadjust
Definition: xmipp_funcs.h:161
doublereal * c
int solve_2nd_degree_eq(double a, double b, double c, double &x1, double &x2, double prec=XMIPP_EQUAL_ACCURACY)
void sqrt(Image< double > &op)
double facadj
Definition: xmipp_funcs.h:162
void annotate_processor_time(ProcessorTimeStamp *time)
double vtable
Definition: xmipp_funcs.h:155
static double * y
size_t divide_equally(size_t N, size_t size, size_t rank, size_t &first, size_t &last)
virtual const unsigned & getVerbosity() const
Definition: xmipp_funcs.h:1065
double icdf_gauss(double p)
void processMemUsage(double &vm_usage, double &resident_set)
double elapsed_time(ProcessorTimeStamp &time, bool _IN_SECS=true)
double time_to_go(ProcessorTimeStamp &time, double fraction_done)
double cdf_FSnedecor(int d1, int d2, double x)
double student_up_to_t0(double t0, double degrees_of_freedom)
double * tabulatedsinc
Definition: xmipp_funcs.h:76
doublereal * x
void RealImag2Complex(const T *_real, const T *_imag, std::complex< double > *_complex, int length)
Definition: xmipp_funcs.h:408
#define i
void init_random_generator(int seed=-1)
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
double chi2_from_t0(double t0, double degrees_of_freedom)
size_t divide_equally_group(size_t N, size_t size, size_t myself)
double student_from_t0(double t0, double degrees_of_freedom)
void time_config()
double gaussian2D(double x, double y, double sigmaX, double sigmaY, double ang, double muX=0, double muY=0)
double sampl
Definition: xmipp_funcs.h:72
double rnd_unif()
glob_log first
double rnd_log(double a, double b)
doublereal * b
void unmapFile(char *&map, size_t &size, int &fileDescriptor)
#define x0
double student_outside_t0(double t0, double degrees_of_freedom)
virtual unsigned & setVerbosity()
Definition: xmipp_funcs.h:1072
int no_elem
Definition: xmipp_funcs.h:75
void computeAvgStddev(const std::vector< T > &V, double &avg, double &stddev)
Definition: xmipp_funcs.h:358
struct tms ProcessorTimeStamp
Definition: xmipp_funcs.h:822
#define XMIPP_EQUAL_ACCURACY
Definition: xmipp_macros.h:119
void progress_bar(long act_time)
int xmax
Definition: xmipp_funcs.h:74
bool compareTwoFiles(const FileName &fn1, const FileName &fn2, size_t offset=0)
binary comparison of two files skipping first "offset" bytes
double chi2_up_to_t0(double t0, double degrees_of_freedom)
double icdf_FSnedecor(int d1, int d2, double p)
#define ABS(x)
Definition: xmipp_macros.h:142
void filltable()
Definition: xmipp_funcs.h:109
void Complex2AmplPhase(const std::complex< double > *_complex, T *_ampl, T *_phase, int length)
Definition: xmipp_funcs.h:479
__host__ __device__ float length(float2 v)
double gaus_within_x0(double x0, double mean=0, double stddev=1)
void print_elapsed_time(ProcessorTimeStamp &time, bool _IN_SECS=true)
void computeStats(const std::vector< T > &V, double &avg, double &stddev, T &minval, T &maxval)
Definition: xmipp_funcs.h:317
double tstudent1D(double x, double df, double sigma, double mu=0)
int get_window_size() const
Definition: xmipp_funcs.h:197
double i0win_tab(double x) const
Definition: xmipp_funcs.h:186
virtual bool & setCancel()
Definition: xmipp_funcs.h:1090
size_t xmippFWRITE(const void *src, size_t size, size_t nitems, FILE *&fp, bool reverse=false)
size_t xmippFREAD(void *dest, size_t size, size_t nitems, FILE *&fp, bool reverse=false)
virtual ~BaseListener()
Definition: xmipp_funcs.h:1030
void mapFile(const FileName &filename, char *&map, size_t &size, int &fileDescriptor, bool readOnly=true)
double dtable
Definition: xmipp_funcs.h:158
void initConstant(std::vector< T > &V, T &value)
Definition: xmipp_funcs.h:390
double operator()(double val) const
Definition: xmipp_funcs.h:102
double alphar
Definition: xmipp_funcs.h:159
void Complex2RealImag(const std::complex< double > *_complex, T *_real, T *_imag, int length)
Definition: xmipp_funcs.h:456
double rnd_gaus()
int mu
constexpr int K
virtual ~Tabsinc()
Definition: xmipp_funcs.h:89
std::vector< double > dump_table() const
Definition: xmipp_funcs.h:174
std::vector< double > i0table
Definition: xmipp_funcs.h:157
void annotate_time(TimeStamp *time)
unsigned int randomize_random_generator()
void AmplPhase2Complex(const T *_ampl, const T *_phase, std::complex< double > *_complex, int length)
Definition: xmipp_funcs.h:431
#define PI
Definition: tools.h:43
double gaus_outside_x0(double x0, double mean=0, double stddev=1)
int * n
doublereal * a
void printMemoryUsed()
size_t TimeStamp
Definition: xmipp_funcs.h:823
void acum_time(ProcessorTimeStamp *orig, ProcessorTimeStamp *dest)
double gaus_from_x0(double x0, double mean=0, double stddev=1)
double gaussian1D(double x, double sigma, double mu=0)