Xmipp  v3.23.11-Nereus
volume_validate_pca.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: AUTHOR_NAME (jvargas@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 
26 #include "volume_validate_pca.h"
27 #include <numeric>
28 #include <core/geometry.h>
29 #include "core/metadata_sql.h"
30 
31 // Define params
33 {
34  //usage
35  addUsageLine("Validate an obtained volume from a set of class averages");
36  //params
37  addParamsLine(" -i <md_file> : Metadata file with input classes");
38  addParamsLine(" [ --vol <file=\"\">] : Input volume");
39  addParamsLine(" [--sym <symfile=c1>] : Enforce symmetry in projections");
40  addParamsLine(" [--numVols <N=5>] : Number of intermediate volumes to generate");
41  addParamsLine(" [--numClasses <N=8>] : Number of classes to generate the intermediate volumes");
42 }
43 
44 // Read arguments ==========================================================
46 {
47  fnClasses = getParam("-i");
48  fnSym = getParam("--sym");
49  NVols = getIntParam("--numVols");
50  NClasses = getIntParam("--numClasses");
51 }
52 
53 // Show ====================================================================
55 {
56  if (verbose > 0)
57  {
58  std::cout << "Input classes metadata : " << fnClasses << std::endl;
59  std::cout << "Number of intermediate volumes to generate : " << NVols << std::endl;
60  std::cout << "Number of classes to be used : " << NClasses << std::endl;
61  if (fnSym != "")
62  std::cout << "Symmetry for projections : " << fnSym << std::endl;
63  }
64 }
65 
67 {
68  doProjMatch = false;
71  getImageSize(mdClasses,xdim, ydim, zdim, ndim);
72 }
73 
75 {
76  //String args=formatString("-i %s -o %s --operate random_subset %d --mode overwrite",fnClasses.c_str(),fnAngles.c_str(),NClasses);
77  String args=formatString("-i %s -o %s --operate bootstrap --mode overwrite",fnClasses.c_str(),fnAngles.c_str());
78  String cmd=(String)"xmipp_metadata_utilities "+args;
79  if (system(cmd.c_str())==-1)
80  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
81 
82  //Metadata with the well sampled projection and random projections assigned
83  MetaDataVec md;
84  double rot, tilt, psi;
85  size_t id;
86 
87  md.read(fnAngles);
89 
90  int anglePertur = 30;
91  for (auto& row : md)
92  {
93  row.getValue(MDL_ANGLE_ROT, rot);
94  row.getValue(MDL_ANGLE_TILT,tilt);
95  row.getValue(MDL_ANGLE_PSI,psi);
96 
97  row.setValue(MDL_ANGLE_ROT, (rnd_unif(-anglePertur,anglePertur))/2+rot);
98  row.setValue(MDL_ANGLE_TILT,(rnd_unif(-anglePertur,anglePertur))/2+tilt);
99  row.setValue(MDL_ANGLE_PSI, (rnd_unif(-anglePertur,anglePertur))/2+psi);
100 
101  md.setRow(row, row.id());
102  }
103 
104  md.write(fnAngles);
105 }
106 
108 {
109  //number of threads in the reconstruction
110  int Nthr = 4;
111  float AngularSampling = 30;
112  int NProj = 10;
113 
114  String args=formatString("-i %s -o %s --sym %s --weight --thr %d -v 0",fnAngles.c_str(),fnVol.c_str(),fnSym.c_str(),Nthr);
115  String cmd=(String)"xmipp_reconstruct_fourier "+args;
116  if (system(cmd.c_str())==-1)
117  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
118 
119  args=formatString("-i %s -o %s --mask circular %d -v 0",fnVol.c_str(),fnVol.c_str(),static_cast<int>(-xdim/2));
120  cmd=(String)"xmipp_transform_mask "+args;
121  if (system(cmd.c_str())==-1)
122  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
123 
124  if (doProjMatch)
125  {
126 
127  for (int ind=0; ind<NProj; ind++)
128  {
129 
130  args=formatString("-i %s -o %s --sampling_rate %f --sym %s --method fourier 1 0.25 bspline --compute_neighbors --angular_distance -1 --experimental_images %s",fnVol.c_str(),fnGallery.c_str(),AngularSampling,fnSym.c_str(),fnAngles.c_str());
131  cmd=(String)"xmipp_angular_project_library "+args;
132  if (system(cmd.c_str())==-1)
133  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
134 
135  args=formatString("-i %s -o %s --ref %s --Ri 0 --Ro %f --max_shift %f --append",fnAngles.c_str(),fnAngles.c_str(),fnGallery.c_str(),xdim/2,xdim/20);
136  cmd=(String)"xmipp_angular_projection_matching "+args;
137  if (system(cmd.c_str())==-1)
138  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
139 
140  args=formatString("-i %s -o %s --sym %s --weight --thr %d -v 0",fnAngles.c_str(),fnVol.c_str(),fnSym.c_str(),Nthr);
141  cmd=(String)"xmipp_reconstruct_fourier "+args;
142  if (system(cmd.c_str())==-1)
143  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
144 
145  args=formatString("-i %s -o %s --mask circular %d -v 0",fnVol.c_str(),fnVol.c_str(),static_cast<int>(-xdim/2));
146  cmd=(String)"xmipp_transform_mask "+args;
147  if (system(cmd.c_str())==-1)
148  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
149  }
150  }
151 
152  args=formatString("-i %s -o %s --sym %s --weight --thr %d -v 0",fnAngles.c_str(),fnVol.c_str(),fnSym.c_str(),Nthr);
153  cmd=(String)"xmipp_reconstruct_fourier "+args;
154  if (system(cmd.c_str())==-1)
155  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
156 
157  args=formatString("-i %s -o %s --mask circular %d -v 0",fnVol.c_str(),fnVol.c_str(),static_cast<int>(-xdim/2));
158  cmd=(String)"xmipp_transform_mask "+args;
159  if (system(cmd.c_str())==-1)
160  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
161 
162  args=formatString("-i %s -o %s --select below 0 --substitute value 0 -v 0",fnVol.c_str(),fnVol.c_str());
163  cmd=(String)"xmipp_transform_threshold "+args;
164  if (system(cmd.c_str())==-1)
165  REPORT_ERROR(ERR_UNCLASSIFIED,"Cannot open shell");
166 }
167 
169 {
170  MetaDataVec md, md2;
171  FileName imag;
172  double rot, tilt, psi, rot2, tilt2, psi2;
173  std::vector<double> error;
174  size_t id, id2;
175 
176  md.read(fnClasses);
177  std::stringstream ss;
178 
179  MDMultiQuery query;
180  MetaDataVec auxMetadata;
181  double distance = 0;
182 
183  for (const auto& row : md)
184  {
185  row.getValue(MDL_IMAGE, imag);
186  row.getValue(MDL_ANGLE_ROT, rot);
187  row.getValue(MDL_ANGLE_TILT,tilt);
188  row.getValue(MDL_ANGLE_PSI,psi);
189 
190  for (int index=0; index<NVols;index++)
191  {
192  ss << index;
194  fnAngles+=".xmd";
195  ss.str(std::string());
196  md2.read(fnAngles);
197  MDValueEQ eq(MDL_IMAGE, imag);
198  query.addAndQuery(eq);
199  auxMetadata.importObjects(md2, eq);
200 
201  for (const auto& rowAux : auxMetadata)
202  {
203  rowAux.getValue(MDL_ANGLE_ROT, rot2);
204  rowAux.getValue(MDL_ANGLE_TILT,tilt2);
205  rowAux.getValue(MDL_ANGLE_PSI,psi2);
206  distance = Euler_distanceBetweenAngleSets(rot,tilt, psi,rot2, tilt2, psi2, true);
207  error.push_back(distance);
208  }
209  }
210  }
211 
212  double sum = std::accumulate(error.begin(), error.end(), 0.0);
213  double mean = sum / error.size();
214  double sq_sum = std::inner_product(error.begin(), error.end(), error.begin(), 0.0);
215  double stdev = std::sqrt(sq_sum / error.size() - mean * mean);
216 
217  std::cout << std::endl;
218  std::cout << "stdev of errors : " << stdev << std::endl;
219  std::cout << "mean of errors : " << mean << std::endl;
220 
221 }
222 
224 {
225  show();
226  produceSideinfo();
227  std::stringstream ss;
228 
229  //Here we read the "mean" volume
230  //fnAngles=fnClasses;
231  //fnVol=fnClasses.removeAllExtensions();
232  //fnVol+=".vol";
233  //reconstruct();
234 
235  doProjMatch = true;
236 
237  for( int index = 0; index< NVols; index++)
238  {
239 
240  ss << index;
242  fnAngles+=".xmd";
243  ss.str(std::string());
244 
245  modifyAngles();
246 
247  ss << index;
248  fnVol=fnClasses.removeAllExtensions()+ss.str();
249  fnVol+=".vol";
250  ss.str(std::string());
251 
252  ss << index;
253  fnGallery=fnClasses.removeAllExtensions()+ss.str();
254  fnGallery+=".stk";
255  ss.str(std::string());
256 
257  //Reconstruct and ProjMatching of current
258  reconstruct();
259 
260  //Some clean up
261  FileName tempSampling = fnVol.removeAllExtensions();
262  tempSampling+="_sampling.xmd";
264  tempDoc+=".doc";
265 
266  //String args=formatString(" %s %s %s %s",fnVol.c_str(),fnGallery.c_str(),tempSampling.c_str(),tempDoc.c_str());
267  //String cmd=(String)"rm "+args;
268  //system(cmd.c_str());
269  }
270 
271  evaluate();
272 }
Just to locate unclassified errors.
Definition: xmipp_error.h:192
Rotation angle of an image (double,degrees)
void addAndQuery(MDQuery &query)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void sqrt(Image< double > &op)
void defineParams()
Read arguments from command line.
Tilting angle of an image (double,degrees)
void getImageSize(const MetaData &md, size_t &Xdim, size_t &Ydim, size_t &Zdim, size_t &Ndim, MDLabel image_label)
Special label to be used when gathering MDs in MpiMetadataPrograms.
FileName removeAllExtensions() const
void modifyAngles()
Modified angles.
double rnd_unif()
const char * getParam(const char *param, int arg=0)
viol index
void importObjects(const MetaData &md, const std::vector< size_t > &objectsToAdd, bool doClear=true) override
void readParams()
Read arguments from command line.
void eq(Image< double > &op1, const Image< double > &op2)
Be careful with integer images for relational operations...due to double comparisons.
T Euler_distanceBetweenAngleSets(T rot1, T tilt1, T psi1, T rot2, T tilt2, T psi2, bool only_projdir)
Definition: geometry.cpp:681
int verbose
Verbosity level.
TYPE distance(struct Point_T *p, struct Point_T *q)
Definition: point.cpp:28
void error(char *s)
Definition: tools.cpp:107
void produceSideinfo()
Produce side info: fill arrays with relevant transformation matrices.
virtual void removeDisabled()
std::string String
Definition: xmipp_strings.h:34
double psi(const double x)
String formatString(const char *format,...)
void reconstruct()
Reconstruct volume with all projections.
void addUsageLine(const char *line, bool verbatim=false)
unsigned int randomize_random_generator()
int getIntParam(const char *param, int arg=0)
Name of an image (std::string)
void addParamsLine(const String &line)
void evaluate()
Modified angles.