Xmipp  v3.23.11-Nereus
classify_compare_classes.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: Carlos Oscar Sorzano (coss@cnb.csic.es)
3  *
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
28 #include "core/matrix2d.h"
29 
30 // Evaluate classes program -------------------------------------------
32 {
33  fnClass1=getParam("--i1");
34  fnClass2=getParam("--i2");
35  fnOut=getParam("-o");
36  append=checkParam("--append");
37 }
38 
40 {
41  addUsageLine("Compare two classifications");
42  addUsageLine("+The program provides information about which class of classification 1 corresponds to which class of classification 2");
43  addUsageLine("+");
44  addParamsLine(" --i1 <infile1> : Metadata with the classification 1");
45  addParamsLine(" --i2 <infile2> : Metadata with the classification 2");
46  addParamsLine(" -o <outfile> : Output text file");
47  addParamsLine(" [--append] : Append text to output");
48  addExampleLine("xmipp_classify_compare_classes --i1 2D/CL2D/run_001/results_level_00_classes.xmd --i2 2D/CL2D/run_001/results_level_01_classes.xmd -o comparison_level_00_01.txt");
49 }
50 
52 {
53  if (!verbose)
54  return;
55  std::cout
56  << "Input1: " << fnClass1 << std::endl
57  << "Input2: " << fnClass2 << std::endl
58  << "Output: " << fnOut << std::endl;
59 }
60 
62 {
63  MetaDataVec MD1(formatString("classes@%s",fnClass1.c_str()));
64  MetaDataVec MD2(formatString("classes@%s",fnClass2.c_str()));
65  std::vector<int> ref1, ref2;
66  int aux;
67  for (size_t objId : MD1.ids())
68  {
69  MD1.getValue(MDL_REF,aux,objId);
70  ref1.push_back(aux);
71  }
72  for (size_t objId : MD2.ids())
73  {
74  MD2.getValue(MDL_REF,aux,objId);
75  ref2.push_back(aux);
76  }
77 
78  Matrix2D<int> comparisonMatrix(MD1.size(),MD2.size());
79  Matrix1D<int> MD1classSize(MAT_YSIZE(comparisonMatrix)), MD2classSize(MAT_XSIZE(comparisonMatrix));
80 
81  // Read the size of the individual classes
82  MetaDataDb MDclass1, MDclass2;
83  FOR_ALL_ELEMENTS_IN_MATRIX1D(MD1classSize)
84  {
85  MDclass1.read(formatString("class%06d_images@%s",ref1[i],fnClass1.c_str()));
86  VEC_ELEM(MD1classSize,i)=MDclass1.size();
87  }
88 
89  FOR_ALL_ELEMENTS_IN_MATRIX1D(MD2classSize)
90  {
91  MDclass2.read(formatString("class%06d_images@%s",ref2[i],fnClass2.c_str()));
92  VEC_ELEM(MD2classSize,i)=MDclass2.size();
93  }
94 
95  // Now compare the two classifications
96  for (size_t i=0; i<MAT_YSIZE(comparisonMatrix); i++)
97  {
98  MDclass1.read(formatString("class%06d_images@%s",ref1[i],fnClass1.c_str()));
99  for (size_t j=0; j<MAT_XSIZE(comparisonMatrix); j++)
100  {
101  MDclass2.read(formatString("class%06d_images@%s",ref2[j],fnClass2.c_str()));
102  MDclass2.intersection(MDclass1,MDL_IMAGE);
103  MAT_ELEM(comparisonMatrix,i,j)=MDclass2.size();
104  }
105  }
106 
107  // Report analysis
108  std::ofstream fhOut;
109  if (append)
110  {
111  fhOut.open(fnOut.c_str(),std::ios::app);
112  fhOut << "\n\n------------------------------------------------------------------------\n";
113  }
114  else
115  fhOut.open(fnOut.c_str());
116  if (!fhOut)
118  fhOut << "Comparison of " << fnClass1 << " and " << fnClass2 << std::endl;
119  fhOut << "Analysis of " << fnClass1 << " =======================\n";
120  for (size_t i=0; i<MAT_YSIZE(comparisonMatrix); i++)
121  {
122  fhOut << "Class " << formatString("class%06d_images@%s",ref1[i],fnClass1.c_str()) << ": " << VEC_ELEM(MD1classSize,i) << " images\n";
123  for (size_t j=0; j<MAT_XSIZE(comparisonMatrix); j++)
124  if (MAT_ELEM(comparisonMatrix,i,j)>0)
125  fhOut << " " << 100.0*MAT_ELEM(comparisonMatrix,i,j)/VEC_ELEM(MD1classSize,i) << "% are in class " << formatString("class%06d_images@%s",j+1,fnClass2.c_str()) << std::endl;
126  }
127  fhOut << "\n\nAnalysis of " << fnClass2 << " =======================\n";
128  for (size_t j=0; j<MAT_XSIZE(comparisonMatrix); j++)
129  {
130  fhOut << "Class " << formatString("class%06d_images@%s",ref2[j],fnClass2.c_str()) << ": " << VEC_ELEM(MD2classSize,j) << " images\n";
131  for (size_t i=0; i<MAT_YSIZE(comparisonMatrix); i++)
132  if (MAT_ELEM(comparisonMatrix,i,j)>0)
133  fhOut << " " << 100.0*MAT_ELEM(comparisonMatrix,i,j)/VEC_ELEM(MD2classSize,j) << "% are in class " << formatString("class%06d_images@%s",i+1,fnClass1.c_str()) << std::endl;
134  }
135  fhOut.close();
136 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
void defineParams()
Define input parameters.
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void intersection(const MetaDataDb &mdIn, const MDLabel label)
Couldn&#39;t write to file.
Definition: xmipp_error.h:140
virtual IdIteratorProxy< false > ids()
size_t size() const override
#define i
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
const char * getParam(const char *param, int arg=0)
FileName fnClass1
Name of the two classification metadatas.
void addExampleLine(const char *example, bool verbatim=true)
int verbose
Verbosity level.
size_t size() const override
void readParams()
Read input parameters.
#define j
bool getValue(MDObject &mdValueOut, size_t id) const override
Class to which the image belongs (int)
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
String formatString(const char *format,...)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=NULL, bool decomposeStack=true) override
bool checkParam(const char *param)
bool append
Append to output.
FileName fnOut
Name of the output text file.
void addUsageLine(const char *line, bool verbatim=false)
Name of an image (std::string)
void addParamsLine(const String &line)