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

#include <classify_kmeans_2d.h>

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

Public Member Functions

void readParams ()
 Read argument. More...
 
void show ()
 Show. More...
 
void defineParams ()
 Define parameters. More...
 
void run ()
 Main routine. More...
 
- 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 fnSel
 
FileName fnOut
 
FileName fnClusters
 
FileName fnPoints
 
int K
 
int maxObjects
 
- 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 34 of file classify_kmeans_2d.h.

Member Function Documentation

◆ defineParams()

void ProgClassifyKmeans2D::defineParams ( )
virtual

Define parameters.

Reimplemented from XmippProgram.

Definition at line 62 of file classify_kmeans_2d.cpp.

63 {
64  addUsageLine("Clusters a set of images");
65  addParamsLine(" -i <selfile> : Selfile containing images to be clustered");
66  addParamsLine(" [-o <image=\"output.xmd\">] : Output selfile");
67  addParamsLine(" -k <int> : Number of clusters");
68  addParamsLine(" [-c <image=\"clusters.xmd\">] : Filename with clusters");
69  addParamsLine(" [-p <image=\"points.xmd\">] : Filename with points");
70  addParamsLine(" [-m <int=\"-1\">] : Threshold for number of particles after which the position of clusters will be fixed");
71 }
void addUsageLine(const char *line, bool verbatim=false)
void addParamsLine(const String &line)

◆ readParams()

void ProgClassifyKmeans2D::readParams ( )
virtual

Read argument.

Reimplemented from XmippProgram.

Definition at line 36 of file classify_kmeans_2d.cpp.

37 {
38  fnSel = getParam("-i");
39  fnOut = getParam("-o");
40  K = getIntParam("-k");
41  fnClusters = getParam("-c");
42  fnPoints = getParam("-p");
43  maxObjects = getIntParam("-m");
44 }
const char * getParam(const char *param, int arg=0)
int getIntParam(const char *param, int arg=0)

◆ run()

void ProgClassifyKmeans2D::run ( )
virtual

Main routine.

Reimplemented from XmippProgram.

Definition at line 417 of file classify_kmeans_2d.cpp.

418 {
419  MetaDataVec SF, MDsummary, MDclass, MDallDone;
420  FileName fnImg, fnClass, fnallDone;
421  Image<double> I, Imasked;
423  CorrelationAux aux;
424  std::vector<std::vector<double> > fvs;
425  std::vector<double> fv, fv_temp;
426  std::vector<KPoint> points;
427  std::vector<Cluster> clusters;
429  srand (time(nullptr));
430 
431  // reading new images from input file
432  SF.read(fnSel);
433  for (size_t objId : SF.ids())
434  {
435  SF.getValue(MDL_IMAGE, fnImg, objId);
436  I.read(fnImg);
437  I().setXmippOrigin();
438  centerImageTranslationally(I(), aux);
439  fv.clear();
440  ef.extractEntropy(I(), Imasked(), fv);
441  //ef.extractZernike(I(), fv);
442  //ef.extractLBP(I(), fv);
443  //ef.extractVariance(I(), fv);
444  //ef.extractGranulo(I(), fv);
445  //ef.extractRamp(I(), fv);
446  //ef.extractHistDist(I(), fv);
447  fvs.push_back(fv);
448  }
449 
450  double min_item, max_item;
451  for(int i = 0; i < fv.size(); i++)
452  {
453  fv_temp.clear();
454  for (int j = 0; j < fvs.size(); j++)
455  fv_temp.push_back(fvs[j][i]);
456 
457  max_item = *std::max_element(fv_temp.begin(), fv_temp.end());
458  min_item = *std::min_element(fv_temp.begin(), fv_temp.end());
459  for (int j = 0; j < fvs.size(); j++)
460  fvs[j][i] = (fvs[j][i] - min_item) / (max_item - min_item);
461  }
462 
463  int allItems = 0;
464  for (size_t objId : SF.ids())
465  {
466  allItems++;
467  KPoint p(allItems, fvs.front());
468  points.push_back(p);
469  fvs.erase(fvs.begin());
470  }
471 
472  // preparing all the paths to external files
473  std::size_t extraPath = fnSel.find_last_of("/");
474  fnOut = fnSel.substr(0, extraPath+1) + fnOut.c_str();
475  fnClusters = fnSel.substr(0, extraPath+1) + fnClusters.c_str();
476  fnPoints = fnSel.substr(0, extraPath+1) + fnPoints.c_str();
477  fnallDone = fnSel.substr(0, extraPath+1) + "allDone.xmd";
478 
479  // loading all the stored points from file (their count is unknown here)
480  std::vector<double> fv_load;
481  std::fstream savedPoints(fnPoints.c_str());
482  std::string line;
483  while (savedPoints.good())
484  {
485  std::getline(savedPoints, line);
486  if (line.size() < 2) break;
487  allItems++;
488  std::stringstream ss(line);
489  fv_load.clear();
490  double point_value;
491  for (int j = 0; j < fv.size(); j++)
492  {
493  ss >> point_value;
494  fv_load.push_back(point_value);
495  }
496  KPoint p(allItems, fv_load);
497  points.push_back(p);
498  }
499 
500  // performing k-means clustering
501  KMeans kmeans(K, allItems, fv.size(), allItems, maxObjects);
502  clusters = kmeans.run(points, fnClusters, fnPoints);
503 
504  // for cycle writing output file
505  for (int i = 0; i < clusters.size(); i++)
506  {
507  size_t total_points_cluster = clusters[i].getTotalPoints();
508 
509  size_t ii = MDsummary.addObject();
510  MDsummary.setValue(MDL_REF, i+1, ii);
511  MDsummary.setValue(MDL_CLASS_COUNT, total_points_cluster, ii);
512 
513  std::ostringstream clusterValues;
514  clusterValues << "[";
515  for (int j = 0; j < fv.size()-1; j++)
516  clusterValues << clusters[i].getCentralValue(j) << ", ";
517  clusterValues << clusters[i].getCentralValue(fv.size()-1) << "]";
518 
519  MDsummary.setValue(MDL_KMEANS2D_CENTROID, clusterValues.str(), ii);
520  MDsummary.write(formatString("classes@%s", fnOut.c_str()), MD_APPEND);
521  MDclass.clear();
522 
523  std::ifstream f(fnallDone.c_str());
524  if (f.good()) MDallDone.read(fnallDone);
525 
526  for (int j = 0; j < total_points_cluster; j++)
527  {
528  MDRowVec row;
529  MDallDone.getRow(row, clusters[i].getPoint(j).getID());
530  size_t recId = MDclass.addRow(row);
531  MDclass.setValue(MDL_REF, i+1, recId);
532  }
533  MDclass.write(formatString("class%06d_images@%s", i+1,
534  fnOut.c_str()), MD_APPEND);
535  }
536 }
void extractEntropy(const MultidimArray< double > &I, MultidimArray< double > &Imasked, std::vector< double > &fv)
Extracting entropy features.
void centerImageTranslationally(MultidimArray< double > &I, CorrelationAux &aux)
Definition: filters.cpp:3212
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
virtual IdIteratorProxy< false > ids()
std::unique_ptr< MDRow > getRow(size_t id) override
#define i
size_t addRow(const MDRow &row) override
void clear() override
Centroid of a cluster for the KMEANS2D classification.
bool setValue(const MDObject &mdValueIn, size_t id)
size_t addObject() override
double * f
#define j
bool getValue(MDObject &mdValueOut, size_t id) const override
Class to which the image belongs (int)
Number of images assigned to the same class as this image.
String formatString(const char *format,...)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
std::vector< Cluster > run(std::vector< KPoint > &points, FileName fnClusters, FileName fnPoints)
Name of an image (std::string)

◆ show()

void ProgClassifyKmeans2D::show ( )

Show.

Definition at line 47 of file classify_kmeans_2d.cpp.

48 {
49  if (verbose==0)
50  return;
51  std::cerr
52  << "Input selfile: " << fnSel << std::endl
53  << "Output selfile: " << fnOut << std::endl
54  << "Number of clusters: " << K << std::endl
55  << "Filename with clusters: " << fnClusters << std::endl
56  << "Filename with points: " << fnPoints << std::endl
57  << "Threshold for number of particles: " << maxObjects << std::endl
58  ;
59 }
int verbose
Verbosity level.

Member Data Documentation

◆ fnClusters

FileName ProgClassifyKmeans2D::fnClusters

Filename clusters

Definition at line 44 of file classify_kmeans_2d.h.

◆ fnOut

FileName ProgClassifyKmeans2D::fnOut

Filename output root

Definition at line 41 of file classify_kmeans_2d.h.

◆ fnPoints

FileName ProgClassifyKmeans2D::fnPoints

Filename points

Definition at line 47 of file classify_kmeans_2d.h.

◆ fnSel

FileName ProgClassifyKmeans2D::fnSel

Filename selection file containing the images

Definition at line 38 of file classify_kmeans_2d.h.

◆ K

int ProgClassifyKmeans2D::K

Number of clusters

Definition at line 50 of file classify_kmeans_2d.h.

◆ maxObjects

int ProgClassifyKmeans2D::maxObjects

Maximum number of objects used for clustering

Definition at line 53 of file classify_kmeans_2d.h.


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