Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Friends | List of all members

#include <pca.h>

Public Member Functions

 PCAAnalyzer (void)
 
 PCAAnalyzer (ClassicTrainingVectors const &ts)
 
 PCAAnalyzer (ClassicTrainingVectors const &ts, std::vector< unsigned > const &idx)
 
void reset (ClassicTrainingVectors const &ts)
 
void reset (ClassicTrainingVectors const &ts, std::vector< unsigned > const &idx)
 
void clear ()
 
void set_Dimension (int _D)
 
int get_Dimension () const
 
int get_eigenDimension () const
 
void setListener (BaseListener *_listener)
 

Public Attributes

std::vector< FeatureVectoreigenvec
 
FeatureVector eigenval
 
FeatureVector mean
 
int D
 
double prod_mean_mean
 
std::vector< double > prod_ei_mean
 
std::vector< double > prod_ei_ei
 
double avg_mean
 
std::vector< double > avg_ei
 

Friends

std::ostream & operator<< (std::ostream &out, const PCAAnalyzer &PC)
 
std::istream & operator>> (std::istream &in, PCAAnalyzer &PC)
 

Detailed Description

Basic PCA class

Definition at line 36 of file pca.h.

Constructor & Destructor Documentation

◆ PCAAnalyzer() [1/3]

PCAAnalyzer::PCAAnalyzer ( void  )
inline

Make an empty PCAAnalyzer

Definition at line 42 of file pca.h.

43  {}

◆ PCAAnalyzer() [2/3]

PCAAnalyzer::PCAAnalyzer ( ClassicTrainingVectors const &  ts)
inline

Construct a PCAAnalyzer object with eigenvectors & eigenvalues from ts. Parameter: ts The vectors.

Definition at line 50 of file pca.h.

51  {
52  reset(ts);
53  }
void reset(ClassicTrainingVectors const &ts)
Definition: pca.cpp:40

◆ PCAAnalyzer() [3/3]

PCAAnalyzer::PCAAnalyzer ( ClassicTrainingVectors const &  ts,
std::vector< unsigned > const &  idx 
)
inline

Construct a PCAAnalyzer object with eigenvectors & eigenvalues from ts, using only the items given in idx. Parameter: ts The vectors. Parameter: idx The indexes of the vectors to use

Definition at line 61 of file pca.h.

62  {
63  reset(ts, idx);
64  }
void reset(ClassicTrainingVectors const &ts)
Definition: pca.cpp:40

Member Function Documentation

◆ clear()

void PCAAnalyzer::clear ( )

Clear. Clean the eigenvector, eigenvalues and D

Definition at line 360 of file pca.cpp.

361 {
362  set_Dimension(0);
363  mean.clear();
364  eigenvec.clear();
365  eigenval.clear();
366 }
void set_Dimension(int _D)
Definition: pca.h:130
FeatureVector mean
Definition: pca.h:92
FeatureVector eigenval
Definition: pca.h:87
std::vector< FeatureVector > eigenvec
Definition: pca.h:82

◆ get_Dimension()

int PCAAnalyzer::get_Dimension ( ) const
inline

Get the number of relevant eigenvalues.

Definition at line 136 of file pca.h.

137  {
138  return D;
139  }
int D
Definition: pca.h:95

◆ get_eigenDimension()

int PCAAnalyzer::get_eigenDimension ( ) const
inline

Get the dimension of the eigenvectors.

Definition at line 142 of file pca.h.

143  {
144  if (eigenvec.size() > 1)
145  return eigenvec[0].size();
146  else
147  return 0;
148  }
std::vector< FeatureVector > eigenvec
Definition: pca.h:82

◆ reset() [1/2]

void PCAAnalyzer::reset ( ClassicTrainingVectors const &  ts)

Calculate the eigenval/vecs Parameter: ts The vectors.

Definition at line 40 of file pca.cpp.

41 {
42  std::vector<unsigned> dummy;
43  for (unsigned i = 0;i < ts.size();i++)
44  dummy.push_back(i);
45  reset(ts, dummy);
46 }
#define i
double dummy
void reset(ClassicTrainingVectors const &ts)
Definition: pca.cpp:40

◆ reset() [2/2]

void PCAAnalyzer::reset ( ClassicTrainingVectors const &  ts,
std::vector< unsigned > const &  idx 
)

Calculate the eigenval/vecs Parameter: ts The vectors. Parameter: idx The indexes of the vectors to use

Definition at line 53 of file pca.cpp.

54 {
55  std::vector<FeatureVector> a;
56  int n = ts.dimension();
57  a.resize(n);
58  int verbosity = listener->getVerbosity();
59 
60  {
61  if (verbosity)
62  listener->OnReportOperation((std::string) "Normalizing....\n");
63  if (verbosity == 1)
64  listener->OnInitOperation(n);
65 
66  //Get the mean of the given cluster of vectors
67  for (int k = 0;k < n;k++)
68  {
69  a[k].resize(n);
70  floatFeature sum = 0.0;
71  int l = 0;
72  for (std::vector<unsigned>::const_iterator i = idx.begin();i != idx.end();i++)
73  {
74  if (finite(ts.itemAt(*i)[k]))
75  {
76  sum += ts.itemAt(*i)[k];
77  l++;
78  }
79  }
80  mean.push_back(sum / l);
81  if (verbosity == 1)
82  listener->OnProgress(k);
83  }
84  if (verbosity == 1)
85  listener->OnProgress(n);
86 
87  if (verbosity == 1)
88  listener->OnInitOperation(n);
89  for (int i = 0;i < n;i++)
90  {
91  for (int j = 0;j <= i;j++)
92  {
93  floatFeature sum = 0.0;
94  int l = 0;
95  for (std::vector<unsigned>::const_iterator it = idx.begin();it != idx.end();it++)
96  {
97  floatFeature d1 = ts.itemAt(*it)[i] - mean[i];
98  floatFeature d2 = ts.itemAt(*it)[j] - mean[j];
99  if (finite(d1) && finite(d2))
100  {
101  sum += d1 * d2;
102  l++;
103  }
104  }
105  if (l)
106  a[i][j] = a[j][i] = sum / l;
107  else
108  a[i][j] = a[j][i] = 0;
109  }
110  if (verbosity == 1)
111  listener->OnProgress(i);
112  }
113  if (verbosity == 1)
114  listener->OnProgress(n);
115 
116  // for(int i=0;i<n;i++)
117  // std::cout << a[i] << std::endl;
118  }
119 
120  eigenval.resize(n);
121  eigenvec.resize(n);
122  set_Dimension(n);
123 
125  b.resize(n);
127  z.resize(n);
129  std::vector<FeatureVector> &v = eigenvec;
130 
131  for (int i = 0;i < n;i++)
132  {
133  v[i].resize(n);
134  v[i][i] = 1.0;
135  b[i] = d[i] = a[i][i];
136  }
137 
138  int nrot = 0;
139 
140  if (verbosity)
141  listener->OnReportOperation((std::string) "Diagonalizing matrix....\n");
142  if (verbosity == 1)
143  listener->OnInitOperation(50);
144 
145  //Jacobi method (it=iterationn number)
146  for (int it = 1;it <= 50;it++)
147  {
148  if ((verbosity == 1) && (it == 1))
149  listener->OnProgress(0);
150 
151  floatFeature tresh;
152  floatFeature sm = 0.0;
153  for (int ip = 0; ip < n - 1; ip++)
154  {
155  for (int iq = ip + 1; iq < n; iq++)
156  sm += fabs(a[iq][ip]);
157  }
158  if (sm == 0.0)
159  {//Done. Sort vectors
160  for (int i = 0; i < n - 1; i++)
161  {
162  int k = i;
163  floatFeature p = d[i];
164 
165  for (int j = i + 1; j < n; j++)
166  if (d[j] >= p)
167  p = d[k = j];
168 
169  if (k != i)
170  {//Swap i<->k
171  d[k] = d[i];
172  d[i] = p;
173  FeatureVector t = v[i];
174  v[i] = v[k];
175  v[k] = t;
176  }
177  }
178  if (verbosity == 1)
179  listener->OnProgress(50);
180  return;
181  }
182 
183  if (it < 4)
184  tresh = 0.2 * sm / (n * n);
185  else
186  tresh = 0;
187 
188  for (int ip = 0; ip < n - 1; ip++)
189  {
190  for (int iq = ip + 1; iq < n; iq++)
191  {
192  floatFeature g = 100.0 * fabs(a[iq][ip]);
193 
194  if (it > 4
195  && fabs(d[ip]) + g == fabs(d[ip])
196  && fabs(d[iq]) + g == fabs(d[iq]))
197  a[iq][ip] = 0.0;
198  else if (fabs(a[iq][ip]) > tresh)
199  {
200  floatFeature tau;
201  floatFeature t;
202  floatFeature s;
203  floatFeature c;
204  floatFeature h = d[iq] - d[ip];
205  if (fabs(h) + g == fabs(h))
206  t = a[iq][ip] / h;
207  else
208  {
209  floatFeature theta = 0.5 * h / a[iq][ip];
210  t = 1.0 / (fabs(theta) + sqrt(1.0 + theta * theta));
211  if (theta < 0.0)
212  t = -t;
213  }
214  c = 1.0 / sqrt(1 + t * t);
215  s = t * c;
216  tau = s / (1.0 + c);
217  h = t * a[iq][ip];
218  z[ip] -= h;
219  z[iq] += h;
220  d[ip] -= h;
221  d[iq] += h;
222  a[iq][ip] = 0.0;
223 
224 #define rotate(a,i,j,k,l) \
225  g = a[i][j]; \
226  h = a[k][l]; \
227  a[i][j] = g - s *(h + g*tau); \
228  a[k][l] = h + s*(g - h*tau);
229 
230  int j;
231  for (j = 0; j < ip; j++)
232  {
233  rotate(a, ip, j, iq, j)
234  }
235  for (j = ip + 1; j < iq; j++)
236  {
237  rotate(a, j, ip, iq, j)
238  }
239  for (j = iq + 1; j < n; j++)
240  {
241  rotate(a, j, ip, j, iq)
242  }
243  for (j = 0; j < n; j++)
244  {
245  rotate(v, ip, j, iq, j)
246  }
247 
248  nrot += 1;
249  }//if
250  }//for iq
251  }//for ip
252  for (int ip = 0; ip < n; ip++)
253  {
254  b[ip] += z[ip];
255  d[ip] = b[ip];
256  z[ip] = 0.0;
257  }
258 
259  if (verbosity == 1)
260  listener->OnProgress(it - 1);
261 
262  }//for it
263 
264  if (verbosity == 1)
265  listener->OnProgress(50);
266 
267 
268  REPORT_ERROR(ERR_NUMERICAL, "too many Jacobi iterations");
269 }
void set_Dimension(int _D)
Definition: pca.h:130
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
doublereal * c
doublereal * g
void sqrt(Image< double > &op)
virtual void OnReportOperation(const std::string &_rsOp)=0
virtual void OnInitOperation(unsigned long _est_it)=0
virtual const unsigned & getVerbosity() const
Definition: xmipp_funcs.h:1065
float floatFeature
Definition: data_types.h:72
#define i
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
#define rotate(a, i, j, k, l)
doublereal * d
double theta
doublereal * b
Error related to numerical calculation.
Definition: xmipp_error.h:179
double z
virtual void OnProgress(unsigned long _it)=0
#define j
FeatureVector mean
Definition: pca.h:92
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86
FeatureVector eigenval
Definition: pca.h:87
int * n
doublereal * a
std::vector< FeatureVector > eigenvec
Definition: pca.h:82

◆ set_Dimension()

void PCAAnalyzer::set_Dimension ( int  _D)
inline

Set the number of relevant eigenvalues.

Definition at line 130 of file pca.h.

131  {
132  D = _D;
133  }
int D
Definition: pca.h:95

◆ setListener()

void PCAAnalyzer::setListener ( BaseListener _listener)
inline

Defines Listener class

Definition at line 168 of file pca.h.

169  {
170  listener = _listener;
171  };

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const PCAAnalyzer PC 
)
friend

Show relevant eigenvectors and eigenvalues

Definition at line 369 of file pca.cpp.

370 {
371  out << "Relevant Dimension: " << PC.get_Dimension() << std::endl;
372  out << "Mean vector: ";
373  int size = PC.mean.size();
374  out << "(" << size << ") ---> ";
375  for (int j = 0; j < size; j++)
376  out << PC.mean[j] << " ";
377  out << std::endl;
378  for (int i = 0; i < PC.get_Dimension(); i++)
379  {
380  out << PC.eigenval[i] << " (" << size << ") ---> ";
381  for (int j = 0; j < size; j++)
382  out << PC.eigenvec[i][j] << " ";
383  out << std::endl;
384  }
385  return out;
386 }
#define i
for(j=1;j<=i__1;++j)
#define j
int get_Dimension() const
Definition: pca.h:136
FeatureVector mean
Definition: pca.h:92
FeatureVector eigenval
Definition: pca.h:87
std::vector< FeatureVector > eigenvec
Definition: pca.h:82

◆ operator>>

std::istream& operator>> ( std::istream &  in,
PCAAnalyzer PC 
)
friend

Read a set of PCA just as shown

Definition at line 388 of file pca.cpp.

389 {
390  PC.clear();
391  int D;
392  std::string read_line;
393  getline(in, read_line);
394  sscanf(read_line.c_str(), "Relevant Dimension: %d", &D);
395  PC.set_Dimension(D);
396  PC.eigenval.resize(D);
397  PC.eigenvec.resize(D);
398 
399  int size;
400  getline(in, read_line);
401  sscanf(read_line.c_str(), "Mean vector: (%d) --->", &size);
402  read_line.erase(0, read_line.find('>') + 1); // remove until --->
403  PC.mean.resize(size);
404  std::istringstream istr1(read_line.c_str());
405  for (int j = 0; j < size; j++)
406  istr1 >> PC.mean[j];
407 
408  for (int i = 0; i < D; i++)
409  {
410  getline(in, read_line);
411  float f;
412  sscanf(read_line.c_str(), "%f (%d) ---> ", &f, &size);
413  PC.eigenval[i] = f;
414  read_line.erase(0, read_line.find('>') + 1); // remove until --->
415  PC.eigenvec[i].resize(size);
416  std::istringstream istr2(read_line.c_str());
417  for (int j = 0; j < size; j++)
418  istr2 >> PC.eigenvec[i][j];
419  }
420 
421  return in;
422 }
void set_Dimension(int _D)
Definition: pca.h:130
#define i
int in
double * f
for(j=1;j<=i__1;++j)
int D
Definition: pca.h:95
#define j
void clear()
Definition: pca.cpp:360
FeatureVector mean
Definition: pca.h:92
FeatureVector eigenval
Definition: pca.h:87
std::vector< FeatureVector > eigenvec
Definition: pca.h:82

Member Data Documentation

◆ avg_ei

std::vector<double> PCAAnalyzer::avg_ei

Average of the ei vectors

Definition at line 110 of file pca.h.

◆ avg_mean

double PCAAnalyzer::avg_mean

Average of the mean vector

Definition at line 107 of file pca.h.

◆ D

int PCAAnalyzer::D

Number of relevant eigenvectors

Definition at line 95 of file pca.h.

◆ eigenval

FeatureVector PCAAnalyzer::eigenval

The eigenvalues

Definition at line 87 of file pca.h.

◆ eigenvec

std::vector<FeatureVector> PCAAnalyzer::eigenvec

The eigenvectors

Definition at line 82 of file pca.h.

◆ mean

FeatureVector PCAAnalyzer::mean

Mean of the input training set

Definition at line 92 of file pca.h.

◆ prod_ei_ei

std::vector<double> PCAAnalyzer::prod_ei_ei

Products <ei,ei>

Definition at line 104 of file pca.h.

◆ prod_ei_mean

std::vector<double> PCAAnalyzer::prod_ei_mean

Products <ei,mean>

Definition at line 101 of file pca.h.

◆ prod_mean_mean

double PCAAnalyzer::prod_mean_mean

Product <mean,mean>

Definition at line 98 of file pca.h.


The documentation for this class was generated from the following files: