Xmipp  v3.23.11-Nereus
pca.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Jorge Garcia de la Nava Ruiz (gdl@ac.uma.es)
4  * Carlos Oscar Sanchez Sorzano
5  * Alberto Pascual Montano (pascual@cnb.csic.es)
6  *
7  * Departamento de Arquitectura de Computadores, Universidad de Malaga
8  *
9  * Copyright (c) 2001 , CSIC/UMA.
10  *
11  * Permission is granted to copy and distribute this file, for noncommercial
12  * use, provided (a) this copyright notice is preserved, (b) no attempt
13  * is made to restrict redistribution of this file, and (c) this file is
14  * restricted by a compilation copyright.
15  *
16  * All comments concerning this program package may be sent to the
17  * e-mail address 'pascual@cnb.csic.es'
18  *
19  *****************************************************************************/
20 
21 #ifndef XMIPPPC_H
22 #define XMIPPPC_H
23 
24 #include "base_algorithm.h"
25 #include "data_types.h"
26 #include "training_vector.h"
27 
28 #include <core/xmipp_funcs.h>
29 #include <core/matrix2d.h>
30 #include <core/multidim_array.h>
31 
37 {
38 public:
43  {}
44 
51  {
52  reset(ts);
53  }
54 
61  PCAAnalyzer(ClassicTrainingVectors const &ts, std::vector<unsigned> const & idx)
62  {
63  reset(ts, idx);
64  }
65 
70  void reset(ClassicTrainingVectors const &ts);
71 
77  void reset(ClassicTrainingVectors const &ts, std::vector<unsigned> const & idx);
78 
82  std::vector<FeatureVector> eigenvec;
83 
88 
93 
95  int D;
96 
99 
101  std::vector<double> prod_ei_mean;
102 
104  std::vector<double> prod_ei_ei;
105 
107  double avg_mean;
108 
110  std::vector<double> avg_ei;
111 #ifdef UNUSED // detected as unused 29.6.2018
112 
114  void setIdentity(int n);
115 #endif
116 
119  void clear();
120 
121 #ifdef UNUSED // detected as unused 29.6.2018
122 
126  void prepare_for_correlation();
127 #endif
128 
130  void set_Dimension(int _D)
131  {
132  D = _D;
133  }
134 
136  int get_Dimension() const
137  {
138  return D;
139  }
140 
142  int get_eigenDimension() const
143  {
144  if (eigenvec.size() > 1)
145  return eigenvec[0].size();
146  else
147  return 0;
148  }
149 
150 #ifdef UNUSED // detected as unused 29.6.2018
151 
154  int Dimension_for_variance(double th_var);
155 
163  void Project(FeatureVector &input, FeatureVector &output);
164 #endif
165 
168  void setListener(BaseListener* _listener)
169  {
170  listener = _listener;
171  };
172 
174  friend std::ostream& operator << (std::ostream &out, const PCAAnalyzer &PC);
175 
178 
179 private:
180 
181  BaseListener* listener; // Listener class
182 
183 
184 };
185 
187 class PCA_set
188 {
189 public:
191  std::vector<PCAAnalyzer> PCA;
192 
193 #ifdef UNUSED // detected as unused 29.6.2018
194 
196  int create_empty_PCA(int n = 1);
197 #endif
198 
200  int PCANo() const
201  {
202  return PCA.size();
203  }
204 
206  const PCAAnalyzer * operator()(int i) const
207  {
208  return &(PCA[i]);
209  }
210 
212  friend std::ostream& operator << (std::ostream &out, const PCA_set &PS);
213 
216 };
217 
228 {
229 public:
231  int J;
232 
234  int d;
235 
238 
240  long n;
241 
245 
249  Running_PCA(int _J, int _d);
250 
251 #ifdef UNUSED // detected as unused 29.6.2018
252 
253  void new_sample(const Matrix1D<double> &sample);
254 #endif
255 
257  void project(const Matrix1D<double> &input, Matrix1D<double> &output) const;
258 
260  void get_eigenvector(int j, Matrix1D<double> &result) const
261  {
262  eigenvectors.getCol(j, result);
263  }
264 
266  double get_eigenvector_variance(int j) const
267  {
268  if (n <= j)
269  return 0.0;
270  double mean_proj = sum_proj(j) / n;
271  return sum_proj2(j) / n - mean_proj*mean_proj;
272  }
273 public:
274  // Sum of all samples so far
276 
277  // Sum of all projections so far
279 
280  // Sum of all projections squared so far
282 };
283 
285 #endif
286 
long n
Current number of samples seen.
Definition: pca.h:240
Matrix1D< double > sum_all_samples
Definition: pca.h:275
void set_Dimension(int _D)
Definition: pca.h:130
PCAAnalyzer(ClassicTrainingVectors const &ts, std::vector< unsigned > const &idx)
Definition: pca.h:61
double get_eigenvector_variance(int j) const
Get the variance associated to a certain eigenvector.
Definition: pca.h:266
int J
Total number of eigenvectors to be computed.
Definition: pca.h:231
PCAAnalyzer(void)
Definition: pca.h:42
std::vector< double > avg_ei
Definition: pca.h:110
double prod_mean_mean
Definition: pca.h:98
#define i
Matrix1D< double > sum_proj
Definition: pca.h:278
double avg_mean
Definition: pca.h:107
int in
void get_eigenvector(int j, Matrix1D< double > &result) const
Get a certain eigenvector.
Definition: pca.h:260
int get_eigenDimension() const
Definition: pca.h:142
std::vector< PCAAnalyzer > PCA
Definition: pca.h:191
void setListener(BaseListener *_listener)
Definition: pca.h:168
Definition: pca.h:187
std::vector< double > prod_ei_mean
Definition: pca.h:101
Matrix2D< double > eigenvectors
Definition: pca.h:244
const PCAAnalyzer * operator()(int i) const
Definition: pca.h:206
void reset(ClassicTrainingVectors const &ts)
Definition: pca.cpp:40
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
int PCANo() const
Definition: pca.h:200
int D
Definition: pca.h:95
#define j
PCAAnalyzer(ClassicTrainingVectors const &ts)
Definition: pca.h:50
Matrix1D< double > sum_proj2
Definition: pca.h:281
void getCol(size_t j, Matrix1D< T > &v) const
Definition: matrix2d.cpp:890
void clear()
Definition: pca.cpp:360
friend std::ostream & operator<<(std::ostream &out, const PCAAnalyzer &PC)
Definition: pca.cpp:369
int get_Dimension() const
Definition: pca.h:136
int d
Dimension of the sample vectors.
Definition: pca.h:234
FeatureVector mean
Definition: pca.h:92
std::vector< double > prod_ei_ei
Definition: pca.h:104
friend std::istream & operator>>(std::istream &in, PCAAnalyzer &PC)
Definition: pca.cpp:388
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86
FeatureVector eigenval
Definition: pca.h:87
int * n
Matrix1D< double > current_sample_mean
Current estimate of the population mean.
Definition: pca.h:237
std::vector< FeatureVector > eigenvec
Definition: pca.h:82