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

#include <spe.h>

Inheritance diagram for SPE:
Inheritance graph
[legend]
Collaboration diagram for SPE:
Collaboration graph
[legend]

Public Member Functions

void setSpecificParameters (bool global=true, int k=12)
 Set specific parameters. More...
 
void reduceDimensionality ()
 Reduce dimensionality. More...
 
- Public Member Functions inherited from DimRedAlgorithm
 DimRedAlgorithm ()
 Empty constructor. More...
 
void setInputData (Matrix2D< double > &X)
 Set input data. More...
 
void setOutputDimensionality (size_t outputDim)
 Set output dimensionality. More...
 
const Matrix2D< double > & getReducedData ()
 Get reduced data. More...
 

Public Attributes

bool global
 
int k
 
- Public Attributes inherited from DimRedAlgorithm
Matrix2D< double > * X
 Pointer to input data. More...
 
size_t outputDim
 Output dim. More...
 
Matrix2D< double > Y
 Output data. More...
 
DimRedDistance2 distance
 Distance function. More...
 
FileName fnMapping
 Save mapping. More...
 

Detailed Description

Class for making a SPE dimensionality reduction

Definition at line 37 of file spe.h.

Member Function Documentation

◆ reduceDimensionality()

void SPE::reduceDimensionality ( )
virtual

Reduce dimensionality.

Implements DimRedAlgorithm.

Definition at line 36 of file spe.cpp.

37 {
38  double lambda = 1; // lambda
39  int s = 100; // number of updates per iteration
40  int max_iter = 20000 + round(0.04 * MAT_YSIZE(*X) * MAT_YSIZE(*X)); // number of iterations
41  double tol = 1e-5;
42  int n = MAT_YSIZE(*X);
44  Matrix1D<double> D, Rt, W;
46  Matrix1D<int> ind1,ind2;
47 
48  ind1.resize(s);
49  ind2.resize(s);
50  Rt.initZeros(VEC_XSIZE(ind1));
51  D.initZeros(VEC_XSIZE(ind1));
52  W.initZeros(VEC_XSIZE(ind1));
53 
55 
56  // Compute distance all vs. all
57  computeDistance(*X, R, distance, true);
58  R /= R.computeMax() / sqrt(2);
59 
60  if (global)
61  max_iter *= 3;
62  int ind1_0=0, ind1_F=s;
63  int ind2_F=std::min(2*s,n-1);
64  for(int nn=0;nn<max_iter;nn++)
65  {
66  // Compute random points
67  randomPermutation(n,J);
68 
69  // Get the ind1 and ind2 indexes vectors
70  if(n>s){
71  memcpy(&VEC_ELEM(ind1, 0),&A1D_ELEM(J,ind1_0),ind1_F*sizeof(int));
72  memcpy(&VEC_ELEM(ind2, 0),&A1D_ELEM(J,ind1_F),ind1_F*sizeof(int));
73  }
74  else{
75  memcpy(&VEC_ELEM(ind1, 0),&A1D_ELEM(J,0),ind2_F*sizeof(int));
76  memcpy(&VEC_ELEM(ind2, 0),&A1D_ELEM(J,ind1_F),ind2_F*sizeof(int));
77  }
78 
79  // Compute distances between points in embedded space
80  computeRandomPointsDistance(Y,D,ind1,ind2,distance,true);
81 
82  // Get corresponding distances in real space
83  for (int l=0;l<VEC_XSIZE(ind1);l++)
84  VEC_ELEM(Rt,l)= R(VEC_ELEM(ind1,l),VEC_ELEM(ind2,l));
85 
86  // Compute (Rt-D) / (D + tol)
88  VEC_ELEM(W,i) = (VEC_ELEM(Rt,i)-VEC_ELEM(D,i))/(VEC_ELEM(D,i)+tol);
89 
90  // Get the index to update locations
91  for(int ii=0;ii<s;ii++)
92  {
93  int i1 = VEC_ELEM(ind1,ii);
94  int i2 = VEC_ELEM(ind2,ii);
95 
96  // Update locations
97  double factor=lambda * 1/2;
98  for (size_t j = 0; j< outputDim; ++j){
99  double aux= MAT_ELEM(Y, i1, j) + factor*VEC_ELEM(W,ii)*(MAT_ELEM(Y, i1,j)-MAT_ELEM(Y, i2,j));
100  MAT_ELEM(Y, i2, j) += factor*VEC_ELEM(W,ii)*(MAT_ELEM(Y,i2,j)-MAT_ELEM(Y,i1,j));
101  MAT_ELEM(Y,i1,j) = aux;
102  }
103  }
104  // Update lambda
105  lambda = lambda - (lambda / max_iter);
106  }
107 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
void min(Image< double > &op1, const Image< double > &op2)
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
Matrix2D< double > Y
Output data.
Definition: dimred_tools.h:147
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
void computeDistance(const Matrix2D< double > &X, Matrix2D< double > &distance, DimRedDistance2 f, bool computeSqrt)
void sqrt(Image< double > &op)
Matrix2D< double > * X
Pointer to input data.
Definition: dimred_tools.h:141
#define A1D_ELEM(v, i)
void computeRandomPointsDistance(const Matrix2D< double > &X, Matrix1D< double > &distance, Matrix1D< int > ind1, Matrix1D< int > ind2, DimRedDistance2 f, bool computeSqrt)
#define i
void randomPermutation(int N, MultidimArray< int > &result)
size_t outputDim
Output dim.
Definition: dimred_tools.h:144
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
double * lambda
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
void initZeros()
Definition: matrix1d.h:592
#define j
bool global
Definition: spe.h:40
DimRedDistance2 distance
Distance function.
Definition: dimred_tools.h:150
int round(double x)
Definition: ap.cpp:7245
void initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode=RND_UNIFORM)
Definition: matrix2d.cpp:1108
T computeMax() const
Definition: matrix2d.cpp:1236
int * n

◆ setSpecificParameters()

void SPE::setSpecificParameters ( bool  global = true,
int  k = 12 
)

Set specific parameters.

Definition at line 30 of file spe.cpp.

31 {
32  this->global = true;
33  this->k = 1;
34 }
int k
Definition: spe.h:41
bool global
Definition: spe.h:40

Member Data Documentation

◆ global

bool SPE::global

Definition at line 40 of file spe.h.

◆ k

int SPE::k

Definition at line 41 of file spe.h.


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