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

#include <image_sort_by_statistics.h>

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

Public Member Functions

void clear ()
 
void readParams ()
 
void defineParams ()
 
void processInputPrepareSPTH (MetaData &SF, bool trained)
 
void run ()
 
- Public Member Functions inherited from XmippProgram
const char * getParam (const char *param, int arg=0)
 
const char * getParam (const char *param, const char *subparam, int arg=0)
 
int getIntParam (const char *param, int arg=0)
 
int getIntParam (const char *param, const char *subparam, int arg=0)
 
double getDoubleParam (const char *param, int arg=0)
 
double getDoubleParam (const char *param, const char *subparam, int arg=0)
 
float getFloatParam (const char *param, int arg=0)
 
float getFloatParam (const char *param, const char *subparam, int arg=0)
 
void getListParam (const char *param, StringVector &list)
 
int getCountParam (const char *param)
 
bool checkParam (const char *param)
 
bool existsParam (const char *param)
 
void addParamsLine (const String &line)
 
void addParamsLine (const char *line)
 
ParamDefgetParamDef (const char *param) const
 
virtual void quit (int exit_code=0) const
 
virtual int tryRun ()
 
void initProgress (size_t total, size_t stepBin=60)
 
void setProgress (size_t value=0)
 
void endProgress ()
 
void processDefaultComment (const char *param, const char *left)
 
void setDefaultComment (const char *param, const char *comment)
 
virtual void initComments ()
 
void setProgramName (const char *name)
 
void addUsageLine (const char *line, bool verbatim=false)
 
void clearUsage ()
 
void addExampleLine (const char *example, bool verbatim=true)
 
void addSeeAlsoLine (const char *seeAlso)
 
void addKeywords (const char *keywords)
 
const char * name () const
 
virtual void usage (int verb=0) const
 
virtual void usage (const String &param, int verb=2)
 
int version () const
 
virtual void show () const
 
virtual void read (int argc, const char **argv, bool reportErrors=true)
 
virtual void read (int argc, char **argv, bool reportErrors=true)
 
void read (const String &argumentsLine)
 
 XmippProgram ()
 
 XmippProgram (int argc, const char **argv)
 
virtual ~XmippProgram ()
 

Public Attributes

FileName fn
 
FileName fn_out
 
FileName fn_train
 
bool addToInput
 
bool addFeatures
 
int targetXdim
 
double cutoff
 
double per
 
std::vector< PCAMahalanobisAnalyzerpcaAnalyzer
 
MetaDataVec SF
 
MetaDataVec SFtrain
 
- Public Attributes inherited from XmippProgram
bool doRun
 
bool runWithoutArgs
 
int verbose
 Verbosity level. More...
 
int debug
 

Additional Inherited Members

- Protected Member Functions inherited from XmippProgram
void defineCommons ()
 
- Protected Attributes inherited from XmippProgram
int errorCode
 
ProgramDefprogDef
 Program definition and arguments parser. More...
 
std::map< String, CommentListdefaultComments
 
int argc
 Original command line arguments. More...
 
const char ** argv
 

Detailed Description

Definition at line 37 of file image_sort_by_statistics.h.

Member Function Documentation

◆ clear()

void ProgSortByStatistics::clear ( )

Definition at line 34 of file image_sort_by_statistics.cpp.

35 {
36  pcaAnalyzer.clear();
37 }
std::vector< PCAMahalanobisAnalyzer > pcaAnalyzer

◆ defineParams()

void ProgSortByStatistics::defineParams ( )
virtual

Function in which the param of each Program are defined.

Reimplemented from XmippProgram.

Definition at line 51 of file image_sort_by_statistics.cpp.

52 {
53  addUsageLine("Sorts the input images for identifying junk particles");
54  addUsageLine("+The program associates to each image four vectors.");
55  addUsageLine("+One vector is composed by descriptors that encodes the particle shape.");
56  addUsageLine("+Another two vectors give information about the SNR of the objects.");
57  addUsageLine("+Finally, the last vector provides information of the particle histogram");
58  addUsageLine("+");
59  addUsageLine("+These vector are then scored according to a multivariate Gaussian distribution");
60  addUsageLine("+");
61  addUsageLine("+You can reject erroneous particles choosing a threshold, you must take into account");
62  addUsageLine("+that it is a zscore.");
63  addUsageLine("+For univariate and mulivariate Gaussian distributions, 99% of the individuals");
64  addUsageLine("+have a Z-score below 3.");
65  addUsageLine("+Additionally, you can discard bad particles selecting an inaccurate particle percentage");
66  addUsageLine("+typically around 10-20%.");
67  addParamsLine(" -i <selfile> : Selfile with input images");
68  addParamsLine(" [-o <rootname=\"\">] : Output rootname");
69  addParamsLine(" : rootname.xmd contains the list of sorted images with their Z-score");
70  addParamsLine(" : rootname_vectors.xmd (if verbose>=2) contains the vectors associated to each image");
71  addParamsLine(" : If no rootname is given, these two files are not created");
72  addParamsLine(" [-t <selfile=\"\">]: : Train on selfile with good particles");
73  addParamsLine(" [--zcut <float=-1>] : Cut-off for Z-scores (negative for no cut-off) ");
74  addParamsLine(" : Images whose Z-score is larger than the cutoff are disabled");
75  addParamsLine(" [--addFeatures] : Add calculated features to the output metadata");
76  addParamsLine(" [--percent <float=0>] : Cut-off for particles (zero for no cut-off) ");
77  addParamsLine(" : Percentage of images with larger Z-scores are disabled");
78  addParamsLine(" [--addToInput] : Add columns also to input MetaData");
79  addParamsLine(" [--dim <d=50>] : Scale images to this size if they are larger.");
80  addParamsLine(" : Set to -1 for no rescaling");
81 }
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ processInputPrepareSPTH()

void ProgSortByStatistics::processInputPrepareSPTH ( MetaData SF,
bool  trained 
)

Definition at line 85 of file image_sort_by_statistics.cpp.

86 {
87  //#define DEBUG
88  PCAMahalanobisAnalyzer tempPcaAnalyzer0;
89  PCAMahalanobisAnalyzer tempPcaAnalyzer1;
90  PCAMahalanobisAnalyzer tempPcaAnalyzer2;
91  PCAMahalanobisAnalyzer tempPcaAnalyzer3;
92  PCAMahalanobisAnalyzer tempPcaAnalyzer4;
93 
94  //Morphology
95  tempPcaAnalyzer0.clear();
96  //Signal to noise ratio
97  tempPcaAnalyzer1.clear();
98  tempPcaAnalyzer2.clear();
99  tempPcaAnalyzer3.clear();
100  //Histogram analysis, to detect black points and saturated parts
101  tempPcaAnalyzer4.clear();
102 
103  double sign = 1;//;-1;
104  int numNorm = 3;
105  int numDescriptors0=numNorm;
106  int numDescriptors1=0;
107  int numDescriptors2=4;
108  int numDescriptors3=11;
109  int numDescriptors4=10;
110 
111  MultidimArray<float> v0(numDescriptors0);
112  MultidimArray<float> v2(numDescriptors2);
113  MultidimArray<float> v3(numDescriptors3);
114  MultidimArray<float> v4(numDescriptors4);
115  std::vector<double> v04;
116  std::vector<std::vector<double> > v04all;
117  v04all.clear();
118 
119  if (verbose>0)
120  {
121  std::cout << " Sorting particle set by new xmipp method..." << std::endl;
122  }
123 
124  int nr_imgs = SF.size();
125  if (verbose>0)
126  init_progress_bar(nr_imgs);
127 
128  int c = XMIPP_MAX(1, nr_imgs / 60);
129  int imgno = 0, imgnoPCA=0;
130 
131  bool thereIsEnable=SF.containsLabel(MDL_ENABLED);
132  bool first=true;
133 
134  // We assume that at least there is one particle
135  size_t Xdim, Ydim, Zdim, Ndim;
136  getImageSize(SF,Xdim,Ydim,Zdim,Ndim);
137 
138  //Initialization:
139  MultidimArray<double> nI, modI, tempI, tempM, ROI;
140  MultidimArray<bool> mask;
141  nI.resizeNoCopy(Ydim,Xdim);
142  modI.resizeNoCopy(Ydim,Xdim);
143  tempI.resizeNoCopy(Ydim,Xdim);
144  tempM.resizeNoCopy(Ydim,Xdim);
145  mask.resizeNoCopy(Ydim,Xdim);
146  mask.initConstant(true);
147 
148  MultidimArray<double> autoCorr(2*Ydim,2*Xdim);
149  MultidimArray<double> smallAutoCorr;
150 
151  Histogram1D hist;
152  Matrix2D<double> U,V,temp;
154 
155  MultidimArray<int> radial_count;
156  MultidimArray<double> radial_avg;
157  Matrix1D<int> center(2);
159  int dim;
160  center.initZeros();
161 
162  v0.initZeros(numDescriptors0);
163  v2.initZeros(numDescriptors2);
164  v3.initZeros(numDescriptors3);
165  v4.initZeros(numDescriptors4);
166 
167  ROI.resizeNoCopy(Ydim,Xdim);
168  ROI.setXmippOrigin();
170  {
171  double temp = std::sqrt(i*i+j*j);
172  if ( temp < (Xdim/2))
173  A2D_ELEM(ROI,i,j)= 1;
174  else
175  A2D_ELEM(ROI,i,j)= 0;
176  }
177 
178  Image<double> img;
179  FourierTransformer transformer(FFTW_BACKWARD);
180 
181  for (size_t objId : SF.ids())
182  {
183  if (thereIsEnable)
184  {
185  int enabled;
186  SF.getValue(MDL_ENABLED,enabled, objId);
187  if (enabled==-1)
188  {
189  imgno++;
190  continue;
191  }
192  }
193 
194  img.readApplyGeo(SF, objId);
195  if (targetXdim!=-1 && targetXdim<XSIZE(img()))
197 
198  MultidimArray<double> &mI=img();
199  mI.setXmippOrigin();
200  mI.statisticsAdjust(0.,1.);
201  mask.setXmippOrigin();
202  //The size of v1 depends on the image size and must be declared here
203  numDescriptors1 = XSIZE(mI)/2; //=100;
204  MultidimArray<float> v1(numDescriptors1);
205  v1.initZeros(numDescriptors1);
206  v04.reserve(numDescriptors0+numDescriptors1+numDescriptors2+numDescriptors3+numDescriptors4);
207  v04.clear();
208 
209  double var = 1;
210  normalize(transformer,mI,tempI,modI,0,var,mask);
211  modI.setXmippOrigin();
212  tempI.setXmippOrigin();
213  nI = sign*tempI*(modI*modI);
214  tempM = (modI*modI);
215 
216  A1D_ELEM(v0,0) = (tempM*ROI).sum();
217  v04.push_back(A1D_ELEM(v0,0));
218  int index = 1;
219  var+=2;
220  while (index < numNorm)
221  {
222  normalize(transformer,mI,tempI,modI,0,var,mask);
223  modI.setXmippOrigin();
224  tempI.setXmippOrigin();
225  nI += sign*tempI*(modI*modI);
226  tempM += (modI*modI);
227  A1D_ELEM(v0,index) = (tempM*ROI).sum();
228  v04.push_back(A1D_ELEM(v0,index));
229  index++;
230  var+=2;
231  }
232 
233  nI /= tempM;
234  tempPcaAnalyzer0.addVector(v0);
235  nI=(nI*ROI);
236 
237  auto_correlation_matrix(mI,autoCorr);
238  if (first)
239  {
240  radialAveragePrecomputeDistance(autoCorr, center, distance, dim);
241  first=false;
242  }
243  fastRadialAverage(autoCorr, distance, dim, radial_avg, radial_count);
244 
245  for (int n = 0; n < numDescriptors1; ++n)
246  {
247  A1D_ELEM(v1,n)=(float)DIRECT_A1D_ELEM(radial_avg,n);
248  v04.push_back(A1D_ELEM(v1,n));
249  }
250 
251  tempPcaAnalyzer1.addVector(v1);
252 
253 #ifdef DEBUG
254 
255  //String name = "000005@Images/Extracted/run_002/extra/BPV_1386.stk";
256  String name = "000010@Images/Extracted/run_001/extra/KLH_Dataset_I_Training_0028.stk";
257  //String name = "001160@Images/Extracted/run_001/DefaultFamily5";
258 
259  std::cout << img.name() << std::endl;
260 
261  if (img.name()==name2)
262  {
263  FileName fpName = "test_1.txt";
264  mI.write(fpName);
265  fpName = "test_2.txt";
266  nI.write(fpName);
267  fpName = "test_3.txt";
268  tempM.write(fpName);
269  fpName = "test_4.txt";
270  ROI.write(fpName);
271  //exit(1);
272  }
273 #endif
274  nI.binarize(0);
275  int im = labelImage2D(nI,nI,8);
276  compute_hist(nI, hist, 0, im, im+1);
277  size_t l;
278  int k,i,j;
279  hist.maxIndex(l,k,i,j);
280  A1D_ELEM(hist,j)=0;
281  hist.maxIndex(l,k,i,j);
282  nI.binarizeRange(j-1,j+1);
283 
284  double x0=0,y0=0,majorAxis=0,minorAxis=0,ellipAng=0;
285  size_t area=0;
286  fitEllipse(nI,x0,y0,majorAxis,minorAxis,ellipAng,area);
287 
288  A1D_ELEM(v2,0)=majorAxis/(img().xdim);
289  A1D_ELEM(v2,1)=minorAxis/(img().xdim);
290  A1D_ELEM(v2,2)= (fabs((img().xdim)/2-x0)+fabs((img().ydim)/2-y0))/((img().xdim)/2);
291  A1D_ELEM(v2,3)=area/( (double)((img().xdim)/2)*((img().ydim)/2) );
292 
293  for (int n=0 ; n < numDescriptors2 ; n++)
294  {
295  if ( std::isnan(std::abs(A1D_ELEM(v2,n))))
296  A1D_ELEM(v2,n)=0;
297  v04.push_back(A1D_ELEM(v2,n));
298  }
299 
300  tempPcaAnalyzer2.addVector(v2);
301 
302  //mI.setXmippOrigin();
303  //auto_correlation_matrix(mI*ROI,autoCorr);
304  //auto_correlation_matrix(nI,autoCorr);
305  autoCorr.window(smallAutoCorr,-5,-5, 5, 5);
306  smallAutoCorr.copy(temp);
307  svdcmp(temp,U,D,V);
308 
309  for (int n = 0; n < numDescriptors3; ++n)
310  {
311  A1D_ELEM(v3,n)=(float)VEC_ELEM(D,n); //A1D_ELEM(v3,n)=(float)VEC_ELEM(D,n)/VEC_ELEM(D,0);
312  v04.push_back(A1D_ELEM(v3,n));
313  }
314 
315  tempPcaAnalyzer3.addVector(v3);
316 
317  double minVal=0.;
318  double maxVal=0.;
319  mI.computeDoubleMinMax(minVal,maxVal);
320  compute_hist(mI, hist, minVal, maxVal, 100);
321 
322  for (int n=0 ; n <= numDescriptors4-1 ; n++)
323  {
324  A1D_ELEM(v4,n)= (hist.percentil((n+1)*10));
325  v04.push_back(A1D_ELEM(v4,n));
326  }
327  tempPcaAnalyzer4.addVector(v4);
328  if (addFeatures)
329  SF.setValue(MDL_SCORE_BY_SCREENING,v04, objId);
330  v04all.push_back(v04);
331 
332 #ifdef DEBUG
333 
334  if (img.name()==name1)
335  {
336  FileName fpName = "test.txt";
337  mI.write(fpName);
338  fpName = "test3.txt";
339  nI.write(fpName);
340  }
341 #endif
342  imgno++;
343  imgnoPCA++;
344 
345  if (imgno % c == 0 && verbose>0)
346  progress_bar(imgno);
347  }
348 
349  if (imgno == 0)
350  REPORT_ERROR(ERR_MD_NOACTIVE, "All metadata images are disable. Skipping...\n");
351 
352  std::size_t beg = fn.find_last_of("@") + 1;
353  std::size_t end = fn.find_last_of("/") + 1;
354  tempPcaAnalyzer0.evaluateZScore(2, 20, trained, (fn.substr(beg, end-beg)
355  + "_tmpPca0").c_str(), numDescriptors0);
356  tempPcaAnalyzer1.evaluateZScore(2, 20, trained, (fn.substr(beg, end-beg)
357  + "_tmpPca1").c_str(), numDescriptors1);
358  tempPcaAnalyzer2.evaluateZScore(2, 20, trained, (fn.substr(beg, end-beg)
359  + "_tmpPca2").c_str(), numDescriptors2);
360  tempPcaAnalyzer3.evaluateZScore(2, 20, trained, (fn.substr(beg, end-beg)
361  + "_tmpPca3").c_str(), numDescriptors3);
362  tempPcaAnalyzer4.evaluateZScore(2, 20, trained, (fn.substr(beg, end-beg)
363  + "_tmpPca4").c_str(), numDescriptors4);
364 
365  pcaAnalyzer.push_back(tempPcaAnalyzer0);
366  pcaAnalyzer.push_back(tempPcaAnalyzer1);
367  pcaAnalyzer.push_back(tempPcaAnalyzer1);
368  pcaAnalyzer.push_back(tempPcaAnalyzer3);
369  pcaAnalyzer.push_back(tempPcaAnalyzer4);
370 
371 }
No active object in MetaData.
Definition: xmipp_error.h:155
void fitEllipse(Matrix1D< double > &xPts, Matrix1D< double > &yPts, double &x0, double &y0, double &majorAxis, double &minorAxis, double &ellipseAngle)
void init_progress_bar(long total)
void binarizeRange(double valMin=0, double valMax=255, MultidimArray< int > *mask=NULL)
#define A2D_ELEM(v, i, j)
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
void addVector(const MultidimArray< float > &_v)
Add vector.
Definition: basic_pca.h:100
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
double sign
doublereal * c
void fastRadialAverage(const MultidimArray< T > &m, const MultidimArray< int > &distance, int dim, MultidimArray< T > &radial_mean, MultidimArray< int > &radial_count)
void resizeNoCopy(const MultidimArray< T1 > &v)
void sqrt(Image< double > &op)
virtual bool containsLabel(const MDLabel label) const =0
#define A1D_ELEM(v, i)
void getImageSize(const MetaData &md, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, MDLabel image_label)
void maxIndex(int &jmax) const
void initConstant(T val)
void abs(Image< double > &op)
const FileName & name() const
double percentil(double percent_mass)
Definition: histogram.cpp:160
virtual IdIteratorProxy< false > ids()
void auto_correlation_matrix(const MultidimArray< double > &Img, MultidimArray< double > &R, CorrelationAux &aux)
Definition: xmipp_fftw.cpp:957
virtual bool getValue(MDObject &mdValueOut, size_t id) const =0
int readApplyGeo(const FileName &name, const MDRow &row, const ApplyGeoParams &params=DefaultApplyGeoParams)
#define i
Is this image enabled? (int [-1 or 1])
void selfScaleToSize(int SplineDegree, MultidimArrayGeneric &V1, int Xdim, int Ydim, int Zdim)
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 FOR_ALL_ELEMENTS_IN_ARRAY2D(m)
glob_log first
#define DIRECT_A1D_ELEM(v, i)
double v1
#define y0
void compute_hist(const MultidimArrayGeneric &array, Histogram1D &hist, int no_steps)
Definition: histogram.cpp:572
#define x0
void binarize(double val=0, double accuracy=XMIPP_EQUAL_ACCURACY, MultidimArray< int > *mask=NULL)
viol index
void evaluateZScore(int NPCA, int Niter, bool trained=false, const char *filename="temp.txt", int numdesc=0)
Definition: basic_pca.cpp:384
Feature vectors used to classify particles (vector double)
#define XSIZE(v)
void progress_bar(long rlen)
void write(const FileName &fn) const
void computeDoubleMinMax(double &minval, double &maxval) const
quaternion_type< T > normalize(quaternion_type< T > q)
Definition: point.cpp:278
int verbose
Verbosity level.
virtual size_t size() const =0
void radialAveragePrecomputeDistance(const MultidimArray< T > &m, Matrix1D< int > &center_of_rot, MultidimArray< int > &distance, int &dim, const bool &rounding=false)
void svdcmp(const Matrix2D< T > &a, Matrix2D< double > &u, Matrix1D< double > &w, Matrix2D< double > &v)
Definition: matrix2d.cpp:125
#define j
const char * name() const
std::vector< PCAMahalanobisAnalyzer > pcaAnalyzer
bool setValue(const MDLabel label, const T &valueIn, size_t id)
TYPE distance(struct Point_T *p, struct Point_T *q)
Definition: point.cpp:28
int labelImage2D(const MultidimArray< double > &I, MultidimArray< double > &label, int neighbourhood)
Definition: filters.cpp:648
std::string String
Definition: xmipp_strings.h:34
void clear()
Clear.
Definition: basic_pca.h:84
void copy(Matrix2D< T > &op1) const
double v0
int * n
void statisticsAdjust(U avgF, U stddevF)

◆ readParams()

void ProgSortByStatistics::readParams ( )
virtual

Function in which each program will read parameters that it need. If some error occurs the usage will be printed out.

Reimplemented from XmippProgram.

Definition at line 39 of file image_sort_by_statistics.cpp.

40 {
41  fn = getParam("-i");
42  fn_out = getParam("-o");
43  addToInput = checkParam("--addToInput");
44  addFeatures = checkParam("--addFeatures");
45  fn_train = getParam("-t");
46  cutoff = getDoubleParam("--zcut");
47  per = getDoubleParam("--percent");
48  targetXdim = getIntParam("--dim");
49 }
double getDoubleParam(const char *param, int arg=0)
const char * getParam(const char *param, int arg=0)
bool checkParam(const char *param)
int getIntParam(const char *param, int arg=0)

◆ run()

void ProgSortByStatistics::run ( )
virtual

This function will be start running the program. it also should be implemented by derived classes.

Reimplemented from XmippProgram.

Definition at line 373 of file image_sort_by_statistics.cpp.

374 {
375  // Process input selfile ..............................................
376  SF.read(fn);
377  SF.removeDisabled();
378  MetaDataVec SF2 = SF;
379  SF = SF2;
380 
381  if (fn_train != "")
382  {
384  //processInputPrepareSPTH(SFtrain,trained);
386  }
387  else
389 
390  int imgno = 0;
391  int numPCAs = pcaAnalyzer.size();
392 
393  MultidimArray<double> finalZscore(SF.size());
394  MultidimArray<double> ZscoreShape1(SF.size()), sortedZscoreShape1;
395  MultidimArray<double> ZscoreShape2(SF.size()), sortedZscoreShape2;
396  MultidimArray<double> ZscoreSNR1(SF.size()), sortedZscoreSNR1;
397  MultidimArray<double> ZscoreSNR2(SF.size()), sortedZscoreSNR2;
398  MultidimArray<double> ZscoreHist(SF.size()), sortedZscoreHist;
399 
400 
401  finalZscore.initConstant(0);
402  ZscoreShape1.resizeNoCopy(finalZscore);
403  ZscoreShape2.resizeNoCopy(finalZscore);
404  ZscoreSNR1.resizeNoCopy(finalZscore);
405  ZscoreSNR2.resizeNoCopy(finalZscore);
406  ZscoreHist.resizeNoCopy(finalZscore);
407  sortedZscoreShape1.resizeNoCopy(finalZscore);
408  sortedZscoreShape2.resizeNoCopy(finalZscore);
409  sortedZscoreSNR1.resizeNoCopy(finalZscore);
410  sortedZscoreSNR2.resizeNoCopy(finalZscore);
411  sortedZscoreHist.resizeNoCopy(finalZscore);
412 
413  double zScore=0;
414  int enabled;
415 
416  for (size_t objId: SF.ids())
417  {
418  SF.getValue(MDL_ENABLED,enabled, objId);
419  if (enabled==-1)
420  {
421  A1D_ELEM(finalZscore,imgno) = 1e3;
422  A1D_ELEM(ZscoreShape1,imgno) = 1e3;
423  A1D_ELEM(ZscoreShape2,imgno) = 1e3;
424  A1D_ELEM(ZscoreSNR1,imgno) = 1e3;
425  A1D_ELEM(ZscoreSNR2,imgno) = 1e3;
426  A1D_ELEM(ZscoreHist,imgno) = 1e3;
427  imgno++;
428  enabled = 0;
429  }
430  else
431  {
432  for (int num = 0; num < numPCAs; ++num)
433  {
434  if (num == 0)
435  {
436  A1D_ELEM(ZscoreSNR1,imgno) = pcaAnalyzer[num].getZscore(imgno);
437  }
438  else if (num == 1)
439  {
440  A1D_ELEM(ZscoreShape2,imgno) = pcaAnalyzer[num].getZscore(imgno);
441  }
442  else if (num == 2)
443  {
444  A1D_ELEM(ZscoreShape1,imgno) = pcaAnalyzer[num].getZscore(imgno);
445  }
446  else if (num == 3)
447  {
448  A1D_ELEM(ZscoreSNR2,imgno) = pcaAnalyzer[num].getZscore(imgno);
449  }
450  else
451  {
452  A1D_ELEM(ZscoreHist,imgno) = pcaAnalyzer[num].getZscore(imgno);
453  }
454 
455  if(zScore < pcaAnalyzer[num].getZscore(imgno))
456  zScore = pcaAnalyzer[num].getZscore(imgno);
457  }
458 
459  A1D_ELEM(finalZscore,imgno)=zScore;
460  imgno++;
461  zScore = 0;
462  }
463  }
464  pcaAnalyzer.clear();
465 
466  // Produce output .....................................................
467  MetaDataVec SFout;
468  std::ofstream fh_zind;
469 
470  if (verbose==2 && !fn_out.empty())
471  fh_zind.open((fn_out.withoutExtension() + "_vectors.xmd").c_str(), std::ios::out);
472 
473  MultidimArray<int> sorted;
474  finalZscore.indexSort(sorted);
475 
476  int nr_imgs = SF.size();
477  bool thereIsEnable=SF.containsLabel(MDL_ENABLED);
478  MDRowVec row;
479 
480  for (int imgno = 0; imgno < nr_imgs; imgno++)
481  {
482  int isort = DIRECT_A1D_ELEM(sorted,imgno) - 1;
483  size_t objId = SF.getRowId(isort);
484  SF.getRow(row, objId);
485 
486  if (thereIsEnable)
487  {
488  int enabled;
489  row.getValue(MDL_ENABLED, enabled);
490  if (enabled==-1)
491  continue;
492  }
493 
494  double zscore=DIRECT_A1D_ELEM(finalZscore,isort);
495  double zscoreShape1=DIRECT_A1D_ELEM(ZscoreShape1,isort);
496  double zscoreShape2=DIRECT_A1D_ELEM(ZscoreShape2,isort);
497  double zscoreSNR1=DIRECT_A1D_ELEM(ZscoreSNR1,isort);
498  double zscoreSNR2=DIRECT_A1D_ELEM(ZscoreSNR2,isort);
499  double zscoreHist=DIRECT_A1D_ELEM(ZscoreHist,isort);
500 
501  DIRECT_A1D_ELEM(sortedZscoreShape1,imgno)=DIRECT_A1D_ELEM(ZscoreShape1,isort);
502  DIRECT_A1D_ELEM(sortedZscoreShape2,imgno)=DIRECT_A1D_ELEM(ZscoreShape2,isort);
503  DIRECT_A1D_ELEM(sortedZscoreSNR1,imgno)=DIRECT_A1D_ELEM(ZscoreSNR1,isort);
504  DIRECT_A1D_ELEM(sortedZscoreSNR2,imgno)=DIRECT_A1D_ELEM(ZscoreSNR2,isort);
505  DIRECT_A1D_ELEM(sortedZscoreHist,imgno)=DIRECT_A1D_ELEM(ZscoreHist,isort);
506 
507  if (zscore>cutoff && cutoff>0)
508  {
509  row.setValue(MDL_ENABLED,-1);
510  if (addToInput)
511  SF.setValue(MDL_ENABLED, -1, objId);
512  }
513  else
514  {
515  row.setValue(MDL_ENABLED,1);
516  if (addToInput)
517  SF.setValue(MDL_ENABLED, 1, objId);
518  }
519 
520  row.setValue(MDL_ZSCORE,zscore);
521  row.setValue(MDL_ZSCORE_SHAPE1,zscoreShape1);
522  row.setValue(MDL_ZSCORE_SHAPE2,zscoreShape2);
523  row.setValue(MDL_ZSCORE_SNR1,zscoreSNR1);
524  row.setValue(MDL_ZSCORE_SNR2,zscoreSNR2);
525  row.setValue(MDL_ZSCORE_HISTOGRAM,zscoreHist);
526 
527  if (addToInput)
528  {
529  SF.setValue(MDL_ZSCORE, zscore, objId);
530  SF.setValue(MDL_ZSCORE_SHAPE1, zscoreShape1, objId);
531  SF.setValue(MDL_ZSCORE_SHAPE2, zscoreShape2, objId);
532  SF.setValue(MDL_ZSCORE_SNR1, zscoreSNR1, objId);
533  SF.setValue(MDL_ZSCORE_SNR2, zscoreSNR2, objId);
534  SF.setValue(MDL_ZSCORE_HISTOGRAM, zscoreHist, objId);
535  }
536 
537  SFout.addRow(row);
538  }
539 
540  //Sorting taking into account a given percentage
541  if (per > 0)
542  {
543  MultidimArray<int> sortedShape1,sortedShape2,sortedSNR1,sortedSNR2,sortedHist,
544  sortedShapeSF1,sortedShapeSF2,sortedSNR1SF,sortedSNR2SF,sortedHistSF;
545 
546  sortedZscoreShape1.indexSort(sortedShape1);
547  sortedZscoreShape2.indexSort(sortedShape2);
548  sortedZscoreSNR1.indexSort(sortedSNR1);
549  sortedZscoreSNR2.indexSort(sortedSNR2);
550  sortedZscoreHist.indexSort(sortedHist);
551  auto numPartReject = (size_t)std::floor((per/100)*SF.size());
552 
553  for (size_t numPar = SF.size()-1; numPar > (SF.size()-numPartReject); --numPar)
554  {
555  SFout.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedShape1,numPar)-1));
556  SFout.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedShape2,numPar)-1));
557  SFout.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedSNR1,numPar)-1));
558  SFout.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedSNR2,numPar)-1));
559  SFout.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedHist,numPar)));
560 
561  if (addToInput)
562  {
563  ZscoreShape1.indexSort(sortedShapeSF1);
564  ZscoreShape2.indexSort(sortedShapeSF2);
565  ZscoreSNR1.indexSort(sortedSNR1SF);
566  ZscoreSNR2.indexSort(sortedSNR2SF);
567  ZscoreHist.indexSort(sortedHistSF);
568 
569  SF.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedShapeSF1,numPar)-1));
570  SF.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedShapeSF2,numPar)-1));
571  SF.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedSNR1SF,numPar)-1));
572  SF.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedSNR2SF,numPar)-1));
573  SF.setValue(MDL_ENABLED, -1, SF.getRowId(DIRECT_A1D_ELEM(sortedHistSF,numPar)-1));
574  }
575  }
576  }
577 
578  if (verbose==2)
579  fh_zind.close();
580  if (!fn_out.empty())
581  {
582  MetaDataVec SFsorted;
583  if (fn_out.exists()) SFsorted.read(fn_out);
584  int countItems = 0;
585  for (const auto& row : SFsorted)
586  {
587  countItems++;
588  SFout.addRow(dynamic_cast<const MDRowVec&>(row));
589  }
590  SFsorted.sort(SFout,MDL_ZSCORE);
591  SFsorted.write(formatString("@%s", fn_out.c_str()), MD_APPEND);
592  }
593  if (addToInput)
594  {
595  MetaDataVec SFsorted;
596  SFsorted.sort(SF,MDL_ZSCORE);
597  SFsorted.write(fn,MD_APPEND);
598  }
599 }
void sort(const MetaDataVec &MDin, const MDLabel sortLabel, bool asc=true, int limit=-1, int offset=0)
__host__ __device__ float2 floor(const float2 v)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void setValue(const MDObject &object) override
void processInputPrepareSPTH(MetaData &SF, bool trained)
#define A1D_ELEM(v, i)
Z Score (double)
void initConstant(T val)
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
Z Score (double)
Z Score (double)
virtual IdIteratorProxy< false > ids()
std::unique_ptr< MDRow > getRow(size_t id) override
Z Score (double)
size_t size() const override
Is this image enabled? (int [-1 or 1])
size_t addRow(const MDRow &row) override
T & getValue(MDLabel label)
#define DIRECT_A1D_ELEM(v, i)
bool setValue(const MDObject &mdValueIn, size_t id)
Z Score (double)
int verbose
Verbosity level.
bool exists() const
size_t getRowId(const MetaDataVecRow &) const
std::vector< PCAMahalanobisAnalyzer > pcaAnalyzer
bool getValue(MDObject &mdValueOut, size_t id) const override
Global Z Score (double)
FileName withoutExtension() const
virtual void removeDisabled()
String formatString(const char *format,...)
bool containsLabel(const MDLabel label) const override
void indexSort(MultidimArray< int > &indx) const

Member Data Documentation

◆ addFeatures

bool ProgSortByStatistics::addFeatures

Definition at line 41 of file image_sort_by_statistics.h.

◆ addToInput

bool ProgSortByStatistics::addToInput

Definition at line 41 of file image_sort_by_statistics.h.

◆ cutoff

double ProgSortByStatistics::cutoff

Definition at line 45 of file image_sort_by_statistics.h.

◆ fn

FileName ProgSortByStatistics::fn

Definition at line 40 of file image_sort_by_statistics.h.

◆ fn_out

FileName ProgSortByStatistics::fn_out

Definition at line 40 of file image_sort_by_statistics.h.

◆ fn_train

FileName ProgSortByStatistics::fn_train

Definition at line 40 of file image_sort_by_statistics.h.

◆ pcaAnalyzer

std::vector<PCAMahalanobisAnalyzer> ProgSortByStatistics::pcaAnalyzer

Definition at line 47 of file image_sort_by_statistics.h.

◆ per

double ProgSortByStatistics::per

Definition at line 45 of file image_sort_by_statistics.h.

◆ SF

MetaDataVec ProgSortByStatistics::SF

Definition at line 48 of file image_sort_by_statistics.h.

◆ SFtrain

MetaDataVec ProgSortByStatistics::SFtrain

Definition at line 48 of file image_sort_by_statistics.h.

◆ targetXdim

int ProgSortByStatistics::targetXdim

Definition at line 42 of file image_sort_by_statistics.h.


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